[Wind yuchong] unity3d tutorial book shader: 20th lecture line texture

Source: Internet
Author: User

[Wind yuchong] unity3d tutorial book shader: 20th lecture line texture (10:42:55)

ReprintedBytes
Tags:

Unity3d tutorial unity3dunityit Game Development
Category:
Unity3d shader
If the original article needs to be reproduced, please note: Reprinted from Fengyu Chong unity3d tutorial Institute

      
Lecture 2Normal Map



In the previous section, we talked about the concave and convex pasters and generate the normal pasters. This article describes how to use the normal map. I. Principle of normal texture II. Realization of normal texture III. Usage of normal texture IV. Format of normal textureI. Principle of normal map The illumination effect is largely determined by the normal line perpendicular to the object surface, because the normal line affects the direction of the reflected light. The even vertical normal is a mirror map. But sometimes we use a brick wall texture for a plane. The brick wall should be uneven. If the normal of the plane is used for the brick wall, the picture will be fake? A wall is reflective like a mirror =. =If a model is made based on a real brick wall, it is a high-precision model. On the one hand, it is difficult to create a model, and on the other hand, it is costly to run. Normal maps solve this problem. The normal map stores the normal information in a worker. When using a normal map, the number of vertices and the number of triangles is usually less than 10 of the precision model.II. Implementation of normal maps Draw the normal of the texture map on a texture map. The unit normal vector information float3 (x, y, z) of the point corresponding to the texture is stored in the color (R, G, B) corresponding to the graph, where x, y, Z corresponds to R, G, and B respectively. The value range of the unit normal vector float3 (x, y, z), x, y, and z is [-1, 1]. The normal map is compressed in the color range [0, 1], so the conversion is required:Color = 0.5 * normal + 0.5; line= 2 *(Color-0.5 );  

3. Use of normal mapsThe main step (1) is to sample the normal texture and obtain the float4 packednormal = tex2d (_ normalmap, in. uv_maintex); (2) convert the normal in [] to 3D space [-](Because it is a unit vector)Float3 expand (float3 v) {return (V-
0.5) * 2 ;}Then use this normal. The method is the same as that in section 16.
 For detailed implementation, see the script at the end of this article.
Iv. normal texture formatNormal maps are divided into two main categories: (1) RGB normal maps, which are used above. It is usually blue. (Suffix can be the latest .png. Jpg, etc)(2) Compressed normal texture. For example, dxt5.0 (Suffix:. DDS)

DDS is short for DirectDraw surface. In fact, it is the product of DirectX texture compression (dxtc. Dxtc reduces texture memory consumption by 50% or more. There are three dxtc formats available: dxt1, dxt3, and dxt5.
Extended reading about DDS Principle of compressing normal maps: Normal (x, y, z) is a unit vector. Therefore, X2 + y2 + Z2
= 1. So we know any two of X, Y, and Z, and the rest can be calculated. Therefore, we can store the two values in X, Y, and Z using two channels, and omit the remaining values in XYZ, which is calculated. Benefits of compressing a normal texture: The compressed normal texture is only about 1/4 in size. Therefore, you can use larger or more textures to improve the image quality.
Unity3d normal texture: The compressed normal texture used by unity3d is in dxt5.0 format. There are two channels, A and G. For normal (x, y, z) A corresponds to X, G corresponds to y. The following function is used to sample the compressed normal texture:

Float4 packednormal = tex2d (_ normalmap, In. uv_maintex );

Packednormal. W corresponds to channel a, that is, X of the normal.

Packednormal. y corresponds to the G channel, that is, y of the normal.


The range is still [0, 1], and still needs to be converted to [-]. The Conversion Function of dxt5.0 normal map is as follows, where v is passed into packednormal.
  1. Float3 expand (float3 V)
  2.  {
  3. Fixed3 normal;
  4. Normal. XY = V. WY * 2-1;
  5. Normal. z = SQRT (1-Normal. x * normal. X-Normal. y * normal. y );
  6. Return normal;
  7. }

The unity3d standard normal decompression function is fixed3 unpacknormal (fixed4 packednormal ). Open unitycg. cginc and find the corresponding function:
  1. Inline fixed3 unpacknormal (fixed4 packednormal)
  2. {
  3. # If defined (shader_api_gles) & defined (shader_api_mobile)
  4. Return packednormal. XYZ * 2-1;
  5. # Else
  6. Fixed3 normal;
  7. Normal. XY = packednormal. WY * 2-1;
  8. Normal. z = SQRT (1-Normal. x * normal. X-Normal. y * normal. y );
  9. Return normal;
  10. # Endif
  11. }
If this function is defined as a mobile platform or OpenGL ES, it is determined that the RGB normal map is used; otherwise, the dxt5.0 texture is used. However, the mobile platform can also use a compressed normal texture, while windows can also use an RGB normal texture. Therefore, it is not recommended to use the unpacknormal function. We recommend that you use the function you write based on the specific format of the normal map.
========================================================== ========================================================== ========================================================== ========================================================== ========================================================== ========================================================== ========================================================== ==========
Script:
  1. //Shader: Surface shader with a normal texture
  2. //Author: Feng yuchong
  3. Shader "Custom/3_normalmap "{
  4.  Properties
  5.  {
  6.  _ Maintex ("texture", 2d) = "white "{}
  7.  _ Normalmap ("normalmap", 2d) = "white "{}
  8.  }
  9.  
  10.  Subshader
  11.  {
  12.  Cgprogram
  13.  # Pragma surface surf blinnphong
  14.  Struct Input
  15.  {
  16.  Float2 uv_maintex;
  17.  };
  18.  
  19.  // Normal range conversion: The value range of float3 (x, y, z), x, y, and z is [-1, 1]. The normal map is compressed in the color range [0, 1], so the conversion is required.
  20.  // (1) RGB normal texture
  21.  Float3 expand (float3 v) {return (V-0.5) * 2 ;}
  22.  // (2) dxt5.0 normal texture
  23.  Float3 expand2 (float4 V)
  24. {
  25. Fixed3 normal;
  26. Normal. XY = V. WY * 2-1;
  27. Normal. z = SQRT (1-Normal. x * normal. X-Normal. y * normal. y );
  28. Return normal;
  29. }
  30.  
  31.  Sampler2d _ maintex;
  32.  Sampler2d _ normalmap;
  33.  
  34.  Void SURF (input in, inout surfaceoutput O)
  35.  {
  36.  Half4 c = tex2d (_ maintex, In. uv_maintex );
  37.  O. albedo = C. RGB;
  38.  O. Alpha = C.;
  39.  
  40. // Sample the normal texture to obtain the normal ([0, 1]) compressed in the color space.
  41.  Float4 packednormal = tex2d (_ normalmap, In. uv_maintex );
  42.  
  43.  // Convert the normal [] in the color space to the normal range in the real 3D space [-]
  44.  // Note: the range is basically converted from [] to [-]. The correspondence between the channel of the graph and the normal XYZ depends on the normal texture format.
  45.  // Unpacknormal, unitycg. cginc Function
  46.  // O. Normal = unpacknormal (packednormal );
  47.  
  48.  // Expand, standard normal decompression Function
  49.  O. Normal = expand (packednormal. XYZ );
  50.  }
  51.  Endcg
  52.  }

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.