Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize spring bone #946

Merged
merged 17 commits into from
Mar 17, 2022
1 change: 1 addition & 0 deletions packages/three-vrm/examples/animations.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
currentVrm = vrm;

vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI;
vrm.springBoneManager.reset();

prepareAnimation( vrm );

Expand Down
1 change: 1 addition & 0 deletions packages/three-vrm/examples/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
scene.add( vrm.scene );

vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI;
vrm.springBoneManager.reset();

} );

Expand Down
1 change: 1 addition & 0 deletions packages/three-vrm/examples/blendshapes.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
currentVrm = vrm;

vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI;
vrm.springBoneManager.reset();

console.log( vrm );

Expand Down
1 change: 1 addition & 0 deletions packages/three-vrm/examples/bones.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
currentVrm = vrm;

vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI;
vrm.springBoneManager.reset();

console.log( vrm );

Expand Down
1 change: 1 addition & 0 deletions packages/three-vrm/examples/debug.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
scene.add( vrm.scene );

vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI;
vrm.springBoneManager.reset();

} );

Expand Down
1 change: 1 addition & 0 deletions packages/three-vrm/examples/dnd.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
scene.add( vrm.scene );

vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI;
vrm.springBoneManager.reset();

console.log( vrm );

Expand Down
1 change: 1 addition & 0 deletions packages/three-vrm/examples/encoding.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
scene.add( vrm.scene );

vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI;
vrm.springBoneManager.reset();

} );

Expand Down
1 change: 1 addition & 0 deletions packages/three-vrm/examples/envmap.html
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
scene.add( vrm.scene );

vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI;
vrm.springBoneManager.reset();

} );

Expand Down
1 change: 1 addition & 0 deletions packages/three-vrm/examples/firstperson.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
currentVrm = vrm;

vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI;
vrm.springBoneManager.reset();

// generate firstperson mesh
vrm.firstPerson.setup();
Expand Down
1 change: 1 addition & 0 deletions packages/three-vrm/examples/lookat-advanced.html
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
currentVrm = vrm;

vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI;
vrm.springBoneManager.reset();

vrm.lookAt.target = camera;

Expand Down
1 change: 1 addition & 0 deletions packages/three-vrm/examples/lookat.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
currentVrm = vrm;

vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI;
vrm.springBoneManager.reset();

vrm.lookAt.target = lookAtTarget;

Expand Down
1 change: 1 addition & 0 deletions packages/three-vrm/examples/materials-debug.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
currentVrm = vrm;

vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI;
vrm.springBoneManager.reset();

console.log( vrm );

Expand Down
1 change: 1 addition & 0 deletions packages/three-vrm/examples/meta.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
scene.add( vrm.scene );

vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI;
vrm.springBoneManager.reset();

// print meta fields
Object.keys( vrm.meta ).forEach( ( key ) => {
Expand Down
1 change: 1 addition & 0 deletions packages/three-vrm/examples/mouse.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
console.log( vrm );

vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI;
vrm.springBoneManager.reset();

} );

Expand Down
5 changes: 1 addition & 4 deletions packages/three-vrm/src/springbone/VRMSpringBone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ export class VRMSpringBone {
this._initialLocalChildPosition.copy(firstChild.position);
}

// Apply updated position to tail states
this.bone.localToWorld(this._currentTail.copy(this._initialLocalChildPosition));
this._prevTail.copy(this._currentTail);
this._nextTail.copy(this._currentTail);
Expand Down Expand Up @@ -241,10 +242,6 @@ export class VRMSpringBone {
public update(delta: number): void {
if (delta <= 0) return;

// 親スプリングボーンの姿勢は常に変化している。
// それに基づいて処理直前に自分のworldMatrixを更新しておく
this.bone.matrixWorld.multiplyMatrices(this._getParentMatrixWorld(), this.bone.matrix);

if (this.bone.parent) {
// SpringBoneは親から順に処理されていくため、
// 親のmatrixWorldは最新状態の前提でworldMatrixからquaternionを取り出す。
Expand Down
21 changes: 21 additions & 0 deletions packages/three-vrm/src/springbone/VRMSpringBoneManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,32 @@ export class VRMSpringBoneManager {
});
}

/**
* Update worldMatrix of given object, respecting its ancestors.
* called before update springbone.
* @param updatedObjectSet Set of node which worldMatrix is updated.
* @param node target bone node.
*/
private _updateWorldMatrix(updatedObjectSet: Set<THREE.Object3D>, node: THREE.Object3D): void {
if (updatedObjectSet.has(node)) return;

if (node.parent) this._updateWorldMatrix(updatedObjectSet, node.parent);
node.updateWorldMatrix(false, false);

updatedObjectSet.add(node);
}

/**
* Update every spring bone attached to this manager.
*
* @param delta deltaTime
*/
public lateUpdate(delta: number): void {
const updatedObjectSet = new Set<THREE.Object3D>();

this.springBoneGroupList.forEach((springBoneGroup) => {
springBoneGroup.forEach((springBone) => {
this._updateWorldMatrix(updatedObjectSet, springBone.bone);
springBone.update(delta);
});
});
Expand All @@ -53,8 +71,11 @@ export class VRMSpringBoneManager {
* Reset every spring bone attached to this manager.
*/
public reset(): void {
const updatedObjectSet = new Set<THREE.Object3D>();

this.springBoneGroupList.forEach((springBoneGroup) => {
springBoneGroup.forEach((springBone) => {
this._updateWorldMatrix(updatedObjectSet, springBone.bone);
springBone.reset();
});
});
Expand Down