summaryrefslogtreecommitdiffstats
path: root/Scripts/Editor/GenerateMetallicGlossMap.cs
blob: e3c6297294394185c3b03730f5932e6de07be7c6 (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
#if UNITY_EDITOR

using UnityEngine;
using UnityEditor;
using System.IO;

public class GenerateMetallicGlossMap : EditorWindow
{
    private Texture2D metallicMap;
    private Texture2D smoothnessMap;
    private bool invertSmoothness = false;

    [MenuItem("Tools/yum_food/GenerateMetallicGlossMap")]
    public static void ShowWindow()
    {
        GetWindow<GenerateMetallicGlossMap>("Metallic Gloss Map Generator");
    }

    private void OnGUI()
    {
        GUILayout.Label("Metallic Gloss Map Generator", EditorStyles.boldLabel);

        metallicMap = (Texture2D)EditorGUILayout.ObjectField(
            "Metallic Map (R)", metallicMap, typeof(Texture2D), false);
        smoothnessMap = (Texture2D)EditorGUILayout.ObjectField(
            "Smoothness Map (R)", smoothnessMap, typeof(Texture2D), false);
        
        invertSmoothness = EditorGUILayout.Toggle("Invert Smoothness", invertSmoothness);

        if (GUILayout.Button("Generate Metallic Gloss Map") && smoothnessMap != null)
        {
            GenerateMap();
        }
    }

    private void GenerateMap()
    {
        // Get path and determine output location
        string directory = metallicMap != null 
            ? Path.GetDirectoryName(AssetDatabase.GetAssetPath(metallicMap))
            : Path.GetDirectoryName(AssetDatabase.GetAssetPath(smoothnessMap));
        string filename = metallicMap != null ? metallicMap.name : "black";
        string newPath = Path.Combine(directory, filename + "_metallicgloss.png");

        // Create new texture
        int width = metallicMap != null ? metallicMap.width : smoothnessMap.width;
        int height = metallicMap != null ? metallicMap.height : smoothnessMap.height;
        Texture2D combinedTexture = new Texture2D(width, height, TextureFormat.RGBA32, false);

        // Make the smoothness texture readable
        TextureImporter smoothnessImporter = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(smoothnessMap)) as TextureImporter;
        bool smoothnessReadable = smoothnessImporter.isReadable;
        smoothnessImporter.isReadable = true;
        AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(smoothnessMap));

        // Handle metallic texture if it exists
        TextureImporter metallicImporter = null;
        bool metallicReadable = false;
        if (metallicMap != null)
        {
            metallicImporter = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(metallicMap)) as TextureImporter;
            metallicReadable = metallicImporter.isReadable;
            metallicImporter.isReadable = true;
            AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(metallicMap));
        }

        // Get pixels
        Color[] smoothnessPixels = smoothnessMap.GetPixels();
        Color[] metallicPixels = metallicMap != null ? metallicMap.GetPixels() : new Color[smoothnessPixels.Length];
        Color[] newPixels = new Color[smoothnessPixels.Length];

        // Combine channels (R from metallic or black, A from smoothness)
        for (int i = 0; i < smoothnessPixels.Length; i++)
        {
            float metallic = metallicMap != null ? metallicPixels[i].r : 0f;  // Use 0 (black) if no metallic map
            float smoothness = invertSmoothness ? 1 - smoothnessPixels[i].r : smoothnessPixels[i].r;
            newPixels[i] = new Color(metallic, 0, 0, smoothness);
        }

        // Apply pixels and save
        combinedTexture.SetPixels(newPixels);
        combinedTexture.Apply();

        // Encode and save
        byte[] bytes = combinedTexture.EncodeToPNG();
        File.WriteAllBytes(newPath, bytes);

        // Restore original import settings
        if (metallicMap != null)
        {
            metallicImporter.isReadable = metallicReadable;
            AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(metallicMap));
        }
        smoothnessImporter.isReadable = smoothnessReadable;
        AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(smoothnessMap));
        AssetDatabase.ImportAsset(newPath);

        Debug.Log("Generated metallic gloss map at: " + newPath);
    }
}

#endif  // UNITY_EDITOR