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

150 lines
6.0 KiB
C#

using Thousandto.Core.Asset;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEngine;
namespace Thousandto.Editor.Test
{
/// <summary>
/// 预览选中物件的光照贴图
/// </summary>
public partial class SplitTerrainWindow : UnityEditor.EditorWindow
{
Texture2D _tex;
private Vector2 _previewScrollRect = new Vector2(0, 0);
private Rect _viewRect;
Rect posRect = new Rect(0, 400, 400, 400);
int _blockW = 200;
int _blockH = 200;
Vector4 _lightScaleInfo;
Vector4 _boundsInPixel;
int _lightIndex = 0;
bool _foldOutPreview = true;
void DrawTexturePreview()
{
var oldColor = GUI.contentColor;
GUI.contentColor = Color.green;
_foldOutPreview = EditorGUILayout.Foldout(_foldOutPreview, "Preview Lightmap");
GUI.contentColor = oldColor;
if (_foldOutPreview)
{
//DrawSplitSetting("lightmapIndex: ", ref _lightIndex, "");
//EditorGUILayout.Vector4Field("ScakeOffset", _lightScaleInfo);
EditorGUILayout.Vector4Field("_boundsInPixel", _boundsInPixel);
DrawSplitSetting("分块大小W: ", ref _blockW, "");
DrawSplitSetting("分块大小H: ", ref _blockH, "");
GUILayout.BeginHorizontal();
if (GUILayout.Button("合并lightmap 预览"))
{
_tex = LightMapSpliter.CombineLightmap(Selection.gameObjects);
}
if (GUILayout.Button("同步选中的prefab"))
{
var gos = Selection.gameObjects;
Utils.SyncPrefabInEditor(gos);
}
if (GUILayout.Button("删除PrefabInfo脚本"))
{
var gos = Selection.gameObjects;
for(int i = 0; i < gos.Length; ++i)
{
var scripts = gos[i].GetComponentsInChildren<PrefabLightmapInfo>();
if(PrefabUtility.GetPrefabType(gos[i]) != PrefabType.PrefabInstance)
{
var path = AssetDatabase.GetAssetPath(gos[i]);
scripts = AssetDatabase.LoadAssetAtPath<GameObject>(path).GetComponentsInChildren<PrefabLightmapInfo>();
}
for(int j = 0; scripts != null && j < scripts.Length; ++j)
{
DestroyImmediate(scripts[j], true);
}
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
GUILayout.EndHorizontal();
if (_tex != null)
{
_viewRect = new Rect(0, 0, _tex.width, _tex.height);
GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.MaxWidth(400), GUILayout.Height(300), GUILayout.MaxHeight(1024) };
_previewScrollRect = EditorGUILayout.BeginScrollView(_previewScrollRect);
var aspect = _tex.width / (float)_tex.height;
Rect aspectRect = GUILayoutUtility.GetAspectRect(aspect, GUILayout.MaxWidth(_tex.width), GUILayout.MaxHeight(_tex.height));
EditorGUI.DrawPreviewTexture(aspectRect, _tex);
EditorGUILayout.EndScrollView();
}
}
}
void PickTexFromLightmap()
{
Vector4 bound;
Vector4 scaleBound;
Rect pickRect;
_tex = LightMapUtil.PickLightmap(_curSelectObj as GameObject, out bound, out scaleBound, out pickRect);
return;
try
{
var lpData = LightmapSettings.lightmaps;
var go = _curSelectObj as GameObject;
if (go == null) return;
var renderer = go.GetComponent<MeshRenderer>();
if (renderer == null)
return;
var meshFileter = go.GetComponent<MeshFilter>();
_lightScaleInfo = renderer.lightmapScaleOffset;
_lightIndex = renderer.lightmapIndex;
var tex = lpData[_lightIndex].lightmapDir;
if (tex == null)
tex = lpData[_lightIndex].lightmapColor;
var uv2s = LightMapUtil.GetMeshUV2(meshFileter.sharedMesh);
var sourceBounds = LightMapUtil.GetBounds(uv2s, renderer);
var bounds = new Vector4(sourceBounds.x * renderer.lightmapScaleOffset.x + renderer.lightmapScaleOffset.z,
sourceBounds.y * renderer.lightmapScaleOffset.y + renderer.lightmapScaleOffset.w,
sourceBounds.z * renderer.lightmapScaleOffset.x + renderer.lightmapScaleOffset.z,
sourceBounds.w * renderer.lightmapScaleOffset.y + renderer.lightmapScaleOffset.w);
_blockW = (int)((bounds.z - bounds.x )* tex.width);
_blockH = (int)((bounds.w - bounds.y) * tex.height);
int startX = (int)(bounds.x * tex.width);
int startY = (int)(bounds.y * tex.height);
_boundsInPixel.x = startX;
_boundsInPixel.y = startY;
_boundsInPixel.z = _blockW;
_boundsInPixel.w = _blockH;
startY = (tex.height - startY - _blockH);
if (_blockH == 0 || _blockW == 0)
return;
var colors = tex.GetPixels(startX, startY, _blockW, _blockH);
Texture2D tex2d = new Texture2D(_blockW, _blockH);
tex2d.SetPixels(colors);
tex2d.Apply();
_tex = tex2d;
}
catch(Exception ex)
{
UnityEngine.Debug.LogException(ex);
}
}
}
}