"Unity Shaders" Learning Note--surfaceshader (vii) normal map

Source: Internet
Author: User
Tags modulus

"Unity Shaders" Learning Note--surfaceshader (vii) normal map

Reprint Please specify source: http://www.cnblogs.com/-867259206/p/5627565.html

This series of articles was written using Unity5.3.
Before writing the code:

  1. Of course, if unity wasn't installed, you wouldn't learn unity shaders, would it?

  2. Before reading this series of articles you need to have some programming concepts.

  3. In VS, Unity shaders does not have syntax highlighting and smart hints, and the VS party can refer to this article to make the code highlighted, shaderlabvs or NShader a plugin such as download or the like to highlight the code.

  4. This is the basic knowledge of the unity shaders for small white, and if you have a foundation or you are a great God, then these articles are not for you.

  5. Due to the limitation of the author's level, there may be some fallacy in the text, and I implore to point out.

Normal mapping is a technique for simulating the effect of high modulus on a low modulus. This is Wiki's introduction to it.
The normal map is similar to the bump map of the upgraded version, bump map records the surface bump of the case, the normal map records the surface of the object of the light information . The illumination information is the angle information of the incident light and the normal.
In order to improve performance, the number of faces in the model is as small as possible, and much of the detail is made up of stickers. But the light is calculated based on vertices, so that the illumination such as high-light shadows is not realistic enough. So the predecessors invented the normal map of the method, with a map to record the surface of the light information, that is, the RGB value to store the normal coordinates of the XYZ value, so that the low modulus can also have high-modulus illumination information, thus showing high-modulus lighting effect. This is a way of storing space for computing time. This article describes the 3DMax method of making normal maps, which should better understand the role of normal maps.
The following diagram illustrates the principle of normal mapping:

Next, learn how to write a shader with a normal map.

first prepare a Normalmap , to change its texture type to normal Map:

