Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 94 additions & 6 deletions type_templates/glb.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as THREE from 'three';

import metaversefile from 'metaversefile';
const {useApp, useFrame, useCleanup, useLocalPlayer, usePhysics, useLoaders, useActivate, useAvatarInternal, useInternals} = metaversefile;
const {useApp, useFrame, useCleanup, useLocalPlayer, usePhysics, useLoaders, useActivate, useAvatarInternal, useInternals, getSpeed} = metaversefile;

const wearableScale = 1;

Expand Down Expand Up @@ -55,6 +55,9 @@ export default e => {

// sit state
let sitSpec = null;
let rideAction = null;
let mountMixer = null;
let mountActions = [];

const petComponent = app.getComponent('pet');
const _makePetMixer = () => {
Expand All @@ -81,6 +84,23 @@ export default e => {
idleAction,
};
};

const sitComponent = app.getComponent('sit');
const _makeMountMixer = () => {
let mountMixer;

let firstMesh = null;
glb.scene.traverse(o => {
if (firstMesh === null && o.isMesh) {
firstMesh = o;
}
});
mountMixer = new THREE.AnimationMixer(firstMesh);

return {
mountMixer,
};
};

let activateCb = null;
e.waitUntil((async () => {
Expand Down Expand Up @@ -122,9 +142,22 @@ export default e => {
let clip = idleAnimation || animations[animationMixers.length];
if (clip) {
const mixer = new THREE.AnimationMixer(o);

const action = mixer.clipAction(clip);
action.play();

if(sitComponent) {
const disableLoop = sitComponent.disableLoop ? sitComponent.disableLoop : false;
const clamp = sitComponent.clampAnimation ? sitComponent.clampAnimation : false;

if(disableLoop) {
action.loop = THREE.LoopOnce; // Only plays once
}

if(clamp) {
action.clampWhenFinished = true;
}
}
//action.play();
mountActions.push(action);

/* let lastTimestamp = Date.now();
const update = now => {
Expand All @@ -138,6 +171,9 @@ export default e => {
update(deltaSeconds) {
mixer.update(deltaSeconds);
},
clear() {
mixer.stopAllAction();
}
});
}
}
Expand Down Expand Up @@ -337,11 +373,16 @@ export default e => {
}
});

if (petComponent) {
if (petComponent) {
const m = _makePetMixer();
petMixer = m.petMixer;
idleAction = m.idleAction;
}

if (sitComponent) {
const m = _makeMountMixer();
mountMixer = m.mountMixer;
}

activateCb = () => {
if (
Expand Down Expand Up @@ -375,6 +416,10 @@ export default e => {
const sitAction = localPlayer.getAction('sit');
if (sitAction) {
localPlayer.removeAction('sit');
sitSpec = null;
for (const mixer of animationMixers) {
mixer.clear();
}
}
}
};
Expand Down Expand Up @@ -529,6 +574,7 @@ export default e => {
const localPlayer = useLocalPlayer();

const rideBone = sitSpec.sitBone ? rideMesh.skeleton.bones.find(bone => bone.name === sitSpec.sitBone) : null;

const sitAction = {
type: 'sit',
time: 0,
Expand Down Expand Up @@ -620,14 +666,56 @@ export default e => {
const deltaSeconds = timeDiff / 1000;
petMixer.update(deltaSeconds);
}
} else {
} /*else {
const deltaSeconds = timeDiff / 1000;
for (const mixer of animationMixers) {
mixer.update(deltaSeconds);
}
}
}*/
};
_updatePet();

const _isMoving = (v) => {
const threshold = 1;
return Math.abs(v.x) > threshold || Math.abs(v.z) > threshold ? true : false; // Excluding velocity.y
}

const _updateRide = () => {
if (!!app.getComponent('sit')) {
if (sitSpec) {
const localPlayer = useLocalPlayer();
const sitAction = localPlayer.getAction('sit');
if(sitAction) {
const velocity = localPlayer.characterPhysics.velocity.clone();
const speed = getSpeed(); // 0.1, ~0.3, 2 Getting speed from game.js
const smooth = sitSpec.damping ? sitSpec.damping : 0.5; // smoothing animation end/start

if(_isMoving(velocity)) {

for (const action of mountActions) {
if(!action.isRunning()) {
action.play();
}
if(action.weight < 1) {
action.weight += (1 - Math.pow(smooth, speed)) / 2;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not frame time independent.

}
}
} else {
for (const action of mountActions) {
if(action.weight > 0) {
action.weight *= Math.pow(smooth, speed);
}
}
}
}
}
}
const deltaSeconds = timeDiff / 1000;
for (const mixer of animationMixers) {
mixer.update(deltaSeconds);
}
};
_updateRide();

const _updateLook = () => {
const lookComponent = app.getComponent('look');
Expand Down