- Home /
How do I add transparency to a cg vertex lit shader
I have a scrolling background shader that I am trying to add transparency two, but I can't seem to get it working. Can anyone help me out with this?
Shader "BRS/Environment/Scrolling Background with Alpha" {
Properties {
_MainTex ("Base layer (RGB)", 2D) = "white" {}
//_DetailTex ("2nd layer (RGB)", 2D) = "white" {}
_ScrollX ("Base layer Scroll speed X", Float) = 1.0
_ScrollY ("Base layer Scroll speed Y", Float) = 0.0
//_Scroll2X ("2nd layer Scroll speed X", Float) = 1.0
//_Scroll2Y ("2nd layer Scroll speed Y", Float) = 0.0
_Intensity ("Intensity", Float) = 0.5
_Alpha ("Alpha", Range(0.0, 1.0)) = 1.0
}
SubShader {
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
Lighting Off
Fog { Mode Off }
ZWrite Off
LOD 100
CGINCLUDE
#pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
#include "UnityCG.cginc"
sampler2D _MainTex;
//sampler2D _DetailTex;
float4 _MainTex_ST;
//float4 _DetailTex_ST;
float _ScrollX;
float _ScrollY;
//float _Scroll2X;
//float _Scroll2Y;
float _Intensity;
float _Alpha;
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
//float2 uv2 : TEXCOORD1;
fixed4 color : TEXCOORD1;
};
v2f vert (appdata_full v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord.xy,_MainTex) + frac(float2(_ScrollX, _ScrollY) * _Time);
//o.uv2 = TRANSFORM_TEX(v.texcoord.xy,_DetailTex) + frac(float2(_Scroll2X, _Scroll2Y) * _Time);
o.color = fixed4(_Intensity, _Intensity, _Intensity, _Alpha);
return o;
}
ENDCG
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
fixed4 frag (v2f i) : COLOR
{
fixed4 o;
fixed4 tex = tex2D (_MainTex, i.uv);
//fixed4 tex2 = tex2D (_DetailTex, i.uv2);
//o = (tex * tex2) * i.color;
o = tex * i.color;
return o;
}
ENDCG
}
}
}
Answer by aldonaletto · Apr 20, 2012 at 04:03 AM
Add the blend mode after ZWrite Off:
...
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
I ended up rewriting the shader, but your answer was what I needed thanks!
Hey Deus,
Do you think you could post your functional shader code? I'm having a similar problem. Thanks!
@z4rd0z: Your post is not an answer, so don't post it as answer.
I've converted it into a comment.
Answer by deus_duke · Jun 18, 2012 at 05:53 PM
Sure. Instead of multiplying to overlay a texture with alpha, you need to use lerp.
// - Unlit
// - Scroll 3 layers /w Multiplicative op
Shader "BRS/Environment/Scroll 3 Layers" { Properties { _MainTex ("Base layer (RGB)", 2D) = "white" {} _FarTex ("2nd layer (RGB)", 2D) = "black" {} _NearTex ("2nd layer (RGB)", 2D) = "black" {} _ScrollX ("Base layer Scroll speed X", Float) = 0.1 _ScrollY ("Base layer Scroll speed Y", Float) = 0.0 _Scroll2X ("2nd layer Scroll speed X", Float) = 0.5 _Scroll2Y ("2nd layer Scroll speed Y", Float) = 0.0 _Scroll3X ("3nd layer Scroll speed X", Float) = 1.0 _Scroll3Y ("3nd layer Scroll speed Y", Float) = 0.0 _ColorIntensity ("Layer Multiplier", Float) = 0.5 }
SubShader{
Tags {"Queue"="Background" "IgnoreProjector"="True" "RenderType"="Background"}
LOD 100
ZWrite Off
Lighting Off
CGPROGRAM
#pragma surface surf NoLighting
half4 LightingNoLighting (SurfaceOutput s, half3 lightDir, half atten) {
half4 c;
c.rgb = s.Albedo;
return c;
}
struct Input {
float2 uv_MainTex;
float2 uv_FarTex;
float2 uv_NearTex;
};
sampler2D _MainTex;
sampler2D _FarTex;
sampler2D _NearTex;
half _ScrollX;
half _ScrollY;
half _Scroll2X;
half _Scroll2Y;
half _Scroll3X;
half _Scroll3Y;
fixed _ColorIntensity;
void surf (Input IN, inout SurfaceOutput o) {
float2 mainTex_uv = IN.uv_MainTex;
float2 farTex_uv = IN.uv_FarTex;
float2 nearTex_uv = IN.uv_NearTex;
mainTex_uv.x += _ScrollX * _Time.x;
mainTex_uv.y += _ScrollY * _Time.x;
farTex_uv.x += _Scroll2X * _Time.x;
farTex_uv.y += _Scroll2Y * _Time.x;
nearTex_uv.x += _Scroll3X * _Time.x;
nearTex_uv.y += _Scroll3Y * _Time.x;
float4 mainTex = tex2D (_MainTex, mainTex_uv);
float4 farTex = tex2D (_FarTex, farTex_uv);
float4 nearTex = tex2D (_NearTex, nearTex_uv);
float3 previous = lerp(mainTex.rgb, farTex.rgb, farTex.a);
o.Albedo = lerp(previous.rgb, nearTex.rgb, nearTex.a) * _ColorIntensity;
}
ENDCG
}
}
Your answer
Follow this Question
Related Questions
Add transparency to mask shader 1 Answer
Add Color Property to Unlit Alpha? 1 Answer
Transparent/Diffuse-shader always transparent by default (alpha)? 1 Answer
An Outline like the Editor does in game, with transparency 1 Answer
Is it possible to create a blur effect based on the alpha of a transparent texture on a plane? 0 Answers