Skip to content

Commit

Permalink
feat: gltf loaders splitted
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-rpm committed Sep 17, 2024
1 parent b3c699c commit ce9e9d9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { GLTFLoader } from 'three-stdlib';

import { Model } from 'src/components/Models/Model';
import { BaseModelProps } from 'src/types';
import { useEmotion, useGltfLoader } from 'src/services';
import { useEmotion, useGltfCachedLoader } from 'src/services';
import { Emotion } from 'src/components/Avatar/Avatar.component';

export interface MultipleAnimationModelProps extends BaseModelProps {
Expand All @@ -30,7 +30,7 @@ export const MultipleAnimationModel: FC<MultipleAnimationModelProps> = ({
const [loadedAnimations, setLoadedAnimations] = useState<Record<string, AnimationClip>>({});
const [activeAction, setActiveAction] = useState<AnimationAction | null>(null);

const { scene } = useGltfLoader(modelSrc);
const { scene } = useGltfCachedLoader(modelSrc);
const { nodes } = useGraph(scene);

useEffect(() => {
Expand Down
16 changes: 15 additions & 1 deletion src/services/Models.service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,21 @@ 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 => {
export const useGltfLoader = (source: Blob | string): GLTF =>
suspend(
async () => {
if (source instanceof Blob) {
const buffer = await source.arrayBuffer();
return (await loader.parseAsync(buffer, '')) as unknown as GLTF;
}

return loader.loadAsync(source);
},
[source],
{ lifespan: 100 }
);

export const useGltfCachedLoader = (source: Blob | string): GLTF => {
const cachedGltf = useRef<Map<string, GLTF>>(new Map<string, GLTF>());

return suspend(async (): Promise<GLTF> => {
Expand Down

0 comments on commit ce9e9d9

Please sign in to comment.