Using worldPos and normal in surface shader

This appears to only be an issue in 5.6. Trying to use both worldPos and assign the SurfaceOutput.Normal results in this error:

Shader error in 'Custom/Boat': 'UnityGetRawBakedOcclusions': no matching 2 parameter function at line 226 (on d3d11)

Compiling Vertex program
Platform defines: UNITY_NO_DXT5nm UNITY_ENABLE_REFLECTION_BUFFERS UNITY_USE_DITHER_MASK_FOR_ALPHABLENDED_SHADOWS UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BOX_PROJECTION UNITY_SPECCUBE_BLENDING UNITY_ENABLE_DETAIL_NORMALMAP SHADER_API_DESKTOP UNITY_COLORSPACE_GAMMA

Shader error in 'Custom/Boat': invalid subscript 'worldPos' at line 226 (on d3d11)

Compiling Vertex program
Platform defines: UNITY_NO_DXT5nm UNITY_ENABLE_REFLECTION_BUFFERS UNITY_USE_DITHER_MASK_FOR_ALPHABLENDED_SHADOWS UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BOX_PROJECTION UNITY_SPECCUBE_BLENDING UNITY_ENABLE_DETAIL_NORMALMAP SHADER_API_DESKTOP UNITY_COLORSPACE_GAMMA

The shader:

Shader "Custom/Boat" {

Properties {

_MainTex ("Albedo (RGB)", 2D) = "white" {}
_BumpMap("Bump Map", 2D) = "bump" {}
}

SubShader {

Tags { "RenderType"="Opaque" }
LOD 200

CGPROGRAM

#pragma surface surf Lambert
#pragma target 3.0

sampler2D _MainTex;
sampler2D _BumpMap;

struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
float3 worldPos;
};

void surf (Input IN, inout SurfaceOutput o) {

fixed3 col = tex2D(_MainTex, IN.uv_MainTex);
col *= saturate(IN.worldPos.y / 20);

o.Albedo = col;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
}

ENDCG
}

FallBack "Diffuse"
}

Am I missing something here?

If I remove this line: “o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));” the error goes away

Hey CDF, it’s a bug which has been fixed and will be in the next beta revision hopefully, thanks for bringing it to our attention.

Ok, Thanks

This bug is NOT yet fixed even in Unity 5.6.0.b7 running on OSX, I can provide more information if you need.

I can confirm that it’s still broken :frowning:

@Zomby138 @CDF

I will update you here in this thread when the fix is in and available. I’m keeping track of it

1 Like

This is fixed in beta 8. Hazar!

1 Like

I was about to announce it, you’re fast :wink:

2958575--219563--tom-cruise-actingcrazy.jpg

This seems like it’s still happening for me in 5.6 beta 8 64bit. Here’s the errors I’m getting :

Shader error in 'Chainsawesome/CharactersEffect': 'UnityGetRawBakedOcclusions': no matching 2 parameter function at line 289 (on d3d11)

Compiling Vertex program with SHADOWS_SHADOWMASK
Platform defines: UNITY_ENABLE_REFLECTION_BUFFERS UNITY_USE_DITHER_MASK_FOR_ALPHABLENDED_SHADOWS UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BOX_PROJECTION UNITY_SPECCUBE_BLENDING UNITY_ENABLE_DETAIL_NORMALMAP SHADER_API_DESKTOP

Shader error in 'Chainsawesome/CharactersEffect': invalid subscript 'worldPos' at line 289 (on d3d11)

Compiling Vertex program with SHADOWS_SHADOWMASK
Platform defines: UNITY_ENABLE_REFLECTION_BUFFERS UNITY_USE_DITHER_MASK_FOR_ALPHABLENDED_SHADOWS UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BOX_PROJECTION UNITY_SPECCUBE_BLENDING UNITY_ENABLE_DETAIL_NORMALMAP SHADER_API_DESKTOP

The shader I’m using looks something like this :

