yum-archive/2ner

A toon shader for Unity's BIRP.

git clone https://git.yummers.dev/yum-archive/2ner

yumAdd oklab color clamp & fix light volumes shading7db318d

master
4.1 KiB102 linesraw
1#if UNITY_EDITOR
2
3using UnityEngine;
4using UnityEditor;
5using System.IO;
6
7public class GenerateMetallicGlossMap : EditorWindow
8{
9    private Texture2D metallicMap;
10    private Texture2D smoothnessMap;
11    private bool invertSmoothness = false;
12
13    [MenuItem("Tools/yum_food/GenerateMetallicGlossMap")]
14    public static void ShowWindow()
15    {
16        GetWindow<GenerateMetallicGlossMap>("Metallic Gloss Map Generator");
17    }
18
19    private void OnGUI()
20    {
21        GUILayout.Label("Metallic Gloss Map Generator", EditorStyles.boldLabel);
22
23        metallicMap = (Texture2D)EditorGUILayout.ObjectField(
24            "Metallic Map (R)", metallicMap, typeof(Texture2D), false);
25        smoothnessMap = (Texture2D)EditorGUILayout.ObjectField(
26            "Smoothness Map (R)", smoothnessMap, typeof(Texture2D), false);
27        
28        invertSmoothness = EditorGUILayout.Toggle("Invert Smoothness", invertSmoothness);
29
30        if (GUILayout.Button("Generate Metallic Gloss Map") && smoothnessMap != null)
31        {
32            GenerateMap();
33        }
34    }
35
36    private void GenerateMap()
37    {
38        // Get path and determine output location
39        string directory = metallicMap != null 
40            ? Path.GetDirectoryName(AssetDatabase.GetAssetPath(metallicMap))
41            : Path.GetDirectoryName(AssetDatabase.GetAssetPath(smoothnessMap));
42        string filename = metallicMap != null ? metallicMap.name : "black";
43        string newPath = Path.Combine(directory, "metallic_gloss.png");
44
45        // Create new texture
46        int width = metallicMap != null ? metallicMap.width : smoothnessMap.width;
47        int height = metallicMap != null ? metallicMap.height : smoothnessMap.height;
48        Texture2D combinedTexture = new Texture2D(width, height, TextureFormat.RGBA32, false);
49
50        // Make the smoothness texture readable
51        TextureImporter smoothnessImporter = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(smoothnessMap)) as TextureImporter;
52        bool smoothnessReadable = smoothnessImporter.isReadable;
53        smoothnessImporter.isReadable = true;
54        AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(smoothnessMap));
55
56        // Handle metallic texture if it exists
57        TextureImporter metallicImporter = null;
58        bool metallicReadable = false;
59        if (metallicMap != null)
60        {
61            metallicImporter = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(metallicMap)) as TextureImporter;
62            metallicReadable = metallicImporter.isReadable;
63            metallicImporter.isReadable = true;
64            AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(metallicMap));
65        }
66
67        // Get pixels
68        Color[] smoothnessPixels = smoothnessMap.GetPixels();
69        Color[] metallicPixels = metallicMap != null ? metallicMap.GetPixels() : new Color[smoothnessPixels.Length];
70        Color[] newPixels = new Color[smoothnessPixels.Length];
71
72        // Combine channels (R from metallic or black, A from smoothness)
73        for (int i = 0; i < smoothnessPixels.Length; i++)
74        {
75            float metallic = metallicMap != null ? metallicPixels[i].r : 0f;  // Use 0 (black) if no metallic map
76            float smoothness = invertSmoothness ? 1 - smoothnessPixels[i].r : smoothnessPixels[i].r;
77            newPixels[i] = new Color(metallic, 0, 0, smoothness);
78        }
79
80        // Apply pixels and save
81        combinedTexture.SetPixels(newPixels);
82        combinedTexture.Apply();
83
84        // Encode and save
85        byte[] bytes = combinedTexture.EncodeToPNG();
86        File.WriteAllBytes(newPath, bytes);
87
88        // Restore original import settings
89        if (metallicMap != null)
90        {
91            metallicImporter.isReadable = metallicReadable;
92            AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(metallicMap));
93        }
94        smoothnessImporter.isReadable = smoothnessReadable;
95        AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(smoothnessMap));
96        AssetDatabase.ImportAsset(newPath);
97
98        Debug.Log("Generated metallic gloss map at: " + newPath);
99    }
100}
101
102#endif  // UNITY_EDITOR