- Home /
Sprite color invert
Hi, so I have black sprite and I want it to change color on moving onto other black cube. I tried doing this with depth masks, but I'm new to shaders, so I couldn't make it work. Here's an example of what I'm trying to do:
Does anybody know how to achieve it?
Any help will be apreciated.
Answer by Techn0man · May 14, 2018 at 07:49 PM
@darius0021 if its for a sprite then I don't have the awnser but if it's for a mesh then the code would be like this
Shader "Custom/Invert Colors" {
Properties {
// defines color property
_Color("Tint Color", Color) = (1,1,1,1)
}
SubShader{
// sets the queue to transparent
Tags{ "Queue" = "Transparent" }
Pass { ColorMask 0 } // turns off rendering to all color channels
Blend OneMinusDstColor OneMinusSrcAlpha //invert blending, so long as FG color is 1,1,1,1
BlendOp Add // adds source and destination together with specified blend modes
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
uniform float4 _Color;
struct vertexInput
{
float4 vertex: POSITION;
float4 color : COLOR;
};
struct fragmentInput
{
float4 pos : SV_POSITION;
float4 color : COLOR0;
};
fragmentInput vert(vertexInput i)
{
fragmentInput o;
o.pos = UnityObjectToClipPos(i.vertex);
o.color = _Color;
return o;
}
half4 frag(fragmentInput i) : COLOR
{
return i.color;
}
ENDCG
}
}
}
I hope this helps
Your answer
Follow this Question
Related Questions
Sprite texture incorrect 0 Answers
2D Sprite Flash Shader - unwanted outline 0 Answers
Depth mask with alpha / adding alpha to frag grabpass shader 0 Answers
Shader for 2D Sprite "Always Rendered on Top" Problem 0 Answers
Apply overlay effect in an area just on specific objects (2D underwater) 0 Answers