Unity 5.6.x VFACE problem (fixed)

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:

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

Here is the Face Orientation shader code:

Shader "Unlit/Face Orientation"
{
    Properties
    {
        _ColorFront ("Front Color", Color) = (1,0.7,0.7,1)
        _ColorBack ("Back Color", Color) = (0.7,1,0.7,1)
    }
    SubShader
    {
        Pass
        {
            Cull Off // turn off backface culling

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma target 3.0

            float4 vert (float4 vertex : POSITION) : SV_POSITION
            {
                return UnityObjectToClipPos(vertex);
            }

            fixed4 _ColorFront;
            fixed4 _ColorBack;

            fixed4 frag (fixed facing : VFACE) : SV_Target
            {
                return facing > 0 ? _ColorFront : _ColorBack;
            }
            ENDCG
        }
    }
}

And another surface Face Orientation shader, which is giving the right, not reversed, output.

Shader "Custom/SurfaceFaceOrientation" {
    Properties {
        _ColorFront ("Front Color", Color) = (1,0.7,0.7,1)
        _ColorBack ("Back Color", Color) = (0.7,1,0.7,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
    }
    SubShader {
        Tags { "RenderType"="Opaque" }
        LOD 200
        Cull Off
      
        CGPROGRAM
        #pragma surface surf Standard fullforwardshadows

        #pragma target 3.0

        sampler2D _MainTex;

        struct Input {
            float2 uv_MainTex;
            float f:VFACE;
        };

        half _Glossiness;
        half _Metallic;
        fixed4 _ColorFront;
        fixed4 _ColorBack;

        void surf (Input IN, inout SurfaceOutputStandard o) {
            // Albedo comes from a texture tinted by color
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * (IN.f>0?_ColorFront:_ColorBack);
            o.Albedo = c.rgb;
            // Metallic and smoothness come from slider variables
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

bump…

Are your meshes by chance scaled negatively on one or more axis?

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.

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?

3086909–232597–vfaceTest.unitypackage (23.9 KB)

A snippet of code from the surface shader’s generated vertex & fragment shader:

  #if UNITY_VFACE_FLIPPED
     vface = -vface;
  #endif
  #if UNITY_VFACE_AFFECTED_BY_PROJECTION
      surfIN.f = vface * _ProjectionParams.x; // take possible upside down rendering into account
  #else
      surfIN.f = vface;
  #endif
1 Like

for me all 3 quads are red,
only when use DX9 then first one is green…

(2017.1.b7 / win10)

https://github.com/UnityCommunity/UnityReleaseNotes/search?utf8=✓&q=vface&type=

1 Like

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:

Shader "Unlit/Face Orientation"
{
    Properties
    {
        _ColorFront ("Front Color", Color) = (1,0.7,0.7,1)
        _ColorBack ("Back Color", Color) = (0.7,1,0.7,1)
    }
    SubShader
    {
        Pass
        {
            Cull Off // turn off backface culling

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma target 3.0

            float4 vert (float4 vertex : POSITION) : SV_POSITION
            {
                return UnityObjectToClipPos(vertex);
            }

            fixed4 _ColorFront;
            fixed4 _ColorBack;

            fixed4 frag (fixed facing : VFACE) : SV_Target
            {
                #if UNITY_VFACE_AFFECTED_BY_PROJECTION
                    facing *= _ProjectionParams.x; // take possible upside down rendering into account
                #endif               
                return facing > 0 ? _ColorFront : _ColorBack;
            }
            ENDCG
        }
    }
}
1 Like