Hi,
So I have had the idea that I can write a custom shader for the built-in SpriteRenderer that lets me do per-sprite based effects without breaking batching because its per-vertex data that is being passed in.
What I would like to do is pack the sprite color into a single float then set that in a single channel of the color leaving me with the other 3 channels for whatever I want.
So this is what I have on the C# side:
[ExecuteInEditMode]
public class SpriteEffects : MonoBehaviour
{
[Range(0, 2)]
public float brightnessAmount = 1;
[Range(0, 2)]
public float greyscaleAmount = 1;
public Color tint;
private SpriteRenderer spriteRenderer;
void Start()
{
spriteRenderer = GetComponent<SpriteRenderer>();
}
public void Update()
{
spriteRenderer.color = GetColor(tint, brightnessAmount, greyscaleAmount, 1f);
}
public Color GetColor(Color color, float brightness, float greyscale, float somethingelse)
{
// Pack the color into a single float
var magic = new Vector4(1.0f, 1/255.0f, 1/65025.0f, 1/160581375.0f);
var v4 = new Vector4(color.r, color.g, color.b, color.a);
var packedColor = Vector4.Dot(v4, magic);
return new Color(packedColor, brightness, greyscale, somethingelse);
}
}
Then on the shader side:
Shader "Sprites/Little Lords"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
}
SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Cull Off
Lighting Off
ZWrite Off
Fog { Mode Off }
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile DUMMY PIXELSNAP_ON
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
half2 texcoord : TEXCOORD0;
};
fixed4 _Color;
v2f vert(appdata_t IN)
{
v2f OUT;
OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
OUT.texcoord = IN.texcoord;
OUT.color = EncodeFloatRGBA(IN.color.r);
#ifdef PIXELSNAP_ON
OUT.vertex = UnityPixelSnap (OUT.vertex);
#endif
return OUT;
}
sampler2D _MainTex;
fixed4 frag(v2f IN) : COLOR
{
half4 texcol = tex2D (_MainTex, IN.texcoord);
texcol.rgb *= IN.color.rgb;
return texcol;
}
ENDCG
}
}
Fallback "Sprites/Default"
}
For now im just using the .r component which should be the color.
But for some reason the result is just black and I cant figure out why:
Does anyone have a clue where im going wrong?