Define several properties first:

 properties {//add these Properties  _main  Tint ( "diffuse Tint" , color) = (1,1,1,1) _normaltex ( "Normal Map" , 2D) =  "bump"  {} _normalintensity ( "Normal Map Intensity" , range  (0,2)) = 1}  pre>

_normaltex is the normal map, _normalintensity is the strength of the normal, that is, control the intensity of the bump.
Define a variable with the same name in Subshader:

        //Link the property to the CG program        sampler2D _NormalTex;        float4 _MainTint;        float _NormalIntensity;

Define the texture coordinates of the normal map in the input structure:

        //Make sure you get the uvs for the texture in the Struct        struct Input         {            float2 uv_NormalTex;        };

Write the surf function:

void surf (Input IN, inout SurfaceOutput o) {    //Get the normal Data out of the normal map textures    //using the UnpackNormal() function.    float3 normalMap = UnpackNormal(tex2D(_NormalTex, IN.uv_NormalTex));    normalMap = float3(normalMap.x * _NormalIntensity,    normalMap.y * _NormalIntensity, normalMap.z);                   //Apply the new normals to the lighting model    o.Normal = normalMap.rgb;    o.Albedo = _MainTint.rgb;    o.Alpha = _MainTint.a;}
Explain

The normal information of the high modulus is recorded in the normal map. The value of the normals is [0,1], so storing the normal information in RGB requires a conversion. -1,1],rgb The method is ((x, y, z) + (1,1,1)) *0.5. That is, the original ( -1,0,1) coordinates became (0,0.5,1). The role of the Unpacknormal function is to convert coordinate points.
There is a definition of unpacknormal function in the Unity.cginc file under the Unity installation directory (/editor/data/cgincludes/).

inline fixed3 UnpackNormalDXT5nm (fixed4 packednormal){    fixed3 normal;    normal.xy = packednormal.wy * 2 - 1;    normal.z = sqrt(1 - saturate(dot(normal.xy, normal.xy)));    return normal;}inline fixed3 UnpackNormal(fixed4 packednormal){#if defined(UNITY_NO_DXT5nm)    return packednormal.xyz * 2 - 1;#else    return UnpackNormalDXT5nm(packednormal);#endif}

We see that there are two functions for converting normals, both of which are for different normal map formats.
In an uncompressed format, the XYZ value stored in the normal map is multiplied by 2 minus 1, changing back to its original coordinates.
In the case of a dxt5nm compression format (presumably this should be the compression format used by Unity), a different conversion method is used. (This is why you set the texture type to normal map because different types of pictures are converted in different ways)
dxt5nm compressed format, only G and a channel. Because normals are unit vectors, you can find another coordinate value (Z2=1-X2-Y2) whenever you know any two coordinate values, as long as two channels store two coordinate values. Because such calculations are not complex, you can save space to use more stickers. dxt5nm moves the value of the R channel to the alpha channel, preserving the value of the G-Channel, and the R and B channels are filled with a certain color.
So, to convert the normal map of dxt5nm, convert the value of the A and G channels (WY) to XY, and then calculate the z-value.
The following code is a good explanation. Normal variables that are assigned normals to the output structure after the normals coordinates have been converted. Because Normals (0,0,1) indicate that the normals of this point are the same as those of high modulus, there is no need for z-coordinates to multiply _normalintensity.

Normal mapping principle

The basic principle of normal mapping has already been mentioned, that is, the high-modulus normal information xyz is stored in RGB, the use of normal mapping when the RGB value to XYZ, in the calculation of light, the use of higher-modulus normals to calculate, resulting in high-modulus lighting effect, resulting in a low modulus of the surface of the more smooth appearance of the illusion , this is a visual deception.
In this section, we will discuss the coordinates of the normals in detail.
To represent the position of a vector in three-dimensional space requires a three-dimensional coordinate system for reference. To indicate the orientation of the normals can be used in world space, or in model coordinates (Object Space). You should have heard of these two coordinate systems.
If you use world coordinates, the model cannot be rotated or moved. Because the position of the model has changed, its coordinates in the world coordinate system have changed, but the coordinates of the normals are based on the world coordinate system, so that the normal position is wrong. Otherwise, the normal information is read and the world coordinates are converted.
If you use model coordinates, there is no limitation that the world coordinates cannot be rotated or moved. Regardless of how the model rotates, the position of the normals is relative to the model, so the coordinates of the normals do not change. Compared to world coordinates, the use of model coordinates for the transformation of the model coordinates to world coordinates, inefficient, but more flexible.
However, the use of model coordinates is also limited, that is, the model cannot be deformed. Because the model is deformed, the coordinates of the normal relative to the model also change, and some objects that are deformed (such as a model with a skeletal skin animation) do not apply to the normal map based on the model coordinates.
So another coordinate system is needed so that the normal map applies not only to a model, but also to other models.
This coordinate system is the tangent coordinate (Tangent Space). Tangent coordinates take the tangent of a point on the surface as the XY axis, and the normal as the z axis (that is, the axis that hangs to the surface). In this case, there are countless types of coordinate systems that meet the requirements. We can directly use the UV coordinate of the texture as the XY axis, so that there is a ready-made coordinate system available.
Tangent coordinates can be understood in this way, which represents the degree of perturbation of the normals on the high modulus relative to the lower-mode normals on the corresponding points. When the normal coordinate is (0,0,1), the high-modulus normal is consistent with the low-modulus normal, which is equivalent to the tangent coordinate of the high-modulus normal relative to the low-modulus normal in the xy direction of the degree of deviation. This also explains why the z-axis does not take the _normalintensity in the above code.
Normal map is used to show the surface of small bumps, such as skin wrinkles, scales, etc., so the normal disturbance value is not too large, that is, the value of the z-coordinate is relatively large, so the ratio of B channel is large, so the normal map is usually blue Ying.
Use tangent coordinates, which means that each polygon has its own coordinate system. The coordinates of the light vector are converted from world space to tangent space when lighting calculations are performed in the vertex shader. So we can use the Lightdir variable and the normal variable to calculate in surface shader, because unity does a lot of work for us.
Compared with the model coordinates, the tangent coordinates are more flexible and can be applied to models with different shapes of the original model, such as the normal map of granite can be applied to the surface of a cylinder, so that the surface of the cylinder also has the concave and convex effect of granite.
Normal maps usually have two kinds, one is object space normal map and one is tangent space normal map. If the model is not animated, then you can use the object space Normal map, if the model is animated, or it will deform (such as flowing water, flames) then use the tangent space normal map. Because tangent space normal map to be more flexible, can be applied to different objects, so tangent space normal map use more.

"Unity Shaders" Learning Note--surfaceshader (vii) normal map

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.