Block baking + dynamic mount lightmap

Because Xiaohei is still a chicken just exposed to rendering and baking,

Some big guys see this article, please give advice. Thank you.

Based on: URP (lightweight rendering pipeline)

HDRP, I think there should be no problem. After all, it is professional

The leader arranged a task for Xiaohei these two days, that is, baking the big map in blocks, and then dynamically mounting the light map. When I first received the task (what is the light map? How to dynamically mount it? Is it up to me to bake the big map in blocks? Can I understand it?), After opening, autistic and doubting himself, Xiaohei succeeded!

However, there is a prerequisite, that is, the cooperation of the scene designer is required.

Xiaohei has a Demo to provide you with reference.

catalogue

edition:

Block baking based on URP project

1: Create project

2: Create map

3: Place objects to reflect shadows

4: Check static, adjust the light and bake in blocks

5: Script, mount lightmaps

end

talk of daily trifles

No recommendation today

edition:

Unity  2020.3.13f1c1

Block baking based on URP project

1: Create project

2: Create map

Xiaohei's map is based on the plug-in meshterrain editor. If you have anything else, you can recommend it to Xiaohei. 50 * 50 topographic maps are created. In order to speed up, Xiaohei directly creates flat land.

3: Place objects to reflect shadows

Next, in order to show that we have light, we will add objects to show shadows.

We can see that we have three new GameObject s entering the scene, and there is an object under each topographic map and a script (my customized Player) outside the topographic map.

4: Check static, adjust the light and bake in blocks

With the map, the objects to be baked exist, and we're about to get down to business.

① First, check the terrain map to be baked and the objects below them

② : changing light properties

③ : turn on light settings: window - > rendering - > lighting

④ : then hide one of the topographic maps, open it for baking, and move the objects on the topographic map. We found that the baking was indeed completed.

⑤: at this time, we have to do a big thing! First record the offset and scaling value of the light map to the object after baking, and then record the content of the pink frame mark in the picture, together with their All mate files are moved outside Unity, and they need to be used later, so they should be saved!

⑥ : after baking, we first Clear the contents of a wave of baking map, then hide the current topographic map, open another one for baking, and then repeat steps ④ and ⑤ for non-standard red operation. Remember to change the index in the middle.

⑦ : then we can't Clear. Because we need to use their common LightingData and ReflectionProbe-0. Then we put all the contents baked twice into a user-defined folder of Unity. It can be seen from the below that Xiaohei puts all the results of the two baking into a folder called xhscenesssss, and changes the map index baked twice to 1.

So far, our block baking has been completed. The next thing we need to do is mount the written map under the Baked Lightmaps panel.

5: Script, mount lightmaps

Xiaohei's work is not very good, to be optimized. There are several steps: 1 empty the lightmap, 2 add the lightmap, and 3 assign the lightmap to the previously baked object.  

Let's see, there are no lights in the scene, and the two baked topographic maps have shadows.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ReplaceBakingMap : MonoBehaviour
{
    public Texture2D[] MyTexture;
    public List<GameObject> MyGameObjects = new List<GameObject>();
    public Vector4[] MyVector4;
      
    // Start is called before the first frame update
    void Start()
    {
       
    }

    [ContextMenu("set up LightMap")]
    private void SettingLightMap()
    {
        if (MyTexture.Length == 0)
        {
            Debug.LogError("MyTextures is null");
            return;
        }

        LightmapData[] __mapData = LightmapSettings.lightmaps;
        int __length = __mapData.Length;
        LightmapData[] __tempMapData = new LightmapData[__length + MyTexture.Length / 2];

        for (int i = 0; i < __length; i++)
        {
            __tempMapData[i] = __mapData[i];
        }
        for (int i = 0; i < MyTexture.Length; i+=2)
        {
            int __indexI = __length + (i / 2);

            LightmapData __date = new LightmapData();
            __date.lightmapDir = MyTexture[i];
            __date.lightmapColor = MyTexture[i + 1];
            __tempMapData[__indexI] = __date; 
        }

        LightmapSettings.lightmaps = __tempMapData;
    }

    [ContextMenu("set up LightMap Empty")]
    private void SettingLightMapToNull()
    {
        LightmapSettings.lightmaps = null;
    }

    [ContextMenu("Get location")]
    private void GetLightmapTexture()
    {
        int __length = MyGameObjects.Count;
        Vector4[] __tempVector = new Vector4[__length];
        for (int i = 0; i < __length; i++)
        {
            __tempVector[i] = MyGameObjects[i].GetComponent<MeshRenderer>().lightmapScaleOffset;
        }
        MyVector4 = __tempVector;
    }

    [ContextMenu("Set map position")]
    private void SetLightmapTexture()
    {
        int __length = MyGameObjects.Count;
        for (int i = 0; i < __length; i++)
        {
            MeshRenderer __render = MyGameObjects[i].GetComponent<MeshRenderer>();
            __render.lightmapScaleOffset = MyVector4[i];
            __render.lightmapIndex = 1;
        }
        
    }
}

After the script is mounted

There is a pit to tell you that our baking lamp seems to be dirty by the system after baking every time. Even if it is set to real time, it will not react.. This problem occurs in the version I use this time (2020.3.13f1c1). I don't know whether other versions are the same.

end

So far, it has been completed. Through this practice theory, we can understand how to quickly load and bake the big map.

talk of daily trifles

  • Xiaohei's sharing today is over. Have you got it, my friends? Do you have a better way? You can leave a message in the comment area to share, or you can add Xiaohei's QQ: 841298494 to make progress to get her.

No recommendation today

Keywords: Unity Game Development Unity3d

Added by imagenesis on Thu, 20 Jan 2022 01:03:54 +0200