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:
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:
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.
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:
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;
}
}
It's worked well
– dunghn94