Hajdu Posted May 20, 2025 Posted May 20, 2025 i am facing a problem where Three.js objects, specifically a sphere with a ShaderMaterial, disappear after certain state changes. The issue seems to occur when the selected state is updated using dispatch(setSelected()). This triggers a React re-render or reconciliation process, which may interfere with the Three.js object's lifecycle, causing the material or mesh to lose its state or become invisible. The object only remains visible if the state is not updated after setting its properties Here is the code for the object : function App() { const selected = useSelector((state) => state.gsap.selected); const dispatch = useDispatch(); function AnimatedSphere() { const sphereRef = useRef(); const material = useRef( new THREE.ShaderMaterial({ vertexShader, fragmentShader, uniforms: { time: { value: 0.0 }, pinkEraser: { value: new THREE.Color(237/255, 163/255, 152/255) }, opacity : {value : 0.0} }, transparent: true, }) ); useFrame(({ clock }) => { material.current.uniforms.time.value = clock.getElapsedTime(); }); useGSAP(() => { console.log(selected) if(selected == "1" || selected == undefined){ gsap.to(material.current.uniforms.opacity, { value: 1, duration: 1, delay: 0.5, onComplete: () => { dispatch(setSelected("2")); } }); } },[]) useGSAP(() => { if(selected === "3") { gsap.set(sphereRef.current.scale, { x: 2.5/3.1, y: 2.5/3.1, z: 2.5/3.1 }); gsap.to(sphereRef.current.scale, { x: 1, y: 1, z: 1, delay: 0.5, duration: 0.8, onComplete: () => { dispatch(setSelected("4")); } }); } }, [selected]); return ( <mesh ref={sphereRef} material={material.current}> <sphereGeometry args={[2.5, 32, 64]} /> </mesh> ); } return ( <Canvas> <ambientLight /> <OrbitControls /> <AnimatedSphere /> </Canvas> ); } export default App; And i have a redux state that basically just has the number which sequence should run
Rodrigo Posted May 20, 2025 Posted May 20, 2025 Hi @Hajdu and welcome to the GSAP Forums! Without a minimal demo, it's very difficult to troubleshoot; the issue could be caused by CSS, markup, a third party library, a 3rd party script, etc. Would you please provide a very simple CodePen or Stackblitz that illustrates the issue? Please don't include your whole project. Just some colored <div> elements and the GSAP code is best. See if you can recreate the issue with as few dependencies as possible. Start minimal and then incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, at least we have a reduced test case which greatly increases your chances of getting a relevant answer. See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen. that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo: Using a framework/library like React, Vue, Next, etc.? CodePen isn't always ideal for these tools, so here are some Stackblitz starter templates that you can fork and import GSAP as shown in the Install Helper in our Learning Center : React (please read this article!) Next Svelte Sveltekit Vue Nuxt I have very little experience with THREE and absolutely no experience with react three fiber so I couldn't really tell you what the issue is without a simple demo (emphasis on small and simple please) that clearly illustrates the problem. The only thing I can see in your code is that you have the function for the AnimatedSphere component inside your App function. In react is a bad idea to have a component inside another component, this most likely will create a rendering/loop/memory-leak issue Please share the StackBlitz link directly to the file in question (where you've put the GSAP code) so we don't need to hunt through all the files. Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. ✅
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now