This blog post contains only source code for shaders. Full tutorial is on YouTube.
Simple Alpha Blending Shader
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
Shader "Alien TechLAB/Alpha Blending" { Properties { _MainTex("RGBA Texture Image", 2D) = "white" {} } SubShader { Tags {"Queue"="Transparent"} Pass { Blend SrcAlpha OneMinusSrcAlpha Cull Off ZWrite Off ZTest LEqual CGPROGRAM #pragma vertex vert #pragma fragment frag uniform sampler2D _MainTex; struct vertexInput { float4 vertex : POSITION; float4 texcoord : TEXCOORD0; }; struct vertexOutput { float4 pos : SV_POSITION; float4 tex : TEXCOORD0; }; vertexOutput vert(vertexInput input) { vertexOutput output; output.tex = input.texcoord; output.pos = UnityObjectToClipPos(input.vertex); return output; } float4 frag(vertexOutput input) : COLOR { float4 textureColor = tex2D(_MainTex, input.tex.xy); return textureColor; } ENDCG } } } |
Correct Alpha Blending Shader
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
Shader "Alien TechLAB/Correct Alpha Blending" { Properties { _MainTex("RGBA Texture Image", 2D) = "white" {} } SubShader { Tags {"Queue"="Transparent"} Pass { Blend SrcAlpha OneMinusSrcAlpha Cull Front ZWrite Off ZTest LEqual CGPROGRAM #pragma vertex vert #pragma fragment frag uniform sampler2D _MainTex; struct vertexInput { float4 vertex : POSITION; float4 texcoord : TEXCOORD0; }; struct vertexOutput { float4 pos : SV_POSITION; float4 tex : TEXCOORD0; }; vertexOutput vert(vertexInput input) { vertexOutput output; output.tex = input.texcoord; output.pos = UnityObjectToClipPos(input.vertex); return output; } float4 frag(vertexOutput input) : COLOR { float4 textureColor = tex2D(_MainTex, input.tex.xy); return textureColor; } ENDCG } Pass { Blend SrcAlpha OneMinusSrcAlpha Cull Back ZWrite Off ZTest LEqual CGPROGRAM #pragma vertex vert #pragma fragment frag uniform sampler2D _MainTex; struct vertexInput { float4 vertex : POSITION; float4 texcoord : TEXCOORD0; }; struct vertexOutput { float4 pos : SV_POSITION; float4 tex : TEXCOORD0; }; vertexOutput vert(vertexInput input) { vertexOutput output; output.tex = input.texcoord; output.pos = UnityObjectToClipPos(input.vertex); return output; } float4 frag(vertexOutput input) : COLOR { float4 textureColor = tex2D(_MainTex, input.tex.xy); return textureColor; } ENDCG } } } |