This commit is contained in:
CortexCore
2024-11-20 11:36:36 +08:00
parent 99253854e8
commit 6cc53eb9dc
383 changed files with 21233 additions and 112159 deletions

View File

@@ -0,0 +1,170 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Pinwheel.Jupiter;
using UnityEditor;
using System.IO;
namespace Pinwheel.Jupiter
{
public class JCubemapCreatorWindow : EditorWindow
{
public Vector3 CameraPosition { get; set; }
public float CameraNearPlane { get; set; }
public float CameraFarPlane { get; set; }
public CameraClearFlags CameraClearFlag { get; set; }
public Color CameraBackgroundColor { get; set; }
public int Resolution { get; set; }
public bool ExportFaceTextures { get; set; }
public string Directory { get; set; }
public static readonly int[] ResolutionValues = new int[] { 16, 32, 64, 128, 256, 512, 1024, 2048 };
public static readonly string[] ResolutionLabels = new string[] { "16", "32", "64", "128", "256", "512", "1024", "2048" };
public const string PREF_PREFIX = "cubemap-creator";
public const string CAM_POS_X = "cam-pos-x";
public const string CAM_POS_Y = "cam-pos-y";
public const string CAM_POS_Z = "cam-pos-z";
public const string CAM_NEAR_PLANE = "cam-near-plane";
public const string CAM_FAR_PLANE = "cam-far-plane";
public const string CAM_CLEAR_FLAG = "cam-clear-flag";
public const string CAM_BG_COLOR = "cam-background-color";
public const string RESOLUTION = "resolution";
public const string EXPORT_FACE_TEXTURES = "export-face-textures";
public const string DIRECTORY = "directory";
public static void ShowWindow()
{
JCubemapCreatorWindow window = EditorWindow.CreateInstance<JCubemapCreatorWindow>();
window.titleContent = new GUIContent("Cubemap Creator");
window.minSize = new Vector2(400, 300);
window.Show();
}
private void OnEnable()
{
float x = EditorPrefs.GetFloat(JEditorCommon.GetProjectRelatedEditorPrefsKey(PREF_PREFIX, CAM_POS_X), 0);
float y = EditorPrefs.GetFloat(JEditorCommon.GetProjectRelatedEditorPrefsKey(PREF_PREFIX, CAM_POS_Y), 0);
float z = EditorPrefs.GetFloat(JEditorCommon.GetProjectRelatedEditorPrefsKey(PREF_PREFIX, CAM_POS_Z), 0);
CameraPosition = new Vector3(x, y, z);
CameraNearPlane = EditorPrefs.GetFloat(JEditorCommon.GetProjectRelatedEditorPrefsKey(PREF_PREFIX, CAM_NEAR_PLANE), 0.1f);
CameraFarPlane = EditorPrefs.GetFloat(JEditorCommon.GetProjectRelatedEditorPrefsKey(PREF_PREFIX, CAM_FAR_PLANE), 100);
CameraClearFlag = (CameraClearFlags)EditorPrefs.GetInt(JEditorCommon.GetProjectRelatedEditorPrefsKey(PREF_PREFIX, CAM_CLEAR_FLAG), 0);
string htmlColor = EditorPrefs.GetString(JEditorCommon.GetProjectRelatedEditorPrefsKey(PREF_PREFIX, CAM_BG_COLOR), "FFFFFF");
Color c = Color.white;
ColorUtility.TryParseHtmlString("#" + htmlColor, out c);
CameraBackgroundColor = c;
Resolution = EditorPrefs.GetInt(JEditorCommon.GetProjectRelatedEditorPrefsKey(PREF_PREFIX, RESOLUTION), 512);
ExportFaceTextures = EditorPrefs.GetBool(JEditorCommon.GetProjectRelatedEditorPrefsKey(PREF_PREFIX, EXPORT_FACE_TEXTURES), false);
Directory = EditorPrefs.GetString(JEditorCommon.GetProjectRelatedEditorPrefsKey(PREF_PREFIX, DIRECTORY), "Assets/");
}
private void OnDisable()
{
EditorPrefs.SetFloat(JEditorCommon.GetProjectRelatedEditorPrefsKey(PREF_PREFIX, CAM_POS_X), CameraPosition.x);
EditorPrefs.SetFloat(JEditorCommon.GetProjectRelatedEditorPrefsKey(PREF_PREFIX, CAM_POS_Y), CameraPosition.y);
EditorPrefs.SetFloat(JEditorCommon.GetProjectRelatedEditorPrefsKey(PREF_PREFIX, CAM_POS_Z), CameraPosition.z);
EditorPrefs.SetFloat(JEditorCommon.GetProjectRelatedEditorPrefsKey(PREF_PREFIX, CAM_NEAR_PLANE), CameraNearPlane);
EditorPrefs.SetFloat(JEditorCommon.GetProjectRelatedEditorPrefsKey(PREF_PREFIX, CAM_FAR_PLANE), CameraFarPlane);
EditorPrefs.SetInt(JEditorCommon.GetProjectRelatedEditorPrefsKey(PREF_PREFIX, CAM_CLEAR_FLAG), (int)CameraClearFlag);
EditorPrefs.SetString(JEditorCommon.GetProjectRelatedEditorPrefsKey(PREF_PREFIX, CAM_BG_COLOR), ColorUtility.ToHtmlStringRGB(CameraBackgroundColor));
EditorPrefs.SetInt(JEditorCommon.GetProjectRelatedEditorPrefsKey(PREF_PREFIX, RESOLUTION), Resolution);
EditorPrefs.SetBool(JEditorCommon.GetProjectRelatedEditorPrefsKey(PREF_PREFIX, EXPORT_FACE_TEXTURES), ExportFaceTextures);
EditorPrefs.SetString(JEditorCommon.GetProjectRelatedEditorPrefsKey(PREF_PREFIX, DIRECTORY), Directory);
}
private void OnGUI()
{
DrawExportGUI();
}
private void DrawExportGUI()
{
string label = "Export";
string id = "export" + GetInstanceID();
JEditorCommon.Foldout(label, true, id, () =>
{
CameraPosition = JEditorCommon.InlineVector3Field("Position", CameraPosition);
CameraNearPlane = EditorGUILayout.FloatField("Near Plane", CameraNearPlane);
CameraFarPlane = EditorGUILayout.FloatField("Far Plane", CameraFarPlane);
CameraClearFlag = (CameraClearFlags)EditorGUILayout.EnumPopup("Clear Flags", CameraClearFlag);
if (CameraClearFlag == CameraClearFlags.Color)
{
CameraBackgroundColor = EditorGUILayout.ColorField("Background Color", CameraBackgroundColor);
}
Resolution = EditorGUILayout.IntPopup("Resolution", Resolution, ResolutionLabels, ResolutionValues);
ExportFaceTextures = EditorGUILayout.Toggle("Export Face Textures", ExportFaceTextures);
string dir = Directory;
JEditorCommon.BrowseFolder("Directory", ref dir);
Directory = dir;
GUI.enabled = !string.IsNullOrEmpty(Directory);
if (GUILayout.Button("Export"))
{
Export();
}
GUI.enabled = true;
});
}
private void Export()
{
JUtilities.EnsureDirectoryExists(Directory);
Cubemap cube = new Cubemap(Resolution, TextureFormat.ARGB32, false);
JCubemapRendererArgs args = new JCubemapRendererArgs()
{
CameraPosition = this.CameraPosition,
CameraNearPlane = this.CameraNearPlane,
CameraFarPlane = this.CameraFarPlane,
CameraClearFlag = this.CameraClearFlag,
CameraBackgroundColor = this.CameraBackgroundColor,
Resolution = this.Resolution,
Cubemap = cube,
Face = (CubemapFace)63
};
JCubemapRenderer.Render(args);
string fileName = Path.Combine(Directory, "Cubemap-" + JCommon.GetUniqueID() + ".cubemap");
AssetDatabase.CreateAsset(cube, fileName);
if (ExportFaceTextures)
{
ExportFace(cube, CubemapFace.PositiveX);
ExportFace(cube, CubemapFace.NegativeX);
ExportFace(cube, CubemapFace.PositiveY);
ExportFace(cube, CubemapFace.NegativeY);
ExportFace(cube, CubemapFace.PositiveZ);
ExportFace(cube, CubemapFace.NegativeZ);
}
AssetDatabase.Refresh();
EditorGUIUtility.PingObject(cube);
Selection.activeObject = cube;
}
private void ExportFace(Cubemap cube, CubemapFace face)
{
Texture2D tex = new Texture2D(cube.width, cube.height);
Color[] data = cube.GetPixels(face);
Color[] flipData = new Color[data.Length];
for (int y = 0; y < Resolution; ++y)
{
for (int x = 0; x < Resolution; ++x)
{
flipData[JUtilities.To1DIndex(x, y, Resolution)] = data[JUtilities.To1DIndex(Resolution - 1 - x, Resolution - 1 - y, Resolution)];
}
}
tex.SetPixels(flipData);
tex.Apply();
byte[] bytes = tex.EncodeToPNG();
string fileName = Path.Combine(Directory, cube.name + "-" + face.ToString() + ".png");
File.WriteAllBytes(fileName, bytes);
JUtilities.DestroyObject(tex);
}
}
}

