Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

mobile 2-sided diffuse shader?

Discussion in 'Shaders' started by yuriythebest, May 16, 2012.

  1. yuriythebest

    yuriythebest

    Joined:
    Nov 21, 2009
    Posts:
    1,110
    does a mobile 2-sided diffuse shader exist anywhere?
     
  2. MADmarine

    MADmarine

    Joined:
    Aug 31, 2010
    Posts:
    627
    Code (csharp):
    1.  
    2. // Simplified Diffuse shader. Differences from regular Diffuse one:
    3. // - no Main Color
    4. // - fully supports only 1 directional light. Other lights can affect it, but it will be per-vertex/SH.
    5.  
    6. Shader "Mobile/Diffuse" {
    7. Properties {
    8.     _MainTex ("Base (RGB)", 2D) = "white" {}
    9. }
    10. SubShader {
    11.     Tags { "RenderType"="Opaque" }
    12.     LOD 150
    13.     Cull off
    14.  
    15. CGPROGRAM
    16. #pragma surface surf Lambert noforwardadd
    17.  
    18. sampler2D _MainTex;
    19.  
    20. struct Input {
    21.     float2 uv_MainTex;
    22. };
    23.  
    24. void surf (Input IN, inout SurfaceOutput o) {
    25.     fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
    26.     o.Albedo = c.rgb;
    27.     o.Alpha = c.a;
    28. }
    29. ENDCG
    30. }
    31.  
    32. Fallback "Mobile/VertexLit"
    33. }
    34.  
    That should be all you need. The only thing I have added myself is "Cull off", look this up in the shader documentation to see why it works.
     
    matiasges and romi-fauzi like this.
  3. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Here's why turning off culling doesn't work as well as you might like, and what else you can do about it.
     
  4. MADmarine

    MADmarine

    Joined:
    Aug 31, 2010
    Posts:
    627
    Daniel does point out a lot of good points in that blog. For simple problems (or certain unique cases), the Cull off solution is usually fine, but as it says in the blog, lighting is an issue (as Cull off will just use the lighting from the opposite side, which is probably going to look wrong in a lot of cases).

    If you can add the other side of the geometry yourself in a 3D modelling package, that's probably the best solution most of the time.
     
    Last edited: May 16, 2012