Shader "Chainsawesome/CharactersEffect"
{
    Properties
    {
        Stealth("Stealth", float) = 0.0

        [Space]_Color ("Color", Color) = (1,1,1,1)
        Saturation("Saturation", float) = 1.0
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        EmissionGain("EmissionGain", float) = 1.0
        _ColorEmi("Color", Color) = (1,1,1,1)
        _Normal ("_Normal", 2D) = "bump" {}      

        [Space]_Stuff ("AO,Metal,Smooth,Emi", 2D) = "white" {}
      
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0      
        StrokesGain("StrokesGain", float) = 1.0
      
        [space]RimPow("RimPow", float) = 1.0
        RimGain("RimGain", float) = 1.0
        RimEmi("RimEmi", float) = 1.0

        [space]AlbedoGain("Gain", float) = 1.0
    }
    SubShader
    {
        Tags { "RenderType" = "Transparent" "Queue" = "Transparent+1"}
        LOD 200
        GrabPass{ "_After" }

        CGPROGRAM
        #pragma surface surf Standard fullforwardshadows
        #pragma target 3.0

        uniform sampler2D _MainTex;
        uniform sampler2D _Emission;
        uniform sampler2D _Normal;
        uniform sampler2D _Stuff;
        uniform sampler2D _After ;
        uniform half _Glossiness;
        uniform half _Metallic;
        uniform fixed4 _Color;
        uniform fixed4 _ColorEmi;
        uniform half Saturation;
      
        uniform half StrokesGain;
        uniform half EmissionGain;

        uniform half RimPow;
        uniform half RimGain;
        uniform half RimEmi;
        uniform half Stealth;  
        uniform half AlbedoGain;

        struct Input
        {
            float2 uv_MainTex;
            float2 uv_Emission;
            float2 uv_Normal;
            float2 uv_Stuff;

            INTERNAL_DATA
            float3 worldNormal;
            float3 worldPos;
            half3 lightDir;
            half4 screenPos;
            half3 worldRefl;
        };

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            float2 screenUV = IN.screenPos.xy / (IN.screenPos.w + 0.00001);//Avoid zero division on dx11
            float3 behind = tex2D(_After ,screenUV  );

            float4 stuff = tex2D(_Stuff,IN.uv_Stuff);
            float AO = stuff.x;
            float Metal = stuff.y;
            float Smooth = stuff.z;
            float Emi = stuff.w;
          
            o.Normal = UnpackNormal(tex2D(_Normal,IN.uv_Normal));
            float3 worldNormal = WorldNormalVector (IN, float3(0, 0, 1));          

            float3 Eye = normalize(_WorldSpaceCameraPos - IN.worldPos);
            float rim = 1 - dot(Eye,worldNormal);
            rim = pow(rim,RimPow);
            rim *= RimGain;
                      
            fixed4 albe = tex2D (_MainTex, IN.uv_MainTex) ;
            fixed4 c = (albe * _Color * AO + (rim * _ColorEmi)) *StrokesGain;

            fixed4 desaturated = (c.x + c.y + c.z) * 0.3333333;
            c = lerp(desaturated,c,Saturation);

            o.Albedo = c *  (1-Stealth) * AlbedoGain;
            o.Metallic = _Metallic * Metal* (1-Stealth);
            float3 normalEmi = Emi * EmissionGain * _ColorEmi + (rim * RimEmi * _ColorEmi);
            float3 StealthEmi =  behind + (Emi * EmissionGain * _ColorEmi)+ (rim * RimEmi * _ColorEmi);

            o.Emission = Emi * EmissionGain + (rim * RimEmi * _ColorEmi) ;
            o.Emission = lerp(normalEmi ,StealthEmi ,Stealth);
            o.Smoothness = _Glossiness * Smooth * (1-Stealth);
            o.Alpha = 1;
        }
        ENDCG
    }
    Fallback "Transparent/VertexLit"
}

Commenting the line seems to fix the issue for some obscure reason, but it can’t be the solution :

o.Normal = UnpackNormal(tex2D(_Normal,IN.uv_Normal));

This issue makes my first build fail systematically which is quite a bummer since it breaks any Continuous Integration we might have. Any tips on why this is happening would be greatly appreciated.

Cheers!

Hi ChainsawesomeJS,
The following simple surface shader seems to work in 5.6.0b8 running on both Mac (Metal) and Windows 10(DX11), would you please give a try.

Shader "MGFX/WaterFlowBump" {
    Properties {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _NormalMapTex ("NormalMap", 2D) = "bump" {}
        _Metallic ("Metallic", Range(0,1)) = 0.0
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _FlowMapTex("FlowMap", 2D) = "white" {}
    }
    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;
        uniform half4 _MainTex_TexelSize;

        sampler2D _NormalMapTex;
        uniform half4 _NormalMapTex_TexelSize;

        struct Input {
            float2 uv_MainTex;
            float2 uv_NormalMapTex;
            float3 worldPos;
            float3 worldNormal; INTERNAL_DATA
        };

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;

        sampler2D _FlowMapTex;
        half4x4 _FlowMapMatrix;

        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_CBUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_CBUFFER_END

        void surf (Input IN, inout SurfaceOutputStandard o) {

            half2 uv_FlowMap = mul(_FlowMapMatrix, half4(IN.worldPos, 1)).xy;
            half4 flow = tex2D(_FlowMapTex, uv_FlowMap);
            half2 uv_Offset = (flow.xy * 2.0 - 1.0) * _Time.yy;
            half2 uv_NormalMap = (IN.worldPos.xz / 10.0) + uv_Offset * _NormalMapTex_TexelSize.xy;

            o.Normal = UnpackNormal (tex2D (_NormalMapTex, uv_NormalMap));
         
            // Albedo comes from a texture tinted by color
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = c.rgb;
         
            // Metallic and smoothness come from slider variables
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}
1 Like

I’ll try it, but just to be clear, my shader does work. It just triggers a build error the first time I do a build after opening Unity. I also have a bunch of other surface shaders using the worldPos and whatnot, they all work. I just have 2 of them triggering this weird build error, one being the one I posted earlier, which is why it’s so odd.