Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Smoothness TextureからUnity Standard Shader用MetallicSmoothnessテクスチャを作ります(複数枚対応)(Metallicは固定値)
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
public class MetallicSmoothnessTextureGenerator : EditorWindow
{
private float _metallic;
private bool _isRoughness;
private string _suffix;
[SerializeField] private List<Texture2D> smoothnessTextures;
[MenuItem("HALBY/MetallicSmoothness Texture Generator")]
private static void Create()
{
// 生成
GetWindow<MetallicSmoothnessTextureGenerator>("MetallicSmoothness Texture Generator");
}
private void OnGUI()
{
var so = new SerializedObject(this);
so.Update();
_metallic = EditorGUILayout.Slider("Metallic", _metallic, 0.0f, 1.0f);
_suffix = EditorGUILayout.TextField("Suffix", _suffix);
_isRoughness = EditorGUILayout.Toggle("Is Roughness", _isRoughness);
EditorGUILayout.PropertyField(so.FindProperty("smoothnessTextures"), true);
so.ApplyModifiedProperties();
// if (GUILayout.Button("Path Test"))
// {
// foreach (var texture in smoothnessTextures)
// {
// var pathWithExt = AssetDatabase.GetAssetPath(texture);
// var assetPath = Regex.Replace(pathWithExt, ".[^.]+$", string.Empty);
// var fullPath = Path.GetFullPath(assetPath);
// Debug.Log(fullPath);
// }
// }
if (GUILayout.Button("Generate"))
{
var ms = smoothnessTextures.Select(t =>
{
var newTex = new Texture2D(t.width, t.height, TextureFormat.RGBAFloat, false);
var colors = t.GetPixels().Select(c =>
new Color(_metallic, _metallic, _metallic, _isRoughness ? 1 - c.r : c.r));
newTex.SetPixels(colors.ToArray());
return (newTex, AssetDatabase.GetAssetPath(t));
});
foreach (var (tex, pathWithExt) in ms)
{
var assetPath = Regex.Replace(pathWithExt, ".[^.]+$", string.Empty);
var fullPath = Path.GetFullPath(assetPath) + _suffix + ".png";
// Debug.Log(fullPath);
var png = tex.EncodeToPNG();
File.WriteAllBytes(fullPath, png);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment