@@ -4,6 +4,7 @@ import React, { useEffect, useRef } from "react";
44import { CameraControls } from "@react-three/drei" ;
55import { useState } from "react" ;
66import { Loader } from "three" ;
7+ import { DRACOLoader } from "three/examples/jsm/loaders/DRACOLoader" ;
78
89type 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 */
5862export 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