Chapter 21 Unity Light Source

Light sources are an essential part of every scene. In addition to illuminating the scene, light sources can also produce shadow effects.

There are four types of light sources in Unity :

1. Directional Light: Directional Light is used to simulate sunlight. Directional light can shine anywhere.

2. Point light source: Point Light is used to simulate the illumination effect of a light bulb.

3. Spot Light: Spot Light is used to simulate the effect of spotlight illumination.

4. Area Light: Area Light is invalid in "real-time lighting" mode and is only used in "baked lighting" mode.

In the default scene, a Directional Light will be automatically added to us, which is equivalent to a sun.

We can select the light source to view its Inspector view. The most important component of the light source game object is the Light component. The screenshot is as follows

Next, let’s briefly explain some properties of the Light component:

Type: The current light source type. Optional values ​​are Directional, Point, Spot, and Area.

Color: Use the color picker to set the color of the glow.

Mode: Specifies the light source mode. The available modes are Realtime, Mixed and Baked.

Intensity: Set the brightness of the light source.

Indirect Multiplier: Use this value to vary the intensity of indirect light.

If it is a Spot spotlight (the Type property of the Light component is Spot), the effect is as follows:

This type of light source is similar to a cone. It has its own two properties: "Range" and "Spot Angle". The former represents the distance of the light irradiation, and the latter represents the opening and closing angle of the cone, which is the range of the light irradiation. As follows:

If it is a Point light source (the Type property of the Light component is Point), the effect is as follows:

This type of light source has a "Range" property, which represents the distance the light shines, which is the radius of the sphere.

The last one is Area light, the effect is as follows:

This type of light source is defined by a rectangle in space. Light is emitted uniformly in all directions over the surface area, but only from the face of the rectangle. There is no manual control over the range of an area light, but the intensity will fall off inversely as the square of the distance as you move away from the light source. Since lighting calculations consume a lot of performance, area light sources cannot be processed in real time and can only be baked into light maps.

Light sources are very easy to use in Unity. Light sources can be added to the scene through the menu bar GameObject > Light. If you enable Scene view lighting (Sun button on the toolbar), you can preview the lighting effects as you move the light object and set its parameters. What needs to be noted here is that the directional light can usually be placed anywhere in the scene, illuminating the entire scene along the positive direction of its own Z-axis. There is no limit to the irradiation range, so the directional light has nothing to do with the position, but has something to do with its own rotation (directly Affects the shadow position of an object). The spotlight has a position and a direction, but its irradiation range is limited. Point light sources are similar to spotlights. They are both local light sources and are mainly used to process light and shadow effects on some local details in the scene.

There are many techniques for using light sources. For example, if a light source must be used in the game scene, but you do not want some game objects to be affected by the light source. We can create a layer and add these game objects to the layer. Find the Culling Mask attribute in the light source's inspection panel and select the layer in the drop-down list. Then all game objects in this layer are no longer affected by the light source.

Regarding the light source, don't ignore "ambient light". It is a true global illumination that can illuminate game objects from all directions. In other words, it will not produce shadow effects. We can find the ambient light settings in the Lighting window, which by default comes from the skybox material map (the essence of light is color). Open the menu bar "Window" -> "Rendering" -> "Lighting", click and select "Environment" in the new window that pops up.

Environment Lighting This item provides ambient lighting for objects. The color source can be the Skybox shader set above, which can be a fixed color or a gradient color. 2D games generally do not require any light source, only ambient light needs to be set. By the way, let's talk about the Environment Reflections below. This item controls the environment light reflection settings. It is also derived from the Skybox skybox material ball. It provides a specular reflection effect and is an important lighting effect in physically based rendering (PBR). In other words, if there is a mirror in the scene, what appears in the mirror is the skybox. However, if this mirror is in a room, it would be unreasonable for the mirror to look like a sky box. Unity provides Reflection Probe technology to capture images of the surrounding scene and store them as cubemaps. The mirror in the room can display the surrounding scene through a reflection probe. That is, when an object is within the range of the reflection probe, the object will reflect the environmental information within the range of the reflection probe. When there is no reflection probe in the scene, objects with reflection in the scene will directly reflect the environmental information generated by the skybox.

Here, I will continue to introduce the sky box. As we introduced in the DirectX course, it actually simulates the sky in the real world. The simplest skybox is just a cube map. The skybox in Unity is essentially a material ball. We can download some free skyboxes from the Unity Asset Store, such as the one below.

