Skip to content

Commit

Permalink
restore character initial animation
Browse files Browse the repository at this point in the history
  • Loading branch information
memelotsqui committed Oct 18, 2023
1 parent 5283c74 commit d74de16
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/components/TraitInformation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down
43 changes: 32 additions & 11 deletions src/library/animationManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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++){
Expand Down Expand Up @@ -106,6 +123,7 @@ export class AnimationManager{
this.curAnimID = 0;
this.animationControls = [];
this.started = false;
this.mouseLookEnabled = true;

this.mixamoModel = null;
this.mixamoAnimations = null;
Expand All @@ -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
Expand All @@ -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)
});
}

Expand Down
2 changes: 0 additions & 2 deletions src/library/loadMixamoAnimation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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`
Expand Down

0 comments on commit d74de16

Please sign in to comment.