Unity lighting model


(1) Lambert lighting model:

            The Lambert lighting model is currently the simplest and most general lighting model for simulating diffuse reflection. It is defined as follows: The brightness of the model surface directly depends on the cosine of the angle between the light vector and the surface normal. value. The ray vector is the direction from which the light enters from this point, and the surface normal defines the orientation of the surface.
        If the diffuse light intensity is set to Diffuse, the incident light intensity is I, and the angle between the light direction and the normal is θ, then the Lambert lighting model can be expressed by the following formula: Diffuse = I * cosθ. Further, we can obtain the angle between the two direction vectors by point multiplication. The incident light direction is set to L, and the normal direction is set to N. If the light direction vector and the normal direction vector are both unit vectors (this This is why we need the normalize operation when writing shaders), then the cosine value of the angle between them can be expressed as: cosθ = dot(L, N), the final diffuse reflection light intensity formula, which is Lambert The lighting model can be expressed as:
       Diffuse = I  * dot(L,N)

Vertex by Vertex:
Pixel by pixel:

        Why does the pixel-by-pixel calculation get better results, because the direction of the light we take pixel by pixel is the same, and the normal direction is also passed through the vertex shader in the previous step. If the pixels and vertices correspond, it is not every Will the pixel calculation result be the same? However, in fact, pixels and vertices do not correspond. This is the legendary rendering pipeline. The results calculated at the vertex stage are not directly passed to the pixel shader, but after a series of interpolation calculations. The normal direction passed by the shader only represents the vertex normal direction of this vertex, and at the pixel stage, the normal and other parameters corresponding to this pixel are equivalent to the result of interpolation of several surrounding vertices. We use the normal direction and the lighting direction corresponding to this pixel to calculate the color value of the pixel under the lighting conditions, instead of calculating the color first and then interpolating to get the result.

(2) Half-Lambert lighting model

        When the above shader calculates the lighting, when we calculate the dot product value of the normal direction and the light direction, the result may be negative, and the Lambert lighting model handles this situation, the dot value is negative, indicating that the point It will not be illuminated by light, so for this light source, this point has no light, directly use max(0, diffuse) to set all the positions that should not be illuminated to black.
        When the Lambert light came out, it seemed that there was no such high-tech technology, so someone came up with a tricky technology (it is said to be "Half-Life"), which not only guaranteed the lighting results calculated by the Lambert model Greater than 0, and the overall brightness is improved, so that the indirect light-receiving surface is not simply set to black. This is a transformation that is often seen in the field of graphics, interval transformation, from (-1,1) to (0,1), if you don't consider meaningless negative values, it can also be said to be transformed from (0,1) to (0.5,1). The method is simple, multiply by 0.5 and add 0.5. In this way, where the original brightness is 1, multiplied by 0.5 becomes 0.5, and when 0.5 is added, it becomes 1, and where the original light intensity is 0, it becomes 0.5, and the original negative number can also be guaranteed. is greater than 0

(3) Phong lighting model

The main part of the Phong lighting model is the calculation of highlights. First look at the following picture:
        Ideally, the light emitted by the light source, through specular reflection, is observed in the direction of the reflected light, and the observer can receive the most reflected light, then the angle between the observer and the direction of reflection determines how much of the highlights can be observed. . The larger the angle, the smaller the highlight, and the smaller the angle, the greater the highlight. Another factor that affects the size of the highlight is the smoothness of the surface. The smoother the surface, the stronger the highlight, and the rougher the surface, the weaker the highlight. L represents the light source direction, N represents the vertex normal direction, V represents the observer direction, and R represents the reflected light direction. First, the direction R of the reflected light needs to be calculated. The direction R of the reflected light can be obtained from the direction of the incident light and the normal vector. R + L = 2dot(N,L)N, and then R = 2dot(N,L)N - L. For the derivation of R calculation, you can see the following picture:
        The Fung reflection model formula is given below: I(spcular) = I * k * pow(max(0,dot(R,V)), gloss) , where I is the color vector of the incident light, k is the specular reflection coefficient, gloss for smoothness. From the above formula, we can see that the specular reflection intensity has an exponential relationship with the cosine value of the reflection vector and the observation vector. The index is gloss, which reflects the smoothness of the surface of the object. The larger the value, the smoother the object. The more concentrated the reflected light, the greater the attenuation of the light when it deviates from the reflection direction. The highlight phenomenon can only be seen when the line of sight direction is very close to the direction of the reflected light. The specularly reflected light forms a brighter and smaller spot; the smaller the value is , indicating that the rougher the object, the more scattered the reflected light, the wider the area where the light spot can be observed, the larger the light spot and the weaker the intensity

(4) Blinn-Phong lighting model


        Blinn-Phong lighting introduces a concept, a half-width vector, denoted by H. The calculation of the half-angle vector is simple. The half-angle vector can be obtained by adding the light source direction L and the line of sight direction V and normalizing it. Phong lighting compares the angle between the reflection direction R and the line of sight direction V, and Blinn-Phong instead compares the angle between the half-angle vector H and the normal direction N. The calculation complexity of the half-angle vector is much simpler than calculating the reflected light, so the performance of Blinn-Phong is much higher, and the effect is similar to that of Phong lighting, so the lighting model of the fixed pipeline in OpenGL is the Blinn-Phong lighting model. The BlinnPhong lighting model is as follows: I(spcular) = I * k * pow(max(0,dot(N,H)), gloss) , where I is the incident light color vector, k is the specular reflection coefficient, and gloss is the smoothness.
(5) Application
Application: One map (knife, other clothes, etc.) is realized, part of the knife is specular, and the rest is diffuse
        Take a knife for example, the blade is metal and the handle is wood, then only the blade is suitable for this type of shader. Perhaps our simplest idea is to split the knife into two parts, using Specular for the blade and Diffuse for the handle; but this approach is cumbersome, and an object can only be rendered after two drall calls. Therefore, the smart predecessors can always think of a good way, and the simplest kind of texture in the next-generation type game was born - the specular map (channel).

        The so-called highlight map, or the highlight channel, is to store the highlight information of the image in a grayscale image or directly in the channel of the map when making the map. If you don't need Alpha Test, you can directly put the highlight channel. The alpha channel of the Diffuse map. When we calculate in the shader, by sampling, we can get whether the position corresponding to each pixel in this texture has highlights. In this way, in the Fragment Shader, the Mask value can be directly multiplied by the specular to render different textures on the same model through a material. For example, in the channel, 0 means no highlight, 1 (255) means the highlight is the strongest, then, we can give 0 when making this picture where we don't need highlight, and paint the color if we need highlight, the closer the color is. White, the stronger the highlights. After knowing the principle, we can find a character model texture, and then add a channel to the RGB format texture in PhotoShop as a highlight channel, and then cut out the parts that need highlights, and set the other parts to black











































Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324520861&siteId=291194637