After downloading, we can import it into the current project, as shown below

 

Among them, there are many skybox shaders in the "Materials" directory, and "Scenes" is a scene provided to us. We can click into "Scenes" to take a look.

Double-click the "Demo.unity" scene file, as shown below

The effect after running is as follows

Next, we go back to the "Materials" directory to view

The picture above shows a lot of skybox shaders, and their essence is just a map. How to use these skybox shaders? We return to the previous default scene, then click the menu bar: Window > Rendering > Lighting, and then select the Environment option panel, as shown below

All we have to do is drag our shader into the red box above.

Our previous scene looked like this, which is the default skybox (Default-Skybox).

What it looks like after changing it to "FS000_Day_01"

Next, we introduce the light map ( LightMap ) . What everyone needs to understand is that the calculation of lighting effects consumes a lot of hardware performance. If we have to perform lighting calculations on all objects in the game at all times to obtain their performance effects, this is obviously not the best way. . Therefore, we can " bake " the lighting information into the map in advance. After the game is running, we no longer perform real-time lighting calculations, but directly obtain the information from the object's light map to achieve the same performance as before. The Unity engine provides us with a powerful light map function, allowing artists to easily bake light maps in the Unity editor. However, the disadvantage is that these light maps can only be applied to static objects ( static ) , that is, objects that cannot move, and the performance effect is fixed. This should be easy for everyone to understand. When introducing the light source mode above, the available modes are Realtime, Mixed and Baked. Realtime is real-time lighting, Baked is baked lighting, and Mixed is a combination of both (the default). Based on the names, we can roughly understand the uses of these three modes. Baked lighting means that the light map will be baked based on the light source. After the light map is generated, the baked light source can no longer be used, and the static objects in the scene will still be It is the previous lighting effect, but dynamic objects have no lighting effect, and if you move a static object, the shadow of the static object will not move with it. From this point of view, although baked lighting reduces computing performance, it cannot meet the lighting effect requirements of dynamic objects. So, how to solve this problem? In fact, it is Mixed mode, which can not only ensure the light map effect of static objects, but also ensure the real-time lighting effect of dynamic objects.

Next, we will introduce two concepts: direct lighting and indirect lighting.

Direct Lighting: The lighting information generated by the light source directly shining on the surface of the object.

Indirect lighting (Indirect Lighting): The lighting information formed by the light source shining on the surface of the object and then reflecting to other objects. Indirect lighting makes our scene more realistic because it is more in line with the actual lighting conditions in our real world. This is like placing a mirror next to an object. The indirect lighting from the mirror will definitely produce a lighting effect on the object. However, the implementation of indirect lighting requires hardware that supports real-time ray tracing, such as Nvidia's RTX series graphics cards. Therefore, on low-end PCs and most mobile phones, we have to rely on pre-computed light maps to provide this indirect lighting information. That's right, light maps can store both direct lighting information and indirect lighting information. In Unity, the light source mainly controls direct lighting, and the Lighting window mainly controls indirect lighting.

Next, we introduce another concept: the global illumination system (approximately equal to baking)

Global illumination systems are a set of techniques for modeling direct and indirect lighting to provide realistic lighting effects. Unity has two global illumination systems: Baked Global Illumination and Realtime Global Illumination. The baked global illumination system includes light maps, light probes and reflection probes. All rendering pipelines support baked global illumination systems. The real-time global illumination system includes real-time global illumination using Enlighten, with additional functionality added for light probes. Please note that Enlighten is currently deprecated and the real-time global illumination system will be removed from Unity soon. It seems that Unity global illumination is mainly used for baking light maps. So, let me introduce Unity’s baked global illumination system, which needs to be set in the Lighting window. We open the Lighting window through the menu bar Window -> Rendering -> Lighting, and then click to select the "Scene" option

By default, there is no lighting configuration, we need to click "New Lighting Settings" to generate one

At the same time, in the Project panel, a lighting configuration file with the same name will also be generated.

