2018-01-14 13:46:20 +01:00
|
|
|
import * as THREE from "three";
|
2018-01-06 11:47:07 +01:00
|
|
|
import { PlayerObject } from "./model";
|
2018-01-06 15:01:12 +01:00
|
|
|
import { invokeAnimation } from "./animation";
|
2018-07-17 20:49:00 +02:00
|
|
|
import { loadSkinToCanvas, loadCapeToCanvas, isSlimSkin } from "./utils";
|
2018-07-02 13:52:36 +02:00
|
|
|
|
2018-08-17 06:05:17 +02:00
|
|
|
export interface SkinViewerOptions {
|
|
|
|
domElement: Node;
|
|
|
|
animation?: Animation;
|
|
|
|
skinUrl?: string;
|
|
|
|
capeUrl?: string;
|
|
|
|
width?: number;
|
|
|
|
height?: number;
|
|
|
|
detectModel?: boolean;
|
|
|
|
}
|
|
|
|
|
2018-08-16 13:07:46 +02:00
|
|
|
export class SkinViewer {
|
2018-07-17 20:49:00 +02:00
|
|
|
|
2018-08-17 06:05:17 +02:00
|
|
|
public readonly domElement: Node;
|
|
|
|
public animation: Animation | null;
|
2018-08-16 13:35:16 +02:00
|
|
|
public detectModel: boolean = true;
|
2018-07-21 14:52:02 +02:00
|
|
|
public animationPaused: boolean = false;
|
|
|
|
public animationTime: number = 0;
|
|
|
|
public disposed: boolean = false;
|
2018-07-17 20:49:00 +02:00
|
|
|
|
2018-08-17 06:05:17 +02:00
|
|
|
public readonly skinImg: HTMLImageElement;
|
|
|
|
public readonly skinCanvas: HTMLCanvasElement;
|
|
|
|
public readonly skinTexture: THREE.Texture;
|
2018-07-17 20:49:00 +02:00
|
|
|
|
2018-08-17 06:05:17 +02:00
|
|
|
public readonly capeImg: HTMLImageElement;
|
|
|
|
public readonly capeCanvas: HTMLCanvasElement;
|
|
|
|
public readonly capeTexture: THREE.Texture;
|
2018-07-17 20:49:00 +02:00
|
|
|
|
2018-08-17 06:05:17 +02:00
|
|
|
public readonly layer1Material: THREE.MeshBasicMaterial;
|
|
|
|
public readonly layer2Material: THREE.MeshBasicMaterial;
|
|
|
|
public readonly capeMaterial: THREE.MeshBasicMaterial;
|
2018-07-17 20:49:00 +02:00
|
|
|
|
2018-08-17 06:05:17 +02:00
|
|
|
public readonly scene: THREE.Scene;
|
|
|
|
public readonly camera: THREE.PerspectiveCamera;
|
|
|
|
public readonly renderer: THREE.WebGLRenderer;
|
2018-07-17 20:49:00 +02:00
|
|
|
|
2018-08-17 06:05:17 +02:00
|
|
|
public readonly playerObject: PlayerObject;
|
2018-07-17 20:49:00 +02:00
|
|
|
|
2018-08-17 06:05:17 +02:00
|
|
|
constructor(options: SkinViewerOptions) {
|
2017-10-01 14:00:45 +02:00
|
|
|
this.domElement = options.domElement;
|
|
|
|
this.animation = options.animation || null;
|
2018-08-16 13:35:16 +02:00
|
|
|
if (options.detectModel === false) {
|
|
|
|
this.detectModel = false;
|
|
|
|
}
|
2017-10-01 14:00:45 +02:00
|
|
|
|
|
|
|
// texture
|
|
|
|
this.skinImg = new Image();
|
|
|
|
this.skinCanvas = document.createElement("canvas");
|
|
|
|
this.skinTexture = new THREE.Texture(this.skinCanvas);
|
|
|
|
this.skinTexture.magFilter = THREE.NearestFilter;
|
2018-04-21 13:10:36 +02:00
|
|
|
this.skinTexture.minFilter = THREE.NearestFilter;
|
2017-10-01 14:00:45 +02:00
|
|
|
|
|
|
|
this.capeImg = new Image();
|
|
|
|
this.capeCanvas = document.createElement("canvas");
|
|
|
|
this.capeTexture = new THREE.Texture(this.capeCanvas);
|
|
|
|
this.capeTexture.magFilter = THREE.NearestFilter;
|
2018-04-21 13:10:36 +02:00
|
|
|
this.capeTexture.minFilter = THREE.NearestFilter;
|
2017-10-01 14:00:45 +02:00
|
|
|
|
|
|
|
this.layer1Material = new THREE.MeshBasicMaterial({ map: this.skinTexture, side: THREE.FrontSide });
|
2018-04-15 05:36:34 +02:00
|
|
|
this.layer2Material = new THREE.MeshBasicMaterial({ map: this.skinTexture, transparent: true, opacity: 1, side: THREE.DoubleSide, alphaTest: 0.5 });
|
|
|
|
this.capeMaterial = new THREE.MeshBasicMaterial({ map: this.capeTexture, transparent: true, opacity: 1, side: THREE.DoubleSide, alphaTest: 0.5 });
|
2017-10-01 14:00:45 +02:00
|
|
|
|
|
|
|
// scene
|
|
|
|
this.scene = new THREE.Scene();
|
|
|
|
|
2018-02-11 12:21:33 +01:00
|
|
|
// Use smaller fov to avoid distortion
|
|
|
|
this.camera = new THREE.PerspectiveCamera(40);
|
2017-10-01 14:00:45 +02:00
|
|
|
this.camera.position.y = -12;
|
2018-02-11 12:21:33 +01:00
|
|
|
this.camera.position.z = 60;
|
2017-10-01 14:00:45 +02:00
|
|
|
|
2018-07-17 20:49:00 +02:00
|
|
|
this.renderer = new THREE.WebGLRenderer({ alpha: true, antialias: false });
|
2017-10-01 14:00:45 +02:00
|
|
|
this.renderer.setSize(300, 300); // default size
|
|
|
|
this.renderer.context.getShaderInfoLog = () => ""; // shut firefox up
|
|
|
|
this.domElement.appendChild(this.renderer.domElement);
|
|
|
|
|
2018-07-02 09:46:48 +02:00
|
|
|
this.playerObject = new PlayerObject(this.layer1Material, this.layer2Material, this.capeMaterial);
|
2017-10-01 14:00:45 +02:00
|
|
|
this.scene.add(this.playerObject);
|
|
|
|
|
|
|
|
// texture loading
|
2018-03-03 15:04:10 +01:00
|
|
|
this.skinImg.crossOrigin = "anonymous";
|
2018-01-06 11:47:07 +01:00
|
|
|
this.skinImg.onerror = () => console.error("Failed loading " + this.skinImg.src);
|
2017-10-01 14:00:45 +02:00
|
|
|
this.skinImg.onload = () => {
|
2018-07-05 04:56:21 +02:00
|
|
|
loadSkinToCanvas(this.skinCanvas, this.skinImg);
|
2017-10-01 14:00:45 +02:00
|
|
|
|
2018-07-02 14:13:01 +02:00
|
|
|
if (this.detectModel) {
|
2018-07-05 04:56:21 +02:00
|
|
|
this.playerObject.skin.slim = isSlimSkin(this.skinCanvas);
|
2018-07-02 14:13:01 +02:00
|
|
|
}
|
2018-07-02 13:52:36 +02:00
|
|
|
|
2017-10-01 14:00:45 +02:00
|
|
|
this.skinTexture.needsUpdate = true;
|
|
|
|
this.layer1Material.needsUpdate = true;
|
|
|
|
this.layer2Material.needsUpdate = true;
|
|
|
|
|
|
|
|
this.playerObject.skin.visible = true;
|
|
|
|
};
|
|
|
|
|
2018-03-03 15:04:10 +01:00
|
|
|
this.capeImg.crossOrigin = "anonymous";
|
2018-01-06 11:47:07 +01:00
|
|
|
this.capeImg.onerror = () => console.error("Failed loading " + this.capeImg.src);
|
2017-10-01 14:00:45 +02:00
|
|
|
this.capeImg.onload = () => {
|
2018-07-05 04:56:21 +02:00
|
|
|
loadCapeToCanvas(this.capeCanvas, this.capeImg);
|
2017-10-01 14:00:45 +02:00
|
|
|
|
|
|
|
this.capeTexture.needsUpdate = true;
|
|
|
|
this.capeMaterial.needsUpdate = true;
|
|
|
|
|
|
|
|
this.playerObject.cape.visible = true;
|
|
|
|
};
|
|
|
|
|
2018-07-21 05:07:52 +02:00
|
|
|
if (options.skinUrl) this.skinUrl = options.skinUrl;
|
|
|
|
if (options.capeUrl) this.capeUrl = options.capeUrl;
|
|
|
|
if (options.width) this.width = options.width;
|
|
|
|
if (options.height) this.height = options.height;
|
2017-10-01 14:00:45 +02:00
|
|
|
|
2018-07-21 14:52:02 +02:00
|
|
|
const draw = () => {
|
2017-10-01 14:00:45 +02:00
|
|
|
if (this.disposed) return;
|
|
|
|
window.requestAnimationFrame(draw);
|
|
|
|
if (!this.animationPaused) {
|
|
|
|
this.animationTime++;
|
|
|
|
if (this.animation) {
|
2018-02-02 11:27:09 +01:00
|
|
|
invokeAnimation(this.animation, this.playerObject, this.animationTime / 100.0);
|
2017-10-01 14:00:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this.renderer.render(this.scene, this.camera);
|
|
|
|
};
|
|
|
|
draw();
|
|
|
|
}
|
|
|
|
|
2018-08-17 06:05:17 +02:00
|
|
|
setSize(width, height) {
|
2017-10-01 14:00:45 +02:00
|
|
|
this.camera.aspect = width / height;
|
|
|
|
this.camera.updateProjectionMatrix();
|
|
|
|
this.renderer.setSize(width, height);
|
|
|
|
}
|
|
|
|
|
2018-08-17 06:05:17 +02:00
|
|
|
dispose() {
|
2017-10-01 14:00:45 +02:00
|
|
|
this.disposed = true;
|
|
|
|
this.domElement.removeChild(this.renderer.domElement);
|
|
|
|
this.renderer.dispose();
|
|
|
|
this.skinTexture.dispose();
|
|
|
|
this.capeTexture.dispose();
|
|
|
|
}
|
|
|
|
|
2018-07-21 05:07:52 +02:00
|
|
|
get skinUrl() {
|
|
|
|
return this.skinImg.src;
|
|
|
|
}
|
2017-10-01 14:00:45 +02:00
|
|
|
|
2018-07-21 05:07:52 +02:00
|
|
|
set skinUrl(url) {
|
|
|
|
this.skinImg.src = url;
|
|
|
|
}
|
2017-10-01 14:00:45 +02:00
|
|
|
|
2018-07-21 05:07:52 +02:00
|
|
|
get capeUrl() {
|
|
|
|
return this.capeImg.src;
|
|
|
|
}
|
2017-10-01 14:00:45 +02:00
|
|
|
|
2018-07-21 05:07:52 +02:00
|
|
|
set capeUrl(url) {
|
|
|
|
this.capeImg.src = url;
|
|
|
|
}
|
2017-10-01 14:00:45 +02:00
|
|
|
|
2018-07-21 05:07:52 +02:00
|
|
|
get width() {
|
|
|
|
return this.renderer.getSize().width;
|
|
|
|
}
|
2017-10-01 14:00:45 +02:00
|
|
|
|
2018-07-21 05:07:52 +02:00
|
|
|
set width(newWidth) {
|
|
|
|
this.setSize(newWidth, this.height);
|
|
|
|
}
|
2017-10-01 14:00:45 +02:00
|
|
|
|
2018-07-21 05:07:52 +02:00
|
|
|
get height() {
|
|
|
|
return this.renderer.getSize().height;
|
|
|
|
}
|
2017-10-01 14:00:45 +02:00
|
|
|
|
2018-07-21 05:07:52 +02:00
|
|
|
set height(newHeight) {
|
|
|
|
this.setSize(this.width, newHeight);
|
|
|
|
}
|
2017-10-02 15:29:41 +02:00
|
|
|
}
|