![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Billy the Boy |
I would like to load a tscn with a material attached. This material has a shader, which I would like to manipulate. However, the manipulated values seem to be shared across all shader (material) instances.
After some search I found following solution which does not work for me:
self.set_material(self.get_material().duplicate(true))
Making the material unique, doesn’t work either:
Furthermore, marking the shader directly as unique, doesn’t work either
Another try was to set a ShaderMaterial and the Shader directly within the GoDot Editor, marking their resources as Local To Scene. That didn’t work either.
The shader resource itself is also set to Local to Scene
I did play around with load/preload and duplicate on nearly everything, without any change. This is how I create the component with the material + shader attached
var shockWave = load("res://Components/ShockWaveComponent.tscn").instance()
get_tree().get_root().add_child(shockWave)
shockWave.explode(get_parent().position, 0.51, 0.15, 0.22, 0.02, 5000)
and this is the components ready function
func _ready():
_shockWave_Material = self.get_material().duplicate(true)
self.set_material(_shockWave_Material)
What else did I try: Loading the material with preload and setting the sprite’s material property afterwards with: preloadedmaterial.duplicate(), sadly with the same result.
GoDot Version:
Godot Engine v3.2.3.stable.official - https://godotengine.org
OpenGL ES 3.0 Renderer: GeForce RTX 3070/PCIe/SSE2
Tested in Godot 4:
It has already been answered but I’d like to give more precision on embedded vs external resource and Make Unique, for those who don’t need to share materials a lot.
Creating a brand new embedded ShaderMaterial for each new node (e.g. AnimatedSprite2D) will guarantee uniqueness, so you’ll have nothing to add in code. You can then save the material externally, but you must make sure not to share it with other entities (unfortunately, that defeats a little the purpose of saving it as external resource, as each entity will need its own material resource; except if the goal is to have clean commits indicating changes affecting materials only). Or, if you do share it, you’ll have to either Make it Unique again (whether kept embedded or then saved), or duplicate it at runtime as suggested by the answers.
In your case, the material was already an external asset, so you’d have to Make it Unique on every other entity using it. If you only clicked Make Unique on the entity from which you saved the resource as file, it changed nothing. If you have many other entities already using the material though, duplicating it at runtime seems your best bet, rather than selecting each of them one by one to Make it Unique (esp. if you need to propagate future changes to those entities).
Hyper Sonic | 2023-01-26 17:32