- Home /
Transparent terrain shader not working
I'm trying to achieve the transparent terrain effect in this thread: http://forum.unity3d.com/threads/31734-Hiding-parts-of-terrain
I need to be able to paint transparent textures on my terrain, so I created a shader with the code from that thread to make the terrain support alpha:
// Upgrade NOTE: replaced 'glstate.lightmodel.ambient' with 'UNITY_LIGHTMODEL_AMBIENT'
// Upgrade NOTE: replaced 'glstate.matrix.mvp' with 'UNITY_MATRIX_MVP'
// Upgrade NOTE: replaced 'glstate.matrix.transpose.modelview[0]' with 'UNITY_MATRIX_T_MV'
Shader "Hidden/TerrainEngine/Splatmap/Lightmap-FirstPass" {
Properties {
_Control ("Control (RGBA)", 2D) = "red" {}
_LightMap ("LightMap (RGB)", 2D) = "white" {}
_Splat3 ("Layer 3 (A)", 2D) = "white" {}
_Splat2 ("Layer 2 (B)", 2D) = "white" {}
_Splat1 ("Layer 1 (G)", 2D) = "white" {}
_Splat0 ("Layer 0 (R)", 2D) = "white" {}
_BaseMap ("BaseMap (RGB)", 2D) = "white" {}
}
Category {
// Fragment program, 4 splats per pass
SubShader {
Tags { "SplatCount" = "4" }
Pass {
Tags { "LightMode" = "Always" }
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex LightmapSplatVertex
#pragma fragment LightmapSplatFragment
#pragma fragmentoption ARB_fog_exp2
#pragma fragmentoption ARB_precision_hint_fastest
#define TEXTURECOUNT 4
#include "UnityCG.cginc"
uniform float4 _Splat0_ST,_Splat1_ST,_Splat2_ST,_Splat3_ST,_Splat4_ST;
uniform float4 _Splat5_ST,_Splat6_ST,_Splat7_ST,_Splat8_ST,_Splat9_ST;
struct v2f {
float4 pos : POSITION;
float fog : FOGC;
float4 uv[(TEXTURECOUNT+1)/2 + 1] : TEXCOORD0;
float4 color : COLOR;
};
uniform sampler2D _Control;
uniform sampler2D _Control1;
uniform sampler2D _Splat0,_Splat1,_Splat2,_Splat3;
uniform sampler2D _Splat4,_Splat5,_Splat6,_Splat7;
float4 CalculateVertexLights (float3 objSpaceNormal) {
float3 normal = mul (objSpaceNormal, (float3x3)UNITY_MATRIX_T_MV);
// Do vertex light calculation
float4 lightColor = UNITY_LIGHTMODEL_AMBIENT;
for (int i = 0; i < 4; i++) {
float3 lightDir = glstate.light[i].position.xyz;
float lightAmt = saturate( dot (normal, lightDir) );
lightColor += glstate.light[i].diffuse * lightAmt;
}
return lightColor;
}
void CalculateSplatUV (float2 baseUV, inout v2f o) {
o.uv[0].xy = baseUV;
#if TEXTURECOUNT >= 1
o.uv[1].xy = TRANSFORM_TEX (baseUV, _Splat0);
#endif
#if TEXTURECOUNT >= 2
o.uv[1].zw = TRANSFORM_TEX (baseUV, _Splat1);
#endif
#if TEXTURECOUNT >= 3
o.uv[2].xy = TRANSFORM_TEX (baseUV, _Splat2);
#endif
#if TEXTURECOUNT >= 4
o.uv[2].zw = TRANSFORM_TEX (baseUV, _Splat3);
#endif
#if TEXTURECOUNT >= 5
o.uv[3].xy = TRANSFORM_TEX (baseUV, _Splat4);
#endif
#if TEXTURECOUNT >= 6
o.uv[3].zw = TRANSFORM_TEX (baseUV, _Splat5);
#endif
#if TEXTURECOUNT >= 7
o.uv[4].xy = TRANSFORM_TEX (baseUV, _Splat6);
#endif
#if TEXTURECOUNT >= 8
o.uv[4].zw = TRANSFORM_TEX (baseUV, _Splat7);
#endif
}
half4 CalculateSplat (v2f i) {
half4 color;
#if TEXTURECOUNT >= 1
half4 control = tex2D (_Control, i.uv[0].xy);
color = control.r * tex2D (_Splat0, i.uv[1].xy);
#endif
#if TEXTURECOUNT >= 2
color += control.g * tex2D (_Splat1, i.uv[1].zw);
#endif
#if TEXTURECOUNT >= 3
color += control.b * tex2D (_Splat2, i.uv[2].xy);
#endif
#if TEXTURECOUNT >= 4
color += control.a * tex2D (_Splat3, i.uv[2].zw);
#endif
#if TEXTURECOUNT >= 5
control = tex2D (_Control1, i.uv[0].xy);
color = control.r * tex2D (_Splat4, i.uv[3].xy);
#endif
#if TEXTURECOUNT >= 6
color += control.g * tex2D (_Splat5, i.uv[3].zw);
#endif
#if TEXTURECOUNT >= 7
color += control.b * tex2D (_Splat6, i.uv[4].xy);
#endif
#if TEXTURECOUNT >= 8
color += control.a * tex2D (_Splat7, i.uv[4].zw);
#endif
return color;
}
float4 VertexlitSplatFragment (v2f i) : COLOR {
half4 col = CalculateSplat (i) * i.color;
col *= float4 (2,2,2,0);
return col;
}
v2f VertexlitSplatVertex (appdata_base v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.fog = o.pos.z;
o.color = CalculateVertexLights (v.normal);
CalculateSplatUV (v.texcoord, o);
return o;
}
uniform sampler2D _LightMap;
float4 LightmapSplatFragment (v2f i) : COLOR {
half4 lightmapTex = tex2D (_LightMap, i.uv[0].xy);
half4 col = CalculateSplat (i) * lightmapTex;
col *= float4 (2,2,2,0);
col.a = lightmapTex.a;
return col;
}
v2f LightmapSplatVertex (appdata_base v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.fog = o.pos.z;
CalculateSplatUV (v.texcoord, o);
return o;
}
ENDCG
}
}
// ATI texture shader, 4 splats per pass
#warning Upgrade NOTE: SubShader commented out because of manual shader assembly
/*SubShader {
Tags { "SplatCount" = "4" }
Pass {
Tags { "LightMode" = "Always" }
Program "" {
SubProgram {
"!!ATIfs1.0
StartConstants;
CONSTANT c0 = {0};
EndConstants;
StartOutputPass;
SampleMap r0, t0.str; # splat0
SampleMap r1, t1.str; # splat1
SampleMap r2, t2.str; # splat2
SampleMap r3, t3.str; # splat3
SampleMap r4, t4.str; # control
SampleMap r5, t5.str; # lightmap
MUL r0.rgb, r0, r4.r;
MAD r0.rgb, r1, r4.g, r0;
MAD r0.rgb, r2, r4.b, r0;
MAD r0.rgb, r3, r4.a, r0;
MUL r0.rgb, r0.2x, r5;
MOV r0.a, c0;
EndPass;
"
}
}
SetTexture [_Splat0]
SetTexture [_Splat1]
SetTexture [_Splat2]
SetTexture [_Splat3]
SetTexture [_Control]
SetTexture [_LightMap]
}
}*/
}
// Fallback to base map
Fallback "Hidden/TerrainEngine/Splatmap/Lightmap-BaseMap"
}
Not only does this not work for me, but I get a bunch of shader errors. Nothing has changed, meaning I still just get the color black when I try to paint my terrain transparent. Is this shader just no longer supported by Unity? Because I can't see why it wouldn't be working, as I copied and pasted it exactly how it was in that thread. This is kind of urgent, so can someone please help me out here?
Can someone please help me? I really need to get this to work.
Answer by Venryx · Mar 26, 2013 at 02:00 AM
There's a shader for this on the Wiki: http://wiki.unity3d.com/index.php/TerrainTransparency
(I also tried basing it off of that shader, but then I figured out how it worked and just made my own; if you still really need help fixing the one above, let me know and I can try helping you.)
Answer by shahinkey · Oct 16, 2013 at 11:19 AM
Hi, I could generate transparent terrain using http://wiki.unity3d.com/index.php/TerrainTransparency. Just paste it in the shader and generate a material then apply the material as painting of the terrain. Unfortunately the transparent terrain can not receive the shadows. If any one can solve this problem please let us know. Thank you in advance Hoshang