Search Unity

Unity 3: Shader for translucent objects

Discussion in 'Shaders' started by larsbertram1, Sep 7, 2010.

  1. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,902
    while working on an enhanced vegetation shader for unity 3 supporting translucency like the new tree leaves shader i tested my first efforts also on other game objects.
    in the example below you can see the result: shading the translucent parts of the tents.
    it works pretty well so far but leads to some issues when turning on SSAO: everything looks alright on my MacBook Pro (Nvidia GeForce 9600M GT) but when testing it on an iMac (ATI Radeon HD 2600 Pro) SSAO is also rendered on the translucent parts… is it a bug in the shader or a bug in the ATI drivers?
    as far as i can see this wrong rendering of SSAO doesn’t appear on the tree leaves even on the iMac.

    lars

    Code (csharp):
    1.  
    2. Shader "My/MyTent" {
    3. Properties {
    4.     _Color ("TransColor", Color) = (1,1,1,1)
    5.     _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    6.     _BumpMap ("Normalmap (RGB)", 2D) = "bump" {}
    7. }
    8.  
    9. SubShader {
    10.     Tags {"IgnoreProjector"="True" "RenderType"="TreeLeaf"}
    11.     LOD 200
    12.    
    13. CGPROGRAM
    14. #pragma surface surf MyLambert addshadow
    15.  
    16. sampler2D _MainTex;
    17. sampler2D _BumpMap;
    18. float4 _Color;
    19.  
    20. #pragma lighting LightingMyLambert exclude_path:prepass
    21. inline half4 LightingMyLambert (SurfaceOutput s, half3 lightDir, half atten)
    22. {
    23.     half diff = dot(s.Normal, lightDir);
    24.    
    25.     half trans = max(0, -diff);
    26.     half3 translucencyColor = _Color * trans * 2.0;
    27.    
    28.     // wrap around diffuse
    29.     diff = max(0, diff * 0.5 + 0.5);
    30.    
    31.     half4 c;
    32.    
    33.     c.rgb = s.Albedo * (diff + translucencyColor);
    34.     c.rgb *= _LightColor0.rgb;
    35.     c.rgb *= (atten * 2);
    36.     //c.a = s.Alpha;
    37.     return c;
    38. }
    39.  
    40. struct Input {
    41.     float2 uv_MainTex;
    42.     float2 uv_BumpMap;
    43.     float4 color : COLOR;      
    44. };
    45.  
    46.  
    47. void surf (Input IN, inout SurfaceOutput o) {
    48.     half4 c = tex2D(_MainTex, IN.uv_MainTex);
    49.     o.Albedo = c.rgb;
    50.     o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    51.     //o.Alpha = c.a;
    52.    
    53. }
    54. ENDCG
    55. }
    56. }
    57.  
     

    Attached Files:

  2. wannabeartist

    wannabeartist

    Joined:
    Jun 20, 2009
    Posts:
    272
    Nice!

    I'm looking for something like this - have you developed this further?