Skip to content

fix: uniforms have stable refs for ShaderMaterial#3715

Open
krispya wants to merge 1 commit intomasterfrom
fix/shader-material-uniforms
Open

fix: uniforms have stable refs for ShaderMaterial#3715
krispya wants to merge 1 commit intomasterfrom
fix/shader-material-uniforms

Conversation

@krispya
Copy link
Copy Markdown
Member

@krispya krispya commented Apr 5, 2026

Currently, if a uniform is recreated due to a rerender or simply an HMR and the uniform is passed in as a prop, then it overwrites the uniforms object.

// This will still regenerate on HMR
const uniforms = useMemo(() => {}, [])

// Overwrites the original reference
return <shaderMaterial uniforms={uniforms} />

This is catastrophic for Three which assumes this object never changes in its program caching. This leads people to do silly things like put Math.random() in the key prop forcing the material itself to regenerate every rerender.

This change makes the uniforms objects on ShaderMaterial and its derivatives have a stable reference. Anything passed into uniforms will instead copy into it. This is the same as behavior for math structures that have copy such as position, rotation, quaternion, etc.

The user would then get the uniforms ref from the material ref itself.

@codesandbox-ci
Copy link
Copy Markdown

codesandbox-ci bot commented Apr 5, 2026

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 6182ec3:

Sandbox Source
example Configuration

@krispya krispya changed the title fix: Uniforms have stable refs for ShaderMaterial fix: uniforms have stable refs for ShaderMaterial Apr 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant