Day / Night Cycle affecting Grass [updated – webplayer]

Playing around with the terrain’s detail meshes in order to have them casting and recieving shadows I have found a solution which provides detail meshes fully corresponing to the actual light settings:

Looking at the 2 images below you will notice that:

  • the yellow weed casts shadows.

  • recives shadows.

  • and is affected by the main directional light correctely as far as its brightness is concerned.

  • the grass in the bottom left corner doesn’t do any of the things described above.

My solution: Both – grass and yellow weed are rendered as detail meshes as part of the terrain engine, but

  • grass uses the stadard render mode: grass
  • yellow weed uses the render mode: VertexLit
  • I have copied the transparent alpha cutoff shader to a new file which replaces the builtin vertexLit shader.

Unfortunatly the edges do not get anti aliased any more as with the vegetation two pass shader but nevertheless…
Due to the lighting you can’t use just “flat” detail meshes and turn off culling – you will have to rework the models to make them 2 sided…

Lars

This is something I’ve been poking Aras and other devs to do with the Terrain engine shaders. No luck so far :wink:

It might be a bit of a hack, but sharing your altered shaders would be a nice contribution to the community. Hopefully this gets fixed/updated in a 2.x release… it would greatly improve the realism with the Terrain Engine.

It is pretty simple as I just copied the code from the transparent cutout shader to a new file and renamed it to “Vertexlit.shader”.
described here: http://forum.unity3d.com/viewtopic.php?t=12979
I had to restart Unity to get the new shader applied and working…

Of course it would be very nice to have the smoothing of the vegetation 2 pass shader, but I guess this is not possible.

Shader "Hidden/TerrainEngine/Details/Vertexlit" {
	Properties {
	_Color ("Main Color", Color) = (1,1,1,1)
	_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
	_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
}

Category {
	Tags {"RenderType"="TransparentCutout"}
	LOD 200
	Alphatest Greater [_Cutoff]
	AlphaToMask True
	ColorMask RGB
	Fog { Color [_AddFog] }
	Blend AppSrcAdd AppDstAdd
	
	
	
	// ------------------------------------------------------------------
	// ARB fragment program
	
	SubShader {
		// Ambient pass
		
		// Cull Off
		
		
		
		Pass {
			Name "BASE"
			Tags {"LightMode" = "PixelOrNone"}
			Color [_PPLAmbient]
			SetTexture [_MainTex] {constantColor [_Color] Combine texture * primary DOUBLE, texture * primary}
		}
		
		
		
		// Vertex lights
		Pass { 
			Name "BASE"
			Tags {"LightMode" = "Vertex"}
			Lighting On
			Material {
				Diffuse [_Color]
				Emission [_PPLAmbient]
			} 
			SetTexture [_MainTex] {combine texture * primary DOUBLE, texture * primary}
		}
		// Pixel lights
		Pass {	
			Name "PPL"
			Tags { "LightMode" = "Pixel" }

CGPROGRAM
#pragma fragment frag
#pragma vertex vert
#pragma multi_compile_builtin
#pragma fragmentoption ARB_fog_exp2
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
#include "AutoLight.cginc"

struct v2f {
	V2F_POS_FOG;
	LIGHTING_COORDS
	float2	uv;
	float3	normal;
	float3	lightDir;
};

uniform float4 _MainTex_ST;

v2f vert (appdata_base v)
{
	v2f o;
	PositionFog( v.vertex, o.pos, o.fog );
	o.normal = v.normal;
	o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
	o.lightDir = ObjSpaceLightDir( v.vertex );
	TRANSFER_VERTEX_TO_FRAGMENT(o);
	return o;
}

uniform sampler2D _MainTex;
uniform float4 _Color;

float4 frag (v2f i) : COLOR
{
	half4 texcol = tex2D( _MainTex, i.uv );	
	half4 c = DiffuseLight( i.lightDir, i.normal, texcol, LIGHT_ATTENUATION(i) );
	c.a = texcol.a * _Color.a;
	return c;
}
ENDCG






			SetTexture [_MainTex] {combine texture}
			SetTexture [_LightTexture0] {combine texture}
			SetTexture [_LightTextureB0] {combine texture}
		}
		
	}
	
	
	
	
	
 	// ------------------------------------------------------------------
	// Radeon 9000

	SubShader {
		// Ambient pass
		Pass {
			Name "BASE"
			Tags {"LightMode" = "PixelOrNone"}
			Color [_PPLAmbient]
			SetTexture [_MainTex] {constantColor [_Color] Combine texture * primary DOUBLE, texture * constant}
		}
		// Vertex lights
		Pass { 
			Name "BASE"
			Tags {"LightMode" = "Vertex"}
			Lighting On
			Material {
				Diffuse [_Color]
				Emission [_PPLAmbient]
			}
			SetTexture [_MainTex] {Combine texture * primary DOUBLE, texture * primary}
		}
		
		// Pixel lights with 0 light textures
		Pass { 
			Name "PPL"
			Tags { 
				"LightMode" = "Pixel" 
				"LightTexCount" = "0"
			}

CGPROGRAM
#pragma vertex vert
#include "UnityCG.cginc"

struct v2f {
	V2F_POS_FOG;
	float2 uv		: TEXCOORD0;
	float3 normal	: TEXCOORD1;
	float3 lightDir	: TEXCOORD2;
};

uniform float4 _MainTex_ST;

v2f vert(appdata_base v)
{
	v2f o;
	PositionFog( v.vertex, o.pos, o.fog );
	o.normal = v.normal;
	o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
	o.lightDir = ObjSpaceLightDir( v.vertex );
	return o; 
}
ENDCG
			Program "" {
				SubProgram {
					Local 0, [_ModelLightColor0]
					Local 1, [_Color]

"!!ATIfs1.0
StartConstants;
	CONSTANT c0 = program.local[0];
	CONSTANT c1 = program.local[1];
EndConstants;

StartOutputPass;
	SampleMap r0, t0.str;			# main texture
	SampleMap r1, t2.str;			# normalized light dir
	PassTexCoord r2, t1.str;		# normal
	
	DOT3 r5.sat, r2, r1.2x.bias;	# R5 = diffuse (N.L)
	
	MUL r0.rgb, r0, r5;
	MUL r0.rgb.2x, r0, c0;
	MUL r0.a, r0, c1;
EndPass; 
"
				}
			}
			SetTexture[_MainTex] {combine texture}
			SetTexture[_CubeNormalize] {combine texture}
		}
		
		// Pixel lights with 1 light texture
		Pass {
			Name "PPL"
			Tags { 
				"LightMode" = "Pixel" 
				"LightTexCount" = "1"
			}

CGPROGRAM
#pragma vertex vert
#include "UnityCG.cginc"

uniform float4 _MainTex_ST;
uniform float4x4 _SpotlightProjectionMatrix0;

struct v2f {
	V2F_POS_FOG;
	float2 uv		: TEXCOORD0;
	float3 normal	: TEXCOORD1;
	float3 lightDir	: TEXCOORD2;
	float4 LightCoord0 : TEXCOORD3;
};

v2f vert(appdata_tan v)
{
	v2f o;
	PositionFog( v.vertex, o.pos, o.fog );
	o.normal = v.normal;
	o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
	o.lightDir = ObjSpaceLightDir( v.vertex );
	
	o.LightCoord0 = mul(_SpotlightProjectionMatrix0, v.vertex);
	
	return o; 
}
ENDCG
			Program "" {
				SubProgram {
					Local 0, [_ModelLightColor0]
					Local 1, [_Color]

"!!ATIfs1.0
StartConstants;
	CONSTANT c0 = program.local[0];
	CONSTANT c1 = program.local[1];
EndConstants;

StartOutputPass;
	SampleMap r0, t0.str;			# main texture
	SampleMap r1, t2.str;			# normalized light dir
	PassTexCoord r4, t1.str;		# normal
	SampleMap r2, t3.str;			# a = attenuation
	
	DOT3 r5.sat, r4, r1.2x.bias;	# R5 = diffuse (N.L)
	
	MUL r0.rgb, r0, r5;
	MUL r0.rgb.2x, r0, c0;
	MUL r0.rgb, r0, r2.a;			# attenuate
	MUL r0.a, r0, c1;
EndPass; 
"
				}
			}
			SetTexture[_MainTex] {combine texture}
			SetTexture[_CubeNormalize] {combine texture}
			SetTexture[_LightTexture0] {combine texture}
		}
		
		// Pixel lights with 2 light textures
		Pass {
			Name "PPL"
			Tags {
				"LightMode" = "Pixel"
				"LightTexCount" = "2"
			}
CGPROGRAM
#pragma vertex vert
#include "UnityCG.cginc"

uniform float4 _MainTex_ST;
uniform float4x4 _SpotlightProjectionMatrix0;
uniform float4x4 _SpotlightProjectionMatrixB0;

struct v2f {
	V2F_POS_FOG;
	float2 uv		: TEXCOORD0;
	float3 normal	: TEXCOORD1;
	float3 lightDir	: TEXCOORD2;
	float4 LightCoord0 : TEXCOORD3;
	float4 LightCoordB0 : TEXCOORD4;
};

v2f vert(appdata_tan v)
{
	v2f o;
	PositionFog( v.vertex, o.pos, o.fog );
	o.normal = v.normal;
	o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
	o.lightDir = ObjSpaceLightDir( v.vertex );
	
	o.LightCoord0 = mul(_SpotlightProjectionMatrix0, v.vertex);
	o.LightCoordB0 = mul(_SpotlightProjectionMatrixB0, v.vertex);
	
	return o; 
}
ENDCG
			Program "" {
				SubProgram {
					Local 0, [_ModelLightColor0]
					Local 1, [_Color]

"!!ATIfs1.0
StartConstants;
	CONSTANT c0 = program.local[0];
	CONSTANT c1 = program.local[1];
EndConstants;

StartOutputPass;
	SampleMap r0, t0.str;			# main texture
	SampleMap r1, t2.str;			# normalized light dir
	PassTexCoord r4, t1.str;		# normal
	SampleMap r2, t3.stq_dq;		# a = attenuation 1
	SampleMap r3, t4.stq_dq;		# a = attenuation 2
	
	DOT3 r5.sat, r4, r1.2x.bias;	# R5 = diffuse (N.L)
	
	MUL r0.rgb, r0, r5;
	MUL r0.rgb.2x, r0, c0;
	MUL r0.rgb, r0, r2.a;			# attenuate
	MUL r0.rgb, r0, r3.a;
	MUL r0.a, r0, c1;
EndPass; 
"
				}
			}
			SetTexture[_MainTex] {combine texture}
			SetTexture[_CubeNormalize] {combine texture}
			SetTexture[_LightTexture0] {combine texture}
			SetTexture[_LightTextureB0] {combine texture}
		}
	}
	
	// ------------------------------------------------------------------
	// Radeon 7000
	
	Category {
		Material {
			Diffuse [_Color]
			Emission [_PPLAmbient]
		}
		Lighting On
		Cull Off
		SubShader {
			Pass {
				Name "BASE"
				Tags {"LightMode" = "PixelOrNone"}
				Color [_PPLAmbient]
				Lighting Off
				SetTexture [_MainTex] {Combine texture * primary DOUBLE}
				SetTexture [_MainTex] {Combine texture * primary DOUBLE}
				SetTexture [_MainTex] {Combine texture * primary DOUBLE, primary * texture}
			}
			Pass { 	
				Name "BASE"
				Tags {"LightMode" = "Vertex"}
				SetTexture [_MainTex] {Combine texture * primary DOUBLE, primary * texture}
			}
			Pass {
				Name "PPL"
				Tags {
					"LightMode" = "Pixel"
					"LightTexCount" = "2"
				}
				SetTexture [_LightTexture0] 	{ combine previous * texture alpha, previous }
				SetTexture [_LightTextureB0]	{
					combine previous * texture alpha + constant, previous
					constantColor [_PPLAmbient]
				}
				SetTexture [_MainTex] 	{ combine previous * texture DOUBLE, primary * texture}
			}
			Pass {
				Name "PPL"
				Tags {
					"LightMode" = "Pixel"
					"LightTexCount"  = "1"
				}
				SetTexture [_LightTexture0] {
					combine previous * texture alpha + constant, previous
					constantColor [_PPLAmbient]
				}
				SetTexture [_MainTex] 	{ combine previous * texture DOUBLE, primary * texture}
			}
			Pass {
				Name "PPL"
				Tags {
					"LightMode" = "Pixel"
					"LightTexCount"  = "0"
				}
				SetTexture [_MainTex] 	{ combine previous * texture DOUBLE, primary * texture}
			}
		}
	}
}

Fallback "Transparent/Cutout/VertexLit", 2

}

Now it is time to make at least the tree barks receiving shadows…

Very impressive, when I was making Delta Maverick - I originally had some giant open world plan (which was cool but completely overwhelming) and I searched EVERYWHERE for light affected grass because In my opinion it ruins some of the immersion etc.

And now I’m working with a dusty alien planet so grass is out of the picture now :stuck_out_tongue:

!!!update!!!

Unfortunately the method described above doesn’t work on exporting. While in the editor everything runs pretty well, both – webplayer and standalone – fall back to the original shaders…

!!!update!!!

!!!update!!!

Finally I got it working: I had to assign the “Hidden/TerrainEngine/Details/Vertexlit” Shader to a game object in order to make it part of the built… (Create a new material and drag and drop the shader from the project folder to the inspector).

Now you can see it in action. Look for fern and the yellow weed and switch to mode 3 or 4 = realtime shadows.

http://wp1089796.vwp0260.webpack.hosteurope.de/unity/terrain_super_realtime05.html

Lars

Good for you. That makes sense, but I imagine it took a while to figure out. I don’t know if I would’ve thought of it.

well, i have found a hint in the shaderlab:
dreamora wrote:
As for the why: If you didn’t assign it anywhere, it won’t be part of the build so you can’t use it.
The content of the resource folder is always part of the build, independent if it is used anywhere through editor assignements or not

http://forum.unity3d.com/viewtopic.php?t=28177

thank you for sharing the shader and the procedure of getting it in (or refresh it as it is not widely known, forgot myself about it again :sweat_smile: ). Glad its working now

wow this trick is great !!! replacing terrain detail shader !!! This allowed me to insert
dirtpiles ( with some alpha in textures ) in my scene as details instead of gameobjects.
I believe this would be almost nothing for UT to allow alphacut in vertexlit detail shader
and it would be a VERY good enhancement. This would simply allow more than
‘massive’ objects.
Anyway congratz larsbertram for the work ! the result in your demo is great !

Ah and also: UT should allow user ( without beeing an hack ) to change the terrain
shader ( warning him he’ll loose future compatibility when doing this ) for his own shaders.
The terrain shaders are pretty good for most common uses, but is unconvenient for advanced
use. imho ofc :wink:

have a nice day !