Skip to content

Instantly share code, notes, and snippets.

@halby24
Last active February 7, 2020 11:49
Show Gist options
  • Save halby24/e14f9c8798399b28a6d70bca23a06ec5 to your computer and use it in GitHub Desktop.
Save halby24/e14f9c8798399b28a6d70bca23a06ec5 to your computer and use it in GitHub Desktop.
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