A composable that creates a named object/material collection from any Object3D
import { useGraph } from '@tresjs/core'
const { nodes, materials, meshes } = useGraph(object3D)
Returns a computed ref containing a TresObjectMap
with the following structure:
interface TresObjectMap {
nodes: { [name: string]: Object3D }
materials: { [name: string]: Material }
meshes: { [name: string]: Mesh }
}
The useGraph
composable is particularly useful when working with complex 3D models (like GLTF) because it:
<script setup lang="ts">
import { useGraph, useLoader } from '@tresjs/core'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
const { state: model } = await useLoader(GLTFLoader, '/path/to/model.gltf')
const graph = useGraph(computed(() => model.value?.scene))
watch(graph, ({ nodes, materials }) => {
const carBody = nodes.carBody
const paintMaterial = materials.paint
})
</script>
The composable uses an util function buildGraph
internally to traverse the object hierarchy and create a map of:
nodes
objectmaterials
objectmeshes
objectThis structure is particularly useful when working with models exported from 3D modeling software, as it maintains the naming conventions used in the original model.