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. Dismiss Notice

Cull Off not working

Discussion in 'Shaders' started by dcecar, Dec 26, 2020.

  1. dcecar

    dcecar

    Joined:
    Dec 7, 2016
    Posts:
    6
    I am trying to disable backface culling in a simple shader but "Cull Off" seems to have no effect. It always culls one side. vertex buffer has no normals. version 2019.3.14f1. What could be the problem?

    Code (CSharp):
    1.  
    2. Shader "Custom/test" {
    3.  
    4.     Properties {
    5.         _MainTex ("Texture", 2D) = "white" { }
    6.     }
    7.     SubShader {
    8.         Tags { "Queue" = "Transparent" }
    9.  
    10.         Pass {
    11.             Blend SrcAlpha OneMinusSrcAlpha
    12.             Ztest always
    13.             ZWrite Off
    14.             Cull Off
    15.  
    16.             CGPROGRAM
    17.  
    18.             #pragma vertex vert
    19.             #pragma fragment frag
    20.             #include "UnityCG.cginc"
    21.  
    22.             sampler2D _MainTex;
    23.  
    24.             struct appdata {
    25.                 float4 vertex : POSITION;
    26.                 float2 texcoord : TEXCOORD0;
    27.                 fixed4 color : COLOR;
    28.             };
    29.  
    30.             struct v2f {
    31.                 float4 pos : SV_POSITION;
    32.                 float2 texcoord : TEXCOORD0;
    33.                 fixed4 color : COLOR;
    34.             };
    35.  
    36.             float4 _MainTex_ST;
    37.  
    38.  
    39.             v2f vert (appdata v)
    40.             {
    41.                 v2f o;
    42.                 o.pos = UnityObjectToClipPos(v.vertex);
    43.                 o.texcoord = TRANSFORM_TEX (v.texcoord, _MainTex);
    44.                 o.color = v.color;
    45.                 return o;
    46.             }
    47.  
    48.             fixed4 frag (v2f i) : SV_Target
    49.             {
    50.                 fixed4 texcol = tex2D (_MainTex, i.texcoord);
    51.                 return texcol * i.color;
    52.             }
    53.             ENDCG
    54.  
    55.         }
    56.     }
    57. }
    58.  
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,238
    How do you know it's always culling one side? What you have above will work, but my guess is either part of your mesh's vertex coloring is fully transparent, or you're mistaking sorting problems with culling.

    Depending on the order of the triangles in the mesh, some triangles will draw on top of others. There isn't really a solution to this because efficient real time sorting of transparent surfaces is an unsolved problem.

    However there are often hacky workarounds that can work for specific situations, if you can post images of the problem you're seeing.
     
  3. dcecar

    dcecar

    Joined:
    Dec 7, 2016
    Posts:
    6
    Looks like the shader wasn't getting compiled during the normal build. I had to re-import asset. Works now.
     
    bgolus likes this.