Skip to content

Commit ff36df6

Browse files
committed
Add callback to be able to customize the instance of the loader that is
created
1 parent 7bce2bc commit ff36df6

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

examples/vite-redux-toolkit-react-app/src/components/viewers/Canvas3DExample.tsx

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import React, { Suspense, useRef, useState } from "react";
22
import { Box } from "@mui/material";
33
import { useFrame } from "@react-three/fiber";
4-
import { CameraControls, Center, Html } from "@react-three/drei";
4+
import {
5+
CameraControls,
6+
Center,
7+
Gltf,
8+
Html,
9+
useCamera,
10+
useGLTF,
11+
} from "@react-three/drei";
512
import { Mesh } from "three";
613
import * as THREE from "three";
714
import { STLLoader } from "three/examples/jsm/loaders/STLLoader";
@@ -40,7 +47,6 @@ function MyRotatingBox() {
4047
setSwitchColor((prev) => !prev);
4148
}}
4249
ref={myMesh}
43-
position={[10, 10, 10]}
4450
>
4551
<boxGeometry />
4652
<meshPhongMaterial color={switchColor ? "royalblue" : "hotpink"} />
@@ -65,21 +71,23 @@ const Canvas3DContent: React.FC = () => {
6571
`http://localhost:${window.location.port}/n.stl`,
6672
];
6773

68-
const stlGeometries = useParallelLoader(
69-
STLLoader,
70-
urls,
71-
(url, error) => {
74+
const stlGeometries = useParallelLoader(STLLoader, urls, undefined, {
75+
onError: (url, error) => {
7276
console.debug("ERROR LOADING STL", url, error);
7377
},
74-
(url, event) => {
78+
onProgress: (url, event) => {
7579
console.debug("STL LOAD PROGRESS", url, event);
76-
}
77-
);
80+
},
81+
});
7882
const objGroups = useParallelLoader(
7983
OBJLoader,
8084
`http://localhost:${window.location.port}/2.obj`
8185
);
8286

87+
useGLTF.setDecoderPath(
88+
"https://raw.githubusercontent.com/mrdoob/three.js/dev/examples/jsm/libs/draco/"
89+
);
90+
8391
return (
8492
<Canvas3D frameloop={"always"}>
8593
<group>
@@ -94,8 +102,14 @@ const Canvas3DContent: React.FC = () => {
94102
<primitive key={url} object={group} />
95103
))}
96104
</Center>
105+
{/* <Center>
106+
<Gltf
107+
src={`http://localhost:${window.location.port}/Avocado.gltf`}
108+
useDraco={true}
109+
/>
110+
</Center> */}
97111
</group>
98-
<MyRotatingBox />
112+
{/* <MyRotatingBox /> */}
99113
</Canvas3D>
100114
);
101115
};

geppetto.js/geppetto-ui/src/3d-canvas/Canvas3D.tsx

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import React, { useEffect, useRef } from "react";
44
import { CameraControls } from "@react-three/drei";
55
import { useState } from "react";
66
import { Loader } from "three";
7+
import { DRACOLoader } from "three/examples/jsm/loaders/DRACOLoader";
78

89
type Canvas3DBaseProps = {
910
children?;
@@ -51,17 +52,23 @@ export const Canvas3D: React.FC<Canvas3DProps> = ({
5152
*
5253
* @param loader The loader class to use for loading (e.g., STLLoader, OBJLoader)
5354
* @param urls the URL or array of URLs to load
54-
* @param onLoadError the callback function that is called when an error occurs during loading of a URL.
55+
* @param loaderInit the callback function that is called just after creating the loader.
56+
* @param progress an object that contains 3 optional keys: onError, onProgress, onFinish.
57+
* @param onError the callback function that is called when an error occurs during loading of a URL.
5558
* @param onProgress the callback function that is called to report progress during loading of a URL.
59+
* @param onFinish the callback function that is called to report that the object at an URL loaded properly.
5660
* @returns an object mapping each URL to its loaded 3D object. If a URL failed to load, it will not be included in the returned object.
5761
*/
5862
export const useParallelLoader = <T,>(
5963
// eslint-disable-next-line @typescript-eslint/no-explicit-any
6064
loader: new (...args: any[]) => Loader<T>,
6165
urls: string[] | string,
62-
onLoadError?: (url, error) => void,
63-
onProgress?: (url, event: ProgressEvent<EventTarget>) => void
64-
// onLoadSuccess?: () => void
66+
loaderInit?: (loader) => void,
67+
progress?: {
68+
onError?: (url, error) => void;
69+
onProgress?: (url, event: ProgressEvent<EventTarget>) => void;
70+
onFinish?: (url) => void;
71+
}
6572
): Record<string, any> => {
6673
const [objects, setObjects] = useState<Record<string, any>>({});
6774
const [isLoading, setIsLoading] = useState(false);
@@ -93,6 +100,7 @@ export const useParallelLoader = <T,>(
93100

94101
setIsLoading(true);
95102
const _loader = new loader();
103+
loaderInit?.(_loader);
96104

97105
// Create the loading promise
98106
const promise = (async (): Promise<Record<string, T>> => {
@@ -102,14 +110,15 @@ export const useParallelLoader = <T,>(
102110
_loader.load(
103111
url,
104112
(geometry) => {
113+
progress?.onFinish?.(url);
105114
resolve({ url, object: geometry });
106115
},
107-
(event) => onProgress?.(url, event),
116+
(event) => progress?.onProgress?.(url, event),
108117
(error) => {
109-
if (!onLoadError) {
118+
if (!progress?.onError) {
110119
console.error(`Error loading ${url}:`, error);
111120
} else {
112-
onLoadError(url, error);
121+
progress?.onError(url, error);
113122
}
114123
resolve({ url, object: null });
115124
}

0 commit comments

Comments
 (0)