Skip to content

Commit

Permalink
fix: useGltfLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-rpm committed Sep 16, 2024
1 parent 95ff5f8 commit 54a13b0
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/services/Models.service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ interface UseHeadMovement {
rotationMargin?: Vector2;
enabled?: boolean;
}

/**
* Avatar head movement relative to cursor.
* When the model isn't a standard Ready Player Me avatar, the head movement won't take effect.
Expand Down Expand Up @@ -218,19 +219,33 @@ 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 () => {
export const useGltfLoader = (source: Blob | string): GLTF => {
const cachedGltf = useRef<GLTF | null>(null);
const prevSource = useRef<Blob | string | null>(null);

return suspend(
async (): Promise<GLTF> => {
if (source === prevSource.current && cachedGltf.current) {
return cachedGltf.current;
}

let gltf: GLTF;
if (source instanceof Blob) {
const buffer = await source.arrayBuffer();
return (await loader.parseAsync(buffer, '')) as unknown as GLTF;
gltf = (await loader.parseAsync(buffer, '')) as GLTF;
} else {
gltf = await loader.loadAsync(source);
}

return loader.loadAsync(source);
cachedGltf.current = gltf;
prevSource.current = source;

return gltf;
},
[source],
{ lifespan: 100 }
);
};

export function usePersistantRotation(scene: Group) {
const refToPreviousScene = useRef(scene);
Expand Down

0 comments on commit 54a13b0

Please sign in to comment.