加载lightmap

载入lightmap

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

public class PrefabLightmapData : MonoBehaviour
{
    [System.Serializable]
    struct RendererInfo
    {
        public Renderer renderer;
        public int lightmapIndex;
        public Vector4 lightmapOffsetScale;
    }


    [UnityEngine.SerializeField]
    Texture2D[] lightmapTexs;   //当前场景的灯光贴图

    [UnityEngine.SerializeField]
    Texture2D[] lightmapDir;   //当前场景的灯光贴图

    [UnityEngine.SerializeField]
    Texture2D[] lightmapMask;   //当前场景的灯光贴图


    [UnityEngine.SerializeField]
    RendererInfo[] rendererList;

    [UnityEngine.SerializeField]
    Material skyBoxMaterial;

#if UNITY_EDITOR
    public void SaveLightmap()
    {
        Renderer[] renders = GetComponentsInChildren<Renderer>(true);
        RendererInfo rendererInfo;
        rendererList = new RendererInfo[renders.Length];

        int index = 0;

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

            rendererInfo.renderer = renders[r];
            rendererInfo.lightmapIndex = renders[r].lightmapIndex;
            rendererInfo.lightmapOffsetScale = renders[r].lightmapScaleOffset;

            rendererList[index] = rendererInfo;

            ++index;
        }

        //序列化光照贴图
        LightmapData[] ldata = LightmapSettings.lightmaps;
        lightmapTexs = new Texture2D[ldata.Length];
        lightmapDir = new Texture2D[ldata.Length];
        lightmapMask = new Texture2D[ldata.Length];
        for (int t = 0, tLength = ldata.Length; t < tLength; ++t)
        {
            lightmapTexs[t] = ldata[t].lightmapColor;
            lightmapDir[t] = ldata[t].lightmapDir;
            lightmapMask[t] = ldata[t].shadowMask;

        }

        skyBoxMaterial = RenderSettings.skybox;
    }

#endif

    private void Awake()
    {
        this.LoadLightmap();
        this.ChangeSkybox(skyBoxMaterial);
    }
    void OnEnable()
    {
        this.LoadLightmap();
        this.ChangeSkybox(skyBoxMaterial);
    }

    void LoadLightmap()
    {
        if (null == rendererList || rendererList.Length == 0)
        {
            Debug.Log(gameObject.name + " 的 光照信息为空");
            return;
        }

        Renderer[] renders = GetComponentsInChildren<Renderer>(true);
 
        for (int r = 0, rLength = renders.Length,index=0; r < rLength;r++ )
        {
           
            if(index < rendererList.Length && renders[r].gameObject.name != rendererList[index].renderer.gameObject.name)
            {
                Debug.LogError("lightMap烘焙时,物体    " + renders[r].gameObject.name + "    未勾选static。");
                break;
            }
            if (renders[r].gameObject.name.Contains("combineMesh_"))
            {
                renders[r].lightmapIndex = 0;
                continue;
            }
            
            renders[r].lightmapIndex = rendererList[index].lightmapIndex;
            renders[r].lightmapScaleOffset = rendererList[index].lightmapOffsetScale;
            ++index;
        }


        if (null == lightmapTexs || lightmapTexs.Length == 0)
        {
            return;
        }

        LightmapSettings.lightmapsMode = LightmapsMode.NonDirectional;
        LightmapData[] ldata = new LightmapData[lightmapTexs.Length];
        LightmapSettings.lightmaps = null;

        for (int t = 0, tLength = lightmapTexs.Length; t < tLength; ++t)
        {
            ldata[t] = new LightmapData();
            ldata[t].lightmapColor = lightmapTexs[t];
            ldata[t].lightmapDir = lightmapDir[t];
            ldata[t].shadowMask = lightmapMask[t];
        }

        LightmapSettings.lightmaps = ldata;

        MeshRenderer[] meshRenderers = GetComponentsInChildren<MeshRenderer>();

        GameObject[] gos = new GameObject[meshRenderers.Length];
        for (int i = 0; i < meshRenderers.Length; i++)
        {
            if (meshRenderers[i].gameObject.name.Contains("combineMesh_"))
            {
                renders[i].lightmapIndex = 0;
                continue;
            }
            gos[i] = meshRenderers[i].gameObject;

        }
        StaticBatchingUtility.Combine(gos, gameObject);
    }

    private void ChangeSkybox(Material newSkybox)
    {
        RenderSettings.skybox = newSkybox;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值