Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(performance): improve performance of threejs usages #55

Merged
merged 2 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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],
blanxii marked this conversation as resolved.
Show resolved Hide resolved
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]);
blanxii marked this conversation as resolved.
Show resolved Hide resolved

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();
blanxii marked this conversation as resolved.
Show resolved Hide resolved
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