The best example is this sandbox
https://codesandbox.io/p/sandbox/re-using-gltfs-forked-nl2mm9?file=%2Fsrc%2FApp.js%3A18%2C1
It loads a glb model within a function that allows the properties to be dynamically changed from each call... Change the colors to two colors that are very different (blue and red) and both models will show up red (whatever is last).
Even added a debug console.log to show the prop.colors are being received, however they are only taking whatever the last color was used.
gltfjsx creates this file
import React, { useRef } from 'react'
import { useGLTF } from '@react-three/drei'
export default function Model(props) {
const { nodes, materials } = useGLTF('/shirt_baked.glb')
console.log(props.color)
return (
<group {...props} dispose={null}>
<mesh geometry={nodes.T_Shirt_male.geometry} material={materials.lambert1} material-color={props.color} />
</group>
)
}
useGLTF.preload('/shirt_baked.glb')
Which pulls in the color from the passed props... from their call in App.jsx
import { Canvas } from '@react-three/fiber'
import { BakeShadows, OrbitControls, Stage } from '@react-three/drei'
import Shoe from './Shoe'
import Shirt from './Shirt'
export default function Viewer() {
return (
<Canvas shadows camera={{ position: [0, 0, 150], fov: 40 }}>
<Stage environment="city" intensity={0.6}>
<Shirt color="blue" position={[0, 0, 0]} />
<Shirt color="red" scale={-1} rotation={[0, 0.5, Math.PI]} position={[0, 0, -2]} />
</Stage>
<BakeShadows />
<OrbitControls makeDefault autoRotate />
</Canvas>
)
}
Which seems like it should work and the console.log shows alternative red/blue...
Is there a better way to load the same model once and then be able to change ALL the properties including the material colors? I assume it is because react 3 fiber doesn't really load a model twice? and/or doesn't keep them separated...