Search Unity

My custom shader is not batching

Discussion in 'Shaders' started by bumble864, Nov 14, 2011.

  1. bumble864

    bumble864

    Joined:
    Jan 27, 2011
    Posts:
    128
    Hiya,
    I'm new to the custom shader world and have been creating my own with Strumpy's Shader editor for about a week now. I can't seem to get my shader to batch though and I can't figure out why. Any help would be appreciated.

    What the shader does is
    1 main texture map
    1 "tint" texture map
    1 "spec,glow,alpha" map
    the shader reads the X,Y channels of the tint map and where those channels are black (or shades of) applies that tint color to the main texture allowing the player to customize the coloration themselves.
    The "spec, glow, alpha" map does the same as the tint map just for the specular, emission, and alpha attributes.

    In my test scene I have about 10 test plane objects that are 2tri's each. They all share this same texture but it's creating 10 unique draw calls. If I change the shader on the material to any other shader it batch's them up. If I make a shader out of strumpy's that simply one solid color and nothing else it still does not batch. I don't know anything about how coding for shaders works which is why I was really excited when I found this Strumpys because it is helping exponentially, I'm just hoping someone can help me figure this out.

    Code (csharp):
    1.  
    2. Shader "UltraShader"
    3. {
    4.     Properties
    5.     {
    6. _MainTexture("_MainTexture", 2D) = "black" {}
    7. _TintMap("_TintMap", 2D) = "black" {}
    8. _Spec_Glow("_Spec_Glow", 2D) = "black" {}
    9. _PrimaryColor("_PrimaryColor", Color) = (1,1,1,1)
    10. _SecondaryColor("_SecondaryColor", Color) = (1,1,1,1)
    11. _Alpha("_Alpha", Range(0,1) ) = 1
    12. _Emission("_Emission", Range(0,1) ) = 0
    13. _Specular("_Specular", Range(0,1) ) = 0
    14.  
    15.     }
    16.    
    17.     SubShader
    18.     {
    19.         Tags
    20.         {
    21. "Queue"="Transparent"
    22. "IgnoreProjector"="True"
    23. "RenderType"="Transparent"
    24.  
    25.         }
    26.  
    27.        
    28. Cull Off
    29. ZWrite On
    30. ZTest LEqual
    31. ColorMask RGBA
    32. Blend SrcAlpha OneMinusSrcAlpha
    33. Fog{
    34. }
    35.  
    36.  
    37.         CGPROGRAM
    38. #pragma surface surf BlinnPhongEditor  nolightmap noforwardadd approxview halfasview vertex:vert
    39. #pragma target 2.0
    40.  
    41.  
    42. sampler2D _MainTexture;
    43. sampler2D _TintMap;
    44. sampler2D _Spec_Glow;
    45. float4 _PrimaryColor;
    46. float4 _SecondaryColor;
    47. float _Alpha;
    48. float _Emission;
    49. float _Specular;
    50.  
    51.             struct EditorSurfaceOutput {
    52.                 half3 Albedo;
    53.                 half3 Normal;
    54.                 half3 Emission;
    55.                 half3 Gloss;
    56.                 half Specular;
    57.                 half Alpha;
    58.                 half4 Custom;
    59.             };
    60.            
    61.             inline half4 LightingBlinnPhongEditor_PrePass (EditorSurfaceOutput s, half4 light)
    62.             {
    63. half3 spec = light.a * s.Gloss;
    64. half4 c;
    65. c.rgb = (s.Albedo * light.rgb + light.rgb * spec);
    66. c.a = s.Alpha;
    67. return c;
    68.  
    69.             }
    70.  
    71.             inline half4 LightingBlinnPhongEditor (EditorSurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
    72.             {
    73.                 half3 h = normalize (lightDir + viewDir);
    74.                
    75.                 half diff = max (0, dot ( lightDir, s.Normal ));
    76.                
    77.                 float nh = max (0, dot (s.Normal, h));
    78.                 float spec = pow (nh, s.Specular*128.0);
    79.                
    80.                 half4 res;
    81.                 res.rgb = _LightColor0.rgb * diff;
    82.                 res.w = spec * Luminance (_LightColor0.rgb);
    83.                 res *= atten * 2.0;
    84.  
    85.                 return LightingBlinnPhongEditor_PrePass( s, res );
    86.             }
    87.            
    88.             struct Input {
    89.                 float2 uv_MainTexture;
    90. float2 uv_TintMap;
    91. float2 uv_Spec_Glow;
    92.  
    93.             };
    94.  
    95.             void vert (inout appdata_full v, out Input o) {
    96. float4 VertexOutputMaster0_0_NoInput = float4(0,0,0,0);
    97. float4 VertexOutputMaster0_1_NoInput = float4(0,0,0,0);
    98. float4 VertexOutputMaster0_2_NoInput = float4(0,0,0,0);
    99. float4 VertexOutputMaster0_3_NoInput = float4(0,0,0,0);
    100.  
    101.  
    102.             }
    103.            
    104.  
    105.             void surf (Input IN, inout EditorSurfaceOutput o) {
    106.                 o.Normal = float3(0.0,0.0,1.0);
    107.                 o.Alpha = 1.0;
    108.                 o.Albedo = 0.0;
    109.                 o.Emission = 0.0;
    110.                 o.Gloss = 0.0;
    111.                 o.Specular = 0.0;
    112.                 o.Custom = 0.0;
    113.                
    114. float4 Tex2D1=tex2D(_MainTexture,(IN.uv_MainTexture.xyxy).xy);
    115. float4 Invert4= float4(1.0, 1.0, 1.0, 1.0) - _PrimaryColor;
    116. float4 Tex2D0=tex2D(_TintMap,(IN.uv_TintMap.xyxy).xy);
    117. float4 Split0=Tex2D0;
    118. float4 Invert0= float4(1.0, 1.0, 1.0, 1.0) - float4( Split0.x, Split0.x, Split0.x, Split0.x);
    119. float4 Multiply0=Invert4 * Invert0;
    120. float4 SplatAlpha0=_PrimaryColor.w;
    121. float4 Multiply2=Multiply0 * SplatAlpha0;
    122. float4 Invert1= float4(1.0, 1.0, 1.0, 1.0) - float4( Split0.y, Split0.y, Split0.y, Split0.y);
    123. float4 Invert5= float4(1.0, 1.0, 1.0, 1.0) - _SecondaryColor;
    124. float4 Multiply1=Invert1 * Invert5;
    125. float4 SplatAlpha1=_SecondaryColor.w;
    126. float4 Multiply3=Multiply1 * SplatAlpha1;
    127. float4 Add1=Multiply2 + Multiply3;
    128. float4 Subtract2=Tex2D1 - Add1;
    129. float4 Tex2D2=tex2D(_Spec_Glow,(IN.uv_Spec_Glow.xyxy).xy);
    130. float4 Split1=Tex2D2;
    131. float4 Invert2= float4(1.0, 1.0, 1.0, 1.0) - float4( Split1.y, Split1.y, Split1.y, Split1.y);
    132. float4 Multiply6=_Emission.xxxx * Invert2;
    133. float4 Multiply4=_PrimaryColor * Multiply6;
    134. float4 Invert3= float4(1.0, 1.0, 1.0, 1.0) - float4( Split1.z, Split1.z, Split1.z, Split1.z);
    135. float4 Multiply5=Invert3 * _Specular.xxxx;
    136. float4 Subtract1=_Alpha.xxxx - float4( Split1.x, Split1.x, Split1.x, Split1.x);
    137. float4 Master0_1_NoInput = float4(0,0,1,1);
    138. float4 Master0_3_NoInput = float4(0,0,0,0);
    139. float4 Master0_7_NoInput = float4(0,0,0,0);
    140. float4 Master0_6_NoInput = float4(1,1,1,1);
    141. o.Albedo = Subtract2;
    142. o.Emission = Multiply4;
    143. o.Gloss = Multiply5;
    144. o.Alpha = Subtract1;
    145.  
    146.                 o.Normal = normalize(o.Normal);
    147.             }
    148.         ENDCG
    149.     }
    150.     Fallback "Diffuse"
    151. }
    152.  
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    transparent will not batch in general, as depth sorting happens basing on object 0,0,0 point, not pixels
     
  3. bumble864

    bumble864

    Joined:
    Jan 27, 2011
    Posts:
    128
    by "transparent" do you just mean the "Queue" and "Render Type"? because changing that to
    Code (csharp):
    1.  
    2. "Queue"="Geometry"
    3. "IgnoreProjector"="False"
    4. "RenderType"="Opaque"
    5.  
    still gives me separate draw calls.
     
  4. jackerlo

    jackerlo

    Joined:
    Sep 19, 2018
    Posts:
    4
    did you get around this problem?
     
  5. PocketJoyApps

    PocketJoyApps

    Joined:
    Jun 3, 2015
    Posts:
    5
    Try calling StaticBatchingUtility.Combine() on your root node. For me the problem was I was adding UV channels (from another editor script), so I just started calling StaticBatchingUtility.Combine(transform.root) on each modified object (note they all shared a root node).