Skip to content

useGLTF

A composable that allows you to easily load glb/glTF models into your TresJS scene.

Usage

vue
<script setup lang="ts">
import { useGLTF } from '@tresjs/cientos'

const path = './blender-cube.glb'
const { state, nodes, materials } = useGLTF(path, { draco: true })
</script>

<template>
  <primitive v-if="state" :object="state?.scene" />
</template>
vue
<script setup lang="ts">
import { OrbitControls } from '@tresjs/cientos'
import { TresCanvas } from '@tresjs/core'
import TheModel from './TheModel.vue'
</script>

<template>
  <TresCanvas clear-color="#F78B3D">
    <TresPerspectiveCamera :position="[3, 2, 5]" />
    <OrbitControls />
    <TheModel />
    <TresDirectionalLight
      :intensity="2"
      :position="[3, 3, 3]"
    />
    <TresAmbientLight :intensity="1" />
  </TresCanvas>
</template>

An advantage of using useGLTF is that you can pass a draco prop to enable Draco compression for the model. This will reduce the size of the model and improve performance.

ts
import { useGLTF } from '@tresjs/cientos'

const { state, nodes, materials } = useGLTF('/models/AkuAku.gltf', { draco: true })

Return Values

NameTypeDescription
stateGLTFThe loaded GLTF model state
nodesobjectComputed object containing all nodes in the scene
materialsobjectComputed object containing all materials in the scene
isLoadingbooleanWhether the model is currently loading
progressnumberThe progress of the model loading
load() => Promise<void>Function to reload the model

Options

NameTypeDefaultDescription
dracobooleanfalseWhether to enable Draco compression.
decoderPathstring'https://www.gstatic.com/draco/versioned/decoders/1.5.6/'Path to the Draco decoder.

Accessing Nodes and Materials

The composable provides computed properties to easily access nodes and materials in your scene:

ts
const { nodes, materials } = useGLTF('/model.glb')

// Access a specific node
const mesh = nodes.value.MeshName

// Access a specific material
const material = materials.value.MaterialName

This makes it easier to manipulate specific parts of your model or apply materials programmatically.