Changing the Sprite Renderer's Color property has no effect in my Custom Shader

I have a material with custom surface shader of which I want to change the “Color” in the Sprite Renderer. However, changing the values seems to do nothing. Changing the “Tint” in the shader itself works fine, but that changes the color for all my Sprites using this material. Is there a way to propagate the Sprite Renderer’s Color value like it works in the “Default-Sprite” material? The shader I’m using:

Shader "Sprites/Diffuse Flash"
{
	Properties
	{
		[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
		//_SelfIllum ("Self Illumination",Range(0.0,1.0)) = 0.0
		[PerRendererData]_FlashAmount ("Flash Amount",Range(0.0,1.0)) = 0.0
		_Color ("Tint", Color) = (1,1,1,1)
		[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
	}

	SubShader
	{
		Tags
		{
			"Queue" = "Transparent"
			"IgnoreProjector" = "True"
			"RenderType" = "Transparent"
			"PreviewType" = "Plane"
			"CanUseSpriteAtlas" = "True"
		}

		Cull Off
		Lighting Off
		ZWrite Off
		Blend SrcAlpha OneMinusSrcAlpha

		CGPROGRAM

		#pragma surface surf Lambert alpha:fade vertex:vert
		#pragma multi_compile DUMMY PIXELSNAP_ON

		sampler2D _MainTex;
		fixed4 _Color;
		float _FlashAmount,_SelfIllum;

		struct Input
		{
			float2 uv_MainTex;
			fixed4 color;
		};

		void vert(inout appdata_full v, out Input o)
		{
			#if defined(PIXELSNAP_ON) && !defined(SHADER_API_FLASH)
			v.vertex = UnityPixelSnap(v.vertex);
			#endif
			v.normal = float3(0,0,-1);

			UNITY_INITIALIZE_OUTPUT(Input, o);
			o.color = _Color;
		}

		void surf(Input IN, inout SurfaceOutput o)
		{
			fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
			o.Albedo = lerp(c.rgb,float3(1.0,1.0,1.0),_FlashAmount);
			//o.Emission = lerp(c.rgb,float3(1.0,1.0,1.0),_FlashAmount) * _SelfIllum;
			o.Alpha = c.a;
		}
	ENDCG
	}
}

Where I want to be able to change the Color value:

I’m not too well versed in shaders, but I already scoured the web and could find any solutions. Thanks in advance!

1 Like

6 Answers

6

The answer of @seandanger did not work me, instead, i looked at the source code that @nhanc18 provided (his answer is not correct as the line 57 uses the tint which is a property on the material, not on the sprite renderer). This is the correct and simple way of accessing the SpriteRenderer’s color field:

            struct appdata
            {
                float4 vertex : POSITION;
                fixed4 color : COLOR;
                float2 texcoord  : TEXCOORD0;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                float4 color : COLOR;
                float2 texcoord : TEXCORD0;
            };

            v2f vert (appdata IN)
            {
                v2f OUT;
                OUT.vertex = UnityObjectToClipPos(IN.vertex);
                OUT.texcoord = IN.texcoord;
                OUT.color = IN.color;
                return OUT;
            }

The “color” field on the appdata struct is the SpriteRenderer’s color field, so all you have to do is set the OUT variable’s color to the IN variable’s color.

It's worked well

The color property on SpriteRenderer is what’s called an Instanced Property, which are changed using a MaterialPropertyBlock, which allows the Material’s Instanced Properties to be changed without requiring a new SetPass call / batch. This page from the docs references that:


A Sprite Renderer uses the texture supplied in the Sprite property but uses the shader and other properties from the Material property (this is actually accomplished using a MaterialPropertyBlock behind the scenes). This means that you can use the same material to render different sprites without worrying about which texture is assigned on the material.


If you’re unfamiliar with MaterialPropertyBlock or Instanced Properties in shaders, then I recommend this excellent tutorial. I’m still learning myself, and it’s a confusing landscape out there with Unity having different information in various places, some of it outdated, much of it incomplete, etc.


Anyway, looking at the way Unity does it in their shader (I’m not sure how up to date that code is, but it can at least give hints), it looks like they’re referencing a reserved variable named unity_SpriteRendererColorArray, so it appears you need to tap into that.


Adding/modifying this code to my shader worked for me (goes inside the CGPROGRAM, see the comments):

// setup instanced property
UNITY_INSTANCING_BUFFER_START(MyProps)
	UNITY_DEFINE_INSTANCED_PROP(fixed4, unity_SpriteRendererColorArray)
UNITY_INSTANCING_BUFFER_END(MyProps)

struct appdata
{
	float4 vertex : POSITION;
	fixed4 color : COLOR; // apparently must be fixed4 to work with instancing
	float2 uv : TEXCOORD0;
	UNITY_VERTEX_INPUT_INSTANCE_ID // store instanced ID
};

struct v2f
{
	float4 vertex : SV_POSITION;
	fixed4 color : COLOR;
	float2 uv : TEXCOORD0;
};

v2f vert (appdata v)
{
	v2f o;
	UNITY_SETUP_INSTANCE_ID(v); // setup instanced id
	o.vertex = UnityObjectToClipPos(v.vertex);
	o.uv = v.uv;
    // get the instanced property
	o.color = v.color * UNITY_ACCESS_INSTANCED_PROP(MyProps, unity_SpriteRendererColorArray);

	return o;
}

p.s. (meta) I’m a newb and can’t apparently figure out how to break text into paragraphs without resorting to a horizontal rule. Anyone have a solution for that?

Hi seandanger, thanks for your answer! It has been quite a while since I asked this and gave up on it shortly after. I will do my due diligence and try out your answer when I have the time!

Tdebel, what you are doing here in the vertex shader is actually using the color of the material tint, rather than the color of the SpriteRenderer.


appdata_full should have a field “appdata_full.color” that needs to be fed into the “Input” struct’s “color” field, instead of using the “_Color” argument. This will use the SpriteRenderer’s color as applied to the vertices.

I don't think that's quite the case. Using the color property as passed into appdata_full like so has no effect: v2f vert (appdata_full IN) { v2f OUT; OUT.vertex = UnityObjectToClipPos(IN.vertex); OUT.texcoord = IN.texcoord; OUT.color = IN.color; return OUT; } You need to grab the Instanced Property from the shader. I think I have a solution, I'll post a separate answer.

I’m having the same problem, did you manage to solve this Tdebel?

EDIT: I managed to solve it! I’m posting it here in case anyone’s stuck like me.

My code looks a little different to Tdebel’s above, but maybe the same principle applies? In my code I had tried to access the standard color in my Sprite Renderer (on an object with a custom shader) a little something like this:

//A renderer I pulled into the code through the editor
public SpriteRenderer myRenderer;
    
    
private void ChangeColor()  {
        myRenderer.material.color = Color.blue
}

This didn’t change the color or alpha for me. What worked instead was if I removed the material-call and just went straight for color like this:

private void ChangeColor()  {
        myRenderer.color = Color.blue
}

Hope this helps someone in the future!

Just read the line 57 of this shader:

Attach this C# component to your sprite renderer Object:
I am not sure if this is the right way, this solved my problem, initially i think I would have to update mbp with some calculation inside my shader, but it turns out update the MPB of the material is the only thing that needs to be done here.

//SpriteColorPusher 
using UnityEngine;

[ExecuteAlways]
[RequireComponent(typeof(SpriteRenderer))]
public class SpriteColorPusher : MonoBehaviour
{
    SpriteRenderer _sr;
    MaterialPropertyBlock _mpb;

    // Last value
    Color _lastColor;
    bool  _lastFlipX;
    bool  _inited;

    void OnEnable() { Cache(); Apply(force:true); }
    void LateUpdate() { Cache(); Apply(force:false); }

    void Cache() {
        if (!_sr) _sr = GetComponent<SpriteRenderer>();
        if (_mpb == null) _mpb = new MaterialPropertyBlock();
    }

    void Apply(bool force)
    {
        if (!_sr) return;

        var c  = _sr.color;
        var fx = _sr.flipX;

        // Update only when value changes
        if (!force && _inited && c == _lastColor && fx == _lastFlipX)
            //return; //save performance but can't see the result in editor

        // get old value
        _sr.GetPropertyBlock(_mpb);


        //_mpb.SetColor("_SpriteColor", c); //use these two if your shader have these two value
        //_mpb.SetFloat("_FlipX", fx ? -1f : 1f);

        _sr.SetPropertyBlock(_mpb); //this is the only important line.

        _lastColor = c;
        _lastFlipX = fx;
        _inited = true;
    }
}