Unity Shader Getting Started Tutorial (ii) the most basic diffuse and normal examples

Source: Internet
Author: User

This tutorial is a reference to what cats can learn Unity3dshaderlab Tutorial . CHM",

1. Please search and download this file online.

2. Then download the material mentioned:

Http://vdisk.weibo.com/s/y-NNpUsxhYhZI

first set of experiments (refresher lessons for the simplest diffuse reflection [ This group of experiments references the normal-diffuse.shader Example in the official website example ] ):

Step 1.1 : Create a shader named "normaldiffuse"

Step 1.2 : see some of the existing content, may wish to delete all (except the first line), it is easier to exercise your code ability and deepen understanding.

I will lead the reader to write this example in one line.

Detailed Explanation:

Code start Shader"Custom/normaldiffuse"{properties{//The modulated color _color and the main texture _maintex will be multiplied to get the modulated color_color ("The Main Color", Color) = (1,1,1,1) _maintex ("Base Picture", 2D) =" White"{}        //two images are white by default (the previous data structure is color, followed by a two-dimensional image)    }                //As I said before, this is a processing subroutine (a pass)subshader{//render type is opaquetags{"Rendertype"="Opaque"}            //This subshader is performed when the machine's capacity exceeds 200 .Lod $Cgprogram//pragma surface processing function Illumination Model            /*the illumination model here is the Lambert model, i.e. ambient light + scattered light + specular highlight + radial light*/            #pragmaSurface Surf Lambert//Do you remember? Variable with the same name, note correspondence: Color corresponds to fixed4,2d sampler2dsampler2d _maintex;            Fixed4 _color; //input structure must be named input, its own input, note that texture coordinates must be written in front of the texture map variable name plus UV            structInput {float2 Uv_maintex;            }; //the input and output of the surf function is dead, that is, input and inout surfaceoutput, which is the output            voidSurf (Input in, InOut surfaceoutput o) {//It is very simple to use tex2d (map, texture coordinates) to remove a point on the texture, and then multiply the color by the modulation to get the resultFixed4 C = tex2d (_maintex, In.uv_maintex) *_color; //Albedo is the meaning of the main color (essentially a factor used to multiply, when the outside light is found on the surface, the reflected light = incident Light * Albedo)O.albedo =C.rgb; //get its transparency AlphaO.alpha =C.A;/*Additional Note: surfaceoutput This fixed structure, there are several values: struct Surfaceoutput {half3 Albedo;     The color of the pixel half3 Normal;   The normal value of pixels is half3 emission;    The divergence color of pixels half specular;       Pixel specular highlight half Gloss;       The luminous intensity of the pixel half Alpha; transparency of pixels};*/} ENDCG}//fallback hidden universe, means that this rollback is all inserted into this code! (Why should I say this:http://www.ceeger.com/forum/read.php?tid=31958)//Which means "Legacy shaders/vertexlit" is completely plugged in here .Fallback"Legacy Shaders/vertexlit"}▲ end of code

Precautions:

There may be an error message below Unity3d, you can modify the code based on this hint.

Step 1.3 : Create a new Material, named mnormaldif, and follow the configuration (note that the resources mentioned above are used here).

Summary: The algorithm used in this experiment is the same as that mentioned in the first introductory lesson, it is easy to remove the texture color, and then the end.

The second set of content:

Step 2.1 :

Code start Shader"custom/normalbumped"{Properties {_color ("Color", Color) = (1,1,1,1) _maintex ("Albedo (RGB)", 2D) =" White"{} _normal ("Normal", 2D) ="Bump"{}        //what is "bump" {}, through experiments can be known, it refers to the blue "bump" (rgba:0.5,0.5,1,0.5), reference:https://docs.unity3d.com/Manual/SL-Properties.html/*simply tell me how I know: I will change the code of the surf function below to only one o. Albedo = tex2d (_normal, In.uv_normal), and set Normal to none in the Inspector panel (thus using the default bump) to get

Of course, this is not the final effect of this experiment, just to illustrate that bump is blue (0.5,0.5,1,0.5) Just*/} subshader {Tags {"Rendertype"="Opaque"} LOD -Cgprogram#pragmaSurface Surf Lambertfixed4 _color;        Sampler2d _maintex;        Sampler2d _normal; structInput {float2 Uv_maintex;        FLOAT2 Uv_normal;        }; voidSurf (Input in, InOut surfaceoutput o) {fixed4 c= tex2d (_maintex, In.uv_maintex) *_color; //O.albedo = (tex2d (_normal, In.uv_normal)). RGB;O.albedo =C.rgb; O.alpha=C.A; //using Unpacknormal (tex2d (_normal, In.uv_normal)) method to obtain the normal direction after unpacking (what is unpacknormal, doubt)O.normal =Unpacknormal (tex2d (_normal, in.uv_normal)); } ENDCG} FallBack"Diffuse"}▲ end of code

Step 2.2 : Modeled after the first set of experiments, create a new material that specifies the main and normal vector maps. The effect is:

The third group of experiments: Introduction to this group of experiments Properties Type

Reference:http://blog.csdn.net/candycat1992/article/details/17152641

Step 3.1 : Create a new shader, named learnpropertiestype, create a new Material apply it.

Step 3.2 : Just add a few variables to the default learnpropertiestype Code, and we'll look at how they're written and in the editor in the same way.

code start Properties {_color ("Color", Color) = (1,1,1,1) _maintex ("Albedo (RGB)", 2D) =" White"{} _glossiness ("Smoothness", Range (0,1)) =0.5_metallic ("Metallic", Range (0,1)) =0.0//newly added parts:_myrange1 ("Rangednumber", Range (0, -)) = -_myfloat2 ("myfloat", Float) =1.1//the others do not say, only that the default value of cube and rect is "" {} This is OK, and if the vector does not write 4 dimensions, the default is 4 dimensions, the default channel is 1_myint3 ("MyInt", Int) =2_myvec ("Myvec", Vector) = (1,1,1,0.5) _MYVEC3 ("MYVEC3", Vector) = (1,2,3) _mycube ("Mycube", Cube) =""{} _myrec ("Myrec", Rect) =""{}/*description of the default in the official website: for Range and Float properties It's just a single number, for example "13.37". For Color and Vector properties It's four numbers in parentheses, for example "(1,0.5,0.2,1)". For 2D textures, the default value is either a empty string, or one of the built-in default textures: "White" (rgba:1,1, ), "Black" (rgba:0,0,0,0), "Gray" (rgba:0.5,0.5,0.5,0.5), "Bump" (rgba:0.5,0.5,1,0.5) or "red" (rgba:1,0,0,0). For non–2d textures (Cube, 2DArray) The default value was an empty string. When a Material does not has a cubemap/3d/array Texture assigned, a gray one (rgba:0.5,0.5,0.5,0.5) is used.*/}▲ end of code

Step 3.3 : See the effect:

Here are some learning notes.

An introduction to the performance of shaders:

Http://www.ceeger.com/Components/shader-Performance.html

Performance comparisons for regular shaders:

unlit< vertex illumination < diffuse < Highlight < Parallax;

Diffuse reflection:

Light intensity is only related to object, light source, that is, with the surface of the object and the angle of light becomes smaller, the lens movement and rotation does not change the light intensity.

The following two models are high-light computing models.

Phong Illumination Model:

specular=ks*lightcolor* (dot ( viewpoint direction, reflected light direction )) shininess

where the reflected light direction The calculation of R uses a clever formula.

Blinn-phong Illumination Model:

specular=ks*lightcolor*(dot(normal direction N, Viewpoint direction - Light incident direction), shininess

Above knowledge Point reference:http://www.cnblogs.com/bluebean/p/5299358.html

specular coloring requires a texture with an Alpha channel, which is used as the shininessabove.

Lambertian Reflection Model:

(refer to http://blog.csdn.net/u010922186/article/details/40680913)

Final Surface = radiated Light (glow color) + ambient light (several times of ambient light color) + Diffuse light (light source color *max( dot ( normal vector N, pointing to the light source vector L), 0several times) + Specular reflection (above-mentioned high-light model)

tangent space: refers to the The Z- axis is defined as the coordinate system on which the surface points outward, and the normal map is applied in the tangent space.

the normal map shader applies a normal map and a basic texture without alpha. The normal map shader only changes the normal direction in the diffuse shader. Therefore, it has nothing to do with the camera's movement and rotation.

Normal Map Highlight shader:

very simple, it is only the fusion of specular coloring and normal map coloring, which requires a basic texture (with an Alpha channel representing a specular map) and a method.

Parallax Diffuse shader:

the required resources have a basic texture (no alpha), a normal map, a height texture ( with parallax depth in the alpha channel)

What's the use of height? Reference:http://blog.chinaunix.net/uid-26651460-id-3198307.html

Parallax Specular map Shader:

the required resources have a basic texture (with Alpha for specular mapping), a normal map, a height texture ( with parallax depth in the alpha channel)

Decals Shader:

is to put a layer of texture on the original basis.

--Xiao Jiang cun son of Wen Jie [email protected]

Unity Shader Getting Started Tutorial (ii) the most basic diffuse and normal examples

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.