Added ChannelMixer
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
Shader "Hidden/ChannelMixer"
|
||||
{
|
||||
Properties
|
||||
{
|
||||
_MainTex ("Red Channel Texture", 2D) = "white"
|
||||
}
|
||||
SubShader
|
||||
{
|
||||
Cull Off
|
||||
ZWrite Off
|
||||
ZTest Always
|
||||
|
||||
Pass
|
||||
{
|
||||
CGPROGRAM
|
||||
|
||||
#pragma vertex vert
|
||||
#pragma fragment frag
|
||||
|
||||
#pragma shader_feature_local _HAS_TEX_R
|
||||
#pragma shader_feature_local _HAS_TEX_G
|
||||
#pragma shader_feature_local _HAS_TEX_B
|
||||
#pragma shader_feature_local _HAS_TEX_A
|
||||
|
||||
#include "UnityCG.cginc"
|
||||
|
||||
struct appdata {
|
||||
float4 vertex : POSITION;
|
||||
float2 uv : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct v2f {
|
||||
float2 uv : TEXCOORD0;
|
||||
float4 vertex : SV_POSITION;
|
||||
};
|
||||
|
||||
v2f vert(appdata v) {
|
||||
v2f o;
|
||||
o.vertex = UnityObjectToClipPos(v.vertex);
|
||||
o.uv = v.uv;
|
||||
return o;
|
||||
}
|
||||
|
||||
sampler2D _MainTex;
|
||||
int4 _ChannelSource;
|
||||
|
||||
float4 frag (v2f i) : SV_Target
|
||||
{
|
||||
float4 color = tex2D(_MainTex, i.uv);
|
||||
|
||||
float r = color[_ChannelSource.x];
|
||||
float g = color[_ChannelSource.y];
|
||||
float b = color[_ChannelSource.z];
|
||||
float a = color[_ChannelSource.w];
|
||||
|
||||
return float4(r, g, b, a);
|
||||
}
|
||||
ENDCG
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 21d62fa27a1e5b1439a779be2e3ac586
|
||||
ShaderImporter:
|
||||
externalObjects: {}
|
||||
defaultTextures: []
|
||||
nonModifiableTextures: []
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,33 @@
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Misaki.ArtToolEditor
|
||||
{
|
||||
internal class ChannelMixerMenu
|
||||
{
|
||||
[MenuItem("Assets/Art Tools/Texture Helpers/Channel Mixer", true)]
|
||||
public static bool ChannelMixerValidator()
|
||||
{
|
||||
foreach (var selectedObject in Selection.objects)
|
||||
{
|
||||
if (selectedObject is not Texture2D)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
[MenuItem("Assets/Art Tools/Texture Helpers/Channel Mixer")]
|
||||
public static void ChannelMixer()
|
||||
{
|
||||
var window = EditorWindow.GetWindow<OutputOptionsWindow>(true, "Channel Mixer Output Options");
|
||||
window.WithItemSource(Selection.objects);
|
||||
window.RegisterVisualProvider<ChannelMixerVisualProvider>();
|
||||
window.RegisterProcessor<ChannelMixerProcessor>();
|
||||
|
||||
window.InitializeAndShow();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fa98d95c359c67b44a90ea5568f872e2
|
||||
@@ -0,0 +1,54 @@
|
||||
using System.IO;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Experimental.Rendering;
|
||||
|
||||
namespace Misaki.ArtToolEditor
|
||||
{
|
||||
internal class ChannelMixerProcessor : IAssetsProcessor
|
||||
{
|
||||
private static class ShaderConstants
|
||||
{
|
||||
internal const string Channel_Mixer_Shader_Path = "Hidden/ChannelMixer";
|
||||
|
||||
internal const string Main_Texture_Property = "_MainTex";
|
||||
internal const string Channel_Source_Property = "_ChannelSource";
|
||||
}
|
||||
|
||||
public TextureChannel RChannelSource = TextureChannel.R;
|
||||
public TextureChannel GChannelSource = TextureChannel.G;
|
||||
public TextureChannel BChannelSource = TextureChannel.B;
|
||||
public TextureChannel AChannelSource = TextureChannel.A;
|
||||
|
||||
public string outputNameSuffix = "_ChannelMixer";
|
||||
|
||||
public void OnPreProcess(AssetsProcessContext context)
|
||||
{
|
||||
}
|
||||
|
||||
public void OnProcess(Object source, string outputDirectory, AssetsProcessContext context)
|
||||
{
|
||||
if (source is not Texture2D texture)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var tempRT = RenderTexture.GetTemporary(texture.width, texture.height, 0, GraphicsFormat.R8G8B8A8_UNorm);
|
||||
var material = new Material(Shader.Find(ShaderConstants.Channel_Mixer_Shader_Path));
|
||||
|
||||
material.SetVector(ShaderConstants.Channel_Source_Property, new Vector4((int)RChannelSource, (int)GChannelSource, (int)BChannelSource, (int)AChannelSource));
|
||||
Graphics.Blit(texture, tempRT, material);
|
||||
|
||||
var texturePath = Path.Combine(outputDirectory, texture.name + outputNameSuffix + Constants.Extensions.PNG);
|
||||
TextureHelpers.ExportRenderTextureToPNG(tempRT, texturePath);
|
||||
AssetDatabase.ImportAsset(texturePath);
|
||||
|
||||
Object.DestroyImmediate(material);
|
||||
RenderTexture.ReleaseTemporary(tempRT);
|
||||
}
|
||||
|
||||
public void OnPostProcess(AssetsProcessContext context)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b764f778d76e1f04f8b3e8edcd9d838d
|
||||
@@ -0,0 +1,35 @@
|
||||
using Unity.Properties;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace Misaki.ArtToolEditor
|
||||
{
|
||||
internal class ChannelMixerVisualProvider : OptionsVisualProvider
|
||||
{
|
||||
public override VisualElement ContentAfterList()
|
||||
{
|
||||
var root = new VisualElement();
|
||||
|
||||
root.Add(CreateChannelSelector("Set Channel R From", nameof(ChannelMixerProcessor.RChannelSource)));
|
||||
root.Add(CreateChannelSelector("Set Channel G From", nameof(ChannelMixerProcessor.GChannelSource)));
|
||||
root.Add(CreateChannelSelector("Set Channel B From", nameof(ChannelMixerProcessor.BChannelSource)));
|
||||
root.Add(CreateChannelSelector("Set Channel A From", nameof(ChannelMixerProcessor.AChannelSource)));
|
||||
|
||||
var nameSuffixField = new TextField("Output Name Suffix")
|
||||
{
|
||||
tooltip = "The suffix to append to the output texture name. Will overwrite original texture if set it to empty and use CurrentDirectory as output directory.",
|
||||
};
|
||||
nameSuffixField.SetBinding(nameof(TextField.value), new DataBinding() { dataSourcePath = PropertyPath.FromName(nameof(ChannelMixerProcessor.outputNameSuffix)) });
|
||||
root.Add(nameSuffixField);
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
private static VisualElement CreateChannelSelector(string label, string binding)
|
||||
{
|
||||
var enumField = new EnumField(label, TextureChannel.R);
|
||||
enumField.SetBinding(nameof(EnumField.value), new DataBinding() { dataSourcePath = PropertyPath.FromName(binding) });
|
||||
|
||||
return enumField;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 21d7828e4486449479a4adaeae5970ff
|
||||
Reference in New Issue
Block a user