-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(types):
useLoader
generics (#781)
* fix(types): improve useLoader generics * docs: jsdocs
- Loading branch information
1 parent
05b3009
commit b51d679
Showing
12 changed files
with
204 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { FBXLoader } from 'three-stdlib' | ||
import { useLoader } from '@tresjs/core' | ||
import type { Object3D } from 'three' | ||
|
||
/** | ||
* Loads an FBX file and returns a THREE.Object3D. | ||
* | ||
* @export | ||
* @param {(string | string[])} path | ||
* @return {*} {Promise<Object3D>} | ||
*/ | ||
export async function useFBX(path: string | string[]): Promise<Object3D> { | ||
return (await useLoader(FBXLoader, path)) as unknown as Object3D | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { type TresLoader, type TresObject3D, useLoader } from '@tresjs/core' | ||
import type { AnimationClip, Material, Scene } from 'three' | ||
import { DRACOLoader, GLTFLoader } from 'three-stdlib' | ||
import type { GLTF } from 'three-stdlib' | ||
|
||
export interface GLTFLoaderOptions { | ||
/** | ||
* Whether to use Draco compression. | ||
* | ||
* @type {boolean} | ||
* @memberof GLTFLoaderOptions | ||
*/ | ||
draco?: boolean | ||
/** | ||
* The path to the Draco decoder. | ||
* | ||
* @type {string} | ||
* @memberof GLTFLoaderOptions | ||
*/ | ||
decoderPath?: string | ||
} | ||
|
||
export interface GLTFResult { | ||
animations: Array<AnimationClip> | ||
nodes: Record<string, TresObject3D> | ||
materials: Record<string, Material> | ||
scene: Scene | ||
} | ||
|
||
let dracoLoader: DRACOLoader | null = null | ||
|
||
export interface TresGLTFLoader extends TresLoader<GLTF> { | ||
setDRACOLoader?: (dracoLoader: DRACOLoader) => void | ||
} | ||
|
||
/** | ||
* Sets the extensions for the GLTFLoader. | ||
* | ||
* @param {GLTFLoaderOptions} options | ||
* @param {(loader: TresGLTFLoader) => void} [extendLoader] | ||
* @return {*} | ||
*/ | ||
function setExtensions(options: GLTFLoaderOptions, extendLoader?: (loader: TresGLTFLoader) => void) { | ||
return (loader: TresGLTFLoader) => { | ||
if (extendLoader) { | ||
extendLoader(loader) | ||
} | ||
if (options.draco) { | ||
if (!dracoLoader) { | ||
dracoLoader = new DRACOLoader() | ||
} | ||
dracoLoader.setDecoderPath(options.decoderPath || 'https://www.gstatic.com/draco/versioned/decoders/1.4.3/') | ||
if (loader.setDRACOLoader) { | ||
loader.setDRACOLoader(dracoLoader) | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Loads a GLTF file and returns a THREE.Object3D. | ||
* | ||
* @export | ||
* @param {(string | string[])} path | ||
* @param {GLTFLoaderOptions} [options] | ||
* | ||
* | ||
* @param {(loader: GLTFLoader) => void} [extendLoader] | ||
* @return {*} {Promise<GLTFResult>} | ||
*/ | ||
export async function useGLTF<T extends string | string[]>( | ||
path: T, | ||
options: GLTFLoaderOptions = { | ||
draco: false, | ||
}, | ||
extendLoader?: (loader: TresGLTFLoader) => void, | ||
): Promise<T extends string[] ? GLTFResult[] : GLTFResult> { | ||
const gltfModel = (await useLoader<GLTF>(GLTFLoader, path, setExtensions(options, extendLoader))) as unknown as GLTFResult | ||
dracoLoader?.dispose() | ||
dracoLoader = null | ||
return gltfModel as T extends string[] ? GLTFResult[] : GLTFResult | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<script setup lang="ts"> | ||
import { OrbitControls } from '@tresjs/cientos' | ||
import TheModel from './TheModel.vue' | ||
</script> | ||
|
||
<template> | ||
<TresPerspectiveCamera :position="[3, 3, 3]" /> | ||
<OrbitControls /> | ||
<TresGridHelper /> | ||
<TresAmbientLight :intensity="1" /> | ||
<Suspense> | ||
<TheModel /> | ||
</Suspense> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<script setup lang="ts"> | ||
import { useFBX } from '../../../composables/useFBX' | ||
const scene = await useFBX('https://raw.githubusercontent.com/Tresjs/assets/main/models/fbx/low-poly-truck/Jeep_done.fbx') | ||
scene.scale.set(0.01, 0.01, 0.01) | ||
</script> | ||
|
||
<template> | ||
<primitive :object="scene" /> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<script setup lang="ts"> | ||
import { TresCanvas } from '@tresjs/core' | ||
import TheExperience from './TheExperience.vue' | ||
</script> | ||
|
||
<template> | ||
<TresCanvas clear-color="#C0ffee"> | ||
<TheExperience /> | ||
</TresCanvas> | ||
</template> |
14 changes: 14 additions & 0 deletions
14
playground/src/pages/loaders/gltf-loader/TheExperience.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<script setup lang="ts"> | ||
import { OrbitControls } from '@tresjs/cientos' | ||
import TheModel from './TheModel.vue' | ||
</script> | ||
|
||
<template> | ||
<TresPerspectiveCamera :position="[3, 3, 3]" /> | ||
<OrbitControls /> | ||
<TresGridHelper /> | ||
<TresAmbientLight :intensity="1" /> | ||
<Suspense> | ||
<TheModel /> | ||
</Suspense> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<script setup lang="ts"> | ||
import { useGLTF } from '../../../composables/useGLTF' | ||
const { nodes } = await useGLTF('https://raw.githubusercontent.com/Tresjs/assets/main/models/gltf/blender-cube.glb', { draco: true }) | ||
const model = nodes.Cube | ||
</script> | ||
|
||
<template> | ||
<primitive :object="model" /> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<script setup lang="ts"> | ||
import { TresCanvas } from '@tresjs/core' | ||
import TheExperience from './TheExperience.vue' | ||
</script> | ||
|
||
<template> | ||
<TresCanvas clear-color="#C0ffee"> | ||
<TheExperience /> | ||
</TresCanvas> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export const loaderRoutes = [ | ||
{ | ||
path: '/loaders/gltf', | ||
name: 'GLTF Loader', | ||
component: () => import('../../pages/loaders/gltf-loader/index.vue'), | ||
}, | ||
{ | ||
path: '/loaders/fbx', | ||
name: 'FBX Loader', | ||
component: () => import('../../pages/loaders/fbx-loader/index.vue'), | ||
}, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters