changing the terrain shader

I want to use the terrain engine to draw tree canopy by planting foliage (instead of trees)
In order to do that I need the terrain to be totally transparent so I replaced the terrain Vertex lighting shader by something like this:

SubShader {
	Pass
    {
		Lighting Off
		ZWrite Off
		//Blend One Zero
		ColorMask A
    }

It works great in the editor : the terrain is invisible when set in lighting = vertex but in any player this seems overriden by the built in shader.
So there is a bug here (#??? still waiting) but until it gets fixed, how do I work around this ?

thank you

you have to assign the shader to a material then assign this material to any object e.g. a simple cube which is placed in your scene. otherwise the modified shader won’t get exported…

I’m trying to override the billboard shader, so I have placed the billboardshader from the buildin shaderlibary in my project, and added it to a cube. But it doesn’t replace the existing as I had hoped. I have tried manipulating all sorts of sensitive parts of this shader, and it has no effect. Is this not doable? I saw a post by Mr. J. Ante where he says you can override terrain shaders by adding a copy with the same name to the project. But?

Shader "hidden/TerrainEngine/BillboardTree" {
	Properties {
		_MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
		
	}
	
	SubShader {
		Tags { "Queue" = "Transparent" }
		
		Pass {

			CGPROGRAM
			#pragma vertex Vertex
			#include "UnityCG.cginc"		// Get standard Unity constants
			
			struct AppData {
				float4 vertex : POSITION; 		
				float4 color : COLOR;			// color.r, color.g, color.b
				float4 texcoord : TEXCOORD0;		// UV Coordinates 
				float2 texcoord1 : TEXCOORD1;		// xy offset
			};
			
			struct v2f {
				float4 pos : POSITION;
				float fog : FOGC;
				float4 color : COLOR0;
				float4 uv : TEXCOORD0;	// [tree uv]
			};
			
			uniform float3 _TreeBillboardCameraRight, _TreeBillboardCameraUp;
			uniform float4 _TreeBillboardCameraPos;
			uniform float4 _TreeBillboardDistances; // x = max distance ^ 2
			
			v2f Vertex (AppData v) {
				v2f o;
				
				float4 vertex = v.vertex;
				float2 xyOffset = v.texcoord1;
				
				float4 offset = vertex - _TreeBillboardCameraPos;
				float distanceSqr = dot(offset,offset);
				if (distanceSqr > _TreeBillboardDistances.x)
				{
					xyOffset.xy = 0.0F;
				}
				
				// Apply billboard extrusion
				vertex.xyz += xyOffset.x * _TreeBillboardCameraRight.xyz;
				vertex.xyz += xyOffset.y * _TreeBillboardCameraUp.xyz;
				
				float4 pos = mul (glstate.matrix.mvp, vertex);
				o.pos = pos;
				o.fog = o.pos.z;
				o.uv = v.texcoord;
				o.color = v.color;
				return o;
			}
			ENDCG			

			// Premultiplied alpha
			ColorMask rgb
			// Doesn't actually look so bad!
			Blend SrcAlpha OneMinusSrcAlpha

			ZWrite Off
			Cull Off
			AlphaTest Greater 0
			SetTexture [_MainTex] { combine texture * primary, texture }
		}
	}
	
	Fallback Off
}

Hi,

If I understood well, there is a way to override the existing terrain material/shader ??
Could someone confirm this please ? and how exactly it is done.

Thanks.

Terrain inherits from MonoBehaviour, and MonoBehaviour has a ‘renderer’ variable. Thus, it is in theory possible to assign a new material to terrain renderer:
terrain.renderer.material = mymaterial;

It is possible to replace the existing terrain shaders. They need to be named the same and reside in a folder named Resources. Restarting Unity might be required.

Yeap, I just found out more or less and was playing with the builtin shaders.
Thanks!

Does anyone know if there is a way to generate tangents for the terrain ? or a way to get bumpmap without tangent :slight_smile:

I was happy to see that we can still change the shader, but the vertex entry is quite limited.

Any hope to get a better terrain/material/shader handling in the next version ?

Thanks.