Skip to content

Commit

Permalink
feat(performance): improve performance of threejs usages (#55)
Browse files Browse the repository at this point in the history
* feat(performance): improve performance of threejs usages
  • Loading branch information
blanxii authored Sep 22, 2023
1 parent 1e4e1a5 commit 17ad2b7
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/components/BaseCanvas/BaseCanvas.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const BaseCanvas: FC<BaseCanvasProps> = ({
fov = 50,
position = new Vector3(0, 0, 5),
style,
dpr = [1, 2],
dpr = [window.devicePixelRatio * 0.5, 2],
className
}) => (
<Canvas
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,22 @@ export const AnimationModel: FC<AnimationModelProps> = ({
const { scene } = useGltfLoader(modelSrc);
const { nodes } = useGraph(scene);

const animationClip = useMemo(async () => loadAnimationClip(animationSrc), [animationSrc]);

const animationMixer = useMemo(async () => {
const mixer = new AnimationMixer(nodes.Armature);
if (animationRunning) {
return mixer;
}

const animationClip = await loadAnimationClip(animationSrc);

const animation = mixer.clipAction(animationClip);
const animation = mixer.clipAction(await animationClip);
animation.fadeIn(0.5);
animation.play();

mixer.update(0);

return mixer;
}, [animationRunning, animationSrc, nodes.Armature]);
}, [animationRunning, animationClip, nodes.Armature]);

useFrame(async (state, delta) => {
(await animationMixer)?.update(delta);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ export const SpawnAnimation: FC<SpawnAnimationProps> = ({ avatar, onLoadedAnimat

const { nodes: avatarNode } = useGraph(avatar);

const animationClip = useMemo(async () => loadAnimationClip(onLoadedAnimation?.src || ''), [onLoadedAnimation?.src]);

const animationMixerAvatar = useMemo(async () => {
const mixer = new AnimationMixer(avatarNode.Armature);
if (!avatarNode.Armature) {
return mixer;
}
const animationClip = await loadAnimationClip(onLoadedAnimation?.src || '');

const animation = mixer.clipAction(animationClip);
const animation = mixer.clipAction(await animationClip);

animation.setLoop(LoopRepeat, onLoadedAnimation?.loop || 1);
animation.clampWhenFinished = true;
Expand All @@ -44,7 +45,7 @@ export const SpawnAnimation: FC<SpawnAnimationProps> = ({ avatar, onLoadedAnimat
});

return mixer;
}, [avatarNode.Armature, onLoadedAnimation?.loop, onLoadedAnimation?.src]);
}, [avatarNode.Armature, onLoadedAnimation?.loop, animationClip]);

useFrame(async (state, delta) => {
(await animationMixerAvatar)?.update(delta);
Expand Down
13 changes: 9 additions & 4 deletions src/components/Spawn/SpawnEffect/SpawnEffect.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,21 @@ export const SpawnEffect: FC<SpawnEffectProps> = ({ onLoadedEffect, onLoadedEffe
}
}, [onLoadedEffectFinish, effectRunning]);

const animationLoadedEffect = useMemo(
async () => loadAnimationClip(onLoadedEffect?.animationSrc || onLoadedEffect.src),
[onLoadedEffect?.animationSrc, onLoadedEffect.src]
);

const spawnEffectMixer = useMemo(async () => {
const animationLoadedEffect = await loadAnimationClip(onLoadedEffect?.animationSrc || onLoadedEffect.src);
const mixer = new AnimationMixer(mountEffectNode.Scene);
const loadedEffect = await animationLoadedEffect;

if (!animationLoadedEffect) {
if (!loadedEffect) {
setEffectRunning(false);
return mixer;
}

const animation = mixer.clipAction(animationLoadedEffect);
const animation = mixer.clipAction(loadedEffect);

animation.setLoop(LoopRepeat, onLoadedEffect?.loop || 1);
animation.clampWhenFinished = true;
Expand All @@ -44,7 +49,7 @@ export const SpawnEffect: FC<SpawnEffectProps> = ({ onLoadedEffect, onLoadedEffe
});

return mixer;
}, [mountEffectNode.Scene, onLoadedEffect?.animationSrc, onLoadedEffect?.loop, onLoadedEffect.src]);
}, [mountEffectNode.Scene, animationLoadedEffect, onLoadedEffect?.loop]);

useFrame(async (state, delta) => {
(await spawnEffectMixer)?.update(delta);
Expand Down
10 changes: 4 additions & 6 deletions src/services/Animation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ interface ClipWithType {
const MIXAMO_PREFIX = 'mixamorig';
const POSITION_SUFFIX = '.position';
const MIXAMO_SCALE = 0.01;

const fbxLoader = new FBXLoader();
const gltfLoader = new GLTFLoader();

function normaliseFbxAnimation(fbx: Group, index: number = 0) {
const { tracks } = fbx.animations[index];

Expand All @@ -31,9 +35,6 @@ function normaliseFbxAnimation(fbx: Group, index: number = 0) {
}

const loadBlobFile = async (blob: Blob): Promise<ClipWithType> => {
const fbxLoader = new FBXLoader();
const gltfLoader = new GLTFLoader();

try {
const buffer = await blob.arrayBuffer();
return {
Expand All @@ -49,9 +50,6 @@ const loadBlobFile = async (blob: Blob): Promise<ClipWithType> => {
};

const loadPathFile = async (source: string): Promise<ClipWithType> => {
const fbxLoader = new FBXLoader();
const gltfLoader = new GLTFLoader();

try {
return {
group: (await gltfLoader.loadAsync(source)) as unknown as Group,
Expand Down
10 changes: 5 additions & 5 deletions src/services/Models.service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,13 @@ export const useEmotion = (nodes: ObjectMap['nodes'], emotion?: Emotion) => {
});
};

const loader = new GLTFLoader();
const dracoLoader = new DRACOLoader();
dracoLoader.setDecoderPath('https://www.gstatic.com/draco/versioned/decoders/1.5.5/');
loader.setDRACOLoader(dracoLoader);

export const useGltfLoader = (source: Blob | string): GLTF =>
suspend(async () => {
const loader = new GLTFLoader();
const dracoLoader = new DRACOLoader();
dracoLoader.setDecoderPath('https://www.gstatic.com/draco/versioned/decoders/1.5.5/');
loader.setDRACOLoader(dracoLoader);

if (source instanceof Blob) {
const buffer = await source.arrayBuffer();
return (await loader.parseAsync(buffer, '')) as unknown as GLTF;
Expand Down

0 comments on commit 17ad2b7

Please sign in to comment.