Unity3D中LightMap动态加载

      在写这边博客之前,对光照贴图的动态加载有一点了解,一直没有自己动手去实现,然后网上百度找了一些例子去实现,但是发现好多例子都是你转载我的我转载你的,写得繁琐,没有讲明重点。

    使用光照贴图有什么好处?好处就是避免动态实时的进行光照计算,提高效率。那么如何进行LightMap的动态加载呢?

    首先,你要知道怎么去烘焙场景(自行百度),烘焙好场景后,会生成你的编辑器下生成 如下的几个文件

Unity3D中LightMap动态加载_第1张图片

    其中Lightmap-0_comp_dir.png和Lightmap-0_comp_light.exr就是我们需求用到的文件。

  1. 将Lightmap-0_comp_dir.png和Lightmap-0_comp_light.exr这两个文件打包成Assetbundle。
  2. 将场景中的Capsule,Plane和Sphere添加LightMapData脚本,通过写编辑器个工具记录他们的光照贴图信息,如下图:

Unity3D中LightMap动态加载_第2张图片

然后把这三个物体打包成Assetbundle。

3.把光照贴图动态加载进来,测试代码如下:
 

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

public class Load : MonoBehaviour
{
    private const string serverIP = "http://127.0.0.1:8001/";
    private List ligthList = new List { "lightmap-0_comp_light", "lightmap-0_comp_dir"};
    private List gameObjectList = new List { "capsule", "plane" , "sphere" };
    // Start is called before the first frame update
    private LightmapData lightmapData = new LightmapData();
    private List lightmapDatas = new List();
   

    private void OnGUI()
    {
        GUILayout.BeginVertical();
        if (GUILayout.Button("LoadLightMaps"))
        {
            for (int num = 0; num < ligthList.Count; num++)
            {
                StartCoroutine(LoadLightMap(serverIP, ligthList[num]));
            }
        }
        if (GUILayout.Button("SetLightMaps"))
        {
            lightmapDatas.Add(lightmapData);
            LightmapSettings.lightmaps = lightmapDatas.ToArray();
        }
        if (GUILayout.Button("LoadGameObject"))
        {
            for (int num = 0; num < gameObjectList.Count; num++)
            {
                StartCoroutine(LoadGameObject(serverIP, gameObjectList[num]));
            }
        }
        GUILayout.EndVertical();
    }

    IEnumerator LoadGameObject(string  path , string resName)
    {
        string url = string.Format("{0}{1}.unity3d" , path , resName );
        UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(url);
        yield return request.SendWebRequest();
        AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
        GameObject go = Instantiate(ab.LoadAsset(resName));
        go.isStatic = true;
        PrefabLightmapData prefabLightmapData = go.GetComponent();
        if(prefabLightmapData != null)
        {
            go.GetComponent().lightmapIndex = prefabLightmapData.lightmapIndex;
            go.GetComponent().lightmapScaleOffset = prefabLightmapData.lightmapOffsetScale;
        }
      

    }
    IEnumerator LoadLightMap(string path, string resName)
    {
        string url = string.Format("{0}{1}.unity3d", path, resName);
        UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(url);
        yield return request.SendWebRequest();
        AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
        Texture2D texture2D = ab.LoadAsset(resName) as Texture2D;

        if (resName.Contains("dir"))
        {
            lightmapData.lightmapDir = texture2D;
        }
        else
        {
            lightmapData.lightmapColor = texture2D;
        }
    }

}

最后运行结果:

Unity3D中LightMap动态加载_第3张图片

 

你可能感兴趣的:(unity)