From 7ab607a3a03bf9d82e3cc0e73188dbbca75e2102 Mon Sep 17 00:00:00 2001 From: Fogrexon Date: Thu, 17 Mar 2022 12:34:58 +0900 Subject: [PATCH 01/17] initialize spring bone --- packages/three-vrm/src/VRM.ts | 19 +++++++++++++++++++ .../three-vrm/src/springbone/VRMSpringBone.ts | 7 +------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/packages/three-vrm/src/VRM.ts b/packages/three-vrm/src/VRM.ts index f24531439..f9ce8e66d 100644 --- a/packages/three-vrm/src/VRM.ts +++ b/packages/three-vrm/src/VRM.ts @@ -103,6 +103,8 @@ export class VRM { */ public readonly springBoneManager?: VRMSpringBoneManager; + private _initializedFlag = false; + /** * Create a new VRM instance. * @@ -119,6 +121,19 @@ export class VRM { this.meta = params.meta; } + /** + * Initialize spring bone. + * + * This function is called just before first update function. + * + * @param delta deltaTime + */ + private _initialize() { + if (this.springBoneManager) { + this.springBoneManager.reset(); + } + } + /** * **You need to call this on your update loop.** * @@ -127,6 +142,10 @@ export class VRM { * @param delta deltaTime */ public update(delta: number): void { + if (!this._initializedFlag) { + this._initialize(); + this._initializedFlag = true; + } if (this.lookAt) { this.lookAt.update(delta); } diff --git a/packages/three-vrm/src/springbone/VRMSpringBone.ts b/packages/three-vrm/src/springbone/VRMSpringBone.ts index 11b3d8bd2..3123f0cd8 100644 --- a/packages/three-vrm/src/springbone/VRMSpringBone.ts +++ b/packages/three-vrm/src/springbone/VRMSpringBone.ts @@ -200,10 +200,6 @@ export class VRMSpringBone { this._initialLocalChildPosition.copy(firstChild.position); } - this.bone.localToWorld(this._currentTail.copy(this._initialLocalChildPosition)); - this._prevTail.copy(this._currentTail); - this._nextTail.copy(this._currentTail); - this._boneAxis.copy(this._initialLocalChildPosition).normalize(); this._centerSpaceBoneLength = _v3A .copy(this._initialLocalChildPosition) @@ -222,8 +218,7 @@ export class VRMSpringBone { this.bone.quaternion.copy(this._initialLocalRotation); // We need to update its matrixWorld manually, since we tweaked the bone by our hand - this.bone.updateMatrix(); - this.bone.matrixWorld.multiplyMatrices(this._getParentMatrixWorld(), this.bone.matrix); + this.bone.updateWorldMatrix(true, false); this._centerSpacePosition.setFromMatrixPosition(this.bone.matrixWorld); // Apply updated position to tail states From cf54da1987523438218a8a7daac9bc906f3ac3b8 Mon Sep 17 00:00:00 2001 From: Fogrexon Date: Thu, 17 Mar 2022 12:41:52 +0900 Subject: [PATCH 02/17] remove comment --- packages/three-vrm/src/VRM.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/three-vrm/src/VRM.ts b/packages/three-vrm/src/VRM.ts index f9ce8e66d..8b2baa8ca 100644 --- a/packages/three-vrm/src/VRM.ts +++ b/packages/three-vrm/src/VRM.ts @@ -125,8 +125,6 @@ export class VRM { * Initialize spring bone. * * This function is called just before first update function. - * - * @param delta deltaTime */ private _initialize() { if (this.springBoneManager) { From 78cf898d3e6f083fe044a4b58afeeba4d953c795 Mon Sep 17 00:00:00 2001 From: Fogrexon Date: Thu, 17 Mar 2022 15:10:20 +0900 Subject: [PATCH 03/17] undo reset changes --- packages/three-vrm/src/springbone/VRMSpringBone.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/three-vrm/src/springbone/VRMSpringBone.ts b/packages/three-vrm/src/springbone/VRMSpringBone.ts index 3123f0cd8..916a86ada 100644 --- a/packages/three-vrm/src/springbone/VRMSpringBone.ts +++ b/packages/three-vrm/src/springbone/VRMSpringBone.ts @@ -218,7 +218,8 @@ export class VRMSpringBone { this.bone.quaternion.copy(this._initialLocalRotation); // We need to update its matrixWorld manually, since we tweaked the bone by our hand - this.bone.updateWorldMatrix(true, false); + this.bone.updateMatrix(); + this.bone.matrixWorld.multiplyMatrices(this._getParentMatrixWorld(), this.bone.matrix); this._centerSpacePosition.setFromMatrixPosition(this.bone.matrixWorld); // Apply updated position to tail states From 88caa4c5188d083acccf96d7f88c17702079bfc2 Mon Sep 17 00:00:00 2001 From: Fogrexon Date: Thu, 17 Mar 2022 15:14:40 +0900 Subject: [PATCH 04/17] initialize --- packages/three-vrm/src/VRM.ts | 17 ----------------- .../src/springbone/VRMSpringBoneManager.ts | 3 +++ 2 files changed, 3 insertions(+), 17 deletions(-) diff --git a/packages/three-vrm/src/VRM.ts b/packages/three-vrm/src/VRM.ts index 8b2baa8ca..f24531439 100644 --- a/packages/three-vrm/src/VRM.ts +++ b/packages/three-vrm/src/VRM.ts @@ -103,8 +103,6 @@ export class VRM { */ public readonly springBoneManager?: VRMSpringBoneManager; - private _initializedFlag = false; - /** * Create a new VRM instance. * @@ -121,17 +119,6 @@ export class VRM { this.meta = params.meta; } - /** - * Initialize spring bone. - * - * This function is called just before first update function. - */ - private _initialize() { - if (this.springBoneManager) { - this.springBoneManager.reset(); - } - } - /** * **You need to call this on your update loop.** * @@ -140,10 +127,6 @@ export class VRM { * @param delta deltaTime */ public update(delta: number): void { - if (!this._initializedFlag) { - this._initialize(); - this._initializedFlag = true; - } if (this.lookAt) { this.lookAt.update(delta); } diff --git a/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts b/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts index 22377a10d..b4ea5eca7 100644 --- a/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts +++ b/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts @@ -12,6 +12,7 @@ export type VRMSpringBoneGroup = VRMSpringBone[]; export class VRMSpringBoneManager { public readonly colliderGroups: VRMSpringBoneColliderGroup[] = []; public readonly springBoneGroupList: VRMSpringBoneGroup[] = []; + private _needInitialize = true /** * Create a new [[VRMSpringBoneManager]] @@ -44,9 +45,11 @@ export class VRMSpringBoneManager { public lateUpdate(delta: number): void { this.springBoneGroupList.forEach((springBoneGroup) => { springBoneGroup.forEach((springBone) => { + if (this._needInitialize) springBone.reset() springBone.update(delta); }); }); + this._needInitialize = false } /** From 886c1f3b630523b364b549b8064f064329bbf775 Mon Sep 17 00:00:00 2001 From: Fogrexon Date: Thu, 17 Mar 2022 15:19:54 +0900 Subject: [PATCH 05/17] add matrix update --- .../src/springbone/VRMSpringBoneManager.ts | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts b/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts index b4ea5eca7..5910b1fcf 100644 --- a/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts +++ b/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts @@ -37,14 +37,35 @@ export class VRMSpringBoneManager { }); } + /** + * Update worldMatrix of springbone's ancestors + * called before update springbone + * @param updated Set of node which worldMatrix is updated. + * @param node target born node. + */ + public updateParentMatrix(updated: Set, node: THREE.Object3D): void { + if (updated.has(node)) return; + node.updateMatrix() + if(node.parent) { + this.updateParentMatrix(updated, node.parent) + node.matrixWorld.multiplyMatrices(node.parent.matrixWorld, node.matrix) + } else { + node.matrixWorld.copy(node.matrix) + } + updated.add(node) + } + /** * Update every spring bone attached to this manager. * * @param delta deltaTime */ public lateUpdate(delta: number): void { + const matrixUpdated = new Set(); + this.springBoneGroupList.forEach((springBoneGroup) => { springBoneGroup.forEach((springBone) => { + this.updateParentMatrix(matrixUpdated, springBone.bone) if (this._needInitialize) springBone.reset() springBone.update(delta); }); From dc48667cc668b64f41c908e1c4d67a79fea702de Mon Sep 17 00:00:00 2001 From: Fogrexon Date: Thu, 17 Mar 2022 15:57:04 +0900 Subject: [PATCH 06/17] rename and change visible --- packages/three-vrm/src/springbone/VRMSpringBoneManager.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts b/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts index 5910b1fcf..56cf3e441 100644 --- a/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts +++ b/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts @@ -43,11 +43,11 @@ export class VRMSpringBoneManager { * @param updated Set of node which worldMatrix is updated. * @param node target born node. */ - public updateParentMatrix(updated: Set, node: THREE.Object3D): void { + private _updateParentMatrix(updated: Set, node: THREE.Object3D): void { if (updated.has(node)) return; node.updateMatrix() if(node.parent) { - this.updateParentMatrix(updated, node.parent) + this._updateParentMatrix(updated, node.parent) node.matrixWorld.multiplyMatrices(node.parent.matrixWorld, node.matrix) } else { node.matrixWorld.copy(node.matrix) @@ -61,11 +61,11 @@ export class VRMSpringBoneManager { * @param delta deltaTime */ public lateUpdate(delta: number): void { - const matrixUpdated = new Set(); + const updatedObjectSet = new Set(); this.springBoneGroupList.forEach((springBoneGroup) => { springBoneGroup.forEach((springBone) => { - this.updateParentMatrix(matrixUpdated, springBone.bone) + this._updateParentMatrix(updatedObjectSet, springBone.bone) if (this._needInitialize) springBone.reset() springBone.update(delta); }); From 131a62278c2475fd1f5939ee12d6df8940e2c2e2 Mon Sep 17 00:00:00 2001 From: Fogrexon Date: Thu, 17 Mar 2022 16:07:35 +0900 Subject: [PATCH 07/17] refactor update parent matrix --- .../src/springbone/VRMSpringBoneManager.ts | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts b/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts index 56cf3e441..b85523ca8 100644 --- a/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts +++ b/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts @@ -40,19 +40,17 @@ export class VRMSpringBoneManager { /** * Update worldMatrix of springbone's ancestors * called before update springbone - * @param updated Set of node which worldMatrix is updated. + * @param updatedObjectSet Set of node which worldMatrix is updated. * @param node target born node. */ - private _updateParentMatrix(updated: Set, node: THREE.Object3D): void { - if (updated.has(node)) return; - node.updateMatrix() - if(node.parent) { - this._updateParentMatrix(updated, node.parent) - node.matrixWorld.multiplyMatrices(node.parent.matrixWorld, node.matrix) - } else { - node.matrixWorld.copy(node.matrix) - } - updated.add(node) + private _updateParentMatrix(updatedObjectSet: Set, node: THREE.Object3D): void { + if (updatedObjectSet.has(node)) return; + + if (node.matrixAutoUpdate) node.updateMatrix() + if (node.parent) this._updateParentMatrix(updatedObjectSet, node.parent) + node.updateWorldMatrix(false, false) + + updatedObjectSet.add(node) } /** From 5c75558dc3f2de5383ab2f57bf37b8418012ceea Mon Sep 17 00:00:00 2001 From: Fogrexon Date: Thu, 17 Mar 2022 16:08:44 +0900 Subject: [PATCH 08/17] lint --- .../src/springbone/VRMSpringBoneManager.ts | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts b/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts index b85523ca8..5d1206e4c 100644 --- a/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts +++ b/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts @@ -12,7 +12,7 @@ export type VRMSpringBoneGroup = VRMSpringBone[]; export class VRMSpringBoneManager { public readonly colliderGroups: VRMSpringBoneColliderGroup[] = []; public readonly springBoneGroupList: VRMSpringBoneGroup[] = []; - private _needInitialize = true + private _needInitialize = true; /** * Create a new [[VRMSpringBoneManager]] @@ -45,12 +45,12 @@ export class VRMSpringBoneManager { */ private _updateParentMatrix(updatedObjectSet: Set, node: THREE.Object3D): void { if (updatedObjectSet.has(node)) return; - - if (node.matrixAutoUpdate) node.updateMatrix() - if (node.parent) this._updateParentMatrix(updatedObjectSet, node.parent) - node.updateWorldMatrix(false, false) - - updatedObjectSet.add(node) + + if (node.matrixAutoUpdate) node.updateMatrix(); + if (node.parent) this._updateParentMatrix(updatedObjectSet, node.parent); + node.updateWorldMatrix(false, false); + + updatedObjectSet.add(node); } /** @@ -63,12 +63,12 @@ export class VRMSpringBoneManager { this.springBoneGroupList.forEach((springBoneGroup) => { springBoneGroup.forEach((springBone) => { - this._updateParentMatrix(updatedObjectSet, springBone.bone) - if (this._needInitialize) springBone.reset() + this._updateParentMatrix(updatedObjectSet, springBone.bone); + if (this._needInitialize) springBone.reset(); springBone.update(delta); }); }); - this._needInitialize = false + this._needInitialize = false; } /** From af1efc9213393f6f1958b987fae0fdcb07f91084 Mon Sep 17 00:00:00 2001 From: Fogrexon Date: Thu, 17 Mar 2022 16:10:22 +0900 Subject: [PATCH 09/17] typo --- packages/three-vrm/src/springbone/VRMSpringBoneManager.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts b/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts index 5d1206e4c..86fd69162 100644 --- a/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts +++ b/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts @@ -41,7 +41,7 @@ export class VRMSpringBoneManager { * Update worldMatrix of springbone's ancestors * called before update springbone * @param updatedObjectSet Set of node which worldMatrix is updated. - * @param node target born node. + * @param node target bone node. */ private _updateParentMatrix(updatedObjectSet: Set, node: THREE.Object3D): void { if (updatedObjectSet.has(node)) return; From 78aa6597e90d3dad620ea19173e426ffe565b6b6 Mon Sep 17 00:00:00 2001 From: Fogrexon Date: Thu, 17 Mar 2022 16:14:39 +0900 Subject: [PATCH 10/17] remove updateMatrix --- packages/three-vrm/src/springbone/VRMSpringBoneManager.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts b/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts index 86fd69162..04e827c3a 100644 --- a/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts +++ b/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts @@ -46,7 +46,6 @@ export class VRMSpringBoneManager { private _updateParentMatrix(updatedObjectSet: Set, node: THREE.Object3D): void { if (updatedObjectSet.has(node)) return; - if (node.matrixAutoUpdate) node.updateMatrix(); if (node.parent) this._updateParentMatrix(updatedObjectSet, node.parent); node.updateWorldMatrix(false, false); From 70fe64498b20914eedfd914c1730e689c24353ea Mon Sep 17 00:00:00 2001 From: Fogrexon Date: Thu, 17 Mar 2022 17:01:41 +0900 Subject: [PATCH 11/17] extract initialize spring bone --- .../src/springbone/VRMSpringBoneManager.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts b/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts index 04e827c3a..82145c2cd 100644 --- a/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts +++ b/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts @@ -12,7 +12,6 @@ export type VRMSpringBoneGroup = VRMSpringBone[]; export class VRMSpringBoneManager { public readonly colliderGroups: VRMSpringBoneColliderGroup[] = []; public readonly springBoneGroupList: VRMSpringBoneGroup[] = []; - private _needInitialize = true; /** * Create a new [[VRMSpringBoneManager]] @@ -52,6 +51,21 @@ export class VRMSpringBoneManager { updatedObjectSet.add(node); } + /** + * Initialize spring bone's position in world coordinate. + * called by user before first update + */ + public initialize(): void { + const updatedObjectSet = new Set(); + + this.springBoneGroupList.forEach((springBoneGroup) => { + springBoneGroup.forEach((springBone) => { + this._updateParentMatrix(updatedObjectSet, springBone.bone); + springBone.reset(); + }); + }); + } + /** * Update every spring bone attached to this manager. * @@ -63,11 +77,9 @@ export class VRMSpringBoneManager { this.springBoneGroupList.forEach((springBoneGroup) => { springBoneGroup.forEach((springBone) => { this._updateParentMatrix(updatedObjectSet, springBone.bone); - if (this._needInitialize) springBone.reset(); springBone.update(delta); }); }); - this._needInitialize = false; } /** From 80eb25117ba1393da6f13a297330f0539d2fc7bf Mon Sep 17 00:00:00 2001 From: Fogrexon Date: Thu, 17 Mar 2022 17:02:26 +0900 Subject: [PATCH 12/17] update examples --- packages/three-vrm/examples/animations.html | 1 + packages/three-vrm/examples/basic.html | 1 + packages/three-vrm/examples/blendshapes.html | 1 + packages/three-vrm/examples/bones.html | 1 + packages/three-vrm/examples/debug.html | 1 + packages/three-vrm/examples/dnd.html | 1 + packages/three-vrm/examples/encoding.html | 1 + packages/three-vrm/examples/envmap.html | 1 + packages/three-vrm/examples/firstperson.html | 1 + packages/three-vrm/examples/lookat-advanced.html | 1 + packages/three-vrm/examples/lookat.html | 1 + packages/three-vrm/examples/materials-debug.html | 1 + packages/three-vrm/examples/meta.html | 1 + packages/three-vrm/examples/mouse.html | 1 + 14 files changed, 14 insertions(+) diff --git a/packages/three-vrm/examples/animations.html b/packages/three-vrm/examples/animations.html index cb20fb9f0..d46fcb1f2 100644 --- a/packages/three-vrm/examples/animations.html +++ b/packages/three-vrm/examples/animations.html @@ -68,6 +68,7 @@ currentVrm = vrm; vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI; + vrm.springBoneManager.initialize(); prepareAnimation( vrm ); diff --git a/packages/three-vrm/examples/basic.html b/packages/three-vrm/examples/basic.html index 141e54ef8..aa0e345a0 100644 --- a/packages/three-vrm/examples/basic.html +++ b/packages/three-vrm/examples/basic.html @@ -70,6 +70,7 @@ scene.add( vrm.scene ); vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI; + vrm.springBoneManager.initialize(); } ); diff --git a/packages/three-vrm/examples/blendshapes.html b/packages/three-vrm/examples/blendshapes.html index edfc7fd89..2d6e278d5 100644 --- a/packages/three-vrm/examples/blendshapes.html +++ b/packages/three-vrm/examples/blendshapes.html @@ -67,6 +67,7 @@ currentVrm = vrm; vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI; + vrm.springBoneManager.initialize(); console.log( vrm ); diff --git a/packages/three-vrm/examples/bones.html b/packages/three-vrm/examples/bones.html index e4a7278ed..e0dd6796f 100644 --- a/packages/three-vrm/examples/bones.html +++ b/packages/three-vrm/examples/bones.html @@ -67,6 +67,7 @@ currentVrm = vrm; vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI; + vrm.springBoneManager.initialize(); console.log( vrm ); diff --git a/packages/three-vrm/examples/debug.html b/packages/three-vrm/examples/debug.html index 4c1757eb4..b1dc602b8 100644 --- a/packages/three-vrm/examples/debug.html +++ b/packages/three-vrm/examples/debug.html @@ -70,6 +70,7 @@ scene.add( vrm.scene ); vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI; + vrm.springBoneManager.initialize(); } ); diff --git a/packages/three-vrm/examples/dnd.html b/packages/three-vrm/examples/dnd.html index b253063a6..a5d26c477 100644 --- a/packages/three-vrm/examples/dnd.html +++ b/packages/three-vrm/examples/dnd.html @@ -77,6 +77,7 @@ scene.add( vrm.scene ); vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI; + vrm.springBoneManager.initialize(); console.log( vrm ); diff --git a/packages/three-vrm/examples/encoding.html b/packages/three-vrm/examples/encoding.html index 746ba77ec..856cb0128 100644 --- a/packages/three-vrm/examples/encoding.html +++ b/packages/three-vrm/examples/encoding.html @@ -79,6 +79,7 @@ scene.add( vrm.scene ); vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI; + vrm.springBoneManager.initialize(); } ); diff --git a/packages/three-vrm/examples/envmap.html b/packages/three-vrm/examples/envmap.html index 56e42b624..7ccc21f8b 100644 --- a/packages/three-vrm/examples/envmap.html +++ b/packages/three-vrm/examples/envmap.html @@ -104,6 +104,7 @@ scene.add( vrm.scene ); vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI; + vrm.springBoneManager.initialize(); } ); diff --git a/packages/three-vrm/examples/firstperson.html b/packages/three-vrm/examples/firstperson.html index 226bd5b0c..f3ba72312 100644 --- a/packages/three-vrm/examples/firstperson.html +++ b/packages/three-vrm/examples/firstperson.html @@ -67,6 +67,7 @@ currentVrm = vrm; vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI; + vrm.springBoneManager.initialize(); // generate firstperson mesh vrm.firstPerson.setup(); diff --git a/packages/three-vrm/examples/lookat-advanced.html b/packages/three-vrm/examples/lookat-advanced.html index 3c8792fd2..18844e71d 100644 --- a/packages/three-vrm/examples/lookat-advanced.html +++ b/packages/three-vrm/examples/lookat-advanced.html @@ -139,6 +139,7 @@ currentVrm = vrm; vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI; + vrm.springBoneManager.initialize(); vrm.lookAt.target = camera; diff --git a/packages/three-vrm/examples/lookat.html b/packages/three-vrm/examples/lookat.html index b6386fd0c..fbb61968a 100644 --- a/packages/three-vrm/examples/lookat.html +++ b/packages/three-vrm/examples/lookat.html @@ -71,6 +71,7 @@ currentVrm = vrm; vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI; + vrm.springBoneManager.initialize(); vrm.lookAt.target = lookAtTarget; diff --git a/packages/three-vrm/examples/materials-debug.html b/packages/three-vrm/examples/materials-debug.html index f2587fa96..cd57fc16a 100644 --- a/packages/three-vrm/examples/materials-debug.html +++ b/packages/three-vrm/examples/materials-debug.html @@ -67,6 +67,7 @@ currentVrm = vrm; vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI; + vrm.springBoneManager.initialize(); console.log( vrm ); diff --git a/packages/three-vrm/examples/meta.html b/packages/three-vrm/examples/meta.html index 2cf8327c8..f32e54a96 100644 --- a/packages/three-vrm/examples/meta.html +++ b/packages/three-vrm/examples/meta.html @@ -80,6 +80,7 @@ scene.add( vrm.scene ); vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI; + vrm.springBoneManager.initialize(); // print meta fields Object.keys( vrm.meta ).forEach( ( key ) => { diff --git a/packages/three-vrm/examples/mouse.html b/packages/three-vrm/examples/mouse.html index 7f91c483b..34b9e3503 100644 --- a/packages/three-vrm/examples/mouse.html +++ b/packages/three-vrm/examples/mouse.html @@ -64,6 +64,7 @@ console.log( vrm ); vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI; + vrm.springBoneManager.initialize(); } ); From 9ad0437c559dce7c18134829736bc10f14f2e17f Mon Sep 17 00:00:00 2001 From: Fogrexon Date: Thu, 17 Mar 2022 17:50:13 +0900 Subject: [PATCH 13/17] merge initialize to reset --- packages/three-vrm/examples/animations.html | 2 +- packages/three-vrm/examples/basic.html | 2 +- packages/three-vrm/examples/blendshapes.html | 2 +- packages/three-vrm/examples/bones.html | 2 +- packages/three-vrm/examples/debug.html | 2 +- packages/three-vrm/examples/dnd.html | 2 +- packages/three-vrm/examples/encoding.html | 2 +- packages/three-vrm/examples/envmap.html | 2 +- packages/three-vrm/examples/firstperson.html | 2 +- .../three-vrm/examples/lookat-advanced.html | 2 +- packages/three-vrm/examples/lookat.html | 2 +- .../three-vrm/examples/materials-debug.html | 2 +- packages/three-vrm/examples/meta.html | 2 +- packages/three-vrm/examples/mouse.html | 2 +- .../src/springbone/VRMSpringBoneManager.ts | 18 +++--------------- 15 files changed, 17 insertions(+), 29 deletions(-) diff --git a/packages/three-vrm/examples/animations.html b/packages/three-vrm/examples/animations.html index d46fcb1f2..bd656feb7 100644 --- a/packages/three-vrm/examples/animations.html +++ b/packages/three-vrm/examples/animations.html @@ -68,7 +68,7 @@ currentVrm = vrm; vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI; - vrm.springBoneManager.initialize(); + vrm.springBoneManager.reset(); prepareAnimation( vrm ); diff --git a/packages/three-vrm/examples/basic.html b/packages/three-vrm/examples/basic.html index aa0e345a0..3491500f9 100644 --- a/packages/three-vrm/examples/basic.html +++ b/packages/three-vrm/examples/basic.html @@ -70,7 +70,7 @@ scene.add( vrm.scene ); vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI; - vrm.springBoneManager.initialize(); + vrm.springBoneManager.reset(); } ); diff --git a/packages/three-vrm/examples/blendshapes.html b/packages/three-vrm/examples/blendshapes.html index 2d6e278d5..d01d12e77 100644 --- a/packages/three-vrm/examples/blendshapes.html +++ b/packages/three-vrm/examples/blendshapes.html @@ -67,7 +67,7 @@ currentVrm = vrm; vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI; - vrm.springBoneManager.initialize(); + vrm.springBoneManager.reset(); console.log( vrm ); diff --git a/packages/three-vrm/examples/bones.html b/packages/three-vrm/examples/bones.html index e0dd6796f..cc7c0afd0 100644 --- a/packages/three-vrm/examples/bones.html +++ b/packages/three-vrm/examples/bones.html @@ -67,7 +67,7 @@ currentVrm = vrm; vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI; - vrm.springBoneManager.initialize(); + vrm.springBoneManager.reset(); console.log( vrm ); diff --git a/packages/three-vrm/examples/debug.html b/packages/three-vrm/examples/debug.html index b1dc602b8..b0a94758d 100644 --- a/packages/three-vrm/examples/debug.html +++ b/packages/three-vrm/examples/debug.html @@ -70,7 +70,7 @@ scene.add( vrm.scene ); vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI; - vrm.springBoneManager.initialize(); + vrm.springBoneManager.reset(); } ); diff --git a/packages/three-vrm/examples/dnd.html b/packages/three-vrm/examples/dnd.html index a5d26c477..f44ba4185 100644 --- a/packages/three-vrm/examples/dnd.html +++ b/packages/three-vrm/examples/dnd.html @@ -77,7 +77,7 @@ scene.add( vrm.scene ); vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI; - vrm.springBoneManager.initialize(); + vrm.springBoneManager.reset console.log( vrm ); diff --git a/packages/three-vrm/examples/encoding.html b/packages/three-vrm/examples/encoding.html index 856cb0128..e2f0cc04a 100644 --- a/packages/three-vrm/examples/encoding.html +++ b/packages/three-vrm/examples/encoding.html @@ -79,7 +79,7 @@ scene.add( vrm.scene ); vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI; - vrm.springBoneManager.initialize(); + vrm.springBoneManager.reset(); } ); diff --git a/packages/three-vrm/examples/envmap.html b/packages/three-vrm/examples/envmap.html index 7ccc21f8b..fdc69390e 100644 --- a/packages/three-vrm/examples/envmap.html +++ b/packages/three-vrm/examples/envmap.html @@ -104,7 +104,7 @@ scene.add( vrm.scene ); vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI; - vrm.springBoneManager.initialize(); + vrm.springBoneManager.reset(); } ); diff --git a/packages/three-vrm/examples/firstperson.html b/packages/three-vrm/examples/firstperson.html index f3ba72312..de1a72ec8 100644 --- a/packages/three-vrm/examples/firstperson.html +++ b/packages/three-vrm/examples/firstperson.html @@ -67,7 +67,7 @@ currentVrm = vrm; vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI; - vrm.springBoneManager.initialize(); + vrm.springBoneManager.reset(); // generate firstperson mesh vrm.firstPerson.setup(); diff --git a/packages/three-vrm/examples/lookat-advanced.html b/packages/three-vrm/examples/lookat-advanced.html index 18844e71d..9ee6badd4 100644 --- a/packages/three-vrm/examples/lookat-advanced.html +++ b/packages/three-vrm/examples/lookat-advanced.html @@ -139,7 +139,7 @@ currentVrm = vrm; vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI; - vrm.springBoneManager.initialize(); + vrm.springBoneManager.reset(); vrm.lookAt.target = camera; diff --git a/packages/three-vrm/examples/lookat.html b/packages/three-vrm/examples/lookat.html index fbb61968a..1678b820b 100644 --- a/packages/three-vrm/examples/lookat.html +++ b/packages/three-vrm/examples/lookat.html @@ -71,7 +71,7 @@ currentVrm = vrm; vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI; - vrm.springBoneManager.initialize(); + vrm.springBoneManager.reset(); vrm.lookAt.target = lookAtTarget; diff --git a/packages/three-vrm/examples/materials-debug.html b/packages/three-vrm/examples/materials-debug.html index cd57fc16a..658c8ecad 100644 --- a/packages/three-vrm/examples/materials-debug.html +++ b/packages/three-vrm/examples/materials-debug.html @@ -67,7 +67,7 @@ currentVrm = vrm; vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI; - vrm.springBoneManager.initialize(); + vrm.springBoneManager.reset(); console.log( vrm ); diff --git a/packages/three-vrm/examples/meta.html b/packages/three-vrm/examples/meta.html index f32e54a96..fd68e3b2f 100644 --- a/packages/three-vrm/examples/meta.html +++ b/packages/three-vrm/examples/meta.html @@ -80,7 +80,7 @@ scene.add( vrm.scene ); vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI; - vrm.springBoneManager.initialize(); + vrm.springBoneManager.reset(); // print meta fields Object.keys( vrm.meta ).forEach( ( key ) => { diff --git a/packages/three-vrm/examples/mouse.html b/packages/three-vrm/examples/mouse.html index 34b9e3503..d5ce0c523 100644 --- a/packages/three-vrm/examples/mouse.html +++ b/packages/three-vrm/examples/mouse.html @@ -64,7 +64,7 @@ console.log( vrm ); vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI; - vrm.springBoneManager.initialize(); + vrm.springBoneManager.reset(); } ); diff --git a/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts b/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts index 82145c2cd..c3dec7ecc 100644 --- a/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts +++ b/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts @@ -51,21 +51,6 @@ export class VRMSpringBoneManager { updatedObjectSet.add(node); } - /** - * Initialize spring bone's position in world coordinate. - * called by user before first update - */ - public initialize(): void { - const updatedObjectSet = new Set(); - - this.springBoneGroupList.forEach((springBoneGroup) => { - springBoneGroup.forEach((springBone) => { - this._updateParentMatrix(updatedObjectSet, springBone.bone); - springBone.reset(); - }); - }); - } - /** * Update every spring bone attached to this manager. * @@ -86,8 +71,11 @@ export class VRMSpringBoneManager { * Reset every spring bone attached to this manager. */ public reset(): void { + const updatedObjectSet = new Set(); + this.springBoneGroupList.forEach((springBoneGroup) => { springBoneGroup.forEach((springBone) => { + this._updateParentMatrix(updatedObjectSet, springBone.bone); springBone.reset(); }); }); From 1d1db39a531a0031ecf7f937c3831e1afc4f3608 Mon Sep 17 00:00:00 2001 From: Fogrexon Date: Thu, 17 Mar 2022 17:56:15 +0900 Subject: [PATCH 14/17] fix typo --- packages/three-vrm/examples/dnd.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/three-vrm/examples/dnd.html b/packages/three-vrm/examples/dnd.html index f44ba4185..a7d906c4b 100644 --- a/packages/three-vrm/examples/dnd.html +++ b/packages/three-vrm/examples/dnd.html @@ -77,7 +77,7 @@ scene.add( vrm.scene ); vrm.humanoid.getBoneNode( THREE.VRMSchema.HumanoidBoneName.Hips ).rotation.y = Math.PI; - vrm.springBoneManager.reset + vrm.springBoneManager.reset(); console.log( vrm ); From d977500bd16e708c2dfb8dabbb89963251ed3383 Mon Sep 17 00:00:00 2001 From: Fogrexon Date: Thu, 17 Mar 2022 18:00:57 +0900 Subject: [PATCH 15/17] set tail in constructor --- packages/three-vrm/src/springbone/VRMSpringBone.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/three-vrm/src/springbone/VRMSpringBone.ts b/packages/three-vrm/src/springbone/VRMSpringBone.ts index 916a86ada..ae1b8f1bd 100644 --- a/packages/three-vrm/src/springbone/VRMSpringBone.ts +++ b/packages/three-vrm/src/springbone/VRMSpringBone.ts @@ -200,6 +200,11 @@ 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); + this._boneAxis.copy(this._initialLocalChildPosition).normalize(); this._centerSpaceBoneLength = _v3A .copy(this._initialLocalChildPosition) From aad551e041fad553c19d2091e5f5eaff1eb8faa8 Mon Sep 17 00:00:00 2001 From: Fogrexon Date: Thu, 17 Mar 2022 18:16:08 +0900 Subject: [PATCH 16/17] rename update matrix --- packages/three-vrm/src/springbone/VRMSpringBone.ts | 4 ---- .../three-vrm/src/springbone/VRMSpringBoneManager.ts | 12 ++++++------ 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/packages/three-vrm/src/springbone/VRMSpringBone.ts b/packages/three-vrm/src/springbone/VRMSpringBone.ts index ae1b8f1bd..b8a28d48f 100644 --- a/packages/three-vrm/src/springbone/VRMSpringBone.ts +++ b/packages/three-vrm/src/springbone/VRMSpringBone.ts @@ -242,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を取り出す。 diff --git a/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts b/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts index c3dec7ecc..ccc81e9ab 100644 --- a/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts +++ b/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts @@ -37,15 +37,15 @@ export class VRMSpringBoneManager { } /** - * Update worldMatrix of springbone's ancestors - * called before update springbone + * 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 _updateParentMatrix(updatedObjectSet: Set, node: THREE.Object3D): void { + private _updateWorldMatrix(updatedObjectSet: Set, node: THREE.Object3D): void { if (updatedObjectSet.has(node)) return; - if (node.parent) this._updateParentMatrix(updatedObjectSet, node.parent); + if (node.parent) this._updateWorldMatrix(updatedObjectSet, node.parent); node.updateWorldMatrix(false, false); updatedObjectSet.add(node); @@ -61,7 +61,7 @@ export class VRMSpringBoneManager { this.springBoneGroupList.forEach((springBoneGroup) => { springBoneGroup.forEach((springBone) => { - this._updateParentMatrix(updatedObjectSet, springBone.bone); + this._updateWorldMatrix(updatedObjectSet, springBone.bone); springBone.update(delta); }); }); @@ -75,7 +75,7 @@ export class VRMSpringBoneManager { this.springBoneGroupList.forEach((springBoneGroup) => { springBoneGroup.forEach((springBone) => { - this._updateParentMatrix(updatedObjectSet, springBone.bone); + this._updateWorldMatrix(updatedObjectSet, springBone.bone); springBone.reset(); }); }); From bb4a99f4b3e241d348281c90ea09fd327f79b242 Mon Sep 17 00:00:00 2001 From: Fogrexon Date: Thu, 17 Mar 2022 18:32:49 +0900 Subject: [PATCH 17/17] move private methods under public --- .../src/springbone/VRMSpringBoneManager.ts | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts b/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts index ccc81e9ab..d242f9e20 100644 --- a/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts +++ b/packages/three-vrm/src/springbone/VRMSpringBoneManager.ts @@ -36,21 +36,6 @@ 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, 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. * @@ -80,4 +65,19 @@ 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, node: THREE.Object3D): void { + if (updatedObjectSet.has(node)) return; + + if (node.parent) this._updateWorldMatrix(updatedObjectSet, node.parent); + node.updateWorldMatrix(false, false); + + updatedObjectSet.add(node); + } }