Unexpected behaviour when sampling reflection probe in URP shader

When writing custom URP shader I stumbled onto a problem with sampling reflection probe using SAMPLE_TEXTURECUBE_LOD() macro. Function flattens the mesh along local X-axis when shader is compatible with SRP Batcher:


Function works as intended if when SRP batching is incompatible:

Here is the shader I used for testing, I remove CBUFFER_START and CBUFFER_END lines to break to SRP Batcher compatibility.

Shader "Unlit/sh_urp_template"
{
    Properties
    {
        _test ("test", Float) = 1
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" "LightMode" = "UniversalForward"}
        LOD 100

        Pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

            struct appdata
            {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
            };

            struct v2f
            {
                float3 normal : NORMAL;
                float4 vertex : SV_POSITION;
            };

            CBUFFER_START(UnityPerMaterial)
            float _test;
            CBUFFER_END

            v2f vert (appdata v)
            {
                v2f o;
                o.normal = mul( UNITY_MATRIX_M, float4( v.normal, 0.0 ) ).xyz;
                o.vertex = TransformObjectToHClip(v.vertex.xyz);
                return o;
            }

            half4 frag (v2f i) : SV_Target
            {
                half4 refl = SAMPLE_TEXTURECUBE_LOD(unity_SpecCube0,samplerunity_SpecCube0, i.normal, 0);
                return refl*_test;
            }
            ENDHLSL
        }
    }
}
1 Like

I am experiencing this as well, in particular I have been following CatlikeCoding’s Custom SRP Tutorials it does seem to be caused due to SRP Batching

GPU Instancing does not have the same issue and works fine

Unity Version: 2022.3.11f1
Core RP Library: 14.0.9

Where you use
o.normal = mul( UNITY_MATRIX_M, float4( v.normal, 0.0 ) ).xyz;

It ought to be:
o.normal = TransformObjectToWorldNormal(v.normal).xyz;

Unity the UNITY_MATRIX_M matrix (and the likes) directly is not in line with SRP shader code best practices. Adopting the function may just resolve the issue right away!

The function unrolls to:
image

it still looks wrong with TransformObjectToWorldNormal():
image