[Unity Shader] coordinate transformation and normal transformation and Unity5 newly added built-in functions

Source: Internet
Author: User
Tags mul

When studying the sixth unity built-in function, the mul matrix multiplication was previously inconsistent with the book, resulting in a different lighting effect when using built-in functions, resulting in the following two issues:

1 When do I use a 3x3 matrix and when do I use a 4x4 matrix?

2 The normal transformation matrix is not the same as the coordinate transformation matrix ?

Answer 1:

The 4.9.1 section describes when to use the 3x3 and 4x4 matrices. Because the 4x4 matrix is more translational than a 3x3 matrix, the 4x4 matrix is typically used when transforming spatial coordinates. For both tangent and normal space vectors, there is no translation, so only the 3x3 matrix can be used (is it lazy to use the 4x4 matrix?). is possible).

Answer 2:

For normal transformations, because the coordinates are scaled, it may not be equal to scale, which causes the normal vector to use the coordinate transformation matrix to no longer be perpendicular to the surface. The normal transformation matrix is obtained from the following derivation process:

Suppose that the model space tangent of a planar point is t and the normal is n. Through the space conversion m, the normal converts G, the tangent is T ', the normal is n '. Then there are:

  TT N = 0-----(1),

(T ') T. N ' = 0-----(2),

T ' = M. T-----(3),

N ' = G. N-----(4).

The formula (2) (3) (4) is:

(M. T) T. (G. N) = 0 ==> tt MT G. N = 0 ==> (Matrix binding Law) (TT Mt G) · N = 0-----(5)

Combined (1) and (5), get: TT = TT MT G, thus getting Mt G = I, then:

G = (MT)-1 = (M-1) T

That is, the transformation matrix of the normal, is the inverse of the transpose of the spatial transformation matrix.

  

Calculation considerations in ======================unity shader =======================

1 in Unity Shader, convert coordinates from model space to world space, using the following methods:

The transformation matrix in Unityshader is the row matrix, the model space coordinate V.vertex as the column matrix, and the transformation matrix right multiply, obtains the world coordinates.

Note that o.worldpos = unityobjecttoworlddir(V.vertex) cannot be used here; View in Unitycg.cginc definition of Unityobjecttoworlddir:

// Transforms Direction from the object to the World space inch float3 dir) {    return  Normalize (Mul ((float3x3) _object2world, dir));}

The 3x3 matrix is used here, which causes the coordinate transformation to lose the translation parameters.

2 convert normals from model space to world space, using the following methods:

O.worldnormal = Mul (V.normal, (float3x3) _world2object);

Here, the matrix _world2object is the inverse matrix of the matrix _object2world. Because it is a normal vector, the 3x3 matrix is computed.

It is important to note that here the Mul implements the matrix multiplication, when the vector is on the left, this vector is equivalent to the line vector, where the value is multiplied and additive with the matrix's column elements. is equivalent to the following wording:

O.worldnormal = Mul (Transpose ((float3x3) _world2object), v.normal);

That is, the transformation of the normal is the transpose transpose (_world2object) of the inverse matrix of the spatial transformation _object2world.

Here unity shader encapsulates a function to convert normals to world coordinates unityobjecttoworldnormal (), replacing the above matrix multiplication notation, which is not prone to error.

 

// Transforms Normal from Object to World space inch FLOAT3 Norm) {    //  Multiply by transposed inverse matrix, actually using transpose () generates badly optimized cod E    return normalize (_world2object[0].xyz * norm.x + _world2object[1].xyz * Norm.y + _world2object[2].xyz * norm.z);}

Complete example of ================ book ===============

Shader"Unity Shaders book/chapter 6/blinn-phong build-in Function"{Properties {_diffuse ("Diffuse", Color) = (1.0,1.0,1.0,1.0) _specular ("Specular", Color) = (1.0,1.0,1.0,1.0) _gloss ("Gloss", Range (8.0,256.0)) =20.0} subshader {Pass {Tags {"Lightmode"="Forwardbase"} cgprogram#pragmaVertex vert#pragmaFragment Frag#include"Lighting.cginc"fixed4 _diffuse;            Fixed4 _specular; float_gloss; structa2v {float4 vertex:position;            FLOAT3 Normal:normal;            }; structv2f {float4 pos:sv_position;                Fixed3 worldnormal:texcoord0;            FLOAT3 Worldpos:texcoord1;            };                v2f Vert (a2v v) {v2f o; O.pos=Mul (UNITY_MATRIX_MVP, V.vertex); O.worldnormal= Unityobjecttoworldnormal (V.normal);//The effect is the same as the next line, because it is a vector transformation, with a 3x3 transformation matrix. //o.worldnormal = Mul (V.normal, (float3x3) _world2object);//O.worldpos = Unityobjecttoworlddir (V.vertex);//Normalize (Mul ((float3x3) _object2world, dir), and the effect is different from the next line//O.worldpos = Mul (V.vertex, Transpose (_object2world)). xyz;//The coordinates are left multiply, the model space coordinates are converted to world space, and the following equation is equalO.worldpos = Mul (_object2world, V.vertex). xyz;//multiply coordinates right, convert model space coordinates to world space                returno; } fixed4 Frag (v2f i): sv_target {fixed3 ambient=unity_lightmodel_ambient.xyz; Fixed3 Worldnormal=normalize (i.worldnormal); Fixed3 Worldlightdir=Normalize (Unityworldspacelightdir (I.worldpos)); FIXED3 Diffuse= _lightcolor0.rgb * _diffuse.rgb *saturate (dot (worldnormal, worldlightdir)); Fixed3 Viewdir=Normalize (Unityworldspaceviewdir (I.worldpos)); Fixed3 Halfdir= Normalize (Viewdir +Worldlightdir); Fixed3 Specular= _lightcolor0.rgb * _specular.rgb * POW (max (0, Dot (Halfdir, worldnormal)), _gloss); Fixed3 Color= ambient + diffuse +specular; returnFIXED4 (Color,1.0); } ENDCG}} Fallback"Specular"}
Use the Unity5 build-in function to implement the Blinn-phong illumination model.

[Unity Shader] coordinate transformation and normal transformation and Unity5 newly added built-in functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.