Switching of Unity lightmaps to achieve night and day effects

There is such a requirement that real-time light cannot be used to dynamically control the light switch, but the effect of day and night must be achieved. There are about a dozen point light sources and parallel lights in my scene.

Implementation steps:

1. Copy the original model to another scene (because the texture can only exist in the current scene folder)

2. Debug the effect of day and night in different scenes, bake two sets of textures, pay attention to the baked textures are Dir type and Linght type

Then why are there the following two types? The reason is that when Unity completes the baking of the lightmap, according to different settings, up to three different lightmaps will be generated. The one ending with _light is the light map, the one ending with _dir is the direction map of parallel light, and the one ending with _shadowmask is the shadow channel map of ShadowMask. For the time being, we only focus on the light map at the end of _light and _dir

 3. In the scene where texture switching is required, create a control switch and mount the following script

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using DG.Tweening;

/// <summary>
/// 该脚本用于控制光照贴图和光照开关
/// </summary>
public class LightingSwitchBtn : MonoBehaviour,IPointerClickHandler
{
    public bool IsTurnOn = false;
    public GameObject CircleSlider;

    // / <summary>
    // / 光照信息
    // / </summary>
    LightmapData[] lightmapDatas;

    /// <summary>
    /// 暗的时候的贴图
    /// </summary>
    public Texture2D[] lightmapDark;
    public Texture2D[] lightmapDarkDir;

    /// <summary>
    /// 亮的时候的贴图
    /// </summary>
    public Texture2D[] lightmapLight;
    public Texture2D[] lightmapLightDir;

    void Awake()
    {
    }
    public void OnPointerClick(PointerEventData eventData)
    {
        SwichState();
    }
    public void SwichState()
    {
        bool ClickState = !IsTurnOn;
        if (ClickState)
        {
            gameObject.GetComponent<Image>().color = Color.blue;
            CircleSlider.transform.DOLocalMoveX(16.7f, 0.2f);
            SetLightMap(lightmapLight,lightmapDarkDir);
        }
        else
        {
            gameObject.GetComponent<Image>().color = Color.white;
            CircleSlider.transform.DOLocalMoveX(-16.7f, 0.2f);

            SetLightMap(lightmapDark,lightmapDarkDir);
        }
        IsTurnOn = ClickState;
    }

    /// <summary>
    /// 替换所有贴图
    /// </summary>
    /// <param name="lightmapTex"></param>
    public void SetLightMap(Texture2D[] lightmapTex,Texture2D[] lightmapDir)
    {
        if(lightmapTex == null)
        {
            return;
        }
        lightmapDatas = new LightmapData[lightmapTex.Length];
        for (int i = 0; i < lightmapTex.Length; i++)
        {
            LightmapData lmd = new LightmapData();
            lmd.lightmapColor = lightmapTex[i];
            lmd.lightmapDir = lightmapDir[i];
            lightmapDatas[i] = lmd;
        }
        LightmapSettings.lightmaps = lightmapDatas;
    }
}

*Note that these four arrays represent your textures in night and day respectively, and are divided into Dir and Light types. Assign the corresponding types of textures in the above screenshots to the following arrays

  public Texture2D[] lightmapDark;
  public Texture2D[] lightmapDarkDir;

  public Texture2D[] lightmapLight;
  public Texture2D[] lightmapLightDir;

*LightmapData[] lightmapDatas is the lighting information group

* Explain the following script

lightmapDatas = new LightmapData[lightmapTex.Length];
        for (int i = 0; i < lightmapTex.Length; i++)
        {
            LightmapData lmd = new LightmapData();
            lmd.lightmapColor = lightmapTex[i];
            lmd.lightmapDir = lightmapDir[i];
            lightmapDatas[i] = lmd;
        }
        LightmapSettings.lightmaps = lightmapDatas;
  1. lightmapDatas = new LightmapData[lightmapTex.Length];: Creates lightmapTexan LightmapDataarray with the same length as the array lightmapDatas.
  2. LightmapData lmd = new LightmapData(); Create a new LightmapDataobjectlmd
  3. lmd.lightmapColor = lightmapTex[i];: Assign the lightmapTexarray lmdto lightmapColorthe property of , that is, set the color map of the light map.
  4. lmd.lightmapDir = lightmapDir[i];: Assign the lightmapDirarray to the propertylmd of , that is, set the direction map of the light map.lightmapDir
  5. lightmapDatas[i] = lmd;: Assign lmdthe value to lightmapDatasthe corresponding index position of the array, and complete the filling of lightmapDatasthe array .
  6. LightmapSettings.lightmaps = lightmapDatas; Assign the filled lightmapDatasarray LightmapSettings.lightmapsto to apply the new lightmap.

can be achieved as above

Guess you like

Origin blog.csdn.net/leikang111/article/details/131226776