Search Unity

Unity shader normal vector confuse

Discussion in 'Getting Started' started by akira32chen, Nov 27, 2016.

  1. akira32chen

    akira32chen

    Joined:
    Dec 21, 2012
    Posts:
    29
    There is code about Shader in a book sample. My confuse is that the v.normal is object space.
    v.normal wants to convert to world space. Should not v.normal multiple with unity_ObjectToWorld as below?
    fixed3 worldNormal = normalize(mul( (float3x3)unity_ObjectToWorld,v.normal,));

    But the book is wroten as below:
    fixed3 worldNormal = normalize(mul(v.normal, (float3x3)unity_WorldToObject));

    Does somebody know the meaing of converting normal vector to world space?

    Code (CSharp):
    1.             struct a2v {
    2.                 float4 vertex : POSITION;
    3.                 float3 normal : NORMAL;
    4.             };
    5.            
    6.             struct v2f {
    7.                 float4 pos : SV_POSITION;
    8.                 fixed3 color : COLOR;
    9.             };
    10.            
    11.             v2f vert(a2v v) {
    12.                 v2f o;
    13.                 // Transform the vertex from object space to projection space
    14.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    15.                
    16.                 // Get ambient term
    17.                 fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
    18.                
    19.                 // Transform the normal from object space to world space
    20.                 fixed3 worldNormal = normalize(mul(v.normal, (float3x3)unity_WorldToObject));