Search Unity

Question Faces culling in certain directions on transparent object

Discussion in 'General Graphics' started by Dragonslash42, Feb 15, 2023.

  1. Dragonslash42

    Dragonslash42

    Joined:
    Aug 27, 2018
    Posts:
    3
    I am building a voxel game, and this involves dynamically creating a mesh at runtime. I have made a custom shader to make use of a texture array and their alpha values, but I am getting a strange issue on my meshes. It seems to be culling faces, but only on certain directions of the mesh, the two pictures are the same mesh, but inverted on the x axis, they should both look like the second picture, but I can’t understand why the top culls some of the faces, all culling has been disabled. I tried reversing the order in which the triangles of the mesh are rendered, but it made no difference, please see below the shader code and screenshots of the issue.

    Please let me know if any of you have any ideas how I can resolve this issue as I am totally stumped, thanks! :)




    Code (CSharp):
    1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    2.  
    3. Shader "Custom/World Transparent"
    4. {
    5.     Properties
    6.     {
    7.         _MainTex("Albedo", 2DArray) = "white" {}
    8.  
    9.     }
    10.     SubShader
    11.     {
    12.         Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    13.         // ZWrite Off
    14.         // Cull back
    15.         // Cull Off
    16.         // Cull front
    17.         Blend SrcAlpha OneMinusSrcAlpha
    18.         Pass
    19.         {
    20.             CGPROGRAM
    21.             // Upgrade NOTE: excluded shader from DX11; has structs without semantics (struct v2f members arrayIndex)
    22.             #pragma exclude_renderers d3d11
    23.             // use "vert" function as the vertex shader
    24.             #pragma vertex vert
    25.             // use "frag" function as the pixel (fragment) shader
    26.             #pragma fragment frag
    27.             #include "UnityCG.cginc"
    28.  
    29.             #pragma multi_compile_fog
    30.  
    31.  
    32.             // vertex shader inputs
    33.             struct appdata
    34.             {
    35.                 float4 vertex : POSITION; // vertex position
    36.                 float3 uv : TEXCOORD0; // texture coordinate
    37.             };
    38.  
    39.             // vertex shader outputs ("vertex to fragment")
    40.             struct v2f
    41.             {
    42.                 float3 uv : TEXCOORD0; // texture coordinate
    43.                 float4 vertex : SV_POSITION; // clip space position
    44.                 fixed4 color : SV_Target;
    45.                 UNITY_FOG_COORDS(1)
    46.  
    47.             };
    48.  
    49.             // vertex shader
    50.             v2f vert (appdata_full v)
    51.             {
    52.                 v2f o;
    53.                 o.vertex = UnityObjectToClipPos(v.vertex);
    54.                 o.uv = v.texcoord.xyz;
    55.                 o.color = v.color;
    56.                 UNITY_TRANSFER_FOG(o, o.vertex);
    57.                 return o;
    58.             }
    59.            
    60.             // texture we will sample
    61.             // sampler2D _MainTex;
    62.             UNITY_DECLARE_TEX2DARRAY(_MainTex);
    63.  
    64.  
    65.             // pixel shader; returns low precision ("fixed4" type)
    66.             // color ("SV_Target" semantic)
    67.             fixed4 frag (v2f i) : SV_Target
    68.             {
    69.                 // sample texture and return it
    70.                 fixed4 col = UNITY_SAMPLE_TEX2DARRAY(_MainTex, i.uv);
    71.                 col *= i.color;
    72.                 UNITY_APPLY_FOG(i.fogCoord, col);
    73.                 return col;
    74.             }
    75.             ENDCG
    76.         }
    77.     }
    78. }
    79.  
    80.  
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    If the shader is exactly as you posted it, then culling is not disabled, because shaders default to
    Cull Back
    if not defined. If you uncomment out
    Cull Off
    then it should look the same regardless of what direction you look at it, but it'll also look different than either of the above images.

    You may need to double up faces if you want to keep back face culling while continuing to have interior surfaces that are visible from all sides.