Search Unity

Multiple target platform lightmap shader

Discussion in 'Shaders' started by sberlemont, Jan 26, 2011.

  1. sberlemont

    sberlemont

    Joined:
    May 16, 2010
    Posts:
    37
    I have a shader with subShaders when lightmap is on and off:

    Code (csharp):
    1.  
    2. Shader "Alpha Blending Light=Off, Cull=Off, ZWrite=Off" {
    3.     Properties {
    4.         _MainTex ("Base (RGB)", 2D) = "white" {}
    5.     }
    6.     SubShader {
    7.         Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
    8.  
    9.         Pass {
    10.             Tags { "LightMode"="Vertex"}       
    11.             Lighting Off
    12.             Cull Off
    13.             ZWrite Off
    14.             Blend SrcAlpha OneMinusSrcAlpha
    15.             SetTexture [_MainTex] {}
    16.         }
    17.     }
    18.     SubShader {
    19.         Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
    20.  
    21.         Pass {
    22.             Tags { "LightMode"="VertexLMRGBM"}     
    23.             Lighting Off
    24.             Cull Off
    25.             ZWrite Off
    26.             Blend SrcAlpha OneMinusSrcAlpha
    27.             SetTexture [_MainTex] {}
    28.         }
    29.     }  
    30.     SubShader {
    31.         Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
    32.  
    33.         Pass {
    34.             Tags { "LightMode"="VertexLM"}     
    35.             Lighting Off
    36.             Cull Off
    37.             ZWrite Off
    38.             Blend SrcAlpha OneMinusSrcAlpha
    39.             SetTexture [_MainTex] {}
    40.         }
    41.     }  
    42. }
    43.  
    When lightmap is on, it appears Unity picks up the first lightmap subshader, whether it is compatible or not with the current target platform. Here is what I have:

    - When the subshader with the pass tagged as "LightMode"="VertexLMRGBM" is first in the shader, it works on Web player but not on iOS (objects on which the shader is applied to are disappearing both from the Editor and the build).

    - When the subshader with the pass tagged as LightMode"="VertexLM is first in the shader, it works on iOS but not on the Web player.

    How to write properly the shader so that it is compatible both for Web player and iOS?
     
  2. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Why are you putting all these passes into separate subshaders? Put them into one subshader. Just like built-in VertexLit shaders do.
     
  3. sberlemont

    sberlemont

    Joined:
    May 16, 2010
    Posts:
    37
    Of course! Thanks.
     
  4. VivienS

    VivienS

    Joined:
    Mar 25, 2010
    Posts:
    24
    Hi there, I have the same problem: Unity picks the first subshader using lightmaps, no matter if it doesn't work on the current platform.

    But I'm using surface shaders, since my shader does some additional operations that are not possible with ShaderLab code. I read somewhere else that it's not possible to make a multipass surface shader. So how do I solve this problem? I need the code to work both on PC and mobile.. ATM it either does one or the other (depending on which subshader I put first: VerexLMRGB or VertexLM.. ).

    Here's my code (The actual functionality of the shader is not so important, only the three subshaders and their LightMode):

    Code (csharp):
    1. Shader "CURE/Level Geometry Transp" {
    2. Properties {
    3.     _BlackNear ("_BlackNear", Float) = 0
    4.     _BlackDepth ("_BlackDepth", Float) = 10
    5.     _MainTex ("_MainTex", 2D) = "white" {}
    6.     _BlackTex ("_BlackTex", 2D) = "white" {}
    7. }
    8.  
    9. SubShader {
    10.     Tags { "RenderType"="Transparent" "Queue"="Transparent" "LightMode"="Vertex" }
    11.     Blend SrcAlpha OneMinusSrcAlpha
    12.     ZWrite Off
    13.     Offset -1, -1
    14.    
    15.     CGPROGRAM
    16.     #pragma surface surf Unlit
    17.    
    18.     struct Input {
    19.       fixed2 uv_MainTex;
    20.       fixed3 worldPos;
    21.     };
    22.    
    23.     sampler2D _MainTex;
    24.     sampler2D _BlackTex;
    25.     fixed _BlackNear;
    26.     fixed _BlackDepth;
    27.    
    28.     void surf (Input IN, inout SurfaceOutput o) {
    29.         fixed4 blackTex = tex2D (_BlackTex, half2(0, (IN.worldPos.z - _BlackNear) / _BlackDepth));
    30.         fixed4 mainTex = tex2D (_MainTex, IN.uv_MainTex);
    31.         o.Albedo = mainTex.rgb * blackTex.rgb;
    32.         o.Alpha = mainTex.a;
    33.     }
    34.      
    35.     fixed4 LightingUnlit_PrePass (SurfaceOutput s, half atten)
    36.     {  
    37.         return fixed4(s.Albedo,s.Alpha);
    38.     }
    39.    
    40.     ENDCG
    41. }
    42.  
    43. SubShader {
    44.     Tags { "RenderType"="Opaque" "LightMode"="VertexLM" }
    45.     Blend SrcAlpha OneMinusSrcAlpha
    46.     ZWrite Off
    47.     Offset -1, -1
    48.    
    49.     CGPROGRAM
    50.     #pragma surface surf Unlit
    51.    
    52.     struct Input {
    53.       fixed2 uv_MainTex;
    54.       fixed3 worldPos;
    55.     };
    56.    
    57.     sampler2D _MainTex;
    58.     sampler2D _BlackTex;
    59.     fixed _BlackNear;
    60.     fixed _BlackDepth;
    61.    
    62.     void surf (Input IN, inout SurfaceOutput o) {
    63.         fixed4 blackTex = tex2D (_BlackTex, half2(0, (IN.worldPos.z - _BlackNear) / _BlackDepth));
    64.         fixed4 mainTex = tex2D (_MainTex, IN.uv_MainTex);
    65.         o.Albedo = mainTex.rgb * blackTex.rgb;
    66.         o.Alpha = mainTex.a;
    67.     }
    68.    
    69.     fixed4 LightingUnlit (SurfaceOutput s, half3 lightDir, half atten)
    70.     {  
    71.         return fixed4(0,0,0,s.Alpha);
    72.     }
    73.    
    74.     fixed4 LightingUnlit_SingleLightmap (SurfaceOutput s, fixed4 color)
    75.     {  
    76.         fixed3 lm = DecodeLightmap(color);
    77.         return fixed4(lm,s.Alpha);
    78.     }
    79.    
    80.     ENDCG
    81. }
    82.  
    83. SubShader {
    84.     Tags { "RenderType"="Opaque" "LightMode"="VertexLMRGBM" }
    85.     Blend SrcAlpha OneMinusSrcAlpha
    86.     ZWrite Off
    87.     Offset -1, -1
    88.    
    89.     CGPROGRAM
    90.     #pragma surface surf Unlit alpha
    91.    
    92.     struct Input {
    93.       fixed2 uv_MainTex;
    94.       fixed3 worldPos;
    95.     };
    96.    
    97.     sampler2D _MainTex;
    98.     sampler2D _BlackTex;
    99.     fixed _BlackNear;
    100.     fixed _BlackDepth;
    101.    
    102.     void surf (Input IN, inout SurfaceOutput o) {
    103.         fixed4 blackTex = tex2D (_BlackTex, half2(0, (IN.worldPos.z - _BlackNear) / _BlackDepth));
    104.         fixed4 mainTex = tex2D (_MainTex, IN.uv_MainTex);
    105.         o.Albedo = mainTex.rgb * blackTex.rgb;
    106.         o.Alpha = mainTex.a;
    107.     }
    108.    
    109.     fixed4 LightingUnlit (SurfaceOutput s, half3 lightDir, half atten)
    110.     {  
    111.         return fixed4(0,0,0,s.Alpha);
    112.     }
    113.    
    114.     fixed4 LightingUnlit_SingleLightmap (SurfaceOutput s, fixed4 color)
    115.     {  
    116.         fixed3 lm = DecodeLightmap(color);
    117.         return fixed4(lm,s.Alpha);
    118.     }
    119.    
    120.     ENDCG
    121. }
    122. }