What’s the most efficient process if adjusting materials “on the fly” and wanting to push those updates out to everything using the material.
For example if I have a material used in a number of places and I take that material and change the DiffuseMap texture on it then what else do I need to do to propagate out the change? Do I need to call setMaterial again on every object using it?
Thanks,
Z
AFAIK you can rootNode.setMaterial() and it will apply the material recursively… If you use only one material for all
No, we use different materials all over. But each material in multiple places.
Then i would probably make a central:
[java]
HashMap< String , List < Spatial > >
[/java]
should at least be much more performant then iterating over all elements…
BTW, if you use the same material in multiple places (really the same and not a clone) it should be enough to update the material once
I use the same material everywhere, really the same. It’s a static final Material. Objects get setMaterial to that cached Material.
I call setTexture in the material.
The models on screen don’t update.
Your idea of a central hashmap looks like its asking for memory leaks. Although I guess it could be done using weak references.
If you change something on the material instance then everything using that material will automatically update. I do this all the time.
@pspeed said:
If you change something on the material instance then everything using that material will automatically update. I do this all the time.
He speaks da troof. I have a central mechanism for storing all materials common and instanced. I can update all materials (similar or not if needed). I'm still trying to determine if preloading common materials is giving me any benefit or just wasting resources. Anyone?
@zarch
Weird… maybe set texture cause a re-compilation of the shaders or something, that could be causing problems? Like there’s a define or something that weird things up. Only thing I can think of.
@t0neg0d
Can you explain more what types of materials you want to pre-load? Like an example.
If I use generated meshes (or meshes that doesn’t come with a material by default) I normally just load the material when the mesh generation class is instantiated, which is normally when the scene is created. A grasslayer in biomonkey for example (representing a type of grass) has a material instance, and it is applied to all newly generated grass meshes of that type. That material is created when the layer is created, which is usually when the scene is loaded. I keep a reference to the grass texture as well in that class, and most other params.
For mostly everything else the materials are tied to something (models, particle emitters or w/e) so they remain as long as the “parent” does - and effects and stuff that are often repeated (or is likely to be) I keep an instance ready throughout the scene. Whether I load that effect the first time its used or when the scene is created I don’t really think matters much. Some stuff i have getters for that creates the objects if the references are null, like emitters that are used very seldom (level up effects perhaps, or similar).
@zarch said:
I use the same material everywhere, really the same. It's a static final Material. Objects get setMaterial to that cached Material.
I call setTexture in the material.
The models on screen don't update.
Something else is going on. I only have a dozen materials in my game reused by nearly every mesh... and for some of them I set uniforms every frame and they update properly.
Maybe somehow they are not really the same material instances or something.
Works fine for me as well ( just tried it with unshaded). Can you show us how u are setting/getting the materials?
Yeah, I was puzzled by this as well since what you are describing is how I expected it to work based on my prior experience. Given confidence that I was on the right track by the answers above I went back through my code with a fine tooth comb (and some breakpoints) and found a break in the notification chain.
Then I just needed to make sure I set the wrap mode on the newly loaded textures correctly since that’s a texture property not a material one and it all worked perfectly.
Thanks for the help