Skip to content

Commit

Permalink
change: VRMAnimation, expose createVRMAnimationHumanoidTracks, create…
Browse files Browse the repository at this point in the history
…VRMAnimationExpressionTracks, createVRMAnimationLookAtTrack

- change the name of these functions to tolerate on the exposal of functions
- Change return type of these functions to make end developers able to filter out unnecessary tracks

See: #1290 (comment)
  • Loading branch information
0b5vr committed Oct 24, 2023
1 parent 0ba8832 commit 86ea315
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
48 changes: 32 additions & 16 deletions packages/three-vrm-animation/src/createVRMAnimationClip.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import * as THREE from 'three';
import type { VRMCore, VRMExpressionManager, VRMHumanoid } from '@pixiv/three-vrm-core';
import type {
VRMCore,
VRMExpressionManager,
VRMExpressionPresetName,
VRMHumanBoneName,
VRMHumanoid,
} from '@pixiv/three-vrm-core';
import type { VRMAnimation } from './VRMAnimation';

function createHumanoidTracks(
export function createVRMAnimationHumanoidTracks(
vrmAnimation: VRMAnimation,
humanoid: VRMHumanoid,
metaVersion: '0' | '1',
): THREE.KeyframeTrack[] {
const tracks: THREE.KeyframeTrack[] = [];
): {
position: Map<VRMHumanBoneName, THREE.VectorKeyframeTrack>;
rotation: Map<VRMHumanBoneName, THREE.QuaternionKeyframeTrack>;
} {
const position = new Map<VRMHumanBoneName, THREE.VectorKeyframeTrack>();
const rotation = new Map<VRMHumanBoneName, THREE.VectorKeyframeTrack>();

for (const [name, origTrack] of vrmAnimation.humanoidTracks.rotation.entries()) {
const nodeName = humanoid.getNormalizedBoneNode(name)?.name;
Expand All @@ -18,7 +28,7 @@ function createHumanoidTracks(
origTrack.times,
origTrack.values.map((v, i) => (metaVersion === '0' && i % 2 === 0 ? -v : v)),
);
tracks.push(track);
rotation.set(name, track);
}
}

Expand All @@ -33,33 +43,36 @@ function createHumanoidTracks(
const track = origTrack.clone();
track.values = track.values.map((v, i) => (metaVersion === '0' && i % 3 !== 1 ? -v : v) * scale);
track.name = `${nodeName}.position`;
tracks.push(track);
position.set(name, track);
}
}

return tracks;
return { position, rotation };
}

function createExpressionTracks(
export function createVRMAnimationExpressionTracks(
vrmAnimation: VRMAnimation,
expressionManager: VRMExpressionManager,
): THREE.KeyframeTrack[] {
const tracks: THREE.KeyframeTrack[] = [];
): Map<string, THREE.NumberKeyframeTrack> {
const map = new Map<string, THREE.NumberKeyframeTrack>();

for (const [name, origTrack] of vrmAnimation.expressionTracks.entries()) {
const trackName = expressionManager.getExpressionTrackName(name);

if (trackName != null) {
const track = origTrack.clone();
track.name = trackName;
tracks.push(track);
map.set(name, track);
}
}

return tracks;
return map;
}

function createLookAtTrack(vrmAnimation: VRMAnimation, trackName: string): THREE.KeyframeTrack | null {
export function createVRMAnimationLookAtTrack(
vrmAnimation: VRMAnimation,
trackName: string,
): THREE.KeyframeTrack | null {
if (vrmAnimation.lookAtTrack == null) {
return null;
}
Expand All @@ -79,10 +92,13 @@ function createLookAtTrack(vrmAnimation: VRMAnimation, trackName: string): THREE
export function createVRMAnimationClip(vrmAnimation: VRMAnimation, vrm: VRMCore): THREE.AnimationClip {
const tracks: THREE.KeyframeTrack[] = [];

tracks.push(...createHumanoidTracks(vrmAnimation, vrm.humanoid, vrm.meta.metaVersion));
const humanoidTracks = createVRMAnimationHumanoidTracks(vrmAnimation, vrm.humanoid, vrm.meta.metaVersion);
tracks.push(...humanoidTracks.position.values());
tracks.push(...humanoidTracks.rotation.values());

if (vrm.expressionManager != null) {
tracks.push(...createExpressionTracks(vrmAnimation, vrm.expressionManager));
const expressionTracks = createVRMAnimationExpressionTracks(vrmAnimation, vrm.expressionManager);
tracks.push(...expressionTracks.values());
}

if (vrm.lookAt != null) {
Expand All @@ -98,7 +114,7 @@ export function createVRMAnimationClip(vrmAnimation: VRMAnimation, vrm: VRMCore)
lookAtTarget.name = 'lookAtTarget';
}

const track = createLookAtTrack(vrmAnimation, `${lookAtTarget.name}.position`);
const track = createVRMAnimationLookAtTrack(vrmAnimation, `${lookAtTarget.name}.position`);

if (track != null) {
tracks.push(track);
Expand Down
7 changes: 6 additions & 1 deletion packages/three-vrm-animation/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
export { createVRMAnimationClip } from './createVRMAnimationClip';
export {
createVRMAnimationHumanoidTracks,
createVRMAnimationExpressionTracks,
createVRMAnimationLookAtTrack,
createVRMAnimationClip,
} from './createVRMAnimationClip';
export { VRMAnimation } from './VRMAnimation';
export { VRMAnimationLoaderPlugin } from './VRMAnimationLoaderPlugin';

0 comments on commit 86ea315

Please sign in to comment.