Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing opacity of transparent material forces shader recompilation #6184

Open
lucaheft opened this issue Mar 18, 2024 · 6 comments
Open

Changing opacity of transparent material forces shader recompilation #6184

lucaheft opened this issue Mar 18, 2024 · 6 comments
Labels
area: graphics Graphics related issue performance Relating to load times or frame rate

Comments

@lucaheft
Copy link

Description

When changing the opacity intensity of a material which was initially set to 1, to some smaller value a shader recomplication is forced.

This seems to happen because MAPFLOAT isn't defined when the intensity is set to 1.
Technically this is correct, because multiplying by 1 doesn't change the ouput. However this introduces micro stutter for the first time the transparent material is faded.

I advocate for always including the intensity in the shader code. As the advantages farly outweigh the disadvantage.

The same applies for emissive intensity and probably a few others.

Pros

  • Fewer shader variants
  • No additional shader compilation

Cons:

  • Additional multiplication in shader

Steps to Reproduce

  1. Launch https://playcanvas.com/project/1200988/overview/bug-shader-generation
  2. After a delay of 2 seconds the cube fades in and out
  3. Shader of material TransparentRed is generated a second time
  4. Shader generation is logged to console
@mvaligursky
Copy link
Contributor

Yep fully agreed, we discussed this and few similar material parameters before. Same for tinting parameters, and likely some others.

@mvaligursky mvaligursky added performance Relating to load times or frame rate area: graphics Graphics related issue labels Mar 18, 2024
@lucaheft
Copy link
Author

Is this on the agenda for some of the next releases? This is bugging us for quite a while.

Or maybe you are open for a PR for this. I had quick look in the standard-material-options-builder.js and it should be a pretty straight forward change. But I'm still not too knowledgeable on the rendering part of the engine.
I also get it if you want a complete PR which includes all the changes on this topic, instead of only opacity and emissive tint.

@Maksims
Copy link
Contributor

Maksims commented Mar 28, 2024

Is this on the agenda for some of the next releases? This is bugging us for quite a while.

Or maybe you are open for a PR for this. I had quick look in the standard-material-options-builder.js and it should be a pretty straight forward change. But I'm still not too knowledgeable on the rendering part of the engine. I also get it if you want a complete PR which includes all the changes on this topic, instead of only opacity and emissive tint.

As a temporary workaround you can use: material.setProperty('material_opacity', opacity) material.setParameter('material_opacity', opacity), which avoids shader recompilation.

@lucaheft
Copy link
Author

setProperty does not seem to be a function of material.
There is setParameter, but it doesn't prevent shader recompilation.
The only workaround I found so far is to set max opacity to 0.999 to prevent shader compilation.

@willeastcott
Copy link
Contributor

@lucaheft PRs are always welcome! 🙏

@Maksims
Copy link
Contributor

Maksims commented Mar 28, 2024

setProperty does not seem to be a function of material. There is setParameter, but it doesn't prevent shader recompilation. The only workaround I found so far is to set max opacity to 0.999 to prevent shader compilation.

setParameter - yes, you are right.
But it does not trigger re-compilation. It does not mark the material to recompile shader (dirty flag on material). It is the most efficient way to set uniform. You can also do it for mesh instance btw.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: graphics Graphics related issue performance Relating to load times or frame rate
Projects
None yet
Development

No branches or pull requests

4 participants