-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CubemapExtractor.cs
99 lines (82 loc) · 3.02 KB
/
CubemapExtractor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Linq;
public class CubemapExtractor : Editor
{
private static readonly CubemapFace[] CubemapFaces = new CubemapFace[]
{
CubemapFace.PositiveX, // Right
CubemapFace.NegativeX, // Left
CubemapFace.PositiveY, // Up
CubemapFace.NegativeY, // Down
CubemapFace.NegativeZ, // Back
CubemapFace.PositiveZ // Front
};
private static readonly string[] FaceNames = new string[]
{
"Right", "Left", "Up", "Down", "Back", "Front"
};
[MenuItem("Assets/Extract Cubemap Images", true)]
private static bool ValidateExtractCubemapImages()
{
return Selection.objects.All(obj => obj is Cubemap);
}
[MenuItem("Assets/Extract Cubemap Images")]
private static void ExtractCubemapImages()
{
foreach (Object obj in Selection.objects)
{
if (obj is Cubemap cubemap)
{
string assetPath = AssetDatabase.GetAssetPath(cubemap);
string folderPath = Path.Combine(Path.GetDirectoryName(assetPath), cubemap.name);
string fileName = cubemap.name;
if (!Directory.Exists(folderPath))
Directory.CreateDirectory(folderPath);
for (int i = 0; i < 6; i++)
{
CubemapFace face = CubemapFaces[i];
Texture2D faceTexture = new Texture2D(cubemap.width, cubemap.height, TextureFormat.RGBA32, false);
faceTexture.SetPixels(MirrorTexture(FlipTexture(cubemap.GetPixels(face))));
faceTexture.Apply();
byte[] bytes = faceTexture.EncodeToPNG();
string saveFilePath = Path.Combine(folderPath, $"{FaceNames[i]}.png");
File.WriteAllBytes(saveFilePath, bytes);
Debug.Log($"Saved {FaceNames[i]}.png at {saveFilePath}");
AssetDatabase.Refresh();
}
}
}
}
private static Color[] FlipTexture(Color[] pixels)
{
int width = Mathf.FloorToInt(Mathf.Sqrt(pixels.Length));
int height = width;
Color[] flippedPixels = new Color[pixels.Length];
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int flippedY = height - 1 - y;
flippedPixels[y * width + x] = pixels[flippedY * width + x];
}
}
return flippedPixels;
}
private static Color[] MirrorTexture(Color[] pixels)
{
int width = Mathf.FloorToInt(Mathf.Sqrt(pixels.Length));
int height = width;
Color[] mirroredPixels = new Color[pixels.Length];
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int mirroredX = width - 1 - x;
mirroredPixels[y * width + x] = pixels[y * width + mirroredX];
}
}
return mirroredPixels;
}
}