Unity3d Normal map for game development

Source: Internet
Author: User

"Dog planing Learning Network" Unity's Ultimate Academy, dedicated to building the industry's first

One: The principle of normal mapping

Second: the implementation of normal map

Third: use of normal maps

Four: Format of normal map

One: The principle of normal mapping

The illumination effect is largely determined by normals perpendicular to the surface of the object, because the normals affect the direction of the reflected light. The uniform perpendicular normal is the mirror map. But sometimes we give a plane a brick wall map, the brick wall should be uneven, and if you let the brick wall use the plane's normal, the screen will be very false, God horse? A wall is as reflective as a mirror. =
And if the real brick wall to do the model, that is, to do a high-precision model, on the one hand make trouble, on the other hand, the performance of the loss of large.
The normal map is to solve thisProblemthe. The normal map is to store the normal information in a picture. When using normal maps, the number of vertices and the number of triangular polygons is usually less than one-tenth of the high-precision model.

Second: The implementation of normal map

The normal of the material map is drawn on a poster. The unit normal vector information float3 (x, Y, z) of the map corresponding point is stored in color (r,g,b) in the graph, where x, Y and Z correspond to r,g,b respectively. The unit normal vector float3 (x, Y, z), X, y, and z values range from [ -1,1]. is compressed in the normal map in the range of colors [0,1], so you need to convert:
color = 0.5 * normal + 0.5;
line = 2 * (color-0.5);

Third: Use of normal maps

Main steps

(1) sampling the normal map to get the normal compressed in the color space [0,1]

FLOAT4 packednormal = tex2d (_normalmap, In.uv_maintex);

(2) Convert normal compressed in [0,1] to 3D space [ -1,1] (because it is a unit vector)
FLOAT3 Expand (Float3 v) {return (v-0.5) * 2;}

The normal can then be used, as in the 16 lecture.

See the script at the end of this article for specific implementations.

Four: Format of normal map

stickers are divided into 2 main categories:

(1) RGB normal map, which is used above. Usually blue. (Suffixes can be common. png. jpg, etc.)

(2) normal map in compressed format. For example, dxt5nm (suffix. DDS)

DDS is the abbreviation for DirectDraw surface, in fact, it is the product of DirectX texture compression (DirectX Texture Compression, abbreviated DXTC). DXTC reduces texture memory consumption by 50% or more, with 3 DXTC formats available, DXT1,DXT3 and DXT5, respectively.

The principle of compressing normal maps:

Normal (x, Y, z) is a unit vector. So X2 + Y2 +z2 = 1. So I know any two in x, Y, Z, and the rest can be calculated. So we can use the 2-channel graph to store two values in X, Y, Z, and omit the remaining values from XYZ, calculated.

Advantages of compressing normal maps:

after compressing the normal map, the size of the original 1/4 or so, you can use larger or more stickers to improve the quality of the picture.

normal map for Unity3d:

unIty3dthe compressed normal map used is in dxt5nm format. There are two channels of a and G. For normals (x, Y, z) a corresponds to X,g y.

Sampling of compressed normal maps is still the following function:

FLOAT4 packednormal = tex2d (_normalmap, In.uv_maintex);

The PACKEDNORMAL.W corresponds to a channel, which is the X of the normals.

The paackednormal.y corresponds to the G-Channel, which is the y of the normals.

The range remains [0,1] and still needs to be converted to [ -1,1].

The function to convert the dxt5nm normal map is as follows, where V is passed in Packednormal

FLOAT3 expand (Float3 v) {fixed3 normal;normal.xy = v.wy * 2-1;normal.z = sqrt (1-NORMAL.X*NORMAL.X-NORMAL.Y * normal.y ); return normal;}
The standard normal decompression function for Unity3d is Fixed3 unpacknormal (fixed4 packednormal).




Open Unitycg.cginc to find the corresponding function:

Inline fixed3 unpacknormal (fixed4 packednormal) {#if defined (shader_api_gles) && defined (shader_api_mobile) return packednormal.xyz * 2-1; #elsefixed3 normal;normal.xy = packednormal.wy * 2-1;normal.z = sqrt (1-normal.x*normal . X-NORMAL.Y * normal.y); return normal; #endif}




The function defines if it is a mobile platform or OpenGL ES, then it is assumed to be using an RGB normal map, otherwise it is a dxt5nm map.

in fact, mobile platforms can also use normal maps in a compressed format, while Windows can also work with RGB normal maps. It is not recommended to use the Unpacknormal function, and it is recommended that you use the corresponding function that you write according to the specific format of the normal map.



Script:

  Shader: Surface shader//with normal map  author:shader "Custom/3_normalmap" {  Properties  {    _maintex (" Texture ", 2D) =" White "{}    _normalmap (" Normalmap ", 2D) = ' white ' {}  }    subshader  {  Cgprogram  # pragma surface surf blinnphong  struct Input  {  float2 uv_maintex;  };    Normal range conversion: Unit normal FLOAT3 (x, Y, z), and x, Y, Z are the range [ -1,1]. is compressed in the normal map in the range of colors [0,1], so you need to convert  //(1) RGB normal map  float3 expand (Float3 v) {return (v-0.5) * 2;}  (2) dxt5nm normal map  float3 Expand2 (Float4 v) {fixed3 normal;normal.xy = v.wy * 2-1;normal.z = sqrt (1-normal.x*normal.x -Normal.y * normal.y);

For more information, please visit the "Dog Planing Learning Network" Unity Ultimate Academy Http://edu.gopedu.com

Statement: This document comes from the "Dog Planing Learning Network" Community-unity Extreme College, is a self-published Unity3d study articles, if anything violates your relevant interests, please communicate with the official, we will deal with the real-time.

Unity3d Normal map for game development

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.