React-three-fiber / Drei useTexture has a caching issue related to useMemo, can someone please sanity check this for me?

Hey guys, I’ve documented an issue I’ve run into in this codesandbox

I believe I’m misunderstanding how useMemo needs to be used and I’m hoping you’d have time to suggest a workaround for this.

Essentially sometimes I want a material to be comprised of ‘ao’ and ‘normal’, sometimes I’d like to include a color/albedo map, other times it may only be normal. However useMemo seems use its last state if null is passed into a property of the material.

It’s difficult to explain, but I hope it’s explained better by looking at the sandbox, link for that one more time: Texture caching issue - CodeSandbox

scratch the previous posts,

you’re missing material.needsUpdate. just add:

const TexturedMaterial = memo(({ type }) => {
  ...
  return (
    <meshStandardMaterial
      ...
      onUpdate={(self) => (self.needsUpdate = true)} />
  )
})

as for the useMemo, it isn’t even needed - i removed it. you can update r3f then that warning goes away.

1 Like

Legend, thanks mate