Pārlūkot izejas kodu

feat(core): use loader test and docs

alvarosabu 2 gadi atpakaļ
vecāks
revīzija
ee7fe5bd66

+ 0 - 7
packages/tres/src/core/useCatalogue/useCatalogue.test.ts

@@ -24,12 +24,5 @@ describe('useCatalogue', () => {
     expect(catalogue.value.MyObject.foo).toEqual('bar')
   })
 
-  it('should register components for new objects', () => {
-    const { extend } = composable
-
-    extend({ MyObject: { foo: 'bar' } })
-
-    expect(app._context.components.MyObject).toBeDefined()
-  })
   // TODO: find a way to mock createComponentInstances to test the component registration
 })

+ 30 - 0
packages/tres/src/core/useLoader/index.ts

@@ -17,6 +17,13 @@ export type LoaderReturnType<T, L extends LoaderProto<T>> = T extends unknown
   ? Awaited<ReturnType<InstanceType<L>['loadAsync']>>
   : T
 
+/**
+ * Traverse an object and return all the nodes and materials
+ *
+ * @export
+ * @param {Object3D} object
+ * @return { [key: string]: any }
+ */
 export function trasverseObjects(object: Object3D) {
   const data: { [key: string]: any } = { nodes: {}, materials: {} }
   if (object) {
@@ -34,6 +41,29 @@ export function trasverseObjects(object: Object3D) {
 
 export type Extensions<T extends { prototype: LoaderProto<any> }> = (loader: T['prototype']) => void
 
+/**
+ * Load resources using THREE loaders and return the result as a promise
+ *
+ * @see https://tresjs.org/api/composables.html#useloader
+ * @see https://threejs.org/docs/index.html?q=loader#api/en/loaders/Loader
+ *
+ * ```ts
+ * import { useLoader } from '@tresjs/core'
+ * import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
+ *
+ * const { scene } = await useLoader(THREE.GLTFLoader, 'path/to/asset.gltf')
+ * ```
+ *
+ * @export
+ * @template T
+ * @template U
+ * @param {T} Loader
+ * @param {U} url
+ * @param {Extensions<T>} [extensions]
+ * @param {(event: ProgressEvent<EventTarget>) => void} [onProgress]
+ * @param {(proto: TresLoader<T>) => void} [cb]
+ * @return {*}
+ */
 export async function useLoader<T extends LoaderProto<T>, U extends string | string[]>(
   Loader: T,
   url: U,

+ 20 - 0
packages/tres/src/core/useLoader/useLoader.test.ts

@@ -0,0 +1,20 @@
+import { useLoader } from './'
+import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
+
+describe('useLoader', () => {
+  test('loads a glTF file using GLTFLoader and returns the result', async () => {
+    const gltfUrl = 'https://raw.githubusercontent.com/Tresjs/assets/main/models/gltf/aku-aku/AkuAku.gltf'
+    const { scene } = await useLoader(GLTFLoader, gltfUrl)
+    expect(scene).toBeDefined()
+  })
+
+  test('applies extensions to the loader before loading', async () => {
+    const gltfUrl = '/aku-aku/AkuAku.gltf'
+    const extensions = (loader: GLTFLoader) => {
+      loader.setPath('https://raw.githubusercontent.com/Tresjs/assets/main/models/gltf')
+    }
+    const { scene } = await useLoader(GLTFLoader, gltfUrl, extensions)
+
+    expect(scene).toBeDefined()
+  })
+})