- Home /
Shader doesn't work when object is inverted by axis
I have simple shader that imitates "Filled" Image Type for SpriteRenderer. It allows to cut off part of sprite depending of it's fill amount. But shader doesn't work if Object is inverted by any axis (no matter is it inverted by rotation, scale or "Flip" checkbox on SpriteRenderer). When Object is inverted Sprite isn't drawn at all.
Shader:
Shader "Custom/ProgressShaderUpToDown" {
Properties{
_MainTex("Base (RGB)", 2D) = "white" {}
_Progress("Progress (Float)", Float) = 0
_Color("Tint", Color) = (1.0, 0.6, 0.6, 1.0)
}
SubShader{
Tags{ "Queue" = "Transparent" }
Pass{
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
uniform float _Progress;
uniform fixed4 _Color;
float4 frag(v2f_img i) : COLOR
{
float4 result = tex2D(_MainTex, i.uv);
if (i.uv.y > _Progress)
{
result.a = 0;
}
return result*_Color;
}
ENDCG
}
}
}
How can I fix this?
Answer by $$anonymous$$ · Dec 27, 2017 at 05:49 PM
Sprites that are flipped by scale or rotation will naturally not be drawn by a shader that does not have culling set to off (see SL-CullAndDepth) because the triangle winding order then ends up inverted in clip space. I don't think that should be the case with the flip switch but I can't check that right now. Maybe just check whether disabling culling (and therefore making your shader double-sided) works for you.
Thanks dude!
If anyone care here is shader that works for both sides:
Shader "Custom/ProgressShaderUpToDown" {
Properties{
_$$anonymous$$ainTex("Base (RGB)", 2D) = "white" {}
_Progress("Progress (Float)", Float) = 0
_Color("Tint", Color) = (1.0, 0.6, 0.6, 1.0)
}
SubShader{
Tags{ "Queue" = "Transparent" }
Pass{
Blend SrcAlpha One$$anonymous$$inusSrcAlpha
Cull Off
CGPROGRA$$anonymous$$
#pragma vertex vert_img
#pragma fragment frag
#include "UnityCG.cginc"
uniform sampler2D _$$anonymous$$ainTex;
uniform float _Progress;
uniform fixed4 _Color;
float4 frag(v2f_img i) : COLOR
{
float4 result = tex2D(_$$anonymous$$ainTex, i.uv);
if (i.uv.y > _Progress)
{
result.a = 0;
}
return result*_Color;
}
ENDCG
}
}
}
Your answer
Follow this Question
Related Questions
Any idea how to get object rotation or transformMatrix in the vertex shader? 0 Answers
Distort shader not showing sprites 2 Answers
U5 shader coming out solid black on newer iOS devices (iPad 2 Air / iPhone 6) 1 Answer
Allow 2D sprite to receive light from any direction and show on both sides 0 Answers