Unity3d Shader Study Notes

Source: Internet
Author: User
Tags reflection

Original address: http://my.oschina.net/u/138823/blog/181131

What?? Shader, it looks like a high-level look, yes, this is a must for unity advanced advanced. So, brother I am here to write down my study on the official website of some experience.

This is one. It mainly introduces some surface shaders knowledge. Specific people can also go to the official website (below) to learn.

Http://docs.unity3d.com/Documentation/Components/SL-SurfaceShaders.html

I. Concept article

1. Benchmark: Shader in Unity is not a unique language, but a way of generating code that simplifies low-level and complex shader programming. But at the same time you still have to use CG/HLSL to write.

2. Principle: Write a function, take Uvs or some data as the entrance, then take surfaceoutput as the output. There are also different attributes in the surfaceoutput structure. So for this function, his execution will generate the shader of vertex and pixel, and pass some rendering paths.

3. Structure: Output structure:

struct Surfaceoutput {
    half3 Albedo;
    Half3 Normal;
    HALF3 emission;
    Half specular;
    Half Gloss;
    Half Alpha;
};

Albedo, which is the color value of the diffuse reflection.
Normal, normal coordinates
Emission, self-luminous color
Specular, specular reflection coefficient
Gloss, Gloss coefficient
Alpha, Transparency factor

II. Programming Rules

1. To write in Cgprogram. ENDCG in the Subshader of the block. Do not write in the pass.

2.shader name can be repeated, after repetition, to later shader-based.

3. Instruction Details:

#pragma surface surfacefunction Lightmodel [Optionalparams]

=>surfacefunction, there's nothing to say, it must be the function name.

The =>lightmodel is the light model used. You can write it yourself or use built-in such as Lambert and Blinnphong.

=>optionalparams: Optional parameters, a bunch of optional include transparency, vertex and color functions, projection decals shader and so on. Specific use can be carefully selected.

There is also a feature here. Add #pragma debug [content] in the cgprogram of surface shader. Can be seen in the file that compiles the results. Write as much as you can. But try to do it under the other kind of shader.

Iii. Examples of learning:

1.Simple:

Shader "Example/diffuse simple" {
subshader {
Tags {"Rendertype" = "Opaque"}
C     Gprogram
#pragma surface surf Lambert
struct Input {float4 color:color;  };   
void Surf (Input in, InOut surfaceoutput o) {
O.albedo = 1;    
 }     
ENDCG
}
Fallback "diffuse"}
The first one. Line to:

First line: Write a name. This also has the fastidious. The left side of the slash is the group of its parent class, none is added, there is an accumulation, the right is the real name. Note that these shader names are not like C # scripts and do not require the same file name as the shader name.

Line second to third: The next step is to add content to the Subshader, Subshader can have more than one. Then the last tags, here only use Rendertype this, and the other rendering order, forcenoshadowcasting. such as These phases are not currently studied.

Line four: The previous instruction, which specifies that the response method is surf and uses the Lambert illumination model. This must be there.

Line five: This structure, remember the name can not be changed, only for input. Inside a four-element color value (RGBA).

Lines seventh through Nineth: The first argument, the pure input of the above structural body parameters. The second parameter, inout, means that it can be an input parameter or an output parameter. Albedo according to the previous introduction, is an RGB value, if given a 1, is actually FLOAT3 (1,1,1), is reflected out of the color of white, if 100, is to strengthen the reflection intensity, and will not change its color. is 0 or negative for a similar reason.

Finally fallback, the rear is the self-brought shader, can use their own custom good. The meaning of this sentence is that if all subshader are not supported on the current video card, the diffuse is returned by default.

2.Texture:

Shader "Example/diffuse Texture" 
{Properties {_maintex ("Texture", 2D) = "white" {}} subshader {
      Tags {       "Rendertype" = "Opaque"}
      cgprogram
      #pragma surface surf Lambert
      struct Input {float2 uv_maintex;};       Sampler2d _maintex;
void Surf (Input in, InOut surfaceoutput o) {
          O.albedo = tex2d (_maintex, In.uv_maintex). RGB;
      ENDCG
    } 
    Fallback "diffuse"
  }
This example. Actually just the first one based on the addition of a 2D attribute display named texture. The following resolves:

First Bold: Adds a property called _maintex, specifying that it is of type 2D and displayed as texture. The "white" piece is not a build-in, it is the name of some textures of unity, not the mere color name. This means that when the default is displayed as a material called white. If you change to red (that is, use a material called red, if there are other names), the effect is as follows:

Second blackbody: Uv_maintex. This is a big mystery, the UV start refers to the UV value of the material behind, so the UV will not change, the latter can be based on the name of the beginning of the dynamic exchange. And oh, this is similar to the _maintex of the naming method is CG recommended, in fact, do not underline also OK.

The third blackbody: This sampler2d, can be understood as referencing a 2D Texture. Because the following tex2d function requires this type. So the name behind this should be the same as in the properties.

The fourth black body: tex2d, this thing is based on the corresponding material on all points to find the texture information on the specified 2DSample, where its RGB information is required, it is typed to assign its reflection value. Therefore, in the case of a material map, to show the picture, or to reflect the original image of the RGB value.

3.Normal Mapping

Shader "Example/diffuse Bump" {Properties {_maintex (         Texture ", 2D) =" White "{}                _bumpmap ("
      BumpMap ", 2D) =" Bump "{}        } subshader {Tags {" rendertype "=" Opaque "}
        Cgprogram #pragma surface surf Lambert struct Input {float2 Uv_maintex;
      FLOAT2 Uv_bumpmap;
      }; Sampler2d _maintex;              sampler2d _bumpmap;            void Surf (Input in, InOut surfaceoutput o) {o.albed o = tex2d (_maintex, In.uv_maintex). RGB;                  o.normal = Unpacknormal (tex2d (_bumpmap, In.uv_bumpmap));
                 }ENDCG} Fallback "Diffuse"} 
This example has a bump map to achieve something like a nice bump effect.

First bold: Add a 2D type of material, the default is bump. (i.e. with a bump effect).

Second Blackbody: the previous collector. Collect the material from the top.

The third blackbody: There is fastidious, this unpacknormal is unity with the standard decompression normal use, so-called decompression, I temporarily learned only the normal interval to transform. Since tex2d (_bumpmap, In.uv_bumpmap) is removed from the compressed [0,1], it needs to be turned into [ -1,1]. This function uses the RGB normal map for the mobile platform or OpenGL ES platform, and the other uses the dxt5nm map. You can write it for yourself. Also found some information on the Internet, the following reference:?

1 2 3 4 5 6 7 8 9 10

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.