Search Unity

Backface culling in Unity

Discussion in 'Editor & General Support' started by Marowe, May 13, 2010.

  1. Marowe

    Marowe

    Joined:
    May 13, 2010
    Posts:
    5
    I've been trying to figure out how to get backface culling to turn off in Unity, since I don't want to waste the polys by making everything have two sides.

    I've read in a few places that I need to type something like CullOff in the Pass of the shader...I'm not sure where to type this in to turn it off, and I'm not much of a coder so I don't know where to look.

    Any help would be greatly appreciated! Thanks!
     
    jabo12 likes this.
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,138
    By default, backface culling is on, which means everything has one side (the back face is culled). You don't want to use Cull Off, except for those cases where you do want to render both sides.

    --Eric
     
  3. Marowe

    Marowe

    Joined:
    May 13, 2010
    Posts:
    5
    Yeah, that's exactly what I want. :(
     
    chunyiugary likes this.
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,138
    Oh, I misunderstood your question. You can look in the shader source for the particle shaders, for example, because they all have backface culling turned off. Basically it's just putting "Cull Off" in the "SubShader" section.

    --Eric
     
    Demanuele likes this.
  5. Marowe

    Marowe

    Joined:
    May 13, 2010
    Posts:
    5
    Thanks for the help, Eric. Unfortunately I'm still not sure exactly what to do though, mostly because I don't know how to access the "SubShader" section, and I only started using Unity yesterday so I'm not sure where to look. I opened the material up in a script editor and didn't see any culling flags (though I know almost nothing about scripting so I may have overlooked something) and I'm not sure if this is a glitch, but if I select "Edit Shader..." in Unity, nothing pops up :(

    I spent a while looking, because I don't want to be like one of those lazy noobs that expects answers to fall in their lap, but I'm really at a loss of what to do...I understand what you suggested, I'm just not sure where to find any of that stuff. Sorry >__<
     
  6. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    If you want your objects to be properly lit, I highly recommend avoiding the Cull Off approach. Basically, Cull Off will get you two-sided geometry with one side lit incorrectly. The work-around is to make complex modifications to existing shaders and take a performance hit due to the extra draw calls.

    Unless you are seriously limited by mesh data size, the easiest and best-performing way to render backfaces is to double your mesh and flip the normals on the duplicate.
     
    artician likes this.
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,138
    You can only edit your own custom shaders. However, you can download the source for all the built-in shaders (look at the sticky in the Shaderlab forum). I agree that, generally speaking, duplicating geometry is the best way. It's still good to know how to do the cull off thing for special cases though.

    --Eric
     
  8. Marowe

    Marowe

    Joined:
    May 13, 2010
    Posts:
    5
    Ah, I didn't realize there was such a drawback to turning off the culling. I guess I'll try it and see how it looks, but it probably won't be pretty by the sound of it. Thank you for the help guys, I REALLY appreciate it!
     
  9. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Reading my post, I don't think I made it clear that turning culling off is appropriate and faster when you don't want to do any lighting. As such, it can be useful for special effects or non-photorealistic rendering.
     
  10. megmaltese

    megmaltese

    Joined:
    Mar 19, 2009
    Posts:
    2,153
    I totally agree with this post: if you need to have a biface mesh the best way is to duplicate it and flip normals.
    This is the right way for so many reasons...

    Otherwise you have to code your shader but will not perform so fast.

    The only drawback, as they told you, is that you will use twice the memory for the mesh... but usually this is not a problem.
     
  11. Marowe

    Marowe

    Joined:
    May 13, 2010
    Posts:
    5
    Thanks for all the tips guys. It's definitely a lot more apparent that Cull Off won't solve the problem.

    I really appreciate all the help! Thank you :D
     
  12. Legion555

    Legion555

    Joined:
    Mar 22, 2014
    Posts:
    2
    4 years later,
    At this moment with unity 4.X back facing culling off works very good for clothes and hair shaders. just depends on your needs, but if you create a shader and then set the culling off in the subshader section everything works very fine, (at least for me)
     
  13. Noah492

    Noah492

    Joined:
    Jul 2, 2014
    Posts:
    44
    Hi Daniel, how exactly do I do the alternative. Ctrl+D to duplicate right? And then how do I flip the normals on the duplicate?
     
    nadeeramy likes this.
  14. otomo

    otomo

    Joined:
    Feb 11, 2015
    Posts:
    4
    You would have to do it in your 3D Editor of choice. As far as I know you cannot edit Meshes in Unity, at least not through the editor.
     
  15. nadeeramy

    nadeeramy

    Joined:
    Aug 13, 2015
    Posts:
    2
    so how exactly?
     
  16. nadeeramy

    nadeeramy

    Joined:
    Aug 13, 2015
    Posts:
    2
    hi all I just installed n tried using Unity yesterday...
     
  17. guywald

    guywald

    Joined:
    Aug 22, 2013
    Posts:
    11
    Thanks! You saved me a lot of time. I had the same problem of a "side not appearing".
    Flipping the normals solved this.

    A method showing how to do this can be found at:
    http://wiki.unity3d.com/index.php/ReverseNormals

    Thanks.
     
  18. Anki13

    Anki13

    Joined:
    Oct 26, 2017
    Posts:
    1
    I read all the comments and answer for this particular question and i agree to most of it. What if someone is doing vertex paint and want its plane to be double sided then how can we do that?? How can we make a plane double side to do vertex painting in Unity???!!!
     
  19. GameDevCouple_I

    GameDevCouple_I

    Joined:
    Oct 5, 2013
    Posts:
    1,854
    I am so sick of saying this, but please DO NOT bring back to life threads that are 2 years old.
     
  20. Honorsoft

    Honorsoft

    Joined:
    Oct 31, 2016
    Posts:
    49
    I found this thread trying to find out how to turn off culling in a shader in Unity 2018.x. I'm getting 'shader compile errors' when I try the 'Unity 5' method. Anyways, someone recommended duplicating all the faces for a solution, so I'll just post an example of a shader that will work. It's the basic Standard Shader (Unity 5) with culling turned off. You can make the same change to any shader, just put the Cull Off in the sub-shader section. (Note: Standard Shaders might vary from version to version, this shader seems to work with Unity 5, but not with Unity 2018.)
    Code (Shader Code):
    1.  
    2. //Save as StandardCullOFF.shader
    3. Shader "StandardCullOFF"
    4. {
    5.     Properties
    6.     {
    7.         _Color("Color", Color) = (1,1,1,1)
    8.         _MainTex("Albedo", 2D) = "white" {}
    9.        
    10.         _Cutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
    11.  
    12.         _Glossiness("Smoothness", Range(0.0, 1.0)) = 0.5
    13.         _GlossMapScale("Smoothness Scale", Range(0.0, 1.0)) = 1.0
    14.         [Enum(Metallic Alpha,0,Albedo Alpha,1)] _SmoothnessTextureChannel ("Smoothness texture channel", Float) = 0
    15.  
    16.         [Gamma] _Metallic("Metallic", Range(0.0, 1.0)) = 0.0
    17.         _MetallicGlossMap("Metallic", 2D) = "white" {}
    18.  
    19.         [ToggleOff] _SpecularHighlights("Specular Highlights", Float) = 1.0
    20.         [ToggleOff] _GlossyReflections("Glossy Reflections", Float) = 1.0
    21.  
    22.         _BumpScale("Scale", Float) = 1.0
    23.         _BumpMap("Normal Map", 2D) = "bump" {}
    24.  
    25.         _Parallax ("Height Scale", Range (0.005, 0.08)) = 0.02
    26.         _ParallaxMap ("Height Map", 2D) = "black" {}
    27.  
    28.         _OcclusionStrength("Strength", Range(0.0, 1.0)) = 1.0
    29.         _OcclusionMap("Occlusion", 2D) = "white" {}
    30.  
    31.         _EmissionColor("Color", Color) = (0,0,0)
    32.         _EmissionMap("Emission", 2D) = "white" {}
    33.        
    34.         _DetailMask("Detail Mask", 2D) = "white" {}
    35.  
    36.         _DetailAlbedoMap("Detail Albedo x2", 2D) = "grey" {}
    37.         _DetailNormalMapScale("Scale", Float) = 1.0
    38.         _DetailNormalMap("Normal Map", 2D) = "bump" {}
    39.  
    40.         [Enum(UV0,0,UV1,1)] _UVSec ("UV Set for secondary textures", Float) = 0
    41.  
    42.  
    43.         // Blending state
    44.          _Mode ("__mode", Float) = 0.0
    45.          _SrcBlend ("__src", Float) = 1.0
    46.          _DstBlend ("__dst", Float) = 0.0
    47.          _ZWrite ("__zw", Float) = 1.0
    48.     }
    49.  
    50.     CGINCLUDE
    51.         #define UNITY_SETUP_BRDF_INPUT MetallicSetup
    52.     ENDCG
    53.  
    54.     SubShader
    55.     {
    56.         Tags { "RenderType"="Opaque" "PerformanceChecks"="False" }
    57.         LOD 300
    58.    
    59.  
    60.         // ------------------------------------------------------------------
    61.         //  Base forward pass (directional light, emission, lightmaps, ...)
    62.         Pass
    63.         {
    64.            
    65.             Name "FORWARD"
    66.             Tags { "LightMode" = "ForwardBase" }
    67.  
    68.             Blend [_SrcBlend] [_DstBlend]
    69.             ZWrite [_ZWrite]
    70.             Cull Off //  HERE IS WHERE YOU PUT CULL OFF
    71.  
    72.             CGPROGRAM
    73.             #pragma target 3.0
    74.  
    75.             // -------------------------------------
    76.  
    77.             #pragma shader_feature _NORMALMAP
    78.             #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    79.             #pragma shader_feature _EMISSION
    80.             #pragma shader_feature _METALLICGLOSSMAP
    81.             #pragma shader_feature ___ _DETAIL_MULX2
    82.             #pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    83.             #pragma shader_feature _ _SPECULARHIGHLIGHTS_OFF
    84.             #pragma shader_feature _ _GLOSSYREFLECTIONS_OFF
    85.             #pragma shader_feature _PARALLAXMAP
    86.  
    87.             #pragma multi_compile_fwdbase
    88.             #pragma multi_compile_fog
    89.  
    90.             #pragma vertex vertBase
    91.             #pragma fragment fragBase
    92.             #include "UnityStandardCoreForward.cginc"
    93.  
    94.             ENDCG
    95.         }
    96.         // ------------------------------------------------------------------
    97.         //  Additive forward pass (one light per pass)
    98.         Pass
    99.         {
    100.             Name "FORWARD_DELTA"
    101.             Tags { "LightMode" = "ForwardAdd" }
    102.             Blend [_SrcBlend] One
    103.             Fog { Color (0,0,0,0) } // in additive pass fog should be black
    104.             ZWrite Off
    105.             ZTest LEqual
    106.  
    107.             CGPROGRAM
    108.             #pragma target 3.0
    109.  
    110.             // -------------------------------------
    111.  
    112.  
    113.             #pragma shader_feature _NORMALMAP
    114.             #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    115.             #pragma shader_feature _METALLICGLOSSMAP
    116.             #pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    117.             #pragma shader_feature _ _SPECULARHIGHLIGHTS_OFF
    118.             #pragma shader_feature ___ _DETAIL_MULX2
    119.             #pragma shader_feature _PARALLAXMAP
    120.  
    121.             #pragma multi_compile_fwdadd_fullshadows
    122.             #pragma multi_compile_fog
    123.  
    124.  
    125.             #pragma vertex vertAdd
    126.             #pragma fragment fragAdd
    127.             #include "UnityStandardCoreForward.cginc"
    128.  
    129.             ENDCG
    130.         }
    131.         // ------------------------------------------------------------------
    132.         //  Shadow rendering pass
    133.         Pass {
    134.             Name "ShadowCaster"
    135.             Tags { "LightMode" = "ShadowCaster" }
    136.  
    137.             ZWrite On ZTest LEqual
    138.  
    139.             CGPROGRAM
    140.             #pragma target 3.0
    141.  
    142.             // -------------------------------------
    143.  
    144.  
    145.             #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    146.             #pragma shader_feature _METALLICGLOSSMAP
    147.             #pragma shader_feature _PARALLAXMAP
    148.             #pragma multi_compile_shadowcaster
    149.  
    150.             #pragma vertex vertShadowCaster
    151.             #pragma fragment fragShadowCaster
    152.  
    153.             #include "UnityStandardShadow.cginc"
    154.  
    155.             ENDCG
    156.         }
    157.         // ------------------------------------------------------------------
    158.         //  Deferred pass
    159.         Pass
    160.         {
    161.             Name "DEFERRED"
    162.             Tags { "LightMode" = "Deferred" }
    163.  
    164.             CGPROGRAM
    165.             #pragma target 3.0
    166.             #pragma exclude_renderers nomrt
    167.  
    168.  
    169.             // -------------------------------------
    170.  
    171.             #pragma shader_feature _NORMALMAP
    172.             #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    173.             #pragma shader_feature _EMISSION
    174.             #pragma shader_feature _METALLICGLOSSMAP
    175.             #pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    176.             #pragma shader_feature _ _SPECULARHIGHLIGHTS_OFF
    177.             #pragma shader_feature ___ _DETAIL_MULX2
    178.             #pragma shader_feature _PARALLAXMAP
    179.  
    180.             #pragma multi_compile ___ UNITY_HDR_ON
    181.             #pragma multi_compile ___ LIGHTMAP_ON
    182.             #pragma multi_compile ___ DIRLIGHTMAP_COMBINED DIRLIGHTMAP_SEPARATE
    183.             #pragma multi_compile ___ DYNAMICLIGHTMAP_ON
    184.  
    185.             #pragma vertex vertDeferred
    186.             #pragma fragment fragDeferred
    187.  
    188.             #include "UnityStandardCore.cginc"
    189.  
    190.             ENDCG
    191.         }
    192.  
    193.         // ------------------------------------------------------------------
    194.         // Extracts information for lightmapping, GI (emission, albedo, ...)
    195.         // This pass it not used during regular rendering.
    196.         Pass
    197.         {
    198.             Name "META"
    199.             Tags { "LightMode"="Meta" }
    200.  
    201.             Cull Off
    202.  
    203.             CGPROGRAM
    204.             #pragma vertex vert_meta
    205.             #pragma fragment frag_meta
    206.  
    207.             #pragma shader_feature _EMISSION
    208.             #pragma shader_feature _METALLICGLOSSMAP
    209.             #pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    210.             #pragma shader_feature ___ _DETAIL_MULX2
    211.  
    212.             #include "UnityStandardMeta.cginc"
    213.             ENDCG
    214.         }
    215.     }
    216.  
    217.     SubShader
    218.     {
    219.         Tags { "RenderType"="Opaque" "PerformanceChecks"="False" }
    220.         LOD 150
    221.  
    222.         // ------------------------------------------------------------------
    223.         //  Base forward pass (directional light, emission, lightmaps, ...)
    224.         Pass
    225.         {
    226.             Name "FORWARD"
    227.             Tags { "LightMode" = "ForwardBase" }
    228.  
    229.             Blend [_SrcBlend] [_DstBlend]
    230.             ZWrite [_ZWrite]
    231.  
    232.             CGPROGRAM
    233.             #pragma target 2.0
    234.            
    235.             #pragma shader_feature _NORMALMAP
    236.             #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    237.             #pragma shader_feature _EMISSION
    238.             #pragma shader_feature _METALLICGLOSSMAP
    239.             #pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    240.             #pragma shader_feature _ _SPECULARHIGHLIGHTS_OFF
    241.             #pragma shader_feature _ _GLOSSYREFLECTIONS_OFF
    242.             // SM2.0: NOT SUPPORTED shader_feature ___ _DETAIL_MULX2
    243.             // SM2.0: NOT SUPPORTED shader_feature _PARALLAXMAP
    244.  
    245.             #pragma skip_variants SHADOWS_SOFT DIRLIGHTMAP_COMBINED DIRLIGHTMAP_SEPARATE
    246.  
    247.             #pragma multi_compile_fwdbase
    248.             #pragma multi_compile_fog
    249.  
    250.             #pragma vertex vertBase
    251.             #pragma fragment fragBase
    252.             #include "UnityStandardCoreForward.cginc"
    253.  
    254.             ENDCG
    255.         }
    256.         // ------------------------------------------------------------------
    257.         //  Additive forward pass (one light per pass)
    258.         Pass
    259.         {
    260.             Name "FORWARD_DELTA"
    261.             Tags { "LightMode" = "ForwardAdd" }
    262.             Blend [_SrcBlend] One
    263.             Fog { Color (0,0,0,0) } // in additive pass fog should be black
    264.             ZWrite Off
    265.             ZTest LEqual
    266.            
    267.             CGPROGRAM
    268.             #pragma target 2.0
    269.  
    270.             #pragma shader_feature _NORMALMAP
    271.             #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    272.             #pragma shader_feature _METALLICGLOSSMAP
    273.             #pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    274.             #pragma shader_feature _ _SPECULARHIGHLIGHTS_OFF
    275.             #pragma shader_feature ___ _DETAIL_MULX2
    276.             // SM2.0: NOT SUPPORTED shader_feature _PARALLAXMAP
    277.             #pragma skip_variants SHADOWS_SOFT
    278.            
    279.             #pragma multi_compile_fwdadd_fullshadows
    280.             #pragma multi_compile_fog
    281.            
    282.             #pragma vertex vertAdd
    283.             #pragma fragment fragAdd
    284.             #include "UnityStandardCoreForward.cginc"
    285.  
    286.             ENDCG
    287.         }
    288.         // ------------------------------------------------------------------
    289.         //  Shadow rendering pass
    290.         Pass {
    291.             Name "ShadowCaster"
    292.             Tags { "LightMode" = "ShadowCaster" }
    293.            
    294.             ZWrite On ZTest LEqual
    295.  
    296.             CGPROGRAM
    297.             #pragma target 2.0
    298.  
    299.             #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    300.             #pragma shader_feature _METALLICGLOSSMAP
    301.             #pragma skip_variants SHADOWS_SOFT
    302.             #pragma multi_compile_shadowcaster
    303.  
    304.             #pragma vertex vertShadowCaster
    305.             #pragma fragment fragShadowCaster
    306.  
    307.             #include "UnityStandardShadow.cginc"
    308.  
    309.             ENDCG
    310.         }
    311.  
    312.         // ------------------------------------------------------------------
    313.         // Extracts information for lightmapping, GI (emission, albedo, ...)
    314.         // This pass it not used during regular rendering.
    315.         Pass
    316.         {
    317.             Name "META"
    318.             Tags { "LightMode"="Meta" }
    319.  
    320.             Cull Off
    321.  
    322.             CGPROGRAM
    323.             #pragma vertex vert_meta
    324.             #pragma fragment frag_meta
    325.  
    326.             #pragma shader_feature _EMISSION
    327.             #pragma shader_feature _METALLICGLOSSMAP
    328.             #pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    329.             #pragma shader_feature ___ _DETAIL_MULX2
    330.  
    331.             #include "UnityStandardMeta.cginc"
    332.             ENDCG
    333.         }
    334.     }
    335.  
    336.  
    337.     FallBack "VertexLit"
    338.     CustomEditor "StandardShaderGUI"
    339. }
    340.  
     
  21. dbchest

    dbchest

    Joined:
    Sep 26, 2016
    Posts:
    18
    Thanks for this, Eric! I found this post via search tonight and it was exactly what I needed,
    Derek
     
  22. mranger

    mranger

    Joined:
    Jan 26, 2019
    Posts:
    7
    I read through this twice and still don't know how to access the sub shader section.
     
    fwehrmaker likes this.
  23. wenyu-zhang

    wenyu-zhang

    Joined:
    Aug 9, 2019
    Posts:
    1
    You have to create a new 'Standard Surface Shader' in Unity, then double click on it to open it in Visual Studio.