Search Unity

how to turn off back face culling

Discussion in 'Editor & General Support' started by GFFG, May 31, 2015.

  1. GFFG

    GFFG

    Joined:
    Nov 13, 2014
    Posts:
    224
    Hi, I've seen countless posts about this but none that describe exactly how to turn off back face culling.
    The issue is the classic one: I have a character with clothes that shouldn't have culling on.

    all threads just say use "Cull Off" and this link doesn't tell me how to turn cull off: http://docs.unity3d.com/Manual/SL-CullAndDepth.html

    I'm utterly confusedo_O

    Do I have to make a script and put it on the object that I want back face culling off on? what scripting language and how should I write the code?

    Please help :confused:
     
  2. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    "cull off" has to be in a shader. I wish with the new standard shader in Unity 5 they would have just included a checkbox for this which would alter the compiled shader for you without the hassle to recreate a shader with cull off declared manually.

    Sketchfab have a toggle for this in the settings and I must admit it is very handy ;)
     
  3. GFFG

    GFFG

    Joined:
    Nov 13, 2014
    Posts:
    224
    Oh okay. I agree, it would really make things easier and quicker with a checkbox in the shader. I'll check out sketchfab, Thanks :)
     
  4. Honorsoft

    Honorsoft

    Joined:
    Oct 31, 2016
    Posts:
    81
    Just a tip for anyone still trying to figure out the culling, you can just add Cull Off to any shader in the SubShader section. You can't edit the default ones, but you can edit copies of them. You have to download the default Unity shaders(assuming you can't find better ones to use) and then edit them(being sure to rename them properly), adding the Cull Off to the sub-shader section of the *.shader. It should look like this:
    Shader "NewShaderName"
    {
    Properties {
    _Color ("Main Color", Color) = (1,1,1,0)
    _MainTex ("Base (RGB)", 2D) = "white" {}
    }
    SubShader {
    Pass {
    Cull Off
    Material {
    Diffuse [_Color]
    Ambient [_Color]
    }
    Lighting On
    SetTexture [_MainTex] {
    Combine texture * primary DOUBLE, texture * primary
    }
    }
    }
    }
    That should be a working(really basic) shader, but shows the layout.

    It would be nice if you could easily switch the culling off, but there's posts spanning years of people still trying to figure out how to use materials and textures properly on Unity. I need culling to be off so often that I wish it was off by default. I had a hard time finding out how to turn culling off. A lot of people were posting online that the only way to fix that was to duplicate all the mesh and faces, and then flip them. That would add so much extra data.
    One other thing, I noticed that I my CULL OFF shader no longer worked in Unity 2018, and after editing the 2018 version of the Standard Shader, I was still not able to get it to work in Unity 2018.x. If someone knows how to turn culling off in Unity 2018, let us know.
     
    Last edited: Jul 1, 2018
    drewjosh likes this.
  5. Hybris_Team

    Hybris_Team

    Joined:
    Oct 17, 2015
    Posts:
    114
    Quick question, where is the PBR shader script file usually saved? I can't find it in my project to edit.
     
  6. Honorsoft

    Honorsoft

    Joined:
    Oct 31, 2016
    Posts:
    81
    I can't remember what PBR shader is, but if it's one of Unity's built-in shaders, you need to download them and use the copies. You can download the Default Shaders for each version of Unity, from the download page on the Unity site.
     
  7. Hybris_Team

    Hybris_Team

    Joined:
    Oct 17, 2015
    Posts:
    114
    Thank you very much! So when I edit the copies, does it automatically overwrite the current one, when I import the copy?
     
  8. Honorsoft

    Honorsoft

    Joined:
    Oct 31, 2016
    Posts:
    81
    I'm not sure if it overwrites the shader or just creates a new shader with (1) added to the name. I just copied the shader and gave it a new name, and I changed the name inside the shader to match.
    I have a lot of previous programming experience, but I had a hard time finding out how to switch culling off in Unity, and I finally found out about the shaders having culling on by default. I liked the standard shader, I just wanted it to render both sides of the model (it was a cape that was only visible from one side because of culling). Anyways, after figuring out how to edit the standard shader (you put CULL OFF in the sub-shader section), I was pleased to see Unity accepted the new shader and listed it with the other shaders in the Inspector automatically. So just remember two things:
    1. If the shader you wrote or edited has errors in it, it might not show up in the list.
    2. Give the shader you edit a different name. I didn't want to overwrite the Standard Shader in case I needed it, so I named my shader "StandardCullOFF.shader" and inside the shader I changed the name (at the start of the shader file):

     Shader "StandardCullOFF"
    { ...etc


    That gave me a duplicate of the Standard shader, with culling-off. If you want to make your shader appear in a submenu of the shader list in the Inspector, name it like this:

    Shader "ShaderFolder/YourShaderName"

    or
    Shader "myShaders/FxShaders/ElecShader"


    The shaders seem to show up in the Inspector list
    I'm not sure if it would overwrite the existing shader if you didn't change the name, but I doubt it, it seems the default Unity shaders are inaccessible (embedded into the Editor), which is why I think they've been offered for download to edit.

    Just in case, I forgot to mention in my first post to save the shader with the *.shader extension, so for that simple example shader I posted, copy and paste it in to a text editor and save it as "NewShaderName.shader"
    Also, the shaders are specific to each version (some will not work on other versions) and they can be found on the Unity download page for the specific version you are downloading. They might be downloadable from the 'Unity Download Assistant' too.
     
    Last edited: Jul 1, 2018
  9. Hybris_Team

    Hybris_Team

    Joined:
    Oct 17, 2015
    Posts:
    114
    Hey sorry about the late reply here. So I am still a bit confused, where is the standard shader file located, so that I can edit or replace with the new shader file? I can't find it within my unity project.
     
  10. bart_the_13th

    bart_the_13th

    Joined:
    Jan 16, 2012
    Posts:
    498
    Can you just right click on project window and select "Create Shader" then choose "Standard Surface shader" from the list?
     
  11. Honorsoft

    Honorsoft

    Joined:
    Oct 31, 2016
    Posts:
    81
    The Standard Shader and other shaders that are in your project (no matter what folder the *.shader file is in) are listed in the Material's shader drop-down menu:

    ShaderEdit.jpg

    It has a context-menu item for 'Edit Shader' but all it seems to do is generate code, which I think I read that the generated code can't be used as a shader, it's for de-bugging:


    ???
    ShaderEdit02.jpg

    The standard shader (and Unity's other 'default' shaders) are built-in to Unity. To edit them you have to download copies of them from Unity or online elsewhere and then use the edited copies. The shaders aren't on the regular Unity download page, which makes them hard to find, they're on the Unity Download Archive page.

    So, go to:

    https://unity3d.com/get-unity/download/archive


    ...and scroll down a bit and find the Unity version you are using:
    UnityDownloadArchive01.jpg

    ...then click on the drop down menu for Downloads(Win) or Downloads(Mac) and look for the Shader pack for that version:
    UnityDownloadArchive02.jpg



    ...now you can extract the shaders from the ZIP Unity gives you, edit them and add them to your project.
     

    Attached Files:

    Last edited: Jul 17, 2018
    pmain8 likes this.
  12. Honorsoft

    Honorsoft

    Joined:
    Oct 31, 2016
    Posts:
    81
    Yes, that makes a basic shader you can open with a text-editor to edit, but it is not the Standard Shader, it looks like a basic version of the shader maybe for diagnostics. For example, the shader that is created by right-clicking in the project window is missing the GlossMapScale. I am still learning shader scripting, I did shaders before but they were legacy and a different shader language than Unity uses, so I don't want to do much editing of a shader yet.
     
    Last edited: Apr 29, 2022
  13. Hybris_Team

    Hybris_Team

    Joined:
    Oct 17, 2015
    Posts:
    114
    Ohh ok now I get what you are saying! Thank you for the detailed instructions, I really appreciate it! I have never edited/coded a shader before and I really benefited from the images and baby steps lol. Thank you very much!!! @Honorsoft @bart_the_13th
     
    Honorsoft likes this.
  14. Honorsoft

    Honorsoft

    Joined:
    Oct 31, 2016
    Posts:
    81
    Yeah, me too, I am still learning the modern shaders used by Unity. I'm not sure what the 'Edit and show compiled code' thing is for in the unity Editor, but making a copy of a shader to edit seems to work great and leaves the original shader intact. One thing I noted, shaders might be different in Unity 2017/2018, because I edited a 2018 shader to add CULL OFF, but it didn't work and I got a "shader compile" error. I asked online and so far no one gave an answer how to fix it.
     
    Last edited: Aug 13, 2018
  15. Hybris_Team

    Hybris_Team

    Joined:
    Oct 17, 2015
    Posts:
    114
    I'll take note of that when I upgrade, thanks for the heads up :)
     
    Honorsoft likes this.
  16. Kezrah1

    Kezrah1

    Joined:
    Mar 14, 2022
    Posts:
    2

    Does this example work as is for 2021 version?
     
  17. Kezrah1

    Kezrah1

    Joined:
    Mar 14, 2022
    Posts:
    2
    of unity?
     
  18. Honorsoft

    Honorsoft

    Joined:
    Oct 31, 2016
    Posts:
    81
    Sorry for the delay in responding...
    I'm not sure actually if it works for versions past 2019 because I have major technical issues with the newer versions of Unity, so I stick to Unity 5, 2018 LTS and 2019 LTS.
     
  19. Honorsoft

    Honorsoft

    Joined:
    Oct 31, 2016
    Posts:
    81
    I'm still trying to achieve no culling in Unity for many years now, so I hope that Unity makes it a little easier to shut off back-face culling for it's users (really, with most models having things like hair, clothing, leaves, etc., culling should be off by default).

    -My main issue is trying to find a shader that has all the features of the "Standard Shader" (with normals, AO, texture tiling, etc.) and also has back-face culling off. Adding "cull off" to the Unity5 shader seemed to work, but not in 2018 or higher, and the best free shaders I've tried for no culling seem to have weird issues, even the ones made by Unity it seems. Some issues I've seen is partial transparency on the reverse side of the model, meaning that both sides are now being rendered but the other side is partially see-through.

    I was able to tweak a shader and it seems to be working in version 2019 (my earlier 2018 2-sided shader does not work properly in 2019), so I will post the new one here...

    First, some screenshots to show my problem:
    I have a 3D barn, and inside is fencing that needs to be transparent and 2-sided.
    In the first shot, the default Unity "Standard Shader" is being used on both the barn and the fencing:

    As you can see the fencing can be seen through the barn.

    Now with a double-sided (CULL OFF) shader:

    It makes it worse, and it doesn't matter whether both the barn and the fencing have a double-sided shader or one or the other.

    Now with the new double-sided shader (with better normal calculation):

    It looks like the shader works, but when I looked from the other side, I saw that the shader was not showing any transparencies.

    *Results: The new shader (below) does not work properly, since it is not supporting alpha-transparency, which is important for a 2-sided shaders since they are often used with transparent objects, like hair.

    Here's the shader anyways:

    Code (CSharp):
    1. // 2 sided standard surface shader (using 2 passes), with fixed normals for flipped faces
    2.  
    3. Shader "Custom/TwoSided (FixedNormals)"
    4. {
    5.     Properties
    6.     {
    7.         _Color ("Color", Color) = (1,1,1,1)
    8.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    9.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    10.         _Metallic ("Metallic", Range(0,1)) = 0.0
    11.     }
    12.     SubShader
    13.     {
    14.  
    15.         Tags { "RenderType" = "Opaque" }
    16.         LOD 200
    17.         Cull Back
    18.  
    19.         CGPROGRAM
    20.         // Physically based Standard lighting model, and enable shadows on all light types
    21.         #pragma surface surf Standard fullforwardshadows
    22.  
    23.         // Use shader model 3.0 target, to get nicer looking lighting
    24.         #pragma target 3.0
    25.  
    26.         sampler2D _MainTex;
    27.  
    28.         struct Input
    29.         {
    30.             float2 uv_MainTex;
    31.         };
    32.  
    33.         half _Glossiness;
    34.         half _Metallic;
    35.         fixed4 _Color;
    36.  
    37.         // To add instancing support for this shader you need to check 'Enable Instancing' on materials that use the shader.
    38.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    39.         // #pragma instancing_options assumeuniformscaling
    40.         UNITY_INSTANCING_BUFFER_START(Props)
    41.             // put more per-instance properties here
    42.         UNITY_INSTANCING_BUFFER_END(Props)
    43.  
    44.         void surf(Input IN, inout SurfaceOutputStandard o)
    45.         {
    46.             // Albedo comes from a texture tinted by color
    47.             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    48.             o.Albedo = c.rgb;
    49.             // Metallic and smoothness come from slider variables
    50.             o.Metallic = _Metallic;
    51.             o.Smoothness = _Glossiness;
    52.             o.Alpha = c.a;
    53.         }
    54.         ENDCG
    55.  
    56.         // 2nd pass to fix flipped normals
    57.         Tags{ "RenderType" = "Opaque" }
    58.         LOD 200
    59.         Cull Front
    60.  
    61.         CGPROGRAM
    62.         // Physically based Standard lighting model, and enable shadows on all light types
    63.         #pragma surface surf Standard fullforwardshadows
    64.         #pragma vertex vert
    65.  
    66.         // Use shader model 3.0 target, to get nicer looking lighting
    67.         #pragma target 3.0
    68.  
    69.         sampler2D _MainTex;
    70.  
    71.         struct Input
    72.         {
    73.             float2 uv_MainTex;
    74.         };
    75.  
    76.         half _Glossiness;
    77.         half _Metallic;
    78.         fixed4 _Color;
    79.  
    80.         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    81.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    82.         // #pragma instancing_options assumeuniformscaling
    83.         UNITY_INSTANCING_BUFFER_START(Props)
    84.         // put more per-instance properties here
    85.         UNITY_INSTANCING_BUFFER_END(Props)
    86.  
    87.         void vert(inout appdata_full v, out Input o) {
    88.             UNITY_INITIALIZE_OUTPUT(Input, o);
    89.             v.normal = -v.normal;
    90.         }
    91.  
    92.         void surf(Input IN, inout SurfaceOutputStandard o)
    93.         {
    94.             // Albedo comes from a texture tinted by color
    95.             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    96.             o.Albedo = c.rgb;
    97.             // Metallic and smoothness come from slider variables
    98.             o.Metallic = _Metallic;
    99.             o.Smoothness = _Glossiness;
    100.             o.Alpha = c.a;
    101.         }
    102.         ENDCG
    103.  
    104.  
    105.     }
    106.     FallBack "Diffuse"
    107. }
    108.  
    *Just save it as "TwoSided (FixedNormals).shader" and it will show up in Unity in a Shader folder called "Custom".

    NOTE: I also tried a "solution" posted by someone that said to use a text shader, which I tried but found that it removed any normals shading and removed any cast shadows (since the shader is meant for text). I also tried Unity's Nature shaders, since leaves are one of the things that never uses back-face culling, and that did not work since they either still culled the backfaces or looked wrong.

    Here's the differences using a fence corner:
    Unity Standard Shader:

    My old 2018 2-Sided shader:

    -Although it renders back faces, they are partially transparent.

    The 2-Sided Fixed Normals Shader (from the Asset Store):

    -the transparency is lost on the fence.

    The UI Text Shader:

    -As I mentioned, no shading or shadows, but not transparent so at least I know that it IS possible to have a shader that renders with both sides looking solid.

    * IMPORTANT UPDATE: Sorry for the long post, but I re-tried all the Unity shaders and did find a Unity shader that worked (although you have to change the shader settings), it's the "Nature>Tree Soft Occlusion Leaves" shader. It didn't look right before but I played with the settings and got something passable:


    Turn the sliders down, also "Directional Occlusion" was making the back-face black. This shader works well with adjusted settings and both parts of the fence are casting shadows now, wonderful!
    -So why hasn't Unity mentioned this before anywhere when users have been asking for years about turning back-face culling off?
     
    Last edited: Jan 14, 2023
    nasos_333 likes this.
  20. unity_oVsJgtACmNlH5w

    unity_oVsJgtACmNlH5w

    Joined:
    Aug 2, 2020
    Posts:
    4
    Hello, Anyone still having trouble with disappearing another side.
    upload_2022-4-30_13-45-5.png
    There is an option in Shader. This solved the issue for me.
     
  21. Honorsoft

    Honorsoft

    Joined:
    Oct 31, 2016
    Posts:
    81
    I don't think that applies here, it looks like you're talking about Shader Graph, this is about regular shaders (*.shader)
     
    Last edited: Jan 14, 2023
  22. drewjosh

    drewjosh

    Joined:
    Sep 24, 2019
    Posts:
    30
    I was looking for a solution how to disable back face culling for objects that have an invisible material and obscure objects behind, for e.g. the 3d scan of an building or environment. This feature is needed quite often for optimal occlusion in AR, e.g. for Vuforia Area Targets where I didn't find an option how to disable back face culling and because of an unknown bug the runtime occlusion stopped working.

    Thanks @Honorsoft for your simple solution. My shader now looks like this and works like a charm!

    Code (CSharp):
    1. Shader "Custom/InvisibleMaskNoCulling" {
    2.     // this shader makes an object invisible and obscure objects behind, it also disables back face culling
    3.     // source: https://answers.unity.com/questions/316064/can-i-obscure-an-object-using-an-invisible-object.html
    4.     SubShader {
    5.         // draw after all opaque objects (queue = 2001):
    6.         Tags { "Queue"="Geometry+1" }
    7.         Pass {
    8.             Cull Off // disable back face culling, source: https://forum.unity.com/threads/how-to-turn-off-back-face-culling.329744/#post-3182708
    9.             Blend Zero One // keep the image behind it
    10.         }
    11.     }
    12. }
     
    Last edited: Apr 5, 2023
  23. Wanyudo

    Wanyudo

    Joined:
    Nov 9, 2021
    Posts:
    2
    Unity URP has Lit shaders with an option to enable both sides of a mesh to be shaded.
    And if i see it correctly, the problem OP is facing is a problem with the depth/z buffer.
    Basically, it does not recognize that the front fence is closer to the screen and overwrites the pixels with the ones from the back fence.