Interpretation of CG compiling shader Series 10 in unity-smooth mirror reflection (Feng's coloring)

Source: Internet
Author: User
Tags pixel coloring

In the previous article, we completed the most basic mirror reflection coloring tool, which is a vertex-by-vertex coloring (Per-vertex lighting) under a single parallel light source, also known as Gouraud shading ). This article, as a more smooth method of reflection in the future, is called Phong Shading (Per-pixcel lighting)

For pixel-by-pixel coloring, do not confuse Feng's coloring with Feng's reflection model. I mentioned the Feng's reflection model before, feng's reflection model is a model proposed to make computer simulation close to the real surface gloss of an object, that is, environmental light (virtual) + diffuse light + mirror light = surface color.
Vertex-by-vertex coloring, so the name is related to the vertex, that is, in our Vertex coloring tool, the color of each vertex is calculated based on the incident vector L, normal vector N, and observation vector V on each vertex, then it is passed to the subsequent links for coloring. we can imagine that because the vertex is discrete and the fragment is continuous, it is easy to understand that the coloring effect is not smooth.
In this case, the pixel-by-pixel coloring corresponds to interpolation of the normal vectors and coordinates in the fragment coloring tool (if you cannot understand interpolation, please Baidu ), in this way, the discrete colors calculated from discrete vertices become continuous and smooth. This is the essence of Feng's coloring.
Changing the color of Guoluo in the previous article to Feng's color is simpler. We can directly calculate the ambient light, diffuse light, and mirror light to the part coloring tool to complete the modification. Why do they not start to be written directly in the fragment shader to make the viewer have an impression of these two different coloring methods.
Before starting the modification, we only need to understand one principle: the calculated method vectors, observation vectors, and incident vectors obtained in the vertex coloring tool are directly transferred to the clip coloring tool by color, therefore, the input parameters of the fragment shader basically only contain colors. Now we need to move the calculation process to the fragment coloring tool, so the normal vector, observation vector, and incident vector need to be passed to the fragment coloring tool, instead of directly passing a color.
The observation vector and the incident vector are calculated by the World camera coordinates and the world light source vector, respectively. These two information is not transmitted to the vertex shader by meshrenderer, instead, the uniform parameter is directly embedded in CG, so it can be directly obtained in the fragment shader. Therefore, we only need to pass the coordinates of vertices and the transformed normal vectors.
Therefore, the output struct (also the input struct) of our vertex shader should be changed:
// Define the output struct of vertex coloring/the input struct of fragment coloring // remove the world coordinate and normal vector of the colored vertex. The semantics here uses two sets of texcoord, the texcoord here is used by ourselves. It is different from the semantics used in vertex coloring machine input. struct vertexoutput {float4 pos: sv_position; float4 posworld: texcoord0; float3 normaldir: texcoord1 ;};


The fragment coloring er receives a certain degree of interpolation after the normal vector, and the process is re-unitized. Why can interpolation be completed after two unitization, and my linear algebra is not very good, you can study it on your own.

Then, move the computing process to segment coloring. The modified code of Series 9 is as follows:
Shader "Custom/phoneshadingspecular" {properties {_ color ("diffuse material color", color) = (1, 1, 1) _ speccolor ("specular Material color", color) = (,) // The gloss degree of the material surface. According to the previous article, when this parameter is infinite, the material will not generate a mirror reflection _ shininess ("shininess", float) = 10} subshader {pass {tags {"lightmode" = "forwardbase"} cgprogram // defines the vertex shader and fragment iterator entry # pragma vertex vert # pragma fragment frag/get the material color defined in property is uniform float4 _ color; uniform float4 _ speccolor; Uniform float _ shininess; // position or direction of the light source // uniform float4 _ worldspacelightpos0; // The color of the light source (from "lighting. cginc ") Uniform float4 _ lightcolor0; // defines the input parameter struct of the vertex shader. // We only need the position of each vertex and the corresponding normal vector struct vertexinput {float4 vertex: position; float3 normal: normal;}; // defines the output struct of vertex coloring/the input struct of fragment coloring // removes the world coordinate of the colored vertex and the normal vector struct vertexoutput {float4 pos: sv_position; float4 posworld: texcoord0; float3 normaldir: texcoord1 ;}; // vertexoutput Vert (vertexinput input) {vertexoutput output; // transformation matrix from the object coordinate system to the world coordinate system // _ object2world and _ world2object are built-in uniform parameter float4x4 modelmatrix = _ object2world provided by unity; // The float4x4 modelmatrixinverse = _ world2object transformation matrix from the world coordinate system to the object coordinate system; // The method vector n changes to the object coordinate system output. normaldir = normalize (float3 (MUL (float4 (input. normal, 0.0), modelmatrixinverse); // convert the vertex coordinate to the world coordinate system output. posworld = MUL (modelmatrix, input. vertex); // International Convention, vertex change three-step output. pos = MUL (unity_matrix_mvp, input. vertex); Return output;} // fragment shader, which uses the output parameter of the vertex shader as the input parameter float4 frag (vertexoutput input) of the fragment shader ): color {// accept the normal vector passed by the vertex coloring tool and the vertex world coordinates. // The normal vector must be normalize again here. // although normalize has been one float3 normaldirection In the vertex coloring Tool = normalize (input. normaldir); float3 worldposition = input. posworld; // The observation vector V is subtracted from the camera coordinate and the vertex coordinate vector // here, the vertex coordinate is changed to the world coordinate float3 viewdirection = normalize (float3 (float4 (_ worldspacecamerapos, 1.0)-worldposition )); /* the following part can be moved directly * // The incident vector l of the parallel light source is directly given by uniform_worldspacelightpos0 float3 lightdirection = normalize (float3 (_ worldspacelightpos0 )); // calculate the reflected light in the mirror. float3 specularreflection = float3 (_ lightcolor0) * float3 (_ speccolor) * POW (max (0.0, dot (reflect (-lightdirection, normaldirection ), viewdirection), _ shininess); // float3 diffusereflection = float3 (_ lightcolor0) * float3 (_ color) * max (0.0, dot (normaldirection, lightdirection); // obtain the environmental light float3 ambientlighting = float3 (unity_lightmodel_ambient) * float3 (_ color); // Add the above three RGB color vectors according to the Feng's reflection model, then add a: Return float4 (ambientlighting + diffusereflection + specularreflection, 1.0);} endcg} fallback "diffuse "}

Finally, we compared the sphere with the sphere in the previous example:




The left part of the figure is Feng's color, and the right part of the figure is the Guoluo color of the previous example. It can be seen that Feng's is still very powerful, and that the reflection model and pixel-by-pixel color are his names.

Interpretation of CG compiling shader Series 10 in unity-smooth mirror reflection (Feng's coloring)

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.