Y-inverted graphics in Scene vs Game views
I'm writing a simple shader that takes Red, Blue, and Yellow primary colors and mixes them with each other like paint (so blue and yellow make green, for instance). Everything looks fine in the Scene view, but pixels containing secondary colors are y-inverted in the game view. Any ideas what the issue could be?
Here's the shader:
Shader "Custom/RBY" {
Properties {
_MainTex("Base (RGB)", 2D) = "white" {}
_Red ("Red", Color) = (1, 0, 0, 1) // RGBA for Red
_Blue ("Blue", Color) = (0, 0, 1, 1) // RGBA for Blue
_Yellow ("Yellow", Color) = (1, 1, 0, 1) // RGBA for Yellow
_Purple ("Purple", Color) = (0.5, 0, 0.5, 1) // RGBA when Red and Blue combine
_Orange ("Orange", Color) = (1, 0.647, 0, 1) // RGBA when Red and Yellow combine
_Green ("Green", Color) = (0, 1, 0, 1) // RGBA when Blue and Yellow combine
_AllColors ("AllColors", Color) = (0, 0, 0, 1) // RGBA when Red, Blue, and Yellow combine
}
SubShader {
Tags{ "Queue" = "Transparent" }
GrabPass { }
Pass {
CGPROGRAM
#pragma target 3.0
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
half4 _Red;
half4 _Blue;
half4 _Yellow;
half4 _Purple;
half4 _Orange;
half4 _Green;
half4 _AllColors;
uniform sampler2D _MainTex;
sampler2D _GrabTexture;
struct v2f {
half4 pos : POSITION;
half2 uv : TEXCOORD0;
float2 screenPos:TEXCOORD1;
};
bool areColorsEqual(half4 colorA, half4 colorB, bool isAlphaSensitive) {
if (colorA.r != colorB.r || colorA.g != colorB.g || colorA.b != colorB.b || (isAlphaSensitive && colorA.a != colorB.a)) {
return false;
} else {
return true;
}
}
v2f vert(appdata_img v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.screenPos = ComputeScreenPos(o.pos);
o.uv = v.texcoord;
return o;
}
half4 frag(v2f i) : COLOR{
half4 sourceColor = tex2D(_MainTex, i.uv);
half4 destinationColor = tex2D(_GrabTexture, half2(i.screenPos.x, i.screenPos.y));
if (areColorsEqual(sourceColor, _Red, false) && sourceColor.a > 0) {
if (areColorsEqual(destinationColor, _Blue, false)) {
destinationColor = _Purple;
} else if (areColorsEqual(destinationColor, _Yellow, false)) {
destinationColor = _Orange;
} else if (areColorsEqual(destinationColor, _Green, false)) {
destinationColor = _AllColors;
} else {
destinationColor = sourceColor;
}
} else if (areColorsEqual(sourceColor, _Blue, false) && sourceColor.a > 0) {
if (areColorsEqual(destinationColor, _Red, false)) {
destinationColor = _Purple;
} else if (areColorsEqual(destinationColor, _Yellow, false)) {
destinationColor = _Green;
} else if (areColorsEqual(destinationColor, _Orange, false)) {
destinationColor = _AllColors;
} else {
destinationColor = sourceColor;
}
} else if (areColorsEqual(sourceColor, _Yellow, false) && sourceColor.a > 0) {
if (areColorsEqual(destinationColor, _Red, false)) {
destinationColor = _Orange;
} else if (areColorsEqual(destinationColor, _Blue, false)) {
destinationColor = _Green;
} else if (areColorsEqual(destinationColor, _Purple, false)) {
destinationColor = _AllColors;
} else {
destinationColor = sourceColor;
}
} else {
discard;
}
return destinationColor;
}
ENDCG
}
}
Fallback off
}
scene-vs-game.png
(246.5 kB)
Comment
Your answer
Follow this Question
Related Questions
Shader invert sprite colors 2D 1 Answer
Get real colors like white or black 1 Answer
Acces shader color and lerp 1 Answer
Tint Multiple Textures Separately 1 Answer
Solid color texture or adding a color property to shader? 1 Answer