Standard Surface Shader with ambient occlusion based on 2nd UV set.

Standard Surface Shader with ambient occlusion based on 2nd UV set does not work when you set a texture read based on the 2nd UV-set as occlusion output.

Shader "Custom/OcclusionOn2ndUV" {
    Properties {

        _MainTex ("Base (RGB)", 2D) = "white" {}
        _AOTex ("AO", 2D) = "white" {}
    }
    SubShader {
        Tags { "RenderType"="Opaque" }
        LOD 200
       
        CGPROGRAM

        #pragma surface surf Standard
        #pragma target 3.0

        sampler2D _MainTex;
        sampler2D _AOTex;

        struct Input
        {
            float2 uv_MainTex : TEXCOORD0;
            float2 uv2_AOTex :  TEXCOORD1;
        };


        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            half3 c = tex2D(_MainTex, IN.uv_MainTex.xy).rgb;
            half ao = tex2D(_AOTex, IN.uv2_AOTex.xy).r;
            o.Albedo = c;
            o.Occlusion = ao;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

Finally at the edge of a mental breakdown and after 11 hours I’ve found a workaround for the bug. The output of Ambient Occlusion will break if the output of o.alpha is not coming from a texture read of the same UV set!

This one works:

Shader "Custom/DOES_WORK" {
    Properties {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Main2Tex ("AO (R)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
    }
    SubShader {
        Tags { "RenderType"="Opaque" }
        LOD 200
   
        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;
        sampler2D _Main2Tex;

        struct Input {
            float2 uv_MainTex : TEXCOORD0;
            float2 uv2_Main2Tex : TEXCOORD1;
        };

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;

        void surf (Input IN, inout SurfaceOutputStandard o) {

            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = c.rgb;
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;;
   
            fixed4 Texture2 = tex2D (_Main2Tex, IN.uv2_Main2Tex);
            o.Alpha = Texture2.a;
            o.Occlusion = Texture2.r;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

This one does not work:

Shader "Custom/DOES_NOT_WORK" {
    Properties {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Main2Tex ("AO (R)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
    }
    SubShader {
        Tags { "RenderType"="Opaque" }
        LOD 200
   
        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;
        sampler2D _Main2Tex;

        struct Input {
            float2 uv_MainTex : TEXCOORD0;
            float2 uv2_Main2Tex : TEXCOORD1;
        };

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;

        void surf (Input IN, inout SurfaceOutputStandard o) {

            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = c.rgb;
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;;
            o.Alpha = c.a;

            fixed4 Texture2 = tex2D (_Main2Tex, IN.uv2_Main2Tex);
            o.Occlusion = Texture2.r;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

It does depend on the UV set AND additionally also on the value of alpha:

works:

o.Alpha = Texture2.a*0.0001;
o.Occlusion = Texture2.r;

works not:

o.Alpha = Texture2.a*0.0;
o.Occlusion = Texture2.r;

works not:

o.Alpha = 1.0;
o.Occlusion = Texture2.r;

works:

o.Alpha = tex2D(_Main2Tex, IN.uv_Main2Tex);
o.Occlusion = Texture2.r;

works:

o.Alpha = tex2D(_MainTex, IN.uv_Main2Tex);
o.Occlusion = Texture2.r;

works not:

o.Alpha = tex2D(_Main2Tex, IN.uv_MainTex);
o.Occlusion = Texture2.r;

EDIT: bug reported, Case 765145

2 Likes

Getting this same issue (5.2.1p4) with Emission. There’s definately a bug with how the surface shader generator is working, it’s simply not setting uv2_* to anything if that UV isn’t being used for Albedo or Alpha.

They already confirmed the bug in the bug tracker yesterday.