Search Unity

Vegitation Shader

Discussion in 'Shaders' started by forestjohnson, Oct 19, 2005.

  1. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    Ok, I'm making a tree!

    I have several flat planes with alpha chaneled textures.

    I am using this shader to render both side of the planes.

    Shader "Vegetation" {
    Properties {
    _Color ("Main Color", Color) = (.5, .5, .5, .5)
    _MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
    _Cutoff ("Base Alpha cutoff", Range (0,.9)) = .5
    }
    SubShader {
    // Set up basic lighting
    Material {
    Diffuse [_Color]
    Ambient [_Color]
    }
    Lighting On

    // Render both front and back facing polygons.
    Cull Off

    // first pass:
    // render any pixels that are more than [_Cutoff] opaque
    Pass {
    AlphaTest Greater [_Cutoff]
    SetTexture [_MainTex] {
    combine texture * primary, texture
    }
    }

    // Second pass:
    // render in the semitransparent details.
    Pass {
    // Dont write to the depth buffer
    ZWrite off

    // Only render pixels less or equal to the value
    AlphaTest LEqual [_Cutoff]

    // Set up alpha blending
    Blend SrcAlpha OneMinusSrcAlpha

    SetTexture [_MainTex] {
    combine texture * primary, texture
    }
    }
    }
    }

    However, when I rotate a tree branch so that one of the far faces of a plane is facing at the lightsource, that whole plane is black, no matter what angle you look at it from. This can be fixed by having lightsources on both sides, but I would rather not do this.

    Here is a image showing the branch looking normal, and what it looks like after it is rotated in a certain way.
     

    Attached Files:

  2. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    First off, please put your code inside code tags, so we don't lose formatting ;-)

    Apart from that, two-sided lighting is pretty expensive. The recommended approach is to either use a custom vertex program, add an emissive color so it doesn't become completely dark, or turn on culling, double the duplicate the planes in you 3D app flip the normals...

    To add the emission color, try this:
    Code (csharp):
    1.  
    2. Shader "Vegetation" {
    3.         Properties {
    4.                 _Color ("Main Color", Color) = (.5, .5, .5, .5)
    5.                 _MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
    6.                 _Cutoff ("Base Alpha cutoff", Range (0,.9)) = .5
    7.         }
    8.         SubShader {
    9.                 // Set up basic lighting
    10.                 Material {
    11.                         Diffuse [_Color]
    12.                         Ambient [_Color]
    13.                 }      
    14.                 Lighting On
    15.  
    16.                 // Render both front and back facing polygons.
    17.                 Cull Off
    18.  
    19.                 // first pass:
    20.                 //   render any pixels that are more than [_Cutoff] opaque
    21.                 Pass {  
    22.                         AlphaTest Greater [_Cutoff]
    23.                         SetTexture [_MainTex] {
    24.                                 combine texture * primary, texture
    25.                         }
    26.                 }
    27.  
    28.                 // Second pass:
    29.                 //   render in the semitransparent details.
    30.                 Pass {
    31.                         // Dont write to the depth buffer
    32.                         ZWrite off
    33.                         // Don't write pixels we have already written.
    34.                         ZTest Less
    35.                         // Only render pixels less or equal to the value
    36.                         AlphaTest LEqual [_Cutoff]
    37.                        
    38.                         // Set up alpha blending
    39.                         Blend SrcAlpha OneMinusSrcAlpha
    40.  
    41.                         SetTexture [_MainTex] {
    42.                                 combine texture * primary, texture
    43.                         }
    44.                 }
    45.         }
    46. }
    47.  
    Basically, you add a color property, which you then use with the emission keyword in your lighting setup.

    (I've also changed the Depth testing of the 2nd pass to optimize fillrate a bit)
     
  3. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    I think most of the time, people don't actually USE lighting for those quad-based thingies...