From 8d51c908c0ef8a14bd1e145d5271b85df00e19d6 Mon Sep 17 00:00:00 2001 From: SimonDarksideJ Date: Sat, 17 Feb 2024 13:15:28 +0000 Subject: [PATCH 1/2] Updating Channel packer tool to fit under the asset store version of the Reality Toolkit --- Editor/ToolkitPreferences.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Editor/ToolkitPreferences.cs b/Editor/ToolkitPreferences.cs index 22589cc..401e5b8 100644 --- a/Editor/ToolkitPreferences.cs +++ b/Editor/ToolkitPreferences.cs @@ -8,7 +8,7 @@ namespace RealityCollective.Utilities.Editor { public static class ToolkitPreferences { - public const string Editor_Menu_Keyword = "Reality Collective"; + public const string Editor_Menu_Keyword = "Tools/Reality Toolkit"; #region Debug Symbolic Links From 6f7bf9c020ffaf24576d7dcfe3767639552db94e Mon Sep 17 00:00:00 2001 From: SimonDarksideJ Date: Sat, 17 Feb 2024 14:13:28 +0000 Subject: [PATCH 2/2] Remove Channel package from Utilities Remove editor preferences static, as each package has their own root --- Editor/ToolkitPreferences.cs | 2 - Editor/Utilities/ChannelPackerWindow.cs | 263 ------------------- Editor/Utilities/ChannelPackerWindow.cs.meta | 11 - 3 files changed, 276 deletions(-) delete mode 100644 Editor/Utilities/ChannelPackerWindow.cs delete mode 100644 Editor/Utilities/ChannelPackerWindow.cs.meta diff --git a/Editor/ToolkitPreferences.cs b/Editor/ToolkitPreferences.cs index 401e5b8..58c3638 100644 --- a/Editor/ToolkitPreferences.cs +++ b/Editor/ToolkitPreferences.cs @@ -8,8 +8,6 @@ namespace RealityCollective.Utilities.Editor { public static class ToolkitPreferences { - public const string Editor_Menu_Keyword = "Tools/Reality Toolkit"; - #region Debug Symbolic Links //private static readonly GUIContent DebugSymbolicContent = new GUIContent("Debug symbolic linking", "Enable or disable the debug information for symbolic linking.\n\nThis setting only applies to the currently running project."); diff --git a/Editor/Utilities/ChannelPackerWindow.cs b/Editor/Utilities/ChannelPackerWindow.cs deleted file mode 100644 index 8f05247..0000000 --- a/Editor/Utilities/ChannelPackerWindow.cs +++ /dev/null @@ -1,263 +0,0 @@ -// Copyright (c) Reality Collective. All rights reserved. -// Licensed under the MIT License. See LICENSE in the project root for license information. - -using RealityCollective.Utilities.Editor; -using System.IO; -using UnityEditor; -using UnityEngine; - -namespace RealityCollective.Editor.Utilities -{ - public class ChannelPackerWindow : EditorWindow - { - private enum Channel - { - Red = 0, - Green = 1, - Blue = 2, - Alpha = 3, - RGBAverage = 4 - } - - private const float DEFAULT_UNIFORM_VALUE = -0.01f; - private const string StandardShaderName = "Standard"; - private const string StandardRoughnessShaderName = "Standard (Roughness setup)"; - private const string StandardSpecularShaderName = "Standard (Specular setup)"; - - - private static readonly GUIContent metallicUniformContent = new GUIContent("Metallic Uniform"); - private static readonly GUIContent occlusionUniformContent = new GUIContent("Occlusion Uniform"); - private static readonly GUIContent smoothnessUniformContent = new GUIContent("Smoothness Uniform"); - private static readonly GUIContent emissionUniformContent = new GUIContent("Emission Uniform"); - - private static readonly int OcclusionMap = Shader.PropertyToID("_OcclusionMap"); - private static readonly int EmissionMap = Shader.PropertyToID("_EmissionMap"); - private static readonly int MetallicGlossMap = Shader.PropertyToID("_MetallicGlossMap"); - private static readonly int SmoothnessTextureChannel = Shader.PropertyToID("_SmoothnessTextureChannel"); - private static readonly int SpecGlossMap = Shader.PropertyToID("_SpecGlossMap"); - private static readonly int MainTex = Shader.PropertyToID("_MainTex"); - private static readonly int MetallicMap = Shader.PropertyToID("_MetallicMap"); - private static readonly int MetallicMapChannel = Shader.PropertyToID("_MetallicMapChannel"); - private static readonly int MetallicUniform = Shader.PropertyToID("_MetallicUniform"); - private static readonly int OcclusionMapChannel = Shader.PropertyToID("_OcclusionMapChannel"); - private static readonly int OcclusionUniform = Shader.PropertyToID("_OcclusionUniform"); - private static readonly int EmissionMapChannel = Shader.PropertyToID("_EmissionMapChannel"); - private static readonly int EmissionUniform = Shader.PropertyToID("_EmissionUniform"); - private static readonly int SmoothnessMap = Shader.PropertyToID("_SmoothnessMap"); - private static readonly int SmoothnessMapChannel = Shader.PropertyToID("_SmoothnessMapChannel"); - private static readonly int SmoothnessUniform = Shader.PropertyToID("_SmoothnessUniform"); - - private Texture2D metallicMap; - private Channel metallicMapChannel = Channel.Red; - private float metallicUniform = DEFAULT_UNIFORM_VALUE; - private Texture2D occlusionMap; - private Channel occlusionMapChannel = Channel.Green; - private float occlusionUniform = DEFAULT_UNIFORM_VALUE; - private Texture2D emissionMap; - private Channel emissionMapChannel = Channel.RGBAverage; - private float emissionUniform = DEFAULT_UNIFORM_VALUE; - private Texture2D smoothnessMap; - private Channel smoothnessMapChannel = Channel.Alpha; - private float smoothnessUniform = DEFAULT_UNIFORM_VALUE; - private Material standardMaterial; - - [MenuItem(ToolkitPreferences.Editor_Menu_Keyword + "/Tools/Channel Packer", false, 20)] - private static void ShowWindow() - { - var window = GetWindow(); - window.titleContent = new GUIContent("Channel Packer"); - window.minSize = new Vector2(380.0f, 680.0f); - window.Show(); - } - - private void OnGUI() - { - GUILayout.Label("Import", EditorStyles.boldLabel); - GUI.enabled = metallicUniform < 0.0f; - metallicMap = (Texture2D)EditorGUILayout.ObjectField("Metallic Map", metallicMap, typeof(Texture2D), false); - metallicMapChannel = (Channel)EditorGUILayout.EnumPopup("Input Channel", metallicMapChannel); - GUI.enabled = true; - metallicUniform = EditorGUILayout.Slider(metallicUniformContent, metallicUniform, DEFAULT_UNIFORM_VALUE, 1.0f); - GUILayout.Box("Output Channel: Red", EditorStyles.helpBox); - EditorGUILayout.Separator(); - GUI.enabled = occlusionUniform < 0.0f; - occlusionMap = (Texture2D)EditorGUILayout.ObjectField("Occlusion Map", occlusionMap, typeof(Texture2D), false); - occlusionMapChannel = (Channel)EditorGUILayout.EnumPopup("Input Channel", occlusionMapChannel); - GUI.enabled = true; - occlusionUniform = EditorGUILayout.Slider(occlusionUniformContent, occlusionUniform, DEFAULT_UNIFORM_VALUE, 1.0f); - GUILayout.Box("Output Channel: Green", EditorStyles.helpBox); - EditorGUILayout.Separator(); - GUI.enabled = emissionUniform < 0.0f; - emissionMap = (Texture2D)EditorGUILayout.ObjectField("Emission Map", emissionMap, typeof(Texture2D), false); - emissionMapChannel = (Channel)EditorGUILayout.EnumPopup("Input Channel", emissionMapChannel); - GUI.enabled = true; - emissionUniform = EditorGUILayout.Slider(emissionUniformContent, emissionUniform, DEFAULT_UNIFORM_VALUE, 1.0f); - GUILayout.Box("Output Channel: Blue", EditorStyles.helpBox); - EditorGUILayout.Separator(); - GUI.enabled = smoothnessUniform < 0.0f; - smoothnessMap = (Texture2D)EditorGUILayout.ObjectField("Smoothness Map", smoothnessMap, typeof(Texture2D), false); - smoothnessMapChannel = (Channel)EditorGUILayout.EnumPopup("Input Channel", smoothnessMapChannel); - GUI.enabled = true; - smoothnessUniform = EditorGUILayout.Slider(smoothnessUniformContent, smoothnessUniform, DEFAULT_UNIFORM_VALUE, 1.0f); - GUILayout.Box("Output Channel: Alpha", EditorStyles.helpBox); - EditorGUILayout.Separator(); - - standardMaterial = (Material)EditorGUILayout.ObjectField("Standard Material", standardMaterial, typeof(Material), false); - - GUI.enabled = standardMaterial != null && IsUnityStandardMaterial(standardMaterial); - - if (GUILayout.Button("Auto populate from Standard Material")) - { - AutoPopulate(); - } - - GUI.enabled = CanSave(); - - EditorGUILayout.Separator(); - - GUILayout.Label("Export", EditorStyles.boldLabel); - - if (GUILayout.Button("Save Channel Map")) - { - Save(); - } - - GUILayout.Box("Metallic (Red), Occlusion (Green), Emission (Blue), Smoothness (Alpha)", EditorStyles.helpBox); - } - - private void AutoPopulate() - { - metallicUniform = DEFAULT_UNIFORM_VALUE; - occlusionUniform = DEFAULT_UNIFORM_VALUE; - emissionUniform = DEFAULT_UNIFORM_VALUE; - smoothnessUniform = DEFAULT_UNIFORM_VALUE; - - occlusionMap = (Texture2D)standardMaterial.GetTexture(OcclusionMap); - occlusionMapChannel = occlusionMap != null ? Channel.Green : occlusionMapChannel; - emissionMap = (Texture2D)standardMaterial.GetTexture(EmissionMap); - emissionMapChannel = emissionMap != null ? Channel.RGBAverage : emissionMapChannel; - - switch (standardMaterial.shader.name) - { - case StandardShaderName: - metallicMap = (Texture2D)standardMaterial.GetTexture(MetallicGlossMap); - metallicMapChannel = metallicMap != null ? Channel.Red : metallicMapChannel; - smoothnessMap = ((int)standardMaterial.GetFloat(SmoothnessTextureChannel) == 0) - ? metallicMap - : (Texture2D)standardMaterial.GetTexture(MainTex); - smoothnessMapChannel = smoothnessMap != null ? Channel.Alpha : smoothnessMapChannel; - break; - case StandardRoughnessShaderName: - metallicMap = (Texture2D)standardMaterial.GetTexture(MetallicGlossMap); - metallicMapChannel = metallicMap != null ? Channel.Red : metallicMapChannel; - smoothnessMap = (Texture2D)standardMaterial.GetTexture(SpecGlossMap); - smoothnessMapChannel = smoothnessMap != null ? Channel.Red : smoothnessMapChannel; - break; - default: - smoothnessMap = ((int)standardMaterial.GetFloat(SmoothnessTextureChannel) == 0) - ? (Texture2D)standardMaterial.GetTexture(SpecGlossMap) - : (Texture2D)standardMaterial.GetTexture(MainTex); - - smoothnessMapChannel = smoothnessMap != null ? Channel.Alpha : smoothnessMapChannel; - break; - } - } - - private void Save() - { - var textures = new Texture[] { metallicMap, occlusionMap, emissionMap, smoothnessMap }; - CalculateChannelMapSize(textures, out int width, out int height); - var channelMap = new Texture2D(width, height); - - RenderTexture renderTexture = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Linear); - - // Use the GPU to pack the various texture maps into a single texture. - var channelPacker = new Material(Shader.Find("Hidden/ChannelPacker")); - - channelPacker.SetTexture(MetallicMap, metallicMap); - channelPacker.SetInt(MetallicMapChannel, (int)metallicMapChannel); - channelPacker.SetFloat(MetallicUniform, metallicUniform); - channelPacker.SetTexture(OcclusionMap, occlusionMap); - channelPacker.SetInt(OcclusionMapChannel, (int)occlusionMapChannel); - channelPacker.SetFloat(OcclusionUniform, occlusionUniform); - channelPacker.SetTexture(EmissionMap, emissionMap); - channelPacker.SetInt(EmissionMapChannel, (int)emissionMapChannel); - channelPacker.SetFloat(EmissionUniform, emissionUniform); - channelPacker.SetTexture(SmoothnessMap, smoothnessMap); - channelPacker.SetInt(SmoothnessMapChannel, (int)smoothnessMapChannel); - channelPacker.SetFloat(SmoothnessUniform, smoothnessUniform); - Graphics.Blit(null, renderTexture, channelPacker); - DestroyImmediate(channelPacker); - - // Save the last render texture to a texture. - RenderTexture previous = RenderTexture.active; - RenderTexture.active = renderTexture; - channelMap.ReadPixels(new Rect(0.0f, 0.0f, width, height), 0, 0); - channelMap.Apply(); - RenderTexture.active = previous; - RenderTexture.ReleaseTemporary(renderTexture); - - // Save the texture to disk. - string filename = string.Format("{0}{1}.png", GetChannelMapName(textures), "_Channel"); - string path = EditorUtility.SaveFilePanel("Save Channel Map", "", filename, "png"); - - if (path.Length == 0) { return; } - - byte[] pngData = channelMap.EncodeToPNG(); - - if (pngData != null) - { - File.WriteAllBytes(path, pngData); - Debug.LogFormat("Saved channel map to: {0}", path); - } - } - - private bool CanSave() - { - return metallicMap != null || occlusionMap != null || emissionMap != null || smoothnessMap != null || - metallicUniform >= 0.0f || occlusionUniform >= 0.0f || emissionUniform >= 0.0f || smoothnessUniform >= 0.0f; - } - - private static bool IsUnityStandardMaterial(Material material) - { - if (material != null) - { - if (material.shader.name == StandardShaderName || - material.shader.name == StandardRoughnessShaderName || - material.shader.name == StandardSpecularShaderName) - { - return true; - } - } - - return false; - } - - private static string GetChannelMapName(Texture[] textures) - { - // Use the first named texture as the channel map name. - foreach (Texture texture in textures) - { - if (texture != null && !string.IsNullOrEmpty(texture.name)) - { - return texture.name; - } - } - - return string.Empty; - } - - private static void CalculateChannelMapSize(Texture[] textures, out int width, out int height) - { - width = 1; - height = 1; - - // Find the max extents of all texture maps. - foreach (Texture texture in textures) - { - width = texture != null ? Mathf.Max(texture.width, width) : width; - height = texture != null ? Mathf.Max(texture.height, height) : height; - } - } - } -} diff --git a/Editor/Utilities/ChannelPackerWindow.cs.meta b/Editor/Utilities/ChannelPackerWindow.cs.meta deleted file mode 100644 index 0037e6a..0000000 --- a/Editor/Utilities/ChannelPackerWindow.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: de594dcc0691c3f4cb9a8a1ce5905342 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {fileID: 2800000, guid: 8ac5213854cf4dbabd140decf8df1946, type: 3} - userData: - assetBundleName: - assetBundleVariant: