How to correctly apply direction to reflection from unity_SpecCube0

Hi Everyone,

I am writing a surface shader which is supposed to reflect the world around it using the built in Unity Reflection Probes. I have the following code:

float3 reflectedDir = reflect(IN.viewDir, normalize(o.Normal));
fixed4 unityReflTex = (UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, reflectedDir));

fixed4 reflcol =   (smoothstep(1 - (_RimThick*4), 1.0, dotProduct))  *  unityReflTex  ;

What do I need to do to the reflectedDir float3 in order to make it position the unityReflTex correctly? At the moment, it seems like the reflection is affected by the UVs of the object.

I could just use the worldRefl from the input, but this doesn’t bend with the normals of the object.

Thanks in advance!

This is using a surface shader, yes? In a surface shader the IN.viewDir and o.Normal are in tangent space, that is to say they’re directions relative to the mesh’s normal and UVs. For a reflection you need a world space direction.

Near there bottom of this page look for worldRefl and read the notes on it.