From 01eeb68639ee4e3a5aeacf321c35a46c7dfe5c3d Mon Sep 17 00:00:00 2001 From: yum Date: Thu, 20 Feb 2025 17:58:06 -0800 Subject: Add metallic+gloss map --- 2ner.shader | 8 ++- Scripts/Editor/GenerateMetallicGlossMap.cs | 94 ++++++++++++++++++++++++++++++ features.cginc | 4 +- globals.cginc | 9 ++- yum_pbr.cginc | 17 +++++- 5 files changed, 123 insertions(+), 9 deletions(-) create mode 100644 Scripts/Editor/GenerateMetallicGlossMap.cs diff --git a/2ner.shader b/2ner.shader index b3dd563..98afd23 100644 --- a/2ner.shader +++ b/2ner.shader @@ -42,12 +42,16 @@ Shader "yum_food/2ner" [HideInInspector] m_end_AO("Metallics", Float) = 0 //endex + //ifex _Metallics_Enabled==0 [HideInInspector] m_reflectionOptions("Reflections", Float) = 0 - [HideInInspector] m_start_Metallic("Metallics", Float) = 0 + [HideInInspector] m_start_Metallic("Metallics", Float) = 0 + [ThryToggle(_METALLICS)]_Metallics_Enabled("Enable", Float) = 0 _MetallicMask("Metallic Mask", 2D) = "white" {} _Metallic("Metallic", Range(0, 1)) = 0 _Smoothness("Smoothness", Range(0, 1)) = 0 - [HideInInspector] m_end_Metallic("Metallics", Float) = 0 + _MetallicGlossMap("Metallic gloss map", 2D) = "white" {} + [HideInInspector] m_end_Metallic("Metallics", Float) = 0 + //endex [HideInInspector] m_gimmicks("Gimmicks", Float) = 0 //ifex _Outlines_Enabled==0 diff --git a/Scripts/Editor/GenerateMetallicGlossMap.cs b/Scripts/Editor/GenerateMetallicGlossMap.cs new file mode 100644 index 0000000..78f54cf --- /dev/null +++ b/Scripts/Editor/GenerateMetallicGlossMap.cs @@ -0,0 +1,94 @@ +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("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") && metallicMap != null && smoothnessMap != null) + { + GenerateMap(); + } + } + + private void GenerateMap() + { + // Get path of metallic map + string path = AssetDatabase.GetAssetPath(metallicMap); + string directory = Path.GetDirectoryName(path); + string newPath = Path.Combine(directory, metallicMap.name + "_metallicgloss.png"); + + // Create new texture + int width = metallicMap.width; + int height = metallicMap.height; + Texture2D combinedTexture = new Texture2D(width, height, TextureFormat.RGBA32, false); + + // Make the texture readable + TextureImporter metallicImporter = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(metallicMap)) as TextureImporter; + TextureImporter smoothnessImporter = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(smoothnessMap)) as TextureImporter; + + bool metallicReadable = metallicImporter.isReadable; + bool smoothnessReadable = smoothnessImporter.isReadable; + + metallicImporter.isReadable = true; + smoothnessImporter.isReadable = true; + + AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(metallicMap)); + AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(smoothnessMap)); + + // Get pixels + Color[] metallicPixels = metallicMap.GetPixels(); + Color[] smoothnessPixels = smoothnessMap.GetPixels(); + Color[] newPixels = new Color[metallicPixels.Length]; + + // Combine channels (R from metallic, A from smoothness) + for (int i = 0; i < metallicPixels.Length; i++) + { + float smoothness = invertSmoothness ? 1 - smoothnessPixels[i].r : smoothnessPixels[i].r; + newPixels[i] = new Color( + metallicPixels[i].r, // Metallic in R + 0, // Empty G + 0, // Empty B + smoothness // Smoothness in A (inverted if flag is set) + ); + } + + // Apply pixels and save + combinedTexture.SetPixels(newPixels); + combinedTexture.Apply(); + + // Encode and save + byte[] bytes = combinedTexture.EncodeToPNG(); + File.WriteAllBytes(newPath, bytes); + + // Restore original import settings + metallicImporter.isReadable = metallicReadable; + smoothnessImporter.isReadable = smoothnessReadable; + + AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(metallicMap)); + AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(smoothnessMap)); + AssetDatabase.ImportAsset(newPath); + + Debug.Log("Generated metallic gloss map at: " + newPath); + } +} diff --git a/features.cginc b/features.cginc index 3f720ec..0bfe479 100644 --- a/features.cginc +++ b/features.cginc @@ -21,8 +21,8 @@ #pragma shader_feature_local _QUANTIZE_SPECULAR //endex -//ifex _Quantize_Diffuse_Enabled==0 -#pragma shader_feature_local _QUANTIZE_DIFFUSE +//ifex _Metallics_Enabled==0 +#pragma shader_feature_local _METALLICS //endex //ifex _Outlines_Enabled==0 diff --git a/globals.cginc b/globals.cginc index 0deee5e..1347a52 100644 --- a/globals.cginc +++ b/globals.cginc @@ -45,14 +45,19 @@ float _Quantize_Diffuse_Steps; float _Clip; int _Mode; -float _Smoothness; -float _Metallic; float _Spherical_Harmonics; float _reflectance; float _specularAntiAliasingVariance; float _specularAntiAliasingThreshold; +#if defined(_METALLICS) +float _Smoothness; +float _Metallic; +sampler2D _MetallicGlossMap; +float4 _MetallicGlossMap_ST; +#endif + #if defined(OUTLINE_PASS) float4 _Outline_Color; float _Outline_Width; diff --git a/yum_pbr.cginc b/yum_pbr.cginc index 93c615d..2f7113b 100644 --- a/yum_pbr.cginc +++ b/yum_pbr.cginc @@ -34,12 +34,23 @@ YumPbr GetYumPbr(v2f i) { float3x3 tangentToWorld = float3x3(i.tangent, i.binormal, i.normal); result.normal = normalize(mul(normal_raw, tangentToWorld)); - result.smoothness = _Smoothness; +#if defined(_METALLICS) + float4 metallic_gloss = tex2D(_MetallicGlossMap, UV_SCOFF(i, _MetallicGlossMap_ST, /*which_channel=*/0)); + float metallic = metallic_gloss.r * _Metallic; + float smoothness = metallic_gloss.a * _Smoothness; + + result.smoothness = smoothness; result.roughness_perceptual = normalFiltering(1.0 - result.smoothness, result.normal); result.roughness = result.roughness_perceptual * result.roughness_perceptual; - - result.metallic = _Metallic; + result.metallic = metallic; +#else + result.smoothness = 0.2; + result.roughness_perceptual = + normalFiltering(1.0 - result.smoothness, result.normal); + result.roughness = result.roughness_perceptual * result.roughness_perceptual; + result.metallic = 0; +#endif #if defined(_AMBIENT_OCCLUSION) result.ao = lerp(1, tex2D(_OcclusionMap, i.uv01), _OcclusionStrength); -- cgit v1.2.3