View File

@@ -0,0 +1,138 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
namespace Pinwheel.Jupiter
{
public class JTwoPaneWindowWindow : EditorWindow
{
protected float toolbarHeight = EditorGUIUtility.singleLineHeight;
protected int toolbarOffsetX = 4;
protected int toolbarOverflowY = 5;
protected int rightPaneWidth = 300;
protected int rightPaneWidthMin = 200;
protected int rightPaneWidthMax = 400;
protected int resizeRectWidth = 10;
protected bool isResizinng = false;
protected Vector2 rightPaneScrollPos;
protected Rect LeftPaneRect
{
get
{
Rect r = new Rect();
r.size = new Vector2(position.size.x - rightPaneWidth, position.size.y - toolbarHeight - toolbarOverflowY);
r.position = new Vector2(0, toolbarHeight + toolbarOverflowY);
return r;
}
}
protected Rect RightPaneRect
{
get
{
Rect r = new Rect();
r.size = new Vector2(rightPaneWidth, position.size.y - toolbarHeight - toolbarOverflowY);
r.position = new Vector2(position.size.x - rightPaneWidth, toolbarHeight + toolbarOverflowY);
return r;
}
}
protected Rect ResizePaneRect
{
get
{
Rect r = new Rect();
r.position = new Vector2(position.size.x - rightPaneWidth - resizeRectWidth * 0.5f, toolbarHeight + toolbarOverflowY);
r.size = new Vector2(resizeRectWidth, position.size.y - toolbarHeight - toolbarOverflowY);
return r;
}
}
public void OnGUI()
{
DrawToolbar();
DrawLeftPane();
DrawRightPane();
HandleResize();
HandleRepaint();
}
private void DrawToolbar()
{
Rect r = EditorGUILayout.GetControlRect();
RectOffset offset = new RectOffset(toolbarOffsetX, toolbarOffsetX, 0, 0);
GUI.Box(offset.Add(r), string.Empty, EditorStyles.toolbar);
OnToolbarGUI(r);
}
protected virtual void OnToolbarGUI(Rect r)
{
}
private void DrawLeftPane()
{
OnLeftPaneGUI(LeftPaneRect);
}
protected virtual void OnLeftPaneGUI(Rect r)
{
}
private void DrawRightPane()
{
Color separatorColor = JEditorCommon.boxBorderColor;
JEditorCommon.DrawLine(
new Vector2(RightPaneRect.min.x - 2, RightPaneRect.min.y - 2),
new Vector2(RightPaneRect.min.x - 2, RightPaneRect.max.y),
separatorColor);
GUILayout.BeginArea(RightPaneRect);
rightPaneScrollPos = EditorGUILayout.BeginScrollView(rightPaneScrollPos);
OnRightPaneScrollViewGUI();
EditorGUILayout.EndScrollView();
GUILayout.EndArea();
}
protected virtual void OnRightPaneScrollViewGUI()
{
}
private void HandleResize()
{
EditorGUIUtility.AddCursorRect(ResizePaneRect, MouseCursor.ResizeHorizontal);
if (Event.current == null)
return;
if (Event.current.type == EventType.MouseDown &&
ResizePaneRect.Contains(Event.current.mousePosition))
{
isResizinng = true;
}
else if (Event.current.type == EventType.MouseUp)
{
isResizinng = false;
}
if (isResizinng)
{
rightPaneWidth = (int)(position.size.x - Event.current.mousePosition.x);
rightPaneWidth = Mathf.Clamp(rightPaneWidth, rightPaneWidthMin, rightPaneWidthMax);
}
}
private void HandleRepaint()
{
if (Event.current == null)
return;
Repaint();
}
}
}