Allow model to be changed on the fly
This commit is contained in:
parent
0a8dafff4e
commit
df3be7ab51
93
src/model.js
93
src/model.js
|
|
@ -36,10 +36,10 @@ function setVertices(box, top, bottom, left, front, right, back) {
|
||||||
const esp = 0.002;
|
const esp = 0.002;
|
||||||
|
|
||||||
class SkinObject extends THREE.Group {
|
class SkinObject extends THREE.Group {
|
||||||
constructor(slim, layer1Material, layer2Material) {
|
constructor(layer1Material, layer2Material) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this.slim = slim;
|
this.modelListeners = []; // called when model(slim property) is changed
|
||||||
|
|
||||||
// Head
|
// Head
|
||||||
this.head = new THREE.Group();
|
this.head = new THREE.Group();
|
||||||
|
|
@ -107,8 +107,14 @@ class SkinObject extends THREE.Group {
|
||||||
this.rightArm = new THREE.Group();
|
this.rightArm = new THREE.Group();
|
||||||
let rightArmPivot = new THREE.Group();
|
let rightArmPivot = new THREE.Group();
|
||||||
|
|
||||||
let rightArmBox = new THREE.BoxGeometry((slim ? 3 : 4) - esp, 12 - esp, 4 - esp, 0, 0, 0);
|
let rightArmBox = new THREE.BoxGeometry(1, 1, 1, 0, 0, 0); // w/d/h is model-related
|
||||||
if (slim) {
|
let rightArmMesh = new THREE.Mesh(rightArmBox, layer1Material);
|
||||||
|
rightArmPivot.add(rightArmMesh);
|
||||||
|
this.modelListeners.push(() => {
|
||||||
|
rightArmMesh.scale.x = (this.slim ? 3 : 4) - esp;
|
||||||
|
rightArmMesh.scale.y = 12 - esp;
|
||||||
|
rightArmMesh.scale.z = 4 - esp;
|
||||||
|
if (this.slim) {
|
||||||
setVertices(rightArmBox,
|
setVertices(rightArmBox,
|
||||||
toSkinVertices(44, 16, 47, 20),
|
toSkinVertices(44, 16, 47, 20),
|
||||||
toSkinVertices(47, 16, 50, 20),
|
toSkinVertices(47, 16, 50, 20),
|
||||||
|
|
@ -127,11 +133,19 @@ class SkinObject extends THREE.Group {
|
||||||
toSkinVertices(52, 20, 56, 32)
|
toSkinVertices(52, 20, 56, 32)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
let rightArmMesh = new THREE.Mesh(rightArmBox, layer1Material);
|
rightArmBox.uvsNeedUpdate = true;
|
||||||
rightArmPivot.add(rightArmMesh);
|
rightArmBox.elementsNeedUpdate = true;
|
||||||
|
});
|
||||||
|
|
||||||
let rightArm2Box = new THREE.BoxGeometry((slim ? 3.375 : 4.5) - esp, 13.5 - esp, 4.5 - esp, 0, 0, 0);
|
let rightArm2Box = new THREE.BoxGeometry(1, 1, 1, 0, 0, 0); // w/d/h is model-related
|
||||||
if (slim) {
|
let rightArm2Mesh = new THREE.Mesh(rightArm2Box, layer2Material);
|
||||||
|
rightArm2Mesh.renderOrder = 1;
|
||||||
|
rightArmPivot.add(rightArm2Mesh);
|
||||||
|
this.modelListeners.push(() => {
|
||||||
|
rightArm2Mesh.scale.x = (this.slim ? 3.375 : 4.5) - esp;
|
||||||
|
rightArm2Mesh.scale.y = 13.5 - esp;
|
||||||
|
rightArm2Mesh.scale.z = 4.5 - esp;
|
||||||
|
if (this.slim) {
|
||||||
setVertices(rightArm2Box,
|
setVertices(rightArm2Box,
|
||||||
toSkinVertices(44, 32, 47, 36),
|
toSkinVertices(44, 32, 47, 36),
|
||||||
toSkinVertices(47, 32, 50, 36),
|
toSkinVertices(47, 32, 50, 36),
|
||||||
|
|
@ -150,14 +164,16 @@ class SkinObject extends THREE.Group {
|
||||||
toSkinVertices(52, 36, 56, 48)
|
toSkinVertices(52, 36, 56, 48)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
let rightArm2Mesh = new THREE.Mesh(rightArm2Box, layer2Material);
|
rightArm2Box.uvsNeedUpdate = true;
|
||||||
rightArm2Mesh.renderOrder = 1;
|
rightArm2Box.elementsNeedUpdate = true;
|
||||||
rightArmPivot.add(rightArm2Mesh);
|
});
|
||||||
|
|
||||||
rightArmPivot.position.y = -6;
|
rightArmPivot.position.y = -6;
|
||||||
this.rightArm.add(rightArmPivot);
|
this.rightArm.add(rightArmPivot);
|
||||||
this.rightArm.position.y = -4;
|
this.rightArm.position.y = -4;
|
||||||
this.rightArm.position.x = slim ? -5.5 : -6;
|
this.modelListeners.push(() => {
|
||||||
|
this.rightArm.position.x = this.slim ? -5.5 : -6;
|
||||||
|
});
|
||||||
this.add(this.rightArm);
|
this.add(this.rightArm);
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -165,8 +181,14 @@ class SkinObject extends THREE.Group {
|
||||||
this.leftArm = new THREE.Group();
|
this.leftArm = new THREE.Group();
|
||||||
let leftArmPivot = new THREE.Group();
|
let leftArmPivot = new THREE.Group();
|
||||||
|
|
||||||
let leftArmBox = new THREE.BoxGeometry((slim ? 3 : 4) - esp, 12 - esp, 4 - esp, 0, 0, 0);
|
let leftArmBox = new THREE.BoxGeometry(1, 1, 1, 0, 0, 0); // w/d/h is model-related
|
||||||
if (slim) {
|
let leftArmMesh = new THREE.Mesh(leftArmBox, layer1Material);
|
||||||
|
leftArmPivot.add(leftArmMesh);
|
||||||
|
this.modelListeners.push(() => {
|
||||||
|
leftArmMesh.scale.x = (this.slim ? 3 : 4) - esp;
|
||||||
|
leftArmMesh.scale.y = 12 - esp;
|
||||||
|
leftArmMesh.scale.z = 4 - esp;
|
||||||
|
if (this.slim) {
|
||||||
setVertices(leftArmBox,
|
setVertices(leftArmBox,
|
||||||
toSkinVertices(36, 48, 39, 52),
|
toSkinVertices(36, 48, 39, 52),
|
||||||
toSkinVertices(39, 48, 42, 52),
|
toSkinVertices(39, 48, 42, 52),
|
||||||
|
|
@ -185,11 +207,19 @@ class SkinObject extends THREE.Group {
|
||||||
toSkinVertices(44, 52, 48, 64)
|
toSkinVertices(44, 52, 48, 64)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
let leftArmMesh = new THREE.Mesh(leftArmBox, layer1Material);
|
leftArmBox.uvsNeedUpdate = true;
|
||||||
leftArmPivot.add(leftArmMesh);
|
leftArmBox.elementsNeedUpdate=true;
|
||||||
|
});
|
||||||
|
|
||||||
let leftArm2Box = new THREE.BoxGeometry((slim ? 3.375 : 4.5) - esp, 13.5 - esp, 4.5 - esp, 0, 0, 0);
|
let leftArm2Box = new THREE.BoxGeometry(1, 1, 1, 0, 0, 0); // w/d/h is model-related
|
||||||
if (slim) {
|
let leftArm2Mesh = new THREE.Mesh(leftArm2Box, layer2Material);
|
||||||
|
leftArm2Mesh.renderOrder = 1;
|
||||||
|
leftArmPivot.add(leftArm2Mesh);
|
||||||
|
this.modelListeners.push(() => {
|
||||||
|
leftArm2Mesh.scale.x = (this.slim ? 3.375 : 4.5) - esp;
|
||||||
|
leftArm2Mesh.scale.y = 13.5 - esp;
|
||||||
|
leftArm2Mesh.scale.z = 4.5 - esp;
|
||||||
|
if (this.slim) {
|
||||||
setVertices(leftArm2Box,
|
setVertices(leftArm2Box,
|
||||||
toSkinVertices(52, 48, 55, 52),
|
toSkinVertices(52, 48, 55, 52),
|
||||||
toSkinVertices(55, 48, 58, 52),
|
toSkinVertices(55, 48, 58, 52),
|
||||||
|
|
@ -208,14 +238,16 @@ class SkinObject extends THREE.Group {
|
||||||
toSkinVertices(60, 52, 64, 64)
|
toSkinVertices(60, 52, 64, 64)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
let leftArm2Mesh = new THREE.Mesh(leftArm2Box, layer2Material);
|
leftArm2Box.uvsNeedUpdate = true;
|
||||||
leftArm2Mesh.renderOrder = 1;
|
leftArm2Box.elementsNeedUpdate = true;
|
||||||
leftArmPivot.add(leftArm2Mesh);
|
});
|
||||||
|
|
||||||
leftArmPivot.position.y = -6;
|
leftArmPivot.position.y = -6;
|
||||||
this.leftArm.add(leftArmPivot);
|
this.leftArm.add(leftArmPivot);
|
||||||
this.leftArm.position.y = -4;
|
this.leftArm.position.y = -4;
|
||||||
this.leftArm.position.x = slim ? 5.5 : 6;
|
this.modelListeners.push(() => {
|
||||||
|
this.leftArm.position.x = this.slim ? 5.5 : 6;
|
||||||
|
});
|
||||||
this.add(this.leftArm);
|
this.add(this.leftArm);
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -288,6 +320,19 @@ class SkinObject extends THREE.Group {
|
||||||
this.leftLeg.position.y = -16;
|
this.leftLeg.position.y = -16;
|
||||||
this.leftLeg.position.x = 2;
|
this.leftLeg.position.x = 2;
|
||||||
this.add(this.leftLeg);
|
this.add(this.leftLeg);
|
||||||
|
|
||||||
|
this.slim = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
get slim() {
|
||||||
|
return this._slim;
|
||||||
|
}
|
||||||
|
|
||||||
|
set slim(value) {
|
||||||
|
if (this._slim !== value) {
|
||||||
|
this._slim = value;
|
||||||
|
this.modelListeners.forEach(listener => listener());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -314,10 +359,10 @@ class CapeObject extends THREE.Group {
|
||||||
}
|
}
|
||||||
|
|
||||||
class PlayerObject extends THREE.Group {
|
class PlayerObject extends THREE.Group {
|
||||||
constructor(slim, layer1Material, layer2Material, capeMaterial) {
|
constructor(layer1Material, layer2Material, capeMaterial) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this.skin = new SkinObject(slim, layer1Material, layer2Material);
|
this.skin = new SkinObject(layer1Material, layer2Material);
|
||||||
this.skin.visible = false;
|
this.skin.visible = false;
|
||||||
this.add(this.skin);
|
this.add(this.skin);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -90,7 +90,8 @@ class SkinViewer {
|
||||||
this.renderer.context.getShaderInfoLog = () => ""; // shut firefox up
|
this.renderer.context.getShaderInfoLog = () => ""; // shut firefox up
|
||||||
this.domElement.appendChild(this.renderer.domElement);
|
this.domElement.appendChild(this.renderer.domElement);
|
||||||
|
|
||||||
this.playerObject = new PlayerObject(options.slim === true, this.layer1Material, this.layer2Material, this.capeMaterial);
|
this.playerObject = new PlayerObject(this.layer1Material, this.layer2Material, this.capeMaterial);
|
||||||
|
this.playerObject.skin.slim = options.slim === true;
|
||||||
this.scene.add(this.playerObject);
|
this.scene.add(this.playerObject);
|
||||||
|
|
||||||
// texture loading
|
// texture loading
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import * as THREE from "three";
|
import * as THREE from "three";
|
||||||
|
|
||||||
export class SkinObject extends THREE.Group {
|
export class SkinObject extends THREE.Group {
|
||||||
public readonly slim: boolean;
|
public slim: boolean;
|
||||||
public readonly head: THREE.Group;
|
public readonly head: THREE.Group;
|
||||||
public readonly body: THREE.Group;
|
public readonly body: THREE.Group;
|
||||||
public readonly rightArm: THREE.Group;
|
public readonly rightArm: THREE.Group;
|
||||||
|
|
@ -10,7 +10,6 @@ export class SkinObject extends THREE.Group {
|
||||||
public readonly leftLeg: THREE.Group;
|
public readonly leftLeg: THREE.Group;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
slim: boolean,
|
|
||||||
layer1Material: THREE.Material,
|
layer1Material: THREE.Material,
|
||||||
layer2Material: THREE.Material,
|
layer2Material: THREE.Material,
|
||||||
);
|
);
|
||||||
|
|
@ -27,7 +26,6 @@ export class PlayerObject extends THREE.Group {
|
||||||
public readonly cape: CapeObject;
|
public readonly cape: CapeObject;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
slim: boolean,
|
|
||||||
layer1Material: THREE.Material,
|
layer1Material: THREE.Material,
|
||||||
layer2Material: THREE.Material,
|
layer2Material: THREE.Material,
|
||||||
capeMaterial: THREE.Material,
|
capeMaterial: THREE.Material,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue