Hi there,
I am trying to create a R3F component (called Door) that can have different scales defined by its prop:
<Door width={2} />
The prop is used in the component to calculate the morphTargetValue:
const meshRef = useRef();
useEffect(() => {
const morphTargetValue = (width - widthDefault) / (widthMax - widthDefault);
meshRef.current.morphTargetInfluences[0] = morphTargetValue;
}, [width]);
that works fine if i instantiate the component once. But if i instantiate multiple Doors with different width values all get the width value of the last instance… why?
<group position-x={-2}>
<Door width={2} />
</group>
<group position-x={2}>
<Door width={3} />
</group>
Here is the full Door component code:
import { useGLTF } from "@react-three/drei";
import { useEffect, useRef } from "react";
const widthDefault = 2;
const widthMax = 3;
export const Door = ({ width }) => {
const { nodes } = useGLTF("./morphTest.glb");
const meshRef = useRef();
useEffect(() => {
const morphTargetValue = (width - widthDefault) / (widthMax - widthDefault);
meshRef.current.morphTargetInfluences[0] = morphTargetValue;
}, [width]);
return (
<group dispose={null}>
<mesh
ref={meshRef}
name="Cube"
castShadow
receiveShadow
geometry={nodes.Cube.geometry}
material={nodes.Cube.material}
morphTargetDictionary={nodes.Cube.morphTargetDictionary}
morphTargetInfluences={nodes.Cube.morphTargetInfluences}
/>
</group>
);
};
Here is a codesandbox providing the problem:
https://codesandbox.io/p/sandbox/morphtest-6z7jqq
Greetings Tom

