Search Unity

changing the terrain shader

Discussion in 'Shaders' started by llavigne, Aug 24, 2009.

  1. llavigne

    llavigne

    Joined:
    Dec 27, 2007
    Posts:
    977
    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:

    Code (csharp):
    1.  
    2. SubShader {
    3.     Pass
    4.     {
    5.         Lighting Off
    6.         ZWrite Off
    7.         //Blend One Zero
    8.         ColorMask A
    9.     }
    10.  
    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
     
  2. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,902
    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…
     
  3. Kragh

    Kragh

    Joined:
    Jan 22, 2008
    Posts:
    657
    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?

    Code (csharp):
    1. Shader "hidden/TerrainEngine/BillboardTree" {
    2.     Properties {
    3.         _MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
    4.        
    5.     }
    6.    
    7.     SubShader {
    8.         Tags { "Queue" = "Transparent" }
    9.        
    10.         Pass {
    11.  
    12.             CGPROGRAM
    13.             #pragma vertex Vertex
    14.             #include "UnityCG.cginc"        // Get standard Unity constants
    15.            
    16.             struct AppData {
    17.                 float4 vertex : POSITION;      
    18.                 float4 color : COLOR;           // color.r, color.g, color.b
    19.                 float4 texcoord : TEXCOORD0;        // UV Coordinates
    20.                 float2 texcoord1 : TEXCOORD1;       // xy offset
    21.             };
    22.            
    23.             struct v2f {
    24.                 float4 pos : POSITION;
    25.                 float fog : FOGC;
    26.                 float4 color : COLOR0;
    27.                 float4 uv : TEXCOORD0;  // [tree uv]
    28.             };
    29.            
    30.             uniform float3 _TreeBillboardCameraRight, _TreeBillboardCameraUp;
    31.             uniform float4 _TreeBillboardCameraPos;
    32.             uniform float4 _TreeBillboardDistances; // x = max distance ^ 2
    33.            
    34.             v2f Vertex (AppData v) {
    35.                 v2f o;
    36.                
    37.                 float4 vertex = v.vertex;
    38.                 float2 xyOffset = v.texcoord1;
    39.                
    40.                 float4 offset = vertex - _TreeBillboardCameraPos;
    41.                 float distanceSqr = dot(offset,offset);
    42.                 if (distanceSqr > _TreeBillboardDistances.x)
    43.                 {
    44.                     xyOffset.xy = 0.0F;
    45.                 }
    46.                
    47.                 // Apply billboard extrusion
    48.                 vertex.xyz += xyOffset.x * _TreeBillboardCameraRight.xyz;
    49.                 vertex.xyz += xyOffset.y * _TreeBillboardCameraUp.xyz;
    50.                
    51.                 float4 pos = mul (glstate.matrix.mvp, vertex);
    52.                 o.pos = pos;
    53.                 o.fog = o.pos.z;
    54.                 o.uv = v.texcoord;
    55.                 o.color = v.color;
    56.                 return o;
    57.             }
    58.             ENDCG          
    59.  
    60.             // Premultiplied alpha
    61.             ColorMask rgb
    62.             // Doesn't actually look so bad!
    63.             Blend SrcAlpha OneMinusSrcAlpha
    64.  
    65.             ZWrite Off
    66.             Cull Off
    67.             AlphaTest Greater 0
    68.             SetTexture [_MainTex] { combine texture * primary, texture }
    69.         }
    70.     }
    71.    
    72.     Fallback Off
    73. }
     
  4. helloween

    helloween

    Joined:
    Nov 19, 2009
    Posts:
    8
    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.
     
  5. KaelisAsur

    KaelisAsur

    Joined:
    Apr 9, 2009
    Posts:
    361
    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;
     
  6. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
    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.
     
  7. helloween

    helloween

    Joined:
    Nov 19, 2009
    Posts:
    8
    Yeap, I just found out more or less and was playing with the builtin shaders.
    Thanks!
     
  8. helloween

    helloween

    Joined:
    Nov 19, 2009
    Posts:
    8
    Does anyone know if there is a way to generate tangents for the terrain ? or a way to get bumpmap without tangent :)

    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.