The useLoader composable provides a reactive and easy-to-use method for loading 3D models and textures with any Three.js loader. It supports progress tracking, error handling, and works seamlessly with Vue's reactivity system. This makes it ideal for loading assets in TresJS scenes, including GLTF, FBX, textures, and more.
<script setup lang="ts">
import { useLoader } from '@tresjs/core'
import { TextureLoader } from 'three'
// Load a texture from a remote URL
const { state: texture, isLoading, error } = useLoader(
TextureLoader,
'https://raw.githubusercontent.com/Tresjs/assets/main/textures/black-rock/Rock035_2K_Color.jpg',
)
</script>
<template>
<TresMesh>
<TresBoxGeometry />
<!-- Use the loaded texture as the material map -->
<TresMeshStandardMaterial v-if="texture" :map="texture" />
</TresMesh>
</template>
<script setup lang="ts">
import { useGraph, useLoader } from '@tresjs/core'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'
import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader.js'
import { computed } from 'vue'
// Setup DRACO loader for compressed GLTFs
const dracoLoader = new DRACOLoader()
dracoLoader.setDecoderPath('https://www.gstatic.com/draco/versioned/decoders/1.5.6/')
// Load a GLTF model
const { state: model, isLoading, error } = useLoader(
GLTFLoader,
'https://raw.githubusercontent.com/Tresjs/assets/main/models/gltf/blender-cube.glb',
{
extensions: (loader) => {
if (loader instanceof GLTFLoader) {
loader.setDRACOLoader(dracoLoader)
}
},
},
)
// Extract the scene and graph
const scene = computed(() => model.value?.scene)
const graph = useGraph(scene)
const nodes = computed(() => graph.value.nodes)
</script>
<template>
<!-- Render the Cube node if it exists -->
<primitive
v-if="nodes?.BlenderCube"
:object="nodes?.BlenderCube"
/>
</template>
<script setup lang="ts">
import { useLoader } from '@tresjs/core'
import { FBXLoader } from 'three/examples/jsm/loaders/FBXLoader.js'
// Load an FBX model
const { state: model, isLoading, error } = useLoader(
FBXLoader,
'https://raw.githubusercontent.com/Tresjs/assets/main/models/fbx/low-poly-truck/Jeep_done.fbx',
)
</script>
<template>
<!-- Render the loaded FBX model -->
<primitive v-if="model" :object="model" :scale="0.01" />
</template>
The useLoader composable returns a reactive object with the following properties:
null if not loaded yet.function useLoader<T, Shallow extends boolean = false>(
Loader: LoaderProto<T>,
path: MaybeRef<string>,
options?: TresLoaderOptions<T, Shallow>,
): UseLoaderReturn<T, Shallow>
GLTFLoader for .glb/.gltf, FBXLoader for .fbx, TextureLoader for images).progress object to show user feedback.LoadingManager for global progress tracking across multiple assets.error ref and providing fallback UI.ref as the path to automatically reload when the path changes.useLoader instances or use a LoadingManager to coordinate progress.