We return to the "Scene" tab and there are two options: "Realtime Lighting" and "Mixed Lighting". They represent Baked Global Illumination and Realtime Global Illumination respectively. From the screenshot, we can see that the real-time global illumination system has been discarded, and what is left is the baked global illumination system, which is under the " Mixed Lighting " option. Why mixed light source? Unity's default light source mode is mixed light source, which supports both real-time lighting and baking, and also supports global illumination technology (a mixture of direct lighting + indirect lighting) during baking. Because, as we have mentioned above, baking light sources reduces performance calculations, but it cannot solve the lighting needs of dynamic objects. Therefore, dynamic objects require real-time light sources to solve. If there are both baked light sources and real-time light sources in the scene, then what role does light mapping technology play? Of course, the difference is between static objects and dynamic objects. The reason is very simple, baked light sources are used for static objects, and real-time light sources are used for dynamic objects. Therefore, it is recommended to use a mixed method for light sources, especially the main light source.

We see that there are two sub-options under "Mixed Lighting", one is the check box to start baking the global illumination system, and the other is "Lighting Mode", the default value is "Shadowmask". In Shadowmask mode, indirect lighting effects are baked into the light map (that is, indirect lighting is not calculated in real time), and shadows generated by direct lighting are also baked into the light map (that is, shadows are not calculated in real time). ). In other words, when running the project, changing the mixed light source will not affect the indirect lighting and shadows of static objects (fixed in the light map), but other renderings are still calculated according to the real-time light source. Therefore, this mode is a combination of real-time lighting and light mapping. It should be noted that the Universal Rendering Pipeline (URP) does not support Shadowmask lighting mode, although it is the default option.

There are also two modes to choose from: Baked Indirect and Subtractive.

In Baked Indirect mode, only indirect lighting is baked (that is, indirect lighting is not calculated in real time), and direct lighting and shadows are calculated in real time, so the performance consumption is relatively large. Note that this mode is supported in all three rendering pipelines.

In Subtractive mode, direct lighting and indirect lighting are baked into the light map at the same time, and the shadows of static objects are also baked into the light map. Please note that the High Definition Rendering Pipeline (HDRP) does not support Subtractive lighting mode.

Comparing the three modes, the Subtractive mode has the lowest performance consumption, because the direct lighting, indirect lighting and shadows of static objects are obtained from the light map, and no real-time calculation is performed. Of course, dynamic objects still require real-time calculation of the light source. Please note that it has the lowest performance cost, but is not supported by the HD rendering pipeline. The second is Shadowmask mode, which only bakes indirect lighting and shadows of static objects. Direct lighting is still calculated based on real-time lighting. Compared with the former mode, the performance consumption is larger. Although the performance consumption is relatively compromised and it is also the default option of Unity, the universal rendering relationship is not supported. Finally, there is Baked Indirect mode, which bakes indirect lighting. Direct lighting and shadows are still calculated based on real-time lighting, which obviously consumes the most performance. Although it consumes the most performance, it is supported by all rendering pipelines. Therefore, when you are developing games, you need to determine these contents and choose which rendering pipeline and which mode.

In the Lighting window, we can also see the Lightmapping Settings option, with some important parameters below.

Lightmapper: Specifies which internal lighting calculation software is used to calculate lightmaps in the scene. Unity uses a computing system called Progressive Lightmapper. The default is Progressive CPU, which means the CPU is used for calculation. You can also select Progressive GPU for calculation. In addition, Enlighten has been abandoned (as mentioned above).

Direct Samples: Direct lighting sampling values

Indirect Samples: Indirect lighting sampling values

Environment Samples: Ambient light sampling values

The last is the Auto Generate check option. If we check this option, Unity will automatically generate light maps for us; if we do not check it (default), we need to click "Generate Lighting" on the right to manually generate the light map. If we modify the lighting or scene, we need to regenerate the light map.

Finally, let's summarize: Unity's four light source types are easy to understand. What is more difficult to understand is Unity's indirect lighting. Its main purpose is to make the scene more realistic, but unfortunately, indirect lighting can only be used for baking, which is used to generate light maps. Light mapping technology is widely used in Unity, so there are three light source modes: real-time, baking and mixing. By default, we recommend using light sources in mixed mode. After baking the light map, static objects in the scene will obtain lighting effects from the light map, while dynamic objects will still obtain lighting effects through real-time light source calculations. In this way, it can not only ensure the overall lighting effect, but also reduce performance consumption. Regarding the baking settings of the light map, you need to understand the three modes of "Lighting Mode" in "Mixed Lighting" in the Lighting window. The difference between them lies in whether direct lighting, indirect lighting and shadows are baked into the light map. The more content is baked, the less real-time calculations are required. This should not be difficult to understand.

Guess you like

Origin blog.csdn.net/konkon2012/article/details/130461175