From d74de16ad6a5e6a90aaf09445c379a17bd233e4a Mon Sep 17 00:00:00 2001 From: memelotsqui Date: Tue, 17 Oct 2023 19:02:37 -0600 Subject: [PATCH] restore character initial animation --- src/components/TraitInformation.jsx | 1 + src/library/animationManager.js | 43 +++++++++++++++++++++-------- src/library/loadMixamoAnimation.js | 2 -- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/components/TraitInformation.jsx b/src/components/TraitInformation.jsx index d9043cf1..c7f570ec 100644 --- a/src/components/TraitInformation.jsx +++ b/src/components/TraitInformation.jsx @@ -67,6 +67,7 @@ export default function TraitInformation({currentVRM, animationManager, lookatMa const handleMouseLookEnable = (event) => { setHasMouseLook(event.target.checked); lookatManager.setActive(event.target.checked); + animationManager.enableMouseLook(event.target.checked); // Perform any additional actions or logic based on the checkbox state change }; diff --git a/src/library/animationManager.js b/src/library/animationManager.js index b2b29bd0..76e6185e 100644 --- a/src/library/animationManager.js +++ b/src/library/animationManager.js @@ -23,6 +23,11 @@ class AnimationControl { this.vrm = vrm; this.animationManager = null; this.animationManager = animationManager; + this.mixamoModel = null; + + this.neckBone = vrm?.humanoid?.humanBones?.neck; + this.spineBone = vrm?.humanoid?.humanBones?.spine; + this.setAnimations(animations); @@ -42,18 +47,30 @@ class AnimationControl { this.actions[curIdx].time = animationManager.getToActionTime(); this.actions[curIdx].play(); } - setAnimations(animations, mixamoModel){ + + setMouseLookEnabled(mouseLookEnabled){ + this.setAnimations(this.animations, this.mixamoModel, mouseLookEnabled); + } + + setAnimations(animations, mixamoModel, mouseLookEnabled = null){ + mouseLookEnabled = mouseLookEnabled == null ? this.animationManager.mouseLookEnabled : mouseLookEnabled; + this.animations = animations; this.mixer.stopAllAction(); if (mixamoModel != null){ - if (this.vrm != null) + if (this.vrm != null){ animations = [getMixamoAnimation(animations, mixamoModel , this.vrm)] + this.mixamoModel = mixamoModel; + } // modify animations } - animations[0].tracks.map((track, index) => { - if(track.name === "neck.quaternion" || track.name === "spine.quaternion"){ - animations[0].tracks.splice(index, 1) - } - }) + if (mouseLookEnabled){ + animations[0].tracks.map((track, index) => { + if(track.name === "neck.quaternion" || track.name === "spine.quaternion"){ + animations[0].tracks.splice(index, 1) + } + }) + } + this.actions = []; for (let i =0; i < animations.length;i++){ @@ -106,6 +123,7 @@ export class AnimationManager{ this.curAnimID = 0; this.animationControls = []; this.started = false; + this.mouseLookEnabled = true; this.mixamoModel = null; this.mixamoAnimations = null; @@ -122,14 +140,17 @@ export class AnimationManager{ }, 1000/30); } - + enableMouseLook(enable){ + this.mouseLookEnabled = enable; + this.animationControls.forEach(animControls => { + animControls.setMouseLookEnabled(enable); + }); + } async loadAnimation(paths, isfbx = true, pathBase = "", name = ""){ - console.log(paths) const path = pathBase + (pathBase != "" ? "/":"") + getAsArray(paths)[0]; name = name == "" ? getFileNameWithoutExtension(path) : name; this.currentAnimationName = name; - console.log(this.currentAnimationName); const loader = isfbx ? fbxLoader : gltfLoader; const animationModel = await loader.loadAsync(path); // if we have mixamo animations store the model @@ -153,7 +174,7 @@ export class AnimationManager{ else{ //cons this.animationControls.forEach(animationControl => { - animationControl.setAnimations(animationModel.animations, this.mixamoModel) + animationControl.setAnimations(animationModel.animations, this.mixamoModel, this.mouseLookEnabled) }); } diff --git a/src/library/loadMixamoAnimation.js b/src/library/loadMixamoAnimation.js index 1b51e703..bf7b0353 100644 --- a/src/library/loadMixamoAnimation.js +++ b/src/library/loadMixamoAnimation.js @@ -10,7 +10,6 @@ import { VRMRigMapMixamo } from './VRMRigMapMixamo.js'; */ export function getMixamoAnimation( animations, model, vrm ) { const clip = THREE.AnimationClip.findByName( animations, 'mixamo.com' ); // extract the AnimationClip - const tracks = []; // KeyframeTracks compatible with VRM will be added here const restRotationInverse = new THREE.Quaternion(); @@ -24,7 +23,6 @@ export function getMixamoAnimation( animations, model, vrm ) { const vrmRootY = vrm.scene.getWorldPosition( _vec3 ).y; const vrmHipsHeight = Math.abs( vrmHipsY - vrmRootY ); const hipsPositionScale = vrmHipsHeight / motionHipsHeight; - clip.tracks.forEach( ( origTrack ) => { const track = origTrack.clone(); // Convert each tracks for VRM use, and push to `tracks`