In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail what is the lighting principle of graphics in unity3d. Xiaobian thinks it is quite practical, so share it with you as a reference. I hope you can gain something after reading this article.
First of all, in physics we know that the color of an object is actually the color of the light it reflects. Light is white, but its whiteness is presented by a mixture of many different colors and different frequencies of light, that is, spectrum. Red objects reflect more red light, while blue and green light are absorbed. White clothes reflect white light and black clothes absorb white light, which is why white clothes are slightly cooler in summer.
And if we want to achieve illumination, we must simulate the reflected light of the object, so that the object has a light and dark relationship, with a bright side and a dark side. Reflected light from objects can be divided into three simple categories:
1. Diffuse reflection
When light shines on an object, the object will reflect its own color, which is the most basic reflection, and it will produce a light and dark relationship according to the direction of light (the light and dark relationship is that some places are bright and some places are dark, not shadows, shadows are projected by other objects onto you, and you have dark shadows on your body. This article does not consider projecting shadows. Because an object, such as a cube, does not make its own shadow, only put a ball in front of it, the shadow of the ball will be reflected on the cube), the reflection direction is perfect in all directions, which is the origin of "diffuse."
Let's take a single slice and analyze it.
N is the normal to the object, L is the direction vector of incident light, le is the color of incident light, then the color intensity lj of reflected light satisfies:
lj = le cos Kd (θ) or lj = le Kd (L·N)
where Kd is the color of the object itself, such as orange.
The formula translates to this: the color you reflect is a mixture of your natural color and the color of the light that hits you.
For example, if you wear white clothes but red light shines on you, the reflection is red. The mixing ratio of your color and the color of the light is determined by θ. When θ is 0, that is, the light is shining hard on you, then you are very bright red. When the light just grazes you parallel to you, then you have no color, black. The more positive the light is for you, the brighter you are, otherwise the darker you are.
Note: All formulas in this article must have normalized light direction vectors and normals. The RGB range of color vectors is 0~12. Specular
This is not necessary.
Specularity is when smooth objects have a small area of extra brightness that approaches specular reflection.
The ball above, the white circle in the middle is the highlight. The same ball, upper left light and lower right dark, such a light and dark relationship is diffuse reflection.
The highlight color intensity formula is:
V is the viewing angle vector, because highlights are dependent on viewing angle, and as you walk around a smooth sphere, you'll notice that the specular reflection of its highlights changes. Ks is the specular color of the object, usually white, of course, if you want to make the object highlight blue can also be very magical. R is the direction vector of the reflected light, which is the symmetry vector of L incident light with respect to normal N. spec is the intensity of reflection. See the three balls in the picture above.
The understanding of this formula is that the color of specular reflection (highlight) is the superposition of the color of incident light and the color of specular reflection. The intensity of specular light depends on the viewing angle and spec.
When V and R are parallel, the reflected light hits your eyes and explodes. When V and R are perpendicular, no light hits your eyes, there is no specular reflection, no highlights. 3. Ambient Light
Assuming that there is only one ball and one light source in the scene, then according to the current formula, the place with light has color and brightness. Where there was no light, it was pitch black.
But some people think that darkness is not very good, should also be able to see something, so they added an ambient light, so that the overall brighter, so that the dark part of some color.
The calculation formula of ambient light color is:
lj = la Ka
la is the color of the ambient light, Ka is the ambient light coefficient of the object, in general Ka = Kd = the color of the object itself, only the highlighted Ks is not the color of the object itself, but white.
Light Calculation Formula
To sum up, the color formula of an object with a light and dark relationship under illumination is as follows:
This is the highlight calculation result of all light sources + diffuse reflection calculation result of all light sources + ambient light color
There is only one ambient light in the scene, and there may be several light sources, so highlights and diffuse reflections count for each light source and then add up.
A labeled version of this formula:
where E is the angle vector, which is V.
Note:
When we calculate illumination, we calculate it in the camera coordinate system space, i.e. image space (refer to my coordinate system space transformation for those who do not understand spatial transformation). So E (angle vector) is always (0, 0,-1), because if we look straight ahead we are looking at Z=1, and its inverse is-1.
Why camera space?
Because illumination calculations involve directional vectors L, E, N, R, we need to transform in affine space (i.e., pre-perspective space, including model, world, image 3 spaces) because transforming from image to perspective space distorts the image and loses information. Model, world, image 3 spaces calculate illumination is OK, this article chooses image.
In the image space, normal and other direction vectors have to be transformed into image space, but it should be noted that the transformation of normal is only rotation, no displacement and scaling!!! Because if the normal displacement, that is, in the normal vector plus a displacement vector, if the two vectors are not parallel, then the direction of the vector and the original normal will not be consistent!
When E and L are not on one side, that is, we are looking at the front of the object, light shines from the back of the object, we are invisible, it is black, this situation is not calculated, directly skip. When E and L are on the same side, but the normal N is on the other side, the calculation is also negative, no, so we have to take the normal and calculate again. In short, it is to ensure that the final color RGB is in the range of 0~1.
We know the illumination colors of the three vertices of the triangle, but what about the colors of the pixels on the screen that fall inside the triangle?
A: Interpolation. In rasterization we compute the Z-interpolation over the plane equations for the interior points of the triangle. All other interpolation can be done in this way.
According to different interpolation methods, shade is divided into two categories:
1. Gouraud Shading
The colors of the interior points of the triangle are interpolated according to the colors of the three vertices, regardless of the normals of the interior pixels, so it is convenient to calculate and fast. But the accuracy was a little off. For example, if the light source is located in the center of the triangle, the colors of the three vertices will be darker because they are far away from the light source, and the colors of the middle points will also be darker. But in fact, the middle point is closer to the light source and should be brighter!
The above figure is the color interpolation method, that is, construct a 3-dimensional plane equation, replace Z with R, G, B to construct 3 planes, and then substitute x and y of pixel points to find 3 Z(i.e. R, G, B).
2. Phong Shading
To solve Gouraud's accuracy problem, we can recalculate the illumination color at each pixel in the inner pixel. So we need to compute the normal N of the interior pixel by interpolation. Then insert the above lighting equation to calculate the color of the point. This is the most accurate, but slightly more computationally intensive. The current mainstream engine is basically the default Phong.
Normal interpolation constructs three plane equations, replacing Z with x, y, z of normal N respectively.
Substitute x,y for the pixel, find Nx, Ny, Nz, and you get N for that pixel.
About "unity3d graphics lighting principle is what" this article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.