Search Unity

Unity 5.6.x VFACE problem (fixed)

Discussion in 'Shaders' started by tomekkie2, May 27, 2017.

  1. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    Looks like VFACE were getting reversed.
    In other words, I am getting errors on my shaders using VFACE .

    When running the simple VFACE shader example (Shader "Unlit/Face Orientation") from here:
    https://docs.unity3d.com/Manual/SL-ShaderSemantics.html

    and I am getting reversed effects.
    Does anyone else experience this?

    Here is the Face Orientation shader code:
    Code (CSharp):
    1. Shader "Unlit/Face Orientation"
    2. {
    3.     Properties
    4.     {
    5.         _ColorFront ("Front Color", Color) = (1,0.7,0.7,1)
    6.         _ColorBack ("Back Color", Color) = (0.7,1,0.7,1)
    7.     }
    8.     SubShader
    9.     {
    10.         Pass
    11.         {
    12.             Cull Off // turn off backface culling
    13.  
    14.             CGPROGRAM
    15.             #pragma vertex vert
    16.             #pragma fragment frag
    17.             #pragma target 3.0
    18.  
    19.             float4 vert (float4 vertex : POSITION) : SV_POSITION
    20.             {
    21.                 return UnityObjectToClipPos(vertex);
    22.             }
    23.  
    24.             fixed4 _ColorFront;
    25.             fixed4 _ColorBack;
    26.  
    27.             fixed4 frag (fixed facing : VFACE) : SV_Target
    28.             {
    29.                 return facing > 0 ? _ColorFront : _ColorBack;
    30.             }
    31.             ENDCG
    32.         }
    33.     }
    34. }
    And another surface Face Orientation shader, which is giving the right, not reversed, output.
    Code (CSharp):
    1. Shader "Custom/SurfaceFaceOrientation" {
    2.     Properties {
    3.         _ColorFront ("Front Color", Color) = (1,0.7,0.7,1)
    4.         _ColorBack ("Back Color", Color) = (0.7,1,0.7,1)
    5.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    6.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    7.         _Metallic ("Metallic", Range(0,1)) = 0.0
    8.     }
    9.     SubShader {
    10.         Tags { "RenderType"="Opaque" }
    11.         LOD 200
    12.         Cull Off
    13.      
    14.         CGPROGRAM
    15.         #pragma surface surf Standard fullforwardshadows
    16.  
    17.         #pragma target 3.0
    18.  
    19.         sampler2D _MainTex;
    20.  
    21.         struct Input {
    22.             float2 uv_MainTex;
    23.             float f:VFACE;
    24.         };
    25.  
    26.         half _Glossiness;
    27.         half _Metallic;
    28.         fixed4 _ColorFront;
    29.         fixed4 _ColorBack;
    30.  
    31.         void surf (Input IN, inout SurfaceOutputStandard o) {
    32.             // Albedo comes from a texture tinted by color
    33.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * (IN.f>0?_ColorFront:_ColorBack);
    34.             o.Albedo = c.rgb;
    35.             // Metallic and smoothness come from slider variables
    36.             o.Metallic = _Metallic;
    37.             o.Smoothness = _Glossiness;
    38.             o.Alpha = c.a;
    39.         }
    40.         ENDCG
    41.     }
    42.     FallBack "Diffuse"
    43. }
     
    Last edited: May 28, 2017
  2. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    bump....
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Are your meshes by chance scaled negatively on one or more axis?
     
  4. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    Not. Just any meshes. Including quads instantiated from the Unity menu.
    The same quad, orientated the same, gives me different results with these shaders:
    the fragment one and the surface one.
     
  5. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    Here is the package with scene containing three identically oriented quads, using face orientation shaders.
    One using VFACE on fragment shader orientates it opposite direction.
    Is it a bug or I am doing something wrong?
     

    Attached Files:

  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    A snippet of code from the surface shader's generated vertex & fragment shader:
    Code (CSharp):
    1.   #if UNITY_VFACE_FLIPPED
    2.      vface = -vface;
    3.   #endif
    4.   #if UNITY_VFACE_AFFECTED_BY_PROJECTION
    5.       surfIN.f = vface * _ProjectionParams.x; // take possible upside down rendering into account
    6.   #else
    7.       surfIN.f = vface;
    8.   #endif
     
    tomekkie2 likes this.
  7. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,448
    tomekkie2 likes this.
  8. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    Thanks a lot!!!
    This insert seems to get the problem fixed for dx9.
    This is the version of "Unlit/Face Orientation" shader corrected for Unity 5.6.x and dx9:

    Code (CSharp):
    1. Shader "Unlit/Face Orientation"
    2. {
    3.     Properties
    4.     {
    5.         _ColorFront ("Front Color", Color) = (1,0.7,0.7,1)
    6.         _ColorBack ("Back Color", Color) = (0.7,1,0.7,1)
    7.     }
    8.     SubShader
    9.     {
    10.         Pass
    11.         {
    12.             Cull Off // turn off backface culling
    13.  
    14.             CGPROGRAM
    15.             #pragma vertex vert
    16.             #pragma fragment frag
    17.             #pragma target 3.0
    18.  
    19.             float4 vert (float4 vertex : POSITION) : SV_POSITION
    20.             {
    21.                 return UnityObjectToClipPos(vertex);
    22.             }
    23.  
    24.             fixed4 _ColorFront;
    25.             fixed4 _ColorBack;
    26.  
    27.             fixed4 frag (fixed facing : VFACE) : SV_Target
    28.             {
    29.                 #if UNITY_VFACE_AFFECTED_BY_PROJECTION
    30.                     facing *= _ProjectionParams.x; // take possible upside down rendering into account
    31.                 #endif              
    32.                 return facing > 0 ? _ColorFront : _ColorBack;
    33.             }
    34.             ENDCG
    35.         }
    36.     }
    37. }
     
    Last edited: May 29, 2017
    bgolus likes this.