Unity Shaders and Effects Cookbook (5-1) Litsphere lighting Model

Source: Internet
Author: User
Tags mul

The fourth chapter reflects the end of the reflection, then continue to study the fifth-light model.

We are already familiar with the illumination model, before the various diffuse and specular highlights have been customized to the Illumination model function. I thought this chapter would be about the light model function that I said earlier, but it's not.

After reading the first section, the feeling is that it's like teaching me how to use the baked pictures. That means how to use a static picture to simulate the lighting effect.


The static images used in this chapter are created by Macrea, the free software. I downloaded it, but it won't work. After you have time to learn, now use the pictures with the book.

Macrea:

Http://pan.baidu.com/s/1eSO53SA

First introduced, because this time is the use of maps to simulate the lighting, so shader inside although there are custom lighting model functions, but this function on the color is not what to do, from the surf function, transmitted from, directly out of the.

Then it used the knowledge of the space conversion, temporarily not able to understand, and so I read and then to make up.


OK, go ahead.

Transfer from Http://blog.csdn.net/huutu http://www.thisisgame.com.cn

First create a scene, a light, a sphere. Since it says that the light model function is of little practical use, what does the light do? Here just add one, get a shadow. Only looks like a 3D effect.

Then create the material and create the Shader.

The Shader code is as follows:

Shader "Cookbookshaders/chapter05/litsphere" {Properties {_maintint ("Diffuse Tint", Color) = (1,1,1,1) _maintex ("Base ( RGB) ", 2D) =" White "{}_normalmap (" Normal Map ", 2D) =" Bump "{}}subshader{tags {" rendertype "=" Opaque "}lod 200cgprogram# pragma surface surf unlit vertex:vertsampler2d _maintex;sampler2d _normalmap;float4 _maintint;inline fixed4 Lightingunlit (Surfaceoutput s, fixed3 lightdir, fixed atten) {fixed4 c = fixed4 (1,1,1,1); C.rgb = c * s.albedo;c.a = S.alph A;return C;}        struct Input {float2 uv_maintex;float2 uv_normalmap;float3 tan1;float3 tan2;};                      void Vert (InOut appdata_full V, out Input o) {unity_initialize_output (input,o);            Tangent_space_rotation;            O.tan1 = Mul (rotation, unity_matrix_it_mv[0].xyz);                  O.tan2 = Mul (rotation, unity_matrix_it_mv[1].xyz); }void Surf (Input in, InOut surfaceoutput o) {float3 normals = Unpacknormal (tex2d (_normalmap, In.uv_normalmap)); O. Normal = Normals;float2 litsphereuv;litsphereuv.x = Dot (in.tan1, o.normal); litsphereuv.y = dot (in.tan2, o.normal); Half4 C = tex2d (_maintex, LitSphere uv*0.5+0.5); O. Albedo = C.rgb * _maintint;o. Alpha = C.A;} ENDCG} FallBack "Diffuse"}

Run results



A little explanation


#pragma surface surf unlit vertex:vert

First, the illumination model function is specified here as unlit, which I understand as unlight, is not affected by the illumination. In fact, this material is really not affected by the light.

And then the first time I met Vertex:vert. This is the specified vertex function. A vert vertex function should be given below. Before learning OpenGL, there are vertexshader and Fragmentshader. So I guess there should also be frag fragment function.



Inline Fixed4 lightingunlit (surfaceoutput s, fixed3 lightdir, fixed atten) {fixed4 c = fixed4 (1,1,1,1); C.rgb = c * s.albed O;C.A = S.alpha;return C;}
In the unlit illumination model function, no processing is done and no illumination is accepted.


void Vert (inout appdata_full v,out Input o) {unity_initialize_output (input,o); Tangent_space_rotation;o.tan1=mul (ROTATION,UNITY_MATRIX_IT_MV[0].XYZ); O.tan2=mul (rotation,UNITY_MATRIX_IT_MV[1 ].XYZ);}

In the vertex function, in order to correctly retrieve the spherical map, we need to multiply the tangent rotation matrix rotation by the reversal model view of the current model, in order to get the correct vector and use it for our spherical map.

I do not understand the meaning of the present, and then to make up the back.




Project Package Download:

http://pan.baidu.com/s/1skRWC0l


Unity Shaders and Effects Cookbook (5-1) Litsphere lighting Model

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.