yum-archive/Tooner

A toon shader for Unity's BIRP.

git clone https://git.yummers.dev/yum-archive/Tooner

yumAdd 4x4 textures to ssfd9b52e08

master
5.0 KiB147 linesraw
1using UnityEngine;
2using UnityEditor;
3using System.Collections.Generic;
4using System.Linq;
5
6public class ImageSequenceToTexture3D : EditorWindow
7{
8    private string sourcePath = "";
9    private string textureName = "Texture3DFromSequence";
10    private FilterMode filterMode = FilterMode.Bilinear;
11    private TextureWrapMode wrapMode = TextureWrapMode.Repeat;
12
13    [MenuItem("Tools/yum_food/Image Sequence to Texture3D")]
14    public static void ShowWindow()
15    {
16        GetWindow<ImageSequenceToTexture3D>("Image Sequence to Texture3D");
17    }
18
19    private void OnGUI()
20    {
21        GUILayout.Label("Image Sequence to Texture3D Converter", EditorStyles.boldLabel);
22        EditorGUILayout.HelpBox("Select a folder containing the image sequence (sorted by filename)", MessageType.Info);
23
24        EditorGUILayout.BeginHorizontal();
25        sourcePath = EditorGUILayout.TextField("Source Folder", sourcePath);
26        if (GUILayout.Button("Browse", GUILayout.Width(60)))
27        {
28            string path = EditorUtility.OpenFolderPanel("Select Image Sequence Folder", "", "");
29            if (!string.IsNullOrEmpty(path))
30            {
31                sourcePath = path;
32            }
33        }
34        EditorGUILayout.EndHorizontal();
35
36        textureName = EditorGUILayout.TextField("Texture Name", textureName);
37        filterMode = (FilterMode)EditorGUILayout.EnumPopup("Filter Mode", filterMode);
38        wrapMode = (TextureWrapMode)EditorGUILayout.EnumPopup("Wrap Mode", wrapMode);
39
40        if (GUILayout.Button("Generate 3D Texture"))
41        {
42            if (ValidateInputs())
43            {
44                Generate3DTexture();
45            }
46        }
47    }
48
49    private bool ValidateInputs()
50    {
51        if (string.IsNullOrEmpty(sourcePath))
52        {
53            EditorUtility.DisplayDialog("Error", "Please select a source folder.", "OK");
54            return false;
55        }
56
57        string[] files = GetImageFiles();
58        if (files.Length == 0)
59        {
60            EditorUtility.DisplayDialog("Error", "No supported image files found in the selected folder.", "OK");
61            return false;
62        }
63
64        // Load first image to check dimensions
65        Texture2D firstImage = LoadImage(files[0]);
66        int width = firstImage.width;
67        int height = firstImage.height;
68        DestroyImmediate(firstImage);
69
70        // Verify all images have the same dimensions
71        for (int i = 1; i < files.Length; i++)
72        {
73            Texture2D img = LoadImage(files[i]);
74            if (img.width != width || img.height != height)
75            {
76                DestroyImmediate(img);
77                EditorUtility.DisplayDialog("Error",
78                    $"All images must have the same dimensions. Expected {width}x{height}, but image {files[i]} is {img.width}x{img.height}",
79                    "OK");
80                return false;
81            }
82            DestroyImmediate(img);
83        }
84
85        return true;
86    }
87
88    private string[] GetImageFiles()
89    {
90        string[] files = System.IO.Directory.GetFiles(sourcePath, "*.*")
91            .Where(file => file.ToLower().EndsWith(".png") || 
92                          file.ToLower().EndsWith(".jpg") || 
93                          file.ToLower().EndsWith(".jpeg"))
94            .OrderBy(file => file)
95            .ToArray();
96        return files;
97    }
98
99    private Texture2D LoadImage(string path)
100    {
101        byte[] fileData = System.IO.File.ReadAllBytes(path);
102        Texture2D tex = new Texture2D(2, 2);
103        tex.LoadImage(fileData);
104        return tex;
105    }
106
107    private void Generate3DTexture()
108    {
109        string[] files = GetImageFiles();
110        if (files.Length == 0) return;
111
112        Texture2D firstImage = LoadImage(files[0]);
113        int width = firstImage.width;
114        int height = firstImage.height;
115        int depth = files.Length;
116        DestroyImmediate(firstImage);
117
118        // Create the 3D texture
119        Texture3D texture3D = new Texture3D(width, height, depth, TextureFormat.RGBA32, false);
120        texture3D.filterMode = filterMode;
121        texture3D.wrapMode = wrapMode;
122
123        // Prepare the color array
124        Color[] colors = new Color[width * height * depth];
125
126        // Copy the pixel data from each source image
127        for (int z = 0; z < depth; z++)
128        {
129            Debug.Log($"Processing layer {z + 1} of {depth}: {System.IO.Path.GetFileName(files[z])}");
130            Texture2D img = LoadImage(files[z]);
131            Color[] imageColors = img.GetPixels();
132            System.Array.Copy(imageColors, 0, colors, z * width * height, width * height);
133            DestroyImmediate(img);
134        }
135
136        texture3D.SetPixels(colors);
137        texture3D.Apply();
138
139        // Save the texture asset
140        string path = $"Assets/{textureName}.asset";
141        AssetDatabase.CreateAsset(texture3D, path);
142        AssetDatabase.SaveAssets();
143        AssetDatabase.Refresh();
144
145        EditorUtility.DisplayDialog("Success", $"3D texture generated and saved at {path}", "OK");
146    }
147}