Files
Main/Assets/Editor/SplitTerrain/EditorForm/Part7Bake.cs
2025-01-25 04:38:09 +08:00

112 lines
3.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Thousandto.Core.Asset;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
namespace Thousandto.Editor.Test
{
/// <summary>
/// 预览选中物件的光照贴图
/// </summary>
public partial class SplitTerrainWindow : UnityEditor.EditorWindow
{
private bool _splitTerrain = true;
private bool _collectThings = true;
private bool _loadAllPrefab = true;
private bool _bakeLightmap = true;
private bool _rebuildLightmap = true;
void DrawBakeGUI()
{
GUILayout.BeginHorizontal();
_splitTerrain = GUILayout.Toggle(_splitTerrain, "切割地形");
_collectThings = GUILayout.Toggle(_collectThings, "划分网格物件");
_loadAllPrefab = GUILayout.Toggle(_loadAllPrefab, "加载所有prefab");
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
_bakeLightmap = GUILayout.Toggle(_bakeLightmap, "烘焙和重建lightmap");
_rebuildLightmap = GUILayout.Toggle(_rebuildLightmap, "仅重建lightmap");
GUILayout.EndHorizontal();
if (GUILayout.Button("Bake"))
{
BakeAll();
}
}
void BakeAll()
{
_bakeStart = false;
var mapName = EditorSceneManager.GetActiveScene().name;
if(_splitTerrain)
{
EditorUtility.DisplayProgressBar("Bake", "开始切割地形", 0);
//1. 切割地形生成网格同时生成prefab
int chunkSize = TerrainConfigDefine.BLOCK_SIZE;
SplitTerrain st = new SplitTerrain();
st.StartSplitTerrain(_curTerrainData, SplitResolution, chunkSize, true);
}
if(_collectThings)
{
EditorUtility.DisplayProgressBar("Bake", "收集场景物件", 0.2f);
//2. 收集场景中物件按照分块生成prefab
CollectThings ct = new CollectThings();
ct.SetResolution(_mapSize, _blockSize);
ct.Collect(mapName);
}
if(_bakeLightmap || _rebuildLightmap)
{
//3. 隐藏SceneRoot
var sceneRoot = GameObject.Find("SceneRoot");
if (sceneRoot != null) sceneRoot.SetActive(false);
}
if(_loadAllPrefab)
{
EditorUtility.DisplayProgressBar("Bake", "加载prefab", 0.4f);
//4. 加载所有生成prefab的地形网格和物件,需要设置为static
_loadedPrefabs = LoadAllTerrainPrefabAndThingPrefab(mapName);
}
if(_bakeLightmap)
{
EditorUtility.DisplayProgressBar("Bake", "烘焙lightmap", 0.8f);
//5. 烘焙lightmap
if (Lightmapping.BakeAsync())
{
_bakeStart = true;
Lightmapping.completed -= OnBakeFinish;
Lightmapping.completed += OnBakeFinish;
}
}
if(!_bakeLightmap && _rebuildLightmap)
{
OnBakeFinish();
}
}
void OnBakeFinish()
{
_bakeStart = false;
Lightmapping.completed -= OnBakeFinish;
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
EditorSceneManager.SaveOpenScenes();
EditorUtility.DisplayProgressBar("Bake", "创建新的lightmap", 0.9f);
if (_loadedPrefabs != null && _loadedPrefabs.Count > 0)
LightMapSpliter.CreateCombinedLightmap(_loadedPrefabs.ToArray());
EditorUtility.ClearProgressBar();
}
}
}