PowerUpT yes.
but first, using boolean comparisons in shaders is not encouraged, you should try to use math instead. I'm guessing there's no logic to this and you are just changing ONE color of the texture by another, ONE SINGLE TIME. If so, you should instead use a texture for your sprite and a mask
, which could be black and white, with the parts of your sprite that you want to change. this might seem like a lot of work, so you can just use your method if you want, but it's not ideal.
this method also uses instances
, which are not supported in compatibility mode.
first create a material
, put your sprite on albedo texture
and set the transparency
of the material and everything else until it looks right, then click on the material and go to convert_to_shader
, this will give you a starting spatial
shader, which is what you need for 3D.
in the spatial shader, go to albedo
and add instance in front.
you also need a mask to change the color, so just add a new sampler2D. this is another texture that you can use in the shader. textures usually have 3 to 4 channels, but for now we are only using one.
instance uniform vec4 albedo : source_color;
uniform sampler2D texture_albedo : source_color,filter_linear_mipmap,repeat_enable;
uniform sampler2D texture_mask : hint_roughness_r,filter_linear_mipmap,repeat_enable;
if the image looks weird, make sure the UVs are set to 1 1 1
. if there's no transparency, change alpha scissor threshold
to something like 0.5
then, in fragment, you want to send data to ALBEDO, this is similar to COLOR in 2D, as it is color information.
ALBEDO = albedo.rgb * albedo_tex.rgb;
you also have to read the mask texture:
vec3 mask = texture(texture_mask, base_uv).rgb;
we will mix
albedo with the texture and the mask, the black parts of the mask will be filled with the texture, and the whiter parts will be colored.
ALBEDO = mix(albedo_tex.rgb, albedo.rgb, mask.r);//we are using the red channel of mask, but you can use all 4 channels for more combinations
in order to use this with a sprite3D, we apply it to Geometry->Material Override
Finally, the reason we are using an instance uniform is because we can change it for any color at runtime and each copy of the node can have a different color. a new tab will appear under geometry where you can test it, and we can now set the instance uniform with mysprite3D.set_instance_shader_parameter("albedo", mycolor)
in the script.
https://docs.godotengine.org/en/stable/classes/class_geometryinstance3d.html#class-geometryinstance3d-method-set-instance-shader-parameter
or you could use your current method, which I guess uses different materials for each unit, in that case just don't add instance
to albedo
.