Unity3D预制体光照贴图加载

本文介绍了一个Unity脚本,用于保存和加载场景中对象的光照贴图信息,并提供了编辑器菜单选项来辅助这一过程。该脚本适用于游戏开发中的光照优化工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

预制体读取光照图

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

public class PrefabLightmapData : MonoBehaviour
{
    [System.Serializable]
    public struct RendererInfo
    {
        public Renderer renderer;
        public int lightingIndex;
        public Vector4 lightmapOffsetScale;
    }
    public List lightmapTexs = new List();   //当前场景的灯光贴图
    public List rendererList = new List();
    public List rendererLightmapIndex = new List();
#if UNITY_EDITOR
    public void SaveLightmap()
    {
        Renderer[] renders = GetComponentsInChildren();
        RendererInfo rendererInfo;
        lightmapTexs.Clear();
        rendererList.Clear();

        for (int r = 0, rLength = renders.Length; r < rLength; ++r)
        {
            if (renders[r].gameObject.isStatic == false || renders[r].lightmapIndex == -1) continue;

            rendererInfo.renderer = renders[r];
            rendererInfo.lightmapOffsetScale = renders[r].lightmapScaleOffset;
            Texture2D lightmapLight = LightmapSettings.lightmaps[renders[r].lightmapIndex].lightmapColor;

            if (!lightmapTexs.Contains(lightmapLight))
            {
                lightmapTexs.Add(lightmapLight);
            }

            rendererInfo.lightingIndex = lightmapTexs.IndexOf(lightmapLight);
            rendererList.Add(rendererInfo);
        }
    }
#endif
    private void OnEnable()
    {
        this.LoadLightMap();
    }
    public void LoadLightMap()
    {
        rendererLightmapIndex.Clear();

        for (int i = 0; i < rendererList.Count; i++)
        {
            rendererLightmapIndex.Add(0);
        }

        if (rendererList.Count == 0 || rendererList == null)
        {
            return;
        }
        for (int i = 0; i < rendererList.Count; i++)
        {
            rendererList[i].renderer.lightmapScaleOffset = rendererList[i].lightmapOffsetScale;
            Texture2D lightmap = lightmapTexs[rendererList[i].lightingIndex];
            int index = LightmapIndexOf(lightmap);
            if (index == -1)
            {
                LightmapData[] lightmaps = new LightmapData[LightmapSettings.lightmaps.Length + 1];
                for (int j = 0; j < LightmapSettings.lightmaps.Length; j++)
                {
                    LightmapData lightdata = new LightmapData();
                    lightdata.lightmapColor = LightmapSettings.lightmaps[j].lightmapColor;
                    lightmaps[j] = lightdata;
                }
                lightmaps[lightmaps.Length - 1] = new LightmapData();
                lightmaps[lightmaps.Length - 1].lightmapColor = lightmap;
                rendererList[i].renderer.lightmapIndex = lightmaps.Length - 1;
                LightmapSettings.lightmaps = lightmaps;
                rendererLightmapIndex[i] = lightmaps.Length - 1;
            }
            else
            {
                rendererList[i].renderer.lightmapIndex = index;
                rendererLightmapIndex[i] = index;
            }
        }
    }
    int LightmapIndexOf(Texture2D lightmap)
    {
        for (int i = 0; i < LightmapSettings.lightmaps.Length; i++)
        {
            if (lightmap == LightmapSettings.lightmaps[i].lightmapColor)
            {
                return i;
            }
        }
        return -1;

    }
    //设置光照贴图
    public void SetLightmap(bool open)
    {
        if (open)
        {
            for (int i = 0; i < rendererList.Count; i++)
            {
                rendererList[i].renderer.lightmapIndex = rendererLightmapIndex[i];
            }
        }
        else
        {
            for (int i = 0; i < rendererList.Count; i++)
            {
                rendererList[i].renderer.lightmapIndex = -1;
            }
        }
    }
}

保存光照贴图信息编辑器

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEditor;

public class PrefabLightingDataEditor : Editor
{
    private const string S_LIGTHMAP_PATH = "Assets" + "/" + "Resources" + "/" + "Lightmaps" + "/";
    [MenuItem("Object Tools/保存该场景预制件的烘焙信息", false, 0)]
    static void SaveLightmapInfoByGameObject()
    {
        GameObject[] gos = Selection.gameObjects;

        Dictionary record = new Dictionary();

        for (int i = 0; i < gos.Length; i++)
        {
            GameObject go = gos[i];

            if (null == go) return;

            PrefabLightmapData data = go.GetComponent();
            if (data == null)
            {
                data = go.AddComponent();
            }

            //save lightmapdata info by mesh.render
            data.SaveLightmap();

            if (!Directory.Exists(S_LIGTHMAP_PATH + data.gameObject.name))
            {
                Directory.CreateDirectory(S_LIGTHMAP_PATH + data.gameObject.name);
            }
            else
            {
                string[] files = Directory.GetFiles(S_LIGTHMAP_PATH + data.gameObject.name, "*.*", SearchOption.TopDirectoryOnly);

                for (int j = 0; j < files.Length; j++)
                {
                    File.Delete(files[j]);
                }
            }

            for (int j = 0; j < data.lightmapTexs.Count; j++)
            {
                string newPath = string.Empty;

                if (record.TryGetValue(data.lightmapTexs[j], out newPath))
                {

                }
                else
                {
                    string path = AssetDatabase.GetAssetPath(data.lightmapTexs[j]);
                    newPath = S_LIGTHMAP_PATH + data.gameObject.name + "/" + data.lightmapTexs[j].name + ".exr";

                    if (AssetDatabase.CopyAsset(path, newPath))
                    {
                        AssetDatabase.Refresh();
                        record.Add(data.lightmapTexs[j], newPath);
                    }
                }

                data.lightmapTexs[j] = AssetDatabase.LoadAssetAtPath(newPath);
            }

            string[] filess = Directory.GetFiles(S_LIGTHMAP_PATH + data.gameObject.name, "*.*", SearchOption.TopDirectoryOnly);

            if (filess.Length < 1)
            {
                Directory.Delete(S_LIGTHMAP_PATH + data.gameObject.name);
            }

            EditorUtility.SetDirty(go);
            //applay prefab
            PrefabUtility.ReplacePrefab(go, PrefabUtility.GetPrefabParent(go), ReplacePrefabOptions.ConnectToPrefab);
        }
    }
    [MenuItem("Object Tools/设置光照贴图", false, 0)]
    static void Light()
    {
        GameObject go = Selection.activeGameObject;

        if (go == null) return;
        PrefabLightmapData data = go.GetComponent();
        if (data == null) return;
        data.LoadLightMap();
    }
    [MenuItem("Object Tools/关闭光照贴图", false, 0)]
    static void Dark()
    {
        GameObject go = Selection.activeGameObject;

        if (go == null) return;
        PrefabLightmapData data = go.GetComponent();
        if (data == null) return;
        data.SetLightmap(false);
    }
    [MenuItem("Object Tools/清空光照贴图", false, 0)]
    static void ClearLightMap()
    {
        LightmapSettings.lightmaps = new LightmapData[0];
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值