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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Flag to toggle backface culling?

Discussion in 'Shaders' started by ronronmx, Jul 19, 2013.

  1. ronronmx

    ronronmx

    Joined:
    Aug 16, 2010
    Posts:
    201
    Hey all,
    I wrote a transparent shader which I use for things like gold coins, chests ect...

    By default the shader has "Cull Back" so that the backside of the object doesn't render, but for some of the objects, I need to turn culling off, "Cull Off". Right now I have 2 copies of the same shader, 1 with culling on and 1 with culling off.

    I don't particularly like the fact that I need 2 identical shaders just to toggle backface culling, so I would like to add a property to the shader, for example "_CullOff", which would be a slider, from 0 to 1 (0 = Cull Off, 1 = Cull Back), so that I can use the same shader for 2 different materials, and toggle culling with the exposed property.

    Is there another way to toggle culling in a shader without using "Cull Off/Back" in the Pass? It would be nice to be able to toggle culling at runtime too...I just don't know how to do that.

    Thanks in advance for your help!
    Stephane
     
  2. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    I don't think so, pre-processor stuff doesn't work outside of the CGPROGRAM block.
     
  3. ronronmx

    ronronmx

    Joined:
    Aug 16, 2010
    Posts:
    201
    That's what I was afraid of...can anyone confirm this?
    Thanks Farfarer!
     
  4. Dolkar

    Dolkar

    Joined:
    Jun 8, 2013
    Posts:
    576
    You could write the shader in a CGINCLUDE block and then just define the passes, but there's no easy way to choose between subshaders / passes either :(
     
  5. ronronmx

    ronronmx

    Joined:
    Aug 16, 2010
    Posts:
    201
    True, good idea though :)
    Thanks for the help guys.
     
  6. Ozego

    Ozego

    Joined:
    Apr 26, 2015
    Posts:
    10
    I know this thread is very old; but it was the first result on my google search and I will probably forget how to do it and need it again so:
    Code (CSharp):
    1. Properties
    2. {
    3.     // add selector inside properties:
    4.     [Enum(UnityEngine.Rendering.CullMode)] _CullMode("Cull Mode", Int) = 0
    5. }
    6. SubShader
    7. {
    8.     // set cull mode inside subshader:
    9.     Cull [_CullMode]
    10. }
     
  7. benjaminacct

    benjaminacct

    Joined:
    May 21, 2018
    Posts:
    2
    You are a legend.
    I am now wondering how many other switches can be implemented using this method.
     
  8. Oxeren

    Oxeren

    Joined:
    Aug 14, 2013
    Posts:
    120
    You can also do it with ZTest:
    Code (CSharp):
    1. Properties
    2. {
    3.     [Enum(UnityEngine.Rendering.CompareFunction)] _ZTest ("ZTest", Float) = 0
    4. }
    5. SubShader
    6. {
    7.     ZTest [_ZTest]
    8. }
    9.  
    I guess it can also be done with blend, there is UnityEngine.Rendering.BlendMode enum, but I haven't tried it.
     
  9. Ozego

    Ozego

    Joined:
    Apr 26, 2015
    Posts:
    10
    Here are the ones I know of:

    Code (CSharp):
    1.  
    2. [Enum(UnityEngine.Rendering.BlendMode)] _Src ("Source Blending", Float) = 1
    3. [Enum(UnityEngine.Rendering.BlendMode)] _Dst ("Destination Blending", Float) = 1
    4. [Enum(UnityEngine.Rendering.CompareFunction)] _ZTest("ZTest", Float) = 0    
    5. [Enum(UnityEngine.Rendering.CullMode)] _CullMode("Cull Mode", Int) = 0
    6. [Enum(UnityEngine.Rendering.CompareFunction)] _StencilComp ("Stencil Comparison", Float) = 3
    7. [Enum(UnityEngine.Rendering.StencilOp)] _StencilOp ("Stencil Operation", Float) = 2
    8.  
    You can probably find more if you look through the UnityEngine.Rendering documentation; they are accessible in c# as well.
     
    Reahreic likes this.