Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Using worldPos and normal in surface shader

Discussion in '5.6 Beta' started by CDF, Jan 28, 2017.

  1. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    968
    This appears to only be an issue in 5.6. Trying to use both worldPos and assign the SurfaceOutput.Normal results in this error:

    Code (CSharp):
    1.  
    2. Shader error in 'Custom/Boat': 'UnityGetRawBakedOcclusions': no matching 2 parameter function at line 226 (on d3d11)
    3.  
    4. Compiling Vertex program
    5. 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
    6.  
    7. Shader error in 'Custom/Boat': invalid subscript 'worldPos' at line 226 (on d3d11)
    8.  
    9. Compiling Vertex program
    10. 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
    11.  
    The shader:

    Code (CSharp):
    1.  
    2. Shader "Custom/Boat" {
    3.  
    4. Properties {
    5.  
    6. _MainTex ("Albedo (RGB)", 2D) = "white" {}
    7. _BumpMap("Bump Map", 2D) = "bump" {}
    8. }
    9.  
    10. SubShader {
    11.  
    12. Tags { "RenderType"="Opaque" }
    13. LOD 200
    14.  
    15. CGPROGRAM
    16.  
    17. #pragma surface surf Lambert
    18. #pragma target 3.0
    19.  
    20. sampler2D _MainTex;
    21. sampler2D _BumpMap;
    22.  
    23. struct Input {
    24. float2 uv_MainTex;
    25. float2 uv_BumpMap;
    26. float3 worldPos;
    27. };
    28.  
    29. void surf (Input IN, inout SurfaceOutput o) {
    30.  
    31. fixed3 col = tex2D(_MainTex, IN.uv_MainTex);
    32. col *= saturate(IN.worldPos.y / 20);
    33.  
    34. o.Albedo = col;
    35. o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    36. }
    37.  
    38. ENDCG
    39. }
    40.  
    41. FallBack "Diffuse"
    42. }
    43.  
    Am I missing something here?

    If I remove this line: "o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));" the error goes away
     
  2. Matt-Roper

    Matt-Roper

    <Of The Graphics> Unity Technologies

    Joined:
    Oct 27, 2015
    Posts:
    106
    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.
     
  3. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    968
    Ok, Thanks
     
  4. ming4883

    ming4883

    Joined:
    Nov 20, 2013
    Posts:
    2
    This bug is NOT yet fixed even in Unity 5.6.0.b7 running on OSX, I can provide more information if you need.
     
  5. Zomby138

    Zomby138

    Joined:
    Nov 3, 2009
    Posts:
    653
    I can confirm that it's still broken :(
     
  6. kemalakay

    kemalakay

    Unity Technologies

    Joined:
    Jul 12, 2016
    Posts:
    224
    @Zomby138 @CDF

    I will update you here in this thread when the fix is in and available. I'm keeping track of it
     
    Matt-Roper likes this.
  7. Zomby138

    Zomby138

    Joined:
    Nov 3, 2009
    Posts:
    653
    This is fixed in beta 8. Hazar!
     
    Matt-Roper likes this.
  8. kemalakay

    kemalakay

    Unity Technologies

    Joined:
    Jul 12, 2016
    Posts:
    224
    I was about to announce it, you're fast ;)
     
  9. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    968
  10. ChainsawesomeJS

    ChainsawesomeJS

    Joined:
    May 15, 2015
    Posts:
    28
    This seems like it's still happening for me in 5.6 beta 8 64bit. Here's the errors I'm getting :

    Code (CSharp):
    1. Shader error in 'Chainsawesome/CharactersEffect': 'UnityGetRawBakedOcclusions': no matching 2 parameter function at line 289 (on d3d11)
    2.  
    3. Compiling Vertex program with SHADOWS_SHADOWMASK
    4. 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
    5.  
    6. Shader error in 'Chainsawesome/CharactersEffect': invalid subscript 'worldPos' at line 289 (on d3d11)
    7.  
    8. Compiling Vertex program with SHADOWS_SHADOWMASK
    9. 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
    10.  
    The shader I'm using looks something like this :

    Code (CSharp):
    1. Shader "Chainsawesome/CharactersEffect"
    2. {
    3.     Properties
    4.     {
    5.         Stealth("Stealth", float) = 0.0
    6.  
    7.         [Space]_Color ("Color", Color) = (1,1,1,1)
    8.         Saturation("Saturation", float) = 1.0
    9.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    10.         EmissionGain("EmissionGain", float) = 1.0
    11.         _ColorEmi("Color", Color) = (1,1,1,1)
    12.         _Normal ("_Normal", 2D) = "bump" {}      
    13.  
    14.         [Space]_Stuff ("AO,Metal,Smooth,Emi", 2D) = "white" {}
    15.      
    16.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    17.         _Metallic ("Metallic", Range(0,1)) = 0.0      
    18.         StrokesGain("StrokesGain", float) = 1.0
    19.      
    20.         [space]RimPow("RimPow", float) = 1.0
    21.         RimGain("RimGain", float) = 1.0
    22.         RimEmi("RimEmi", float) = 1.0
    23.  
    24.         [space]AlbedoGain("Gain", float) = 1.0
    25.     }
    26.     SubShader
    27.     {
    28.         Tags { "RenderType" = "Transparent" "Queue" = "Transparent+1"}
    29.         LOD 200
    30.         GrabPass{ "_After" }
    31.  
    32.         CGPROGRAM
    33.         #pragma surface surf Standard fullforwardshadows
    34.         #pragma target 3.0
    35.  
    36.         uniform sampler2D _MainTex;
    37.         uniform sampler2D _Emission;
    38.         uniform sampler2D _Normal;
    39.         uniform sampler2D _Stuff;
    40.         uniform sampler2D _After ;
    41.         uniform half _Glossiness;
    42.         uniform half _Metallic;
    43.         uniform fixed4 _Color;
    44.         uniform fixed4 _ColorEmi;
    45.         uniform half Saturation;
    46.      
    47.         uniform half StrokesGain;
    48.         uniform half EmissionGain;
    49.  
    50.         uniform half RimPow;
    51.         uniform half RimGain;
    52.         uniform half RimEmi;
    53.         uniform half Stealth;  
    54.         uniform half AlbedoGain;
    55.  
    56.         struct Input
    57.         {
    58.             float2 uv_MainTex;
    59.             float2 uv_Emission;
    60.             float2 uv_Normal;
    61.             float2 uv_Stuff;
    62.  
    63.             INTERNAL_DATA
    64.             float3 worldNormal;
    65.             float3 worldPos;
    66.             half3 lightDir;
    67.             half4 screenPos;
    68.             half3 worldRefl;
    69.         };
    70.  
    71.         void surf (Input IN, inout SurfaceOutputStandard o)
    72.         {
    73.             float2 screenUV = IN.screenPos.xy / (IN.screenPos.w + 0.00001);//Avoid zero division on dx11
    74.             float3 behind = tex2D(_After ,screenUV  );
    75.  
    76.             float4 stuff = tex2D(_Stuff,IN.uv_Stuff);
    77.             float AO = stuff.x;
    78.             float Metal = stuff.y;
    79.             float Smooth = stuff.z;
    80.             float Emi = stuff.w;
    81.          
    82.             o.Normal = UnpackNormal(tex2D(_Normal,IN.uv_Normal));
    83.             float3 worldNormal = WorldNormalVector (IN, float3(0, 0, 1));          
    84.  
    85.             float3 Eye = normalize(_WorldSpaceCameraPos - IN.worldPos);
    86.             float rim = 1 - dot(Eye,worldNormal);
    87.             rim = pow(rim,RimPow);
    88.             rim *= RimGain;
    89.                      
    90.             fixed4 albe = tex2D (_MainTex, IN.uv_MainTex) ;
    91.             fixed4 c = (albe * _Color * AO + (rim * _ColorEmi)) *StrokesGain;
    92.  
    93.             fixed4 desaturated = (c.x + c.y + c.z) * 0.3333333;
    94.             c = lerp(desaturated,c,Saturation);
    95.  
    96.             o.Albedo = c *  (1-Stealth) * AlbedoGain;
    97.             o.Metallic = _Metallic * Metal* (1-Stealth);
    98.             float3 normalEmi = Emi * EmissionGain * _ColorEmi + (rim * RimEmi * _ColorEmi);
    99.             float3 StealthEmi =  behind + (Emi * EmissionGain * _ColorEmi)+ (rim * RimEmi * _ColorEmi);
    100.  
    101.             o.Emission = Emi * EmissionGain + (rim * RimEmi * _ColorEmi) ;
    102.             o.Emission = lerp(normalEmi ,StealthEmi ,Stealth);
    103.             o.Smoothness = _Glossiness * Smooth * (1-Stealth);
    104.             o.Alpha = 1;
    105.         }
    106.         ENDCG
    107.     }
    108.     Fallback "Transparent/VertexLit"
    109. }
    Commenting the line seems to fix the issue for some obscure reason, but it can't be the solution :

    Code (CSharp):
    1. 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!
     
  11. ming4883

    ming4883

    Joined:
    Nov 20, 2013
    Posts:
    2
    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.
    Code (csharp):
    1.  
    2. Shader "MGFX/WaterFlowBump" {
    3.     Properties {
    4.         _Color ("Color", Color) = (1,1,1,1)
    5.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    6.         _NormalMapTex ("NormalMap", 2D) = "bump" {}
    7.         _Metallic ("Metallic", Range(0,1)) = 0.0
    8.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    9.         _FlowMapTex("FlowMap", 2D) = "white" {}
    10.     }
    11.     SubShader {
    12.         Tags { "RenderType"="Opaque" }
    13.         LOD 200
    14.      
    15.         CGPROGRAM
    16.         // Physically based Standard lighting model, and enable shadows on all light types
    17.         #pragma surface surf Standard fullforwardshadows
    18.  
    19.         // Use shader model 3.0 target, to get nicer looking lighting
    20.         #pragma target 3.0
    21.  
    22.         sampler2D _MainTex;
    23.         uniform half4 _MainTex_TexelSize;
    24.  
    25.         sampler2D _NormalMapTex;
    26.         uniform half4 _NormalMapTex_TexelSize;
    27.  
    28.         struct Input {
    29.             float2 uv_MainTex;
    30.             float2 uv_NormalMapTex;
    31.             float3 worldPos;
    32.             float3 worldNormal; INTERNAL_DATA
    33.         };
    34.  
    35.         half _Glossiness;
    36.         half _Metallic;
    37.         fixed4 _Color;
    38.  
    39.         sampler2D _FlowMapTex;
    40.         half4x4 _FlowMapMatrix;
    41.  
    42.         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    43.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    44.         // #pragma instancing_options assumeuniformscaling
    45.         UNITY_INSTANCING_CBUFFER_START(Props)
    46.             // put more per-instance properties here
    47.         UNITY_INSTANCING_CBUFFER_END
    48.  
    49.         void surf (Input IN, inout SurfaceOutputStandard o) {
    50.  
    51.             half2 uv_FlowMap = mul(_FlowMapMatrix, half4(IN.worldPos, 1)).xy;
    52.             half4 flow = tex2D(_FlowMapTex, uv_FlowMap);
    53.             half2 uv_Offset = (flow.xy * 2.0 - 1.0) * _Time.yy;
    54.             half2 uv_NormalMap = (IN.worldPos.xz / 10.0) + uv_Offset * _NormalMapTex_TexelSize.xy;
    55.  
    56.             o.Normal = UnpackNormal (tex2D (_NormalMapTex, uv_NormalMap));
    57.          
    58.             // Albedo comes from a texture tinted by color
    59.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    60.             o.Albedo = c.rgb;
    61.          
    62.             // Metallic and smoothness come from slider variables
    63.             o.Metallic = _Metallic;
    64.             o.Smoothness = _Glossiness;
    65.             o.Alpha = c.a;
    66.         }
    67.         ENDCG
    68.     }
    69.     FallBack "Diffuse"
    70. }
    71.  
     
    Last edited: Feb 15, 2017
    Matt-Roper likes this.
  12. ChainsawesomeJS

    ChainsawesomeJS

    Joined:
    May 15, 2015
    Posts:
    28
    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.
     
    Last edited: Feb 15, 2017
unityunity