diff --git a/Editor/SmartTextureImporterEditor.cs b/Editor/SmartTextureImporterEditor.cs index a313cab..472bbba 100644 --- a/Editor/SmartTextureImporterEditor.cs +++ b/Editor/SmartTextureImporterEditor.cs @@ -16,7 +16,11 @@ internal static class Styles EditorGUIUtility.TrTextContent("Alpha Channel", "This texture source channel will be packed into the Output texture alpha channel"), }; + public static readonly GUIContent sourceTexture = EditorGUIUtility.TrTextContent("Source Texture", "The texture from which the channel will be extracted"); + public static readonly GUIContent sourceChannel = EditorGUIUtility.TrTextContent("Source Channel", "The channel of the texture that should be extracted"); + public static readonly GUIContent invertColor = EditorGUIUtility.TrTextContent("Invert Color", "If enabled outputs the inverted color (1.0 - color)"); + public static readonly GUIContent readWrite = EditorGUIUtility.TrTextContent("Read/Write Enabled", "Enable to be able to access the raw pixel data from code."); public static readonly GUIContent generateMipMaps = EditorGUIUtility.TrTextContent("Generate Mip Maps"); public static readonly GUIContent streamingMipMaps = EditorGUIUtility.TrTextContent("Streaming Mip Maps"); @@ -36,6 +40,7 @@ internal static class Styles }; public static readonly string[] textureCompressionOptions = Enum.GetNames(typeof(TextureImporterCompression)); + public static readonly string[] textureChannels = Enum.GetNames(typeof(TextureChannels)); public static readonly string[] textureFormat = Enum.GetNames(typeof(TextureFormat)); public static readonly string[] resizeAlgorithmOptions = Enum.GetNames(typeof(TextureResizeAlgorithm)); } @@ -121,11 +126,21 @@ void DrawInputTexture(int index) if (index < 0 || index >= 4) return; - EditorGUILayout.PropertyField(m_InputTextures[index], Styles.labelChannels[index]); + EditorGUILayout.LabelField(Styles.labelChannels[index], EditorStyles.boldLabel); + using (new EditorGUI.IndentLevelScope()) + { + EditorGUILayout.PropertyField(m_InputTextures[index], Styles.sourceTexture); + + SerializedProperty sourceChannel = m_InputTextureSettings[index].FindPropertyRelative("sourceChannel"); + sourceChannel.intValue = EditorGUILayout.Popup(Styles.sourceChannel, sourceChannel.intValue, Styles.textureChannels); SerializedProperty invertColor = m_InputTextureSettings[index].FindPropertyRelative("invertColor"); invertColor.boolValue = EditorGUILayout.Toggle(Styles.invertColor, invertColor.boolValue); + EditorGUILayout.Space(); + + } + } void DrawTextureImporterSettings() diff --git a/Editor/TextureChannels.cs b/Editor/TextureChannels.cs new file mode 100644 index 0000000..95196e1 --- /dev/null +++ b/Editor/TextureChannels.cs @@ -0,0 +1,7 @@ +public enum TextureChannels +{ + Red, + Green, + Blue, + Alpha +} \ No newline at end of file diff --git a/Runtime/TextureChannelPacker.cs b/Runtime/TextureChannelPacker.cs index 20cd558..a46b6fe 100644 --- a/Runtime/TextureChannelPacker.cs +++ b/Runtime/TextureChannelPacker.cs @@ -16,6 +16,11 @@ public struct TexturePackingSettings /// Outputs the inverted color (1.0 - color) /// public bool invertColor; + + /// + /// Outputs the inverted color (1.0 - color) + /// + public int sourceChannel; } public static class TextureExtension @@ -86,11 +91,24 @@ public static void PackChannels(this Texture2D mask, Texture2D[] textures, Textu settings[2].invertColor ? 1.0f : 0.0f, settings[3].invertColor ? 1.0f : 0.0f, }; + + int[] channelSource = + { + settings[0].sourceChannel, + settings[1].sourceChannel, + settings[2].sourceChannel, + settings[3].sourceChannel + }; + packChannelMaterial.SetTexture("_RedChannel", textures[0] != null ? textures[0] : Texture2D.blackTexture); packChannelMaterial.SetTexture("_GreenChannel", textures[1] != null ? textures[1] : Texture2D.blackTexture); packChannelMaterial.SetTexture("_BlueChannel", textures[2] != null ? textures[2] : Texture2D.blackTexture); packChannelMaterial.SetTexture("_AlphaChannel", textures[3] != null ? textures[3] : Texture2D.blackTexture); packChannelMaterial.SetVector("_InvertColor", new Vector4(invertColor[0], invertColor[1], invertColor[2], invertColor[3])); + packChannelMaterial.SetInt("_RedSource", channelSource[0]); + packChannelMaterial.SetInt("_GreenSource", channelSource[1]); + packChannelMaterial.SetInt("_BlueSource", channelSource[2]); + packChannelMaterial.SetInt("_AlphaSource", channelSource[3]); var rt = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear); diff --git a/Shaders/PackShader.shader b/Shaders/PackShader.shader index 0c8410c..646039e 100644 --- a/Shaders/PackShader.shader +++ b/Shaders/PackShader.shader @@ -7,6 +7,10 @@ _BlueChannel("BlueChannel", 2D) = "white" {} _AlphaChannel("AlphaChannel", 2D) = "white" {} _InvertColor("_InvertColor", Vector) = (0.0, 0.0, 0.0, 0.0) + _RedSource("_RedSource", Int) = 0 + _GreenSource("_GreenSource", Int) = 0 + _BlueSource("_BlueSource", Int) = 0 + _AlphaSource("_AlphaSource", Int) = 0 } HLSLINCLUDE @@ -14,6 +18,10 @@ Texture2D _GreenChannel; Texture2D _BlueChannel; Texture2D _AlphaChannel; + int _RedSource; + int _GreenSource; + int _BlueSource; + int _AlphaSource; sampler sampler_point_clamp; ENDHLSL @@ -45,10 +53,30 @@ half4 frag(float4 positionHCS : SV_POSITION) : SV_Target { float2 uv = positionHCS * _RedChannel_TexelSize.xy; - half r = _RedChannel.Sample(sampler_point_clamp, uv).r; - half g = _GreenChannel.Sample(sampler_point_clamp, uv).r; - half b = _BlueChannel.Sample(sampler_point_clamp, uv).r; - half a = _AlphaChannel.Sample(sampler_point_clamp, uv).r; + + half r = + _RedSource == 0 ? _RedChannel.Sample(sampler_point_clamp, uv).r : + _RedSource == 1 ? _RedChannel.Sample(sampler_point_clamp, uv).g : + _RedSource == 2 ? _RedChannel.Sample(sampler_point_clamp, uv).b : + _RedChannel.Sample(sampler_point_clamp, uv).a; + + half g = + _GreenSource == 0 ? _GreenChannel.Sample(sampler_point_clamp, uv).r : + _GreenSource == 1 ? _GreenChannel.Sample(sampler_point_clamp, uv).g : + _GreenSource == 2 ? _GreenChannel.Sample(sampler_point_clamp, uv).b : + _GreenChannel.Sample(sampler_point_clamp, uv).a; + + half b = + _BlueSource == 0 ? _BlueChannel.Sample(sampler_point_clamp, uv).r : + _BlueSource == 1 ? _BlueChannel.Sample(sampler_point_clamp, uv).g : + _BlueSource == 2 ? _BlueChannel.Sample(sampler_point_clamp, uv).b : + _BlueChannel.Sample(sampler_point_clamp, uv).a; + + half a = + _AlphaSource == 0 ? _AlphaChannel.Sample(sampler_point_clamp, uv).r : + _AlphaSource == 1 ? _AlphaChannel.Sample(sampler_point_clamp, uv).g : + _AlphaSource == 2 ? _AlphaChannel.Sample(sampler_point_clamp, uv).b : + _AlphaChannel.Sample(sampler_point_clamp, uv).a; if (_InvertColor.x > 0.0) r = 1.0 - r;