You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to switch textures based on props passed from the parent component, but it doesn't work!
constLoader=newTextureLoader()consttextures=[(()=>Loader.load('URL to texture source 01'))(),(()=>Loader.load('URL to texture source 02'))(),(()=>Loader.load('URL to texture source 03'))(),]exportconstPlane:FC<MeshProps&{index: number}>=props=>{const{ index }=propsconsttextureRef=useRef<ShaderMaterial>(null!)constuniform={u_time: {value: 0.0},u_mouse: {value: newVector2(0,0)},u_dist: {value: 0.0},u_texture: {value: textures.at(index)}}constsize=useThree(({size})=>size)constcenter={x: size.width/2,y: size.height/2}useEffect(()=>{window.addEventListener('mousemove',e=>{uniform.u_dist.value=e.movementY*.001constdistX=(e.clientX-center.x)/center.xconstdistY=(e.clientY-center.y)*-1/center.yuniform.u_mouse.value=newVector2(distX,distY)})},[])useEffect(()=>{textureRef.current.needsUpdate=true},[index])return(<mesh{...props}><planeGeometryargs={[1.618*3,1*3,16,16]}/><shaderMaterialref={textureRef}uniforms={uniform}vertexShader={vertex}fragmentShader={fragment}/></mesh>)}
I cheked console.log(uniforms.u_texture.value) inside a useEffect which dependens on props , it displays swiched value. However, the actual texture on the screen was not switched.
And the following code confirms that switching textures works.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I want to switch textures based on props passed from the parent component, but it doesn't work!
I cheked
console.log(uniforms.u_texture.value)
inside a useEffect which dependens on props , it displays swiched value. However, the actual texture on the screen was not switched.And the following code confirms that switching textures works.
What is the difference between these?
Beta Was this translation helpful? Give feedback.
All reactions