Interpretation of the shader Series 6 Written by CG in unity -- diffuse reflection

Source: Internet
Author: User

If the content of the previous series of articles is too long and interesting, I am sorry. Due to the time, the mixed content has not been completed yet. I will try again later. Now I am beginning to discuss reflection content.

Refraction and reflection

In the physical world, the reflection and refraction of light often exist at the same time. The light source is injected into a material in a vacuum or air, and the light enters this material while it is refraction, the degree of refraction is related to the refractive index of each media, so that the optical transmission line deviates from the original line;

Then, if the light passes through the surface of different transmission media, it will bounce back like a table tennis ball. Our human eyes can see things because things reflect the light source. If a substance cannot reflect light, or we can't see anything without a light source. For different materials, the higher the passing property of light, the less obvious the reflection, such as glass (but glass also has a certain amount of reflection, otherwise we will not see the glass, in fact, we can see); the lower the light passing rate, the more obvious the reflection. For example, on the water surface, we can see the fish in the water (from the water to the skin of the fish, the fish's skin has a diffuse reflection), you can also see your reflection (the water surface has a mirror reflection ).


The difference between the mirror reflection and the diffuse reflection is that, when the surface of an object is bounce back, the smoother the plane is, the closer the plane is to the mirror reflection, and the rougher it is, the more diffuse reflection. The vast majority of things we see in real life are superficial, so they are diffuse reflection.
Before discussing the diffuse reflection, you must go back and review the mirror reflection and refraction in our physical science, because the diffuse reflection can be split into countless tiny mirror reflections (just like a circle can be seen as a regular polygon with numerous sides), after all, the game world is also designed to simulate the physical world, the game engine must comply with physical rules. Draw a picture to recall:


The picture above is not very good. The angle between the incident light and the normal plane is the incident angle, the angle between the reflected light and the normal plane is the reflection angle, and the incident angle is always equal to the reflection angle.
Similarly, for uneven media intersections, we can be infinitely subdivided into countless legal planes, for the reflection of each surface point, we can create a plane along the current point and tangent to the surface of the object at this point to obtain a normal plane. Therefore, the law of mirror reflection can be applied directly to diffuse reflection. Let's see:
When we analyze the reflection of a certain point on the surface of Medium B, we will make a plane at this point and the surface is tangent to this point, the normal plane, normal, angle of incidence, and angle of reflection at this position are also obtained.
Now let's review it here. In the previous series, we have removed the normal and normal planes. We will see Medium B for our 3D object, so the black curve part is its surface, the normal orientation of each point on the surface is different, because the cut of each point is not necessarily parallel to the plane on the surface, therefore, each vertex transmitted in the mesh information in CG has a normal value, that is, the normal vector.
The normal vector is the unit vector of the tangent point in our normal to the direction of Medium.
The reflection process is good. Next we will replace the incident light and normal as the incident vector L (the direction from the surface to the light source is positive) and the normal vector n. In this case, the amount of diffuse reflection is determined by the incident vector I and the normal vector N at each vertex position: diffuse = L · n this process is a point multiplication between vector I and N. The process is diffuse = | L | * | n | * Cos variance (L, n)
Because we directly discuss the incident vector and the unit vector of the normal vector, so the two absolute values are 1, then the diffuse reflection duuse = cos attention (L, n)
From this, we can obtain the diffuse reflection intensity diffuse value of the incident light of the unit vector: [-] From the cosine curve, diffuse is 0 ~ At the angle ~ 90 ° is positive, 90 ° ~ 270 ° is negative, 270 ~ 360 ° positive
For cos degrees (L, n) over 90 °, we think that the incident light is already on the other side of the media surface. In this case, we do not consider that generally, the diffuse reflection is performed on the outer surface.
Therefore, when the angle of diffuse is greater than 90 °, the reflection volume is considered to be 0, so we modify this formula:
Diffuse = max (0, cos variance (L, n ))
That is, the cosine of the angle between the incident vector and the normal vector is set to the maximum value of 0, which ensures that there is no negative number in the reflection volume.
Next, let's go back to the CG world from the physical and mathematical worlds.
Write a diffuse shader Using CG. before writing our first single-source diffuse shader, first describe the source of the parameters we need: 1. the position of the light source is given by the built-in uniform parameter _ worldspacelightpos0 of unity. Therefore, we can calculate the incident vector of each vertex. 2. the color of the light source is provided by the uniform parameter _ lightcolor0 3. the normal semantics in the input parameters of the vertex coloring machine is used to obtain the color of the incident vector, normal vector, and light source, we can calculate the amount of reflection at each vertex position in the vertex coloring tool, and multiply it with the color of the light source and the color of the material to obtain the actual color of the final surface coloring. User-Defined parameters: 1. Material color, defined by shader's property





Interpretation of the shader Series 6 Written by CG in unity -- diffuse reflection

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.