Baking lightmaps unity and related issues of some light (a)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/wangjiangrong/article/details/100096586

In the games scene, because of the general scene most of the objects are stationary (static), if these objects using real-time lighting, it will be a great performance overhead, such as the shadow of the object. However, because in many cases, the position of most of the objects in the scene will not change when the game is running, the corresponding shadow will not change. It does not change the light environment (e.g., there will be no switching of day and night) to show the effect of the scene is not changed. Therefore, for this situation, we will use light maps (Lightmap) technology baked in advance (bake) effective, saving consumption performance.

Note: Some fields of the name of the article may be different in different versions of Unity, we all expand ~

First, we set up a simple scene (as shown), to facilitate the test results. Scene objects within the scene checked as static, dynamic objects People capsule body, then there is a white light. In order to prevent the effects of sky boxes, we will set the ambient light Lighting all set to black (Window-> Rendering-> Lighting Settings).

 

Unity GI (global illumination global illumination), there are two kinds Realtime GI and Baked GI (Lighting is provided as in the figure). When we checked objects in the scene are static objects will find the time corresponding to the Mesh Renderer, the will automatically configure the settings on Lightmap (Lighting settings checked Auto Generate, or bake it manually)

RealtimeGI then the choice will generate a corresponding Realtime Lightmap, updated in real time according to the lighting scene selection BakedGI then will generate a corresponding Baked Lightmap, only when baking will according to set a good light updated (checked automatically generated if each light or change the static objects will be re-baking)

Unity light source illumination mode (Mode Light assembly property), there are three, Realtime, Mixed and Baked. GI under different circumstances, different modes of light sources will produce different results, and we can add baked mixed light sources were tested in the above test scenarios:

Realtime GI and Baked GI is not checked (no static object lightmap)

Realtime When the real-time rendering: static objects and dynamic objects are calculated source
Mixed This mode can not be selected
Baked This mode can not be selected

 

Just check the Realtime GI (static objects have realtime lightmap)

Realtime

Baking: static object realtime lightmap calculates the light source receives indirect light

When the real-time rendering: static and dynamic objects are calculated source

Mixed This mode can not be selected
Baked This mode can not be selected

We find that the case of the scene and lights are the same, check the Realtime GI, several small squares surface static objects become whiter than before, at the bottom there will be some white shadows. We observed the resulting realtime lightmap (double-click on details) can be found in the upper left part of the picture represented part of a large area of six square faces two respective lower left represents the plane Plane. Those white part realtime information is indirect light source, close to the plane where small blocks, refracted light from the refracting surface to the plane of the small box, the small box is also refracted onto a plane surface, forming the indirect light ( GI refraction is the most important part of bounces) . Away from the small square areas not receiving indirect light, so lightmap in black. Real-time rendering time will be calculated lightmap realtime information and light source information, it created a whiter phenomenon.

Just check Baked GI (static objects have baked lightmap)

Realtime

烘焙时:静态物体的baked lightmap不会计算该光源

实时渲染时:静态和动态物体都会计算该光源

Mixed

烘焙时:静态物体的baked lightmap会计算接受到该光源的间接光信息

实时渲染时:静态和动态物体都会计算该光源

注:不同的Lighting Mode设置会有不同的效果,文章的后面详细描述(当前为默认的shadowmask模式)

Baked

烘焙时:静态物体的baked lightmap会计算接受到该光源的直接光和间接光信息

实时渲染时:静态和动态物体都不会计算该光源

baked:

我们可以在场景中新增一个光源Baked,颜色选为红色,Mode选为baked,烘焙好后的效果图与光照贴图如下:

可以发现,动态物体(胶囊体)不受红光影响,lightmap为baked光源的红色,实时渲染的时候计算了realtime的白色光源。此时运行Unity,再修改baked光源颜色,或删除该光源都不会对场景产生影响。

但是此时你会发现场景中静态物体会产生两个阴影,一个realtime的实时阴影,一个baked的烘焙好的阴影,这肯定不是我们要的效果。若隐藏了realtime光源,则动态物体不会产生阴影,这显然也是不对的,游戏中不可能场景中的房子有阴影但是行走的人却没有。那如何使静态物体只有烘焙的阴影,但是动态物体有实时的阴影呢,这就需要下面的混合光源Mixed的帮助了。

mixed:

在bakedGI下,若场景只有realtime光源,则不会有lightmap,动态物体和静态物体的阴影都需要实时计算。若只有baked光源,则静态物体会烘焙好阴影,但是动态物体却不受该光源的影响,无法产生阴影。若你想把静态物体烘焙成lightmap,同时同一个光源又对动态物体产生影响,可以形成动态阴影,那就需要mixed光源的帮助了。

我们隐藏场景中的其他光源,新建一个光源,颜色选为绿色,Mode选为mixed,烘焙好后的效果图与光照贴图如下:

此时运行unity,移动胶囊体,阴影会随着跟着移动。隐藏小方块阴影并不会跟着消失,因为是事先烘焙好的。修改mixed光源的方向的话,动态物体的阴影和光照效果都会随之改变,静态物体的光照效果会改变但是阴影不会。

 

Realtime GI 和 Baked GI 都勾选(静态物体有 realtime lightmap和baked lightmap)

Realtime

烘焙时:静态物体的realtime lightmap会计算接受到该光源的间接光信息,baked lightmap不会计算该光源

实时渲染时:静态和动态物体都会计算该光源

Mixed

烘焙时:静态物体的realtime lightmap不会计算该光源信息,baked lightmap会计算接受到该光源的间接光信息

实时渲染时:静态和动态物体都会计算该光源

Baked

烘焙时:静态物体的realtime lightmap不会计算该光源信息,baked lightmap会计算接受到该光源的直接光和间接光信息

实时渲染时:静态和动态物体都不会计算该光源

若Lighting设置中Environment Lighting的Ambient Mode选择Realtime,运行时改变环境光的属性静态物体和动态物体的光照效果都会发生改变(静态物体的Realtime lightmap改变),而选择Baked,则会把环境光也烘培起来,运行时改变环境光,并不会改变静态物体的光照效果 。

 

搞清楚GI和光源的关系后,我们接下来看看Lighting设置中Mixed Lighting的Lighting Mode的三个设置。首先我们只勾选Baked GI,然后场景中只留之前绿色的mixed光源。

baked Indirect

只烘培静态物体的受到的间接光照,直接光以及阴影进行实时计算。运行时改变灯光,直接光照和阴影就会变,但间接光不变(如果间接光弱,视觉影响不大)。只减少间接光照的实时计算,适合对光照要求较高的场景,内存消耗小,当然性能的消耗也大。 

运行后的效果如下,动态与静态物体的阴影随着mixed光源实时计算,静态物体烘焙的间接光效果不变。

shadowmask

除了烘焙静态物体受到的光照,同时也会把静态物体受直接光照产生的阴影烘培到ShadowMask中,动态物体不受影响。运行时改变灯光,只有静态物体的间接光照和阴影不变。内存中多了一份Shadowmask,减少了静态物体阴影的计算,占用性能小了一点。

注意:开启Shadowmask需要在QualitySettings设置Shadowmask Mode为Shadowmask。

subtractive

烘焙静态物体受到的直接光和间接光的信息(包含阴影,如同baked光源)。运行时改变光照,静态物体直接光、间接光、阴影不受影响,性能消耗最小。 

Guess you like

Origin blog.csdn.net/wangjiangrong/article/details/100096586