BabylonJS / Extensions

Extensions for Babylon.js
177 stars 157 forks source link

Material's uniform cannot be updated after another material is added #122

Closed sebavan closed 6 years ago

sebavan commented 6 years ago

Playground: https://www.babylonjs-playground.com/#3WN9C6#4

I asked related questions here: http://www.html5gamedevs.com/topic/32203-custommaterial-updating-custom-uniforms-custom-alpha-and-precision/

Is it considered a bug or not supported? Is there any plan for it? Is there any way I can work around it, if I really need to update uniforms values for my shader?

Thank you very much!

sebavan commented 6 years ago

Ping @NasimiAsl who maintains the custom material

NasimiAsl commented 6 years ago

hi thanks for starting this

let i say what i am thinking about this problem ( may be wrong) i think this problem in standardMaterial update uniform when we have some mesh if this is true custom material is inherited this bug too

check it and make one special uniform in standard material and change it by Mat.getEffect().setFloat() (in scene with 2 mesh) if you see this problem this is standard material bug if dont it is custommaterial bug

thanks (i am so excited to find solution for this )

NasimiAsl commented 6 years ago

and i think this is babylon bug (not babylon extension )

NasimiAsl commented 6 years ago

ping @sebavan

sebavan commented 6 years ago

Actually you can not update uniform directly in standard material by design as there is a cache mecanism in place and you need to use the property to not break the cache consistency (this is the case for all the pushMaterial). to support this in your case you need to refresh the cache after setting the value or associated ubo will not be updated.

NasimiAsl commented 6 years ago

1.so why this work for single mesh ?

  1. how can we have method for clear (some cache not all) for solve this
sebavan commented 6 years ago
  1. only one material would basically have less issues accross materials cache.
  2. look at the markAllMaterialsAsDirty on the scen this might help
NasimiAsl commented 6 years ago

any way i can mark all custommaterial as dirty :D

NasimiAsl commented 6 years ago

so i cant remove any cache

deltakosh commented 6 years ago

There is a way. I'll be back this tuesday and tell you how to proceed

NasimiAsl commented 6 years ago

everything is possible with deltakosh

deltakosh commented 6 years ago

Every mesh can have his own version of the effect so you can go through all meshes and check material properties. Then if the mesh material is the one that you want, iterate through all submeshes and call submesh.getEffect().setXXXX

Does it make sense?

NasimiAsl commented 6 years ago

hi look this sample https://www.babylonjs-playground.com/#3WN9C6#4

when we have one mesh (no any submesh) sphere update uniform correctly when i add other mesh ( after some time in this sample ) sphere .getEffect frizzed and don't be update mesh material uniform

this is base problem

deltakosh commented 6 years ago

The problem is about timing. You cannot change the value ahead of time as the effect is shared between multiple meshes. You have to use the onBindObservable of the material for that: https://www.babylonjs-playground.com/#3WN9C6#5

I also had to fix an issue with the custom material here: https://github.com/BabylonJS/Babylon.js/commit/4e89757d696252bda902968f719d7e32961647a4#diff-638e8dcec06012300f357b139bada80bR5939

Hope this helps ;)