Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion Editor/SmartTextureImporterEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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));
}
Expand Down Expand Up @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions Editor/TextureChannels.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public enum TextureChannels
{
Red,
Green,
Blue,
Alpha
}
18 changes: 18 additions & 0 deletions Runtime/TextureChannelPacker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public struct TexturePackingSettings
/// Outputs the inverted color (1.0 - color)
/// </summary>
public bool invertColor;

/// <summary>
/// Outputs the inverted color (1.0 - color)
/// </summary>
public int sourceChannel;
}

public static class TextureExtension
Expand Down Expand Up @@ -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);
Expand Down
36 changes: 32 additions & 4 deletions Shaders/PackShader.shader
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@
_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
Texture2D _RedChannel;
Texture2D _GreenChannel;
Texture2D _BlueChannel;
Texture2D _AlphaChannel;
int _RedSource;
int _GreenSource;
int _BlueSource;
int _AlphaSource;
sampler sampler_point_clamp;

ENDHLSL
Expand Down Expand Up @@ -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;
Expand Down