The useGraph composable provides a convenient way to extract and reactively access all named nodes, materials, and meshes from a Three.js object or scene. This is especially useful when working with loaded models or complex object hierarchies, allowing you to reference and manipulate specific parts of your 3D scene by name.
import { useGraph } from '@tresjs/core'
import { BoxGeometry, Group, Mesh, MeshStandardMaterial } from 'three'
// Create a group and add a mesh with a named material
const group = new Group()
const box = new Mesh(
new BoxGeometry(1, 1, 1),
new MeshStandardMaterial({ name: 'FancyMaterial', color: 'red' })
)
box.name = 'Box'
group.add(box)
// Use useGraph to extract nodes and materials
const { nodes, materials } = useGraph(group)
// Change the position of the box by name
nodes.Box.position.set(1, 0, 0)
// Change the color of the material by name
materials.FancyMaterial.color.set('blue')
useGraph is especially useful for working with loaded GLTF/FBX models, where you want to access specific meshes or materials by their names as defined in the 3D modeling tool.When loading a GLTF model, you can use useGraph to easily access and manipulate specific parts of the model:
<script setup lang="ts">
import { useGraph, useLoader } from '@tresjs/core'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'
const { state: model } = useLoader(GLTFLoader, 'path/to/model.glb')
const scene = computed(() => model.value?.scene)
const { nodes, materials } = useGraph(scene)
materials.FancyMaterial.color.set('purple')
</script>
<template>
<primitive v-if="nodes.Rig" :object="nodes.Rig" />
</template>
The useGraph composable returns a computed ref containing a TresObjectMap with the following structure:
name property.name property. Only the first material with a given name is included.name property. Only the first mesh with a given name is included.function useGraph(object: MaybeRef<TresObject>): ComputedRef<TresObjectMap>
interface TresObjectMap {
nodes: { [name: string]: TresObject }
materials: { [name: string]: TresMaterial }
meshes: { [name: string]: Mesh }
scene?: Scene
}
nodes map.null or undefined, all maps will be empty.