Search Unity

Surface Shader World Normal

Discussion in 'Shaders' started by MaT227, Sep 17, 2014.

  1. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    Hey everyone,

    I would like to know what is the difference between in term of performance, usage and compatibility :
    Code (CSharp):
    1. float3 worldNormal = WorldNormalVector(IN, o.Normal);
    and
    Code (CSharp):
    1. float3 worldNormal = mul( _Object2World, float4(o.Normal, 0.0)).xyz;
    Thank you very much ! :)
     
  2. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    Any idea ?
     
  3. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Presumably you just want to rotate your normal, whereas _Object2World is not a rotation matrix.
     
  4. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    To be clear the aim is to calculate the worldNormal to use them inside the lighting function of my surface shader but I don't want to pass it using my output values and
    Code (CSharp):
    1. float3 worldNormal = WorldNormalVector(IN, o.Normal);
    gives good results but I need to use an interpolator to store it to the lighting function.
    Code (CSharp):
    1. float3 worldNormal = mul( _Object2World, float4(o.Normal, 0.0)).xyz;
    doesn't gives good results but I can calculate it inside the lighting function.
     
  5. spraycanmansam

    spraycanmansam

    Joined:
    Nov 22, 2012
    Posts:
    254
    In your second example you're trying to calculate the world normal as if o.Normal were in object space. o.Normal is in tangent space not object space. Tangent -> Object -> World -> etc.. That's why you're getting bad results.

    When you add worldNormal and INTERNAL_DATA to a surface shader it calculates the rotation matrix in the vertex shader to transform a value from tangent to world space. Those interpolators get passed to the fragment shader (these extra interpolators are normally what causes the errors about too many interpolators if you're already packing a lot of data in!). WorldNormalVector is just a define for a function that uses those interpolators to transform your supplied tangent space value to world space.
     
    Last edited: Sep 21, 2014