From 747a05667691ae60978385eb95a5d4bb036a4296 Mon Sep 17 00:00:00 2001 From: memelotsqui Date: Tue, 9 Aug 2022 14:47:08 -0500 Subject: [PATCH 01/14] removing multiple mixers, adding only 1 to manage all animations --- type_templates/glb.js | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/type_templates/glb.js b/type_templates/glb.js index 81ede9b..69bc7b2 100644 --- a/type_templates/glb.js +++ b/type_templates/glb.js @@ -27,7 +27,8 @@ export default e => { } app.glb = null; - const animationMixers = []; + let mixer = null; + const actions = []; const uvScrolls = []; const physicsIds = []; app.physicsIds = physicsIds; @@ -77,26 +78,20 @@ export default e => { const _loadHubsComponents = () => { const _loadAnimations = () => { const animationEnabled = !!(app.getComponent('animation') ?? true); - if (animationEnabled) { - o.traverse(o => { - // if (o.isMesh) { - const idleAnimation = animations.find(a => a.name === 'idle'); - let clip = idleAnimation || animations[animationMixers.length]; - if (clip) { - const mixer = new THREE.AnimationMixer(o); - - const action = mixer.clipAction(clip); - action.play(); - animationMixers.push(mixer); - } - // } - }); + if (animationEnabled && animations.length > 0){ + mixer = new THREE.AnimationMixer(o); + for (let i =0 ; i < animations.length; i++){ + actions.push(mixer.clipAction(animations[i])); + } + const idleAction = actions.filter(a => a._clip.name.toLowerCase() === 'idle')[0] || actions[0]; + idleAction.play(); } + }; const petComponent = app.getComponent('pet'); if (!petComponent) { - _loadAnimations(); + _loadAnimations(); } const _loadLightmaps = () => { @@ -290,7 +285,7 @@ export default e => { useFrame(({timestamp, timeDiff}) => { const _updateAnimation = () => { const deltaSeconds = timeDiff / 1000; - for (const mixer of animationMixers) { + if (mixer){ mixer.update(deltaSeconds); app.updateMatrixWorld(); } @@ -317,11 +312,10 @@ export default e => { }); app.stop = () => { - for (const mixer of animationMixers) { - console.log('got mixer', mixer); + if (mixer){ mixer.stopAllAction(); + mixer = null; } - animationMixers.length = 0; }; return app; From d5cdc17ded8cae58a2de29b790faf0bcb5e0806f Mon Sep 17 00:00:00 2001 From: memelotsqui Date: Tue, 9 Aug 2022 15:38:32 -0500 Subject: [PATCH 02/14] playing all animations instead of the first one to keep consistency --- type_templates/glb.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/type_templates/glb.js b/type_templates/glb.js index 69bc7b2..2cfd127 100644 --- a/type_templates/glb.js +++ b/type_templates/glb.js @@ -84,8 +84,11 @@ export default e => { for (let i =0 ; i < animations.length; i++){ actions.push(mixer.clipAction(animations[i])); } - const idleAction = actions.filter(a => a._clip.name.toLowerCase() === 'idle')[0] || actions[0]; - idleAction.play(); + const act = actions.filter(a => a._clip.name.toLowerCase() === 'idle')[0]; + const idleAction = act ? [act] : actions; + for (let i =0 ; i < idleAction.length; i++){ + idleAction[i].play(); + } } }; From 49d67fcc5304c591ba9ade63416ae65944035766 Mon Sep 17 00:00:00 2001 From: memelotsqui Date: Tue, 9 Aug 2022 16:23:44 -0500 Subject: [PATCH 03/14] extracted fetch actions by string to be reusable --- type_templates/glb.js | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/type_templates/glb.js b/type_templates/glb.js index 2cfd127..b7f8cf5 100644 --- a/type_templates/glb.js +++ b/type_templates/glb.js @@ -15,6 +15,7 @@ const localMatrix = new THREE.Matrix4(); */ // const z180Quaternion = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0, 1, 0), Math.PI); + export default e => { const app = useApp(); @@ -29,6 +30,7 @@ export default e => { app.glb = null; let mixer = null; const actions = []; + let currentActions = []; const uvScrolls = []; const physicsIds = []; app.physicsIds = physicsIds; @@ -84,10 +86,10 @@ export default e => { for (let i =0 ; i < animations.length; i++){ actions.push(mixer.clipAction(animations[i])); } - const act = actions.filter(a => a._clip.name.toLowerCase() === 'idle')[0]; - const idleAction = act ? [act] : actions; - for (let i =0 ; i < idleAction.length; i++){ - idleAction[i].play(); + const idleAction = _getActions(['idle']); + currentActions = idleAction.length > 0 ? idleAction : actions; + for (let i =0 ; i < currentActions.length; i++){ + currentActions[i].play(); } } @@ -252,6 +254,16 @@ export default e => { } } }; + const _getActions = (actionStrings) => { + const result = []; + if (actionStrings.length){ + for (let i =0; i < actionStrings.length ; i++){ + const act = actions.filter(a => a._clip.name.toLowerCase() === actionStrings[i])[0]; + if (act) result.push(act); + } + } + return result; + } app.addEventListener('wearupdate', e => { if (e.wear) { if (app.glb) { @@ -320,6 +332,12 @@ export default e => { mixer = null; } }; + + app.playClips = (clips, time) => { + if (mixer){ + + } + } return app; }; From 8cfc5dd999671de612346157506e47fcf8c668fd Mon Sep 17 00:00:00 2001 From: memelotsqui Date: Tue, 9 Aug 2022 18:21:16 -0500 Subject: [PATCH 04/14] Ass play animations call --- type_templates/glb.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/type_templates/glb.js b/type_templates/glb.js index b7f8cf5..b293e5d 100644 --- a/type_templates/glb.js +++ b/type_templates/glb.js @@ -256,10 +256,13 @@ export default e => { }; const _getActions = (actionStrings) => { const result = []; + actionStrings = actionStrings.length ? actionStrings :[actionStrings] if (actionStrings.length){ for (let i =0; i < actionStrings.length ; i++){ - const act = actions.filter(a => a._clip.name.toLowerCase() === actionStrings[i])[0]; - if (act) result.push(act); + if (actionStrings[i]){ + const act = actions.filter(a => a._clip.name.toLowerCase() === actionStrings[i].toLowerCase())[0]; + if (act) result.push(act); + } } } return result; @@ -333,9 +336,19 @@ export default e => { } }; - app.playClips = (clips, time) => { + app.playAnimations = (animationStrings, transitionTime = 0.1) => { if (mixer){ - + const nextActions = animationStrings ? _getActions(animationStrings) : actions; + console.log(nextActions) + for (let i =0 ; i < currentActions.length; i++){ + currentActions[i].fadeOut(transitionTime); + } + for (let i =0 ; i < nextActions.length; i++){ + nextActions[i].reset(); + nextActions[i].play(); + nextActions[i].fadeIn(transitionTime); + } + currentActions = nextActions; } } From 93d403587b4c135ba2f25407f821762d6854fc99 Mon Sep 17 00:00:00 2001 From: memelotsqui Date: Tue, 9 Aug 2022 18:22:12 -0500 Subject: [PATCH 05/14] Add Array validation --- type_templates/glb.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/type_templates/glb.js b/type_templates/glb.js index b293e5d..b907037 100644 --- a/type_templates/glb.js +++ b/type_templates/glb.js @@ -256,13 +256,11 @@ export default e => { }; const _getActions = (actionStrings) => { const result = []; - actionStrings = actionStrings.length ? actionStrings :[actionStrings] - if (actionStrings.length){ - for (let i =0; i < actionStrings.length ; i++){ - if (actionStrings[i]){ - const act = actions.filter(a => a._clip.name.toLowerCase() === actionStrings[i].toLowerCase())[0]; - if (act) result.push(act); - } + actionStrings = actionStrings.length ? actionStrings : [actionStrings]; + for (let i =0; i < actionStrings.length ; i++){ + if (actionStrings[i]){ + const act = actions.filter(a => a._clip.name.toLowerCase() === actionStrings[i].toLowerCase())[0]; + if (act) result.push(act); } } return result; From 5953f6b99aa198f5ee166bd6313c6f24bba58435 Mon Sep 17 00:00:00 2001 From: memelotsqui Date: Tue, 9 Aug 2022 18:54:05 -0500 Subject: [PATCH 06/14] Add further validations and warnings --- type_templates/glb.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/type_templates/glb.js b/type_templates/glb.js index b907037..786d038 100644 --- a/type_templates/glb.js +++ b/type_templates/glb.js @@ -256,13 +256,15 @@ export default e => { }; const _getActions = (actionStrings) => { const result = []; - actionStrings = actionStrings.length ? actionStrings : [actionStrings]; + actionStrings = Array.isArray(actionStrings) ? actionStrings : [actionStrings]; for (let i =0; i < actionStrings.length ; i++){ - if (actionStrings[i]){ + if (typeof actionStrings[i] === "string"){ const act = actions.filter(a => a._clip.name.toLowerCase() === actionStrings[i].toLowerCase())[0]; if (act) result.push(act); } } + if (result.length === 0) + console.warn("No animations found"); return result; } app.addEventListener('wearupdate', e => { @@ -337,7 +339,6 @@ export default e => { app.playAnimations = (animationStrings, transitionTime = 0.1) => { if (mixer){ const nextActions = animationStrings ? _getActions(animationStrings) : actions; - console.log(nextActions) for (let i =0 ; i < currentActions.length; i++){ currentActions[i].fadeOut(transitionTime); } From 326250c165d6dd6a11c0eebd2d05d5a654bd574b Mon Sep 17 00:00:00 2001 From: memelotsqui Date: Tue, 9 Aug 2022 19:19:28 -0500 Subject: [PATCH 07/14] added loop argument and animation clamp on end --- type_templates/glb.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/type_templates/glb.js b/type_templates/glb.js index 786d038..7dabbce 100644 --- a/type_templates/glb.js +++ b/type_templates/glb.js @@ -82,11 +82,12 @@ export default e => { const animationEnabled = !!(app.getComponent('animation') ?? true); if (animationEnabled && animations.length > 0){ - mixer = new THREE.AnimationMixer(o); + mixer = new THREE.AnimationMixer(o); // create the animation mixer with the root of the glb file for (let i =0 ; i < animations.length; i++){ actions.push(mixer.clipAction(animations[i])); + actions[i].clampWhenFinished = true; // make sure to stop it in the last frame just in case loop is set to false } - const idleAction = _getActions(['idle']); + const idleAction = _getActions('idle'); currentActions = idleAction.length > 0 ? idleAction : actions; for (let i =0 ; i < currentActions.length; i++){ currentActions[i].play(); @@ -264,7 +265,7 @@ export default e => { } } if (result.length === 0) - console.warn("No animations found"); + console.warn("No animations found: " + actionStrings); return result; } app.addEventListener('wearupdate', e => { @@ -336,13 +337,14 @@ export default e => { } }; - app.playAnimations = (animationStrings, transitionTime = 0.1) => { + app.playAnimations = (animationStrings, transitionTime = 0.1, animationLoop = true) => { if (mixer){ const nextActions = animationStrings ? _getActions(animationStrings) : actions; for (let i =0 ; i < currentActions.length; i++){ currentActions[i].fadeOut(transitionTime); } for (let i =0 ; i < nextActions.length; i++){ + nextActions[i].loop = animationLoop ? THREE.LoopRepeat : THREE.LoopOnce; nextActions[i].reset(); nextActions[i].play(); nextActions[i].fadeIn(transitionTime); From 5f16144de22498e2ed897a092482316d3af30342 Mon Sep 17 00:00:00 2001 From: memelotsqui Date: Tue, 9 Aug 2022 20:19:37 -0500 Subject: [PATCH 08/14] Add More descriptive Warning when no animation was found --- type_templates/glb.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/type_templates/glb.js b/type_templates/glb.js index 7dabbce..5a00076 100644 --- a/type_templates/glb.js +++ b/type_templates/glb.js @@ -265,7 +265,7 @@ export default e => { } } if (result.length === 0) - console.warn("No animations found: " + actionStrings); + console.warn("No animation(s) found with name(s): " + actionStrings); return result; } app.addEventListener('wearupdate', e => { From d003a27668e195634c01d37d046d6f3419e38b57 Mon Sep 17 00:00:00 2001 From: memelotsqui Date: Wed, 10 Aug 2022 11:19:32 -0500 Subject: [PATCH 09/14] remove playAnimations api call --- type_templates/glb.js | 39 +++++++++++---------------------------- 1 file changed, 11 insertions(+), 28 deletions(-) diff --git a/type_templates/glb.js b/type_templates/glb.js index 5a00076..1ec4ebe 100644 --- a/type_templates/glb.js +++ b/type_templates/glb.js @@ -28,9 +28,8 @@ export default e => { } app.glb = null; - let mixer = null; + app.mixer = null; const actions = []; - let currentActions = []; const uvScrolls = []; const physicsIds = []; app.physicsIds = physicsIds; @@ -82,13 +81,13 @@ export default e => { const animationEnabled = !!(app.getComponent('animation') ?? true); if (animationEnabled && animations.length > 0){ - mixer = new THREE.AnimationMixer(o); // create the animation mixer with the root of the glb file + app.mixer = new THREE.AnimationMixer(o); // create the animation mixer with the root of the glb file for (let i =0 ; i < animations.length; i++){ - actions.push(mixer.clipAction(animations[i])); + actions.push(app.mixer.clipAction(animations[i])); actions[i].clampWhenFinished = true; // make sure to stop it in the last frame just in case loop is set to false } const idleAction = _getActions('idle'); - currentActions = idleAction.length > 0 ? idleAction : actions; + const currentActions = idleAction.length > 0 ? idleAction : actions; for (let i =0 ; i < currentActions.length; i++){ currentActions[i].play(); } @@ -264,8 +263,8 @@ export default e => { if (act) result.push(act); } } - if (result.length === 0) - console.warn("No animation(s) found with name(s): " + actionStrings); + //if (result.length === 0) + //console.warn("No animation(s) found with name(s): " + actionStrings); return result; } app.addEventListener('wearupdate', e => { @@ -304,8 +303,8 @@ export default e => { useFrame(({timestamp, timeDiff}) => { const _updateAnimation = () => { const deltaSeconds = timeDiff / 1000; - if (mixer){ - mixer.update(deltaSeconds); + if (app.mixer){ + app.mixer.update(deltaSeconds); app.updateMatrixWorld(); } }; @@ -331,27 +330,11 @@ export default e => { }); app.stop = () => { - if (mixer){ - mixer.stopAllAction(); - mixer = null; + if (app.mixer){ + app.mixer.stopAllAction(); + app.mixer = null; } }; - - app.playAnimations = (animationStrings, transitionTime = 0.1, animationLoop = true) => { - if (mixer){ - const nextActions = animationStrings ? _getActions(animationStrings) : actions; - for (let i =0 ; i < currentActions.length; i++){ - currentActions[i].fadeOut(transitionTime); - } - for (let i =0 ; i < nextActions.length; i++){ - nextActions[i].loop = animationLoop ? THREE.LoopRepeat : THREE.LoopOnce; - nextActions[i].reset(); - nextActions[i].play(); - nextActions[i].fadeIn(transitionTime); - } - currentActions = nextActions; - } - } return app; }; From d4ace6e70a885c461ad54abaaf155aa6ad001407 Mon Sep 17 00:00:00 2001 From: memelotsqui Date: Wed, 10 Aug 2022 11:27:23 -0500 Subject: [PATCH 10/14] cleanup spaces --- type_templates/glb.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/type_templates/glb.js b/type_templates/glb.js index 1ec4ebe..a8696b4 100644 --- a/type_templates/glb.js +++ b/type_templates/glb.js @@ -15,7 +15,6 @@ const localMatrix = new THREE.Matrix4(); */ // const z180Quaternion = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0, 1, 0), Math.PI); - export default e => { const app = useApp(); @@ -92,11 +91,10 @@ export default e => { currentActions[i].play(); } } - }; const petComponent = app.getComponent('pet'); if (!petComponent) { - _loadAnimations(); + _loadAnimations(); } const _loadLightmaps = () => { From 121ffc1e0e6008e85f393946fe71b8734168e209 Mon Sep 17 00:00:00 2001 From: memelotsqui Date: Wed, 10 Aug 2022 11:35:54 -0500 Subject: [PATCH 11/14] removed set loop line since its no longer necessary here --- type_templates/glb.js | 1 - 1 file changed, 1 deletion(-) diff --git a/type_templates/glb.js b/type_templates/glb.js index a8696b4..e8c1d61 100644 --- a/type_templates/glb.js +++ b/type_templates/glb.js @@ -83,7 +83,6 @@ export default e => { app.mixer = new THREE.AnimationMixer(o); // create the animation mixer with the root of the glb file for (let i =0 ; i < animations.length; i++){ actions.push(app.mixer.clipAction(animations[i])); - actions[i].clampWhenFinished = true; // make sure to stop it in the last frame just in case loop is set to false } const idleAction = _getActions('idle'); const currentActions = idleAction.length > 0 ? idleAction : actions; From 631380304eec6b8b22e55ed308b2be642f127034 Mon Sep 17 00:00:00 2001 From: memelotsqui Date: Wed, 10 Aug 2022 11:49:00 -0500 Subject: [PATCH 12/14] cleared functions and made it more consistent with current code --- type_templates/glb.js | 28 +++++++--------------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/type_templates/glb.js b/type_templates/glb.js index e8c1d61..e7dd421 100644 --- a/type_templates/glb.js +++ b/type_templates/glb.js @@ -28,7 +28,6 @@ export default e => { app.glb = null; app.mixer = null; - const actions = []; const uvScrolls = []; const physicsIds = []; app.physicsIds = physicsIds; @@ -81,14 +80,13 @@ export default e => { if (animationEnabled && animations.length > 0){ app.mixer = new THREE.AnimationMixer(o); // create the animation mixer with the root of the glb file - for (let i =0 ; i < animations.length; i++){ - actions.push(app.mixer.clipAction(animations[i])); - } - const idleAction = _getActions('idle'); - const currentActions = idleAction.length > 0 ? idleAction : actions; - for (let i =0 ; i < currentActions.length; i++){ - currentActions[i].play(); + const idleAnimation = animations.find(a => a.name === 'idle'); + const clips = idleAnimation ? [idleAnimation] : animations; + for (const clip of clips) { + const action = app.mixer.clipAction(clip); + action.play(); } + } }; const petComponent = app.getComponent('pet'); @@ -251,19 +249,7 @@ export default e => { } } }; - const _getActions = (actionStrings) => { - const result = []; - actionStrings = Array.isArray(actionStrings) ? actionStrings : [actionStrings]; - for (let i =0; i < actionStrings.length ; i++){ - if (typeof actionStrings[i] === "string"){ - const act = actions.filter(a => a._clip.name.toLowerCase() === actionStrings[i].toLowerCase())[0]; - if (act) result.push(act); - } - } - //if (result.length === 0) - //console.warn("No animation(s) found with name(s): " + actionStrings); - return result; - } + app.addEventListener('wearupdate', e => { if (e.wear) { if (app.glb) { From b87ea8e07497b3c7a3f7f280e738779916b40640 Mon Sep 17 00:00:00 2001 From: memelotsqui Date: Wed, 10 Aug 2022 11:50:07 -0500 Subject: [PATCH 13/14] code cleanup --- type_templates/glb.js | 1 - 1 file changed, 1 deletion(-) diff --git a/type_templates/glb.js b/type_templates/glb.js index e7dd421..0372334 100644 --- a/type_templates/glb.js +++ b/type_templates/glb.js @@ -249,7 +249,6 @@ export default e => { } } }; - app.addEventListener('wearupdate', e => { if (e.wear) { if (app.glb) { From f0c5811e06f5726419747ad6f5dc4d3ee0b19509 Mon Sep 17 00:00:00 2001 From: memelotsqui Date: Wed, 10 Aug 2022 13:47:41 -0500 Subject: [PATCH 14/14] add idleAnimation component --- type_templates/glb.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/type_templates/glb.js b/type_templates/glb.js index 0372334..c4ae8bd 100644 --- a/type_templates/glb.js +++ b/type_templates/glb.js @@ -79,8 +79,13 @@ export default e => { const animationEnabled = !!(app.getComponent('animation') ?? true); if (animationEnabled && animations.length > 0){ + app.mixer = new THREE.AnimationMixer(o); // create the animation mixer with the root of the glb file - const idleAnimation = animations.find(a => a.name === 'idle'); + + const userIdle = app.getComponent('idleAnimation'); + const idleString = typeof userIdle === 'string' ? userIdle : 'idle'; + + const idleAnimation = animations.find(a => a.name === idleString); const clips = idleAnimation ? [idleAnimation] : animations; for (const clip of clips) { const action = app.mixer.clipAction(clip);