summaryrefslogtreecommitdiffstats
path: root/Scripts/Editor/StackTextures3D.cs
blob: c3c65c2e7c7155226b393d7f1b806053176abd76 (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using UnityEngine;
using UnityEditor;

public class StackTextures3D : EditorWindow
{
    Texture2D[] _slices = new Texture2D[0];
    SerializedObject _so;
    SerializedProperty _slicesProp;
    [SerializeField] bool reverseStackOrder = false;

    [MenuItem("Tools/yum_food/Stack Textures to 3D")]
    static void Open() => GetWindow<StackTextures3D>("Stack Textures to 3D");

    void OnEnable()
    {
        _so = new SerializedObject(this);
    }

    // SerializedObject needs a backing field with SerializeField for array GUI.
    // Unity EditorWindow supports this natively.
    [SerializeField] Texture2D[] slices = new Texture2D[0];

    void OnGUI()
    {
        _so.Update();

        EditorGUILayout.LabelField("Stack Textures to 3D", EditorStyles.boldLabel);
        EditorGUILayout.HelpBox(
            "Provide an ordered array of 2D textures. Each texture becomes one depth slice. " +
            "All textures must share the same dimensions and format.",
            MessageType.Info);

        var prop = _so.FindProperty("slices");
        EditorGUILayout.PropertyField(prop, new GUIContent("Slices"), true);
        EditorGUILayout.PropertyField(
            _so.FindProperty("reverseStackOrder"),
            new GUIContent("Reverse Stack Order"));
        _so.ApplyModifiedProperties();

        if (slices == null || slices.Length == 0)
            return;

        EditorGUILayout.Space();

        // Show preview info.
        int nullCount = 0;
        foreach (var s in slices)
            if (s == null) nullCount++;

        if (nullCount > 0)
            EditorGUILayout.HelpBox($"{nullCount} null slot(s) — fill all slots before generating.", MessageType.Warning);

        if (nullCount == 0 && slices.Length > 0)
        {
            var first = slices[0];
            EditorGUILayout.LabelField($"Resolution: {first.width} x {first.height} x {slices.Length}");
        }

        EditorGUI.BeginDisabledGroup(nullCount > 0);
        if (GUILayout.Button("Generate"))
            DoGenerate();
        EditorGUI.EndDisabledGroup();
    }

    void DoGenerate()
    {
        if (slices.Length == 0)
            return;

        int width = slices[0].width;
        int height = slices[0].height;
        int depth = slices.Length;
        TextureFormat fmt = slices[0].format;

        for (int i = 1; i < depth; i++)
        {
            if (slices[i].width != width || slices[i].height != height)
            {
                EditorUtility.DisplayDialog("Error",
                    $"Slice {i} ({slices[i].name}) is {slices[i].width}x{slices[i].height}, " +
                    $"expected {width}x{height}.", "OK");
                return;
            }
            if (slices[i].format != fmt)
            {
                EditorUtility.DisplayDialog("Error",
                    $"Slice {i} ({slices[i].name}) format is {slices[i].format}, " +
                    $"expected {fmt}.", "OK");
                return;
            }
        }

        bool mipMaps = false;
        var tex = new Texture3D(width, height, depth, fmt, mipMaps);

        for (int z = 0; z < depth; z++)
        {
            int sourceIndex = reverseStackOrder ? (depth - 1 - z) : z;
            var pixels = slices[sourceIndex].GetPixels();
            for (int y = 0; y < height; y++)
            for (int x = 0; x < width; x++)
            {
                tex.SetPixel(x, y, z, pixels[y * width + x]);
            }
        }

        tex.wrapMode = TextureWrapMode.Repeat;
        tex.filterMode = FilterMode.Bilinear;
        tex.Apply();

        string dir = System.IO.Path.GetDirectoryName(AssetDatabase.GetAssetPath(slices[0]));
        string path = $"{dir}/Stacked3D_{width}x{height}x{depth}.asset";
        path = AssetDatabase.GenerateUniqueAssetPath(path);

        AssetDatabase.CreateAsset(tex, path);
        AssetDatabase.SaveAssets();
        EditorGUIUtility.PingObject(tex);
        Debug.Log($"[StackTextures3D] Saved {width}x{height}x{depth} ({fmt}) to {path}");
    }
}