Search Unity

How to correctly apply direction to reflection from unity_SpecCube0

Discussion in 'Shaders' started by HopelessHyena, May 21, 2016.

  1. HopelessHyena

    HopelessHyena

    Joined:
    May 26, 2015
    Posts:
    18
    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:

    Code (CSharp):
    1. float3 reflectedDir = reflect(IN.viewDir, normalize(o.Normal));
    2. fixed4 unityReflTex = (UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, reflectedDir));
    3.  
    4. 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!
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    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.

    http://docs.unity3d.com/Manual/SL-SurfaceShaders.html

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