Files
HauntedBloodlines/Assets/Editor/CreateMaterialsFromTextures.cs
2025-05-29 22:31:40 +03:00

177 lines
6.2 KiB
C#

using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
public class CreateHDRPMaterials : EditorWindow
{
private string texturesFolderPath = "";
private string materialsFolderPath = "";
private float defaultMetallic = 0f;
private float defaultSmoothness = 0.5f;
private Shader hdrpShader;
private static readonly string[] baseColorKeys = { "albedo", "albedotransparency", "basecolor" };
private static readonly string[] normalMapKeys = { "normal", "normalmap" };
private static readonly string[] maskMapKeys = { "metallic", "metallicsmoothness", "maskmap" };
private static readonly string[] emissionKeys = { "emission", "emissive" };
[MenuItem("Tools/HDRP: Create Materials From Textures")]
public static void ShowWindow()
{
GetWindow<CreateHDRPMaterials>("HDRP Material Creator");
}
private void OnGUI()
{
GUILayout.Label("HDRP Material Creator", EditorStyles.boldLabel);
EditorGUILayout.BeginHorizontal();
texturesFolderPath = EditorGUILayout.TextField("Textures Folder Path", texturesFolderPath);
if (GUILayout.Button("Select", GUILayout.MaxWidth(60)))
{
string path = EditorUtility.OpenFolderPanel("Select Texture Folder", "Assets", "");
if (!string.IsNullOrEmpty(path) && path.StartsWith(Application.dataPath))
{
texturesFolderPath = "Assets" + path.Substring(Application.dataPath.Length);
}
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
materialsFolderPath = EditorGUILayout.TextField("Materials Output Path", materialsFolderPath);
if (GUILayout.Button("Select", GUILayout.MaxWidth(60)))
{
string path = EditorUtility.OpenFolderPanel("Select Output Folder", "Assets", "");
if (!string.IsNullOrEmpty(path) && path.StartsWith(Application.dataPath))
{
materialsFolderPath = "Assets" + path.Substring(Application.dataPath.Length);
}
}
EditorGUILayout.EndHorizontal();
defaultMetallic = EditorGUILayout.Slider("Default Metallic", defaultMetallic, 0f, 1f);
defaultSmoothness = EditorGUILayout.Slider("Default Smoothness", defaultSmoothness, 0f, 1f);
if (GUILayout.Button("Create HDRP Materials"))
{
CreateMaterials();
}
}
private void CreateMaterials()
{
hdrpShader = Shader.Find("HDRP/Lit");
if (hdrpShader == null)
{
Debug.LogError("HDRP/Lit shader not found.");
return;
}
if (!Directory.Exists(texturesFolderPath) || !Directory.Exists(materialsFolderPath))
{
Debug.LogError("Folders not found.");
return;
}
AssetDatabase.Refresh();
string[] guids = AssetDatabase.FindAssets("t:Texture2D", new[] { texturesFolderPath });
Dictionary<string, Dictionary<string, Texture2D>> grouped = new();
foreach (string guid in guids)
{
string path = AssetDatabase.GUIDToAssetPath(guid);
Texture2D tex = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
if (tex == null) continue;
string fileName = Path.GetFileNameWithoutExtension(path);
string[] parts = fileName.Split('_');
if (parts.Length < 2) continue;
string suffix = parts[^1].ToLower();
string name = string.Join("_", parts, 1, parts.Length - 2).ToLower(); // supports: T_bottles_01_Albedo
if (parts.Length == 2) name = parts[1].ToLower(); // supports: T_bottles_Albedo
if (!grouped.ContainsKey(name))
grouped[name] = new Dictionary<string, Texture2D>();
grouped[name][suffix] = tex;
}
foreach (var pair in grouped)
{
string objectName = pair.Key;
var textures = pair.Value;
Material mat = new Material(hdrpShader);
mat.SetFloat("_Metallic", defaultMetallic);
mat.SetFloat("_Smoothness", defaultSmoothness);
Texture2D tex;
// BaseColor
if (TryGetTexture(textures, baseColorKeys, out tex))
{
mat.SetTexture("_BaseColorMap", tex);
mat.SetColor("_BaseColor", Color.white);
}
// NormalMap
if (TryGetTexture(textures, normalMapKeys, out tex))
{
EnsureNormalMapType(tex);
mat.SetTexture("_NormalMap", tex);
mat.EnableKeyword("_NORMALMAP");
}
// MaskMap
if (TryGetTexture(textures, maskMapKeys, out tex))
{
mat.SetTexture("_MaskMap", tex);
mat.EnableKeyword("_MASKMAP");
mat.SetFloat("_Metallic", 1f);
}
// Emission (optional)
if (TryGetTexture(textures, emissionKeys, out tex))
{
mat.SetTexture("_EmissiveColorMap", tex);
mat.EnableKeyword("_EMISSIVE_COLOR_MAP");
mat.SetColor("_EmissiveColor", Color.white);
}
string matPath = Path.Combine(materialsFolderPath, $"M_{objectName}_HDRP.mat");
AssetDatabase.CreateAsset(mat, matPath);
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Debug.Log(" HDRP Materials created (simple & complex names supported).");
}
private static bool TryGetTexture(Dictionary<string, Texture2D> texDict, string[] possibleKeys, out Texture2D found)
{
foreach (string key in possibleKeys)
{
if (texDict.TryGetValue(key, out found))
return true;
}
found = null;
return false;
}
private void EnsureNormalMapType(Texture2D normalTex)
{
string path = AssetDatabase.GetAssetPath(normalTex);
TextureImporter importer = (TextureImporter)TextureImporter.GetAtPath(path);
if (importer != null && importer.textureType != TextureImporterType.NormalMap)
{
importer.textureType = TextureImporterType.NormalMap;
importer.SaveAndReimport();
}
}
}