I worked on a shader that should replace colors (based on this color-swap shader) and saved it to a file (the material too). So, when I load the material with the shader into a sprite, all the sprites applies the same color. My shader has a parameter (an int that should be 0 or 1) and based on that parameter, the shader will apply a set of colors.
My question is: I know shaders and materials are Resource, so they will apply their effects to all objects that load them. Do I have a way to make my shader apply its effects separately for each object, even with the shader being a file loaded? To contextualize, this is for chess pieces, so the parameter would define whether the piece is white or black, and set the colors in the sprite.
I thought that I could just save the file, define a shader parameter to set the color, but when I change the parameter for one piece, it changes to all pieces. Pieces and shaders should be instantiated and loaded at run time.
Shader parameters are kept in material resources. In order to have unique parameters you need to duplicate the material resource (while shader resource can still be shared). You can do this either by manual duplication or, if the material is inside a scene, enable material’s resource_local_to_scene_flag, which will instruct the engine to automatically duplicate upon scene instantiation.
What normalized said is correct for normal uniforms, but you also have the option of using instance uniforms that can be set on a per-object basis. See the guide below: