question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[InstancedUniformsMesh] Does .setUniformAt() erase material.onBeforeCompile?

See original GitHub issue

I created a ShaderMaterial with some custom Matrix4 uniforms, to rotate geometry on the vertex shader. This works when used on InstancedMesh and even InstancedUniformsMesh.

But once I invoke .setUniformAt() to bind per-instance uniforms, those material-bound uniforms no longer work-- those rotation matrices no longer rotate anything, which leads me to suspect that they fell back to identity matrices (mat4(1.0)) when the shader couldn’t access the uniform anymore.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
peterqliucommented, Aug 26, 2021

I was also able to sidestep it by calling onBeforeCompile after setUniformAt, but agree that fixing this would better match expectations and get the small perf boost.

1reaction
lojjiccommented, Aug 26, 2021

Aha! Yup, I see the problem. When the set of instanced uniforms changes, I currently create a new derived material instance. That will carry over any onBeforeCompile from the base material, but not any that was assigned to the previous derived material.

In your testcase, if you assign material.onBeforeCompile instead of cube.material.onBeforeCompile, it’ll work.

But, I should be able to reuse the derived material when the instanced uniforms change rather than instantiating a new one, which would fix the unexpected behavior in addition to being slightly more performant.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Enhancement] onBeforeCompile may break material's .clone ...
onBeforeCompile seems to be ignored when cloning materials: ... document that .clone() is non deterministic, it's going to be a different ...
Read more >
InstancedUniformsMesh - Troika JS
When you call setUniformAt , the geometry and the material's shaders will be automatically upgraded behind the scenes to turn that uniform ...
Read more >
Material#onBeforeCompile – three.js docs
Sets the alpha value to be used when running an alpha test. The material will not be rendered if the opacity is lower...
Read more >
three.js - Applying two different fragment shaders to two ...
My goal is to give each mesh a material with a unique custom fragment shader using onBeforeCompile. Each mesh has the same type...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found