2025-04-26 19:40:12 +01:00

78 lines
2.8 KiB
C#

using ImageProcessingGraph.Editor.Nodes.NodeAttributes;
using Unity.Collections;
using Unity.Jobs;
using UnityEngine;
namespace ImageProcessingGraph.Editor.Nodes.Fun_Nodes.Texture
{
[NodeInfoAttribute("RGBA Split", "Channels/RGBA Split", true)]
public class RGBASplit : BaseImageNode
{
[NodeAttributes.Input("")]
public ImageData inputTexture;
[NodeAttributes.Output("R")]
public SplitChannelData r;
[NodeAttributes.Output("G")]
public SplitChannelData g;
[NodeAttributes.Output("B")]
public SplitChannelData b;
[NodeAttributes.Output("A")]
public SplitChannelData a;
public override void Process()
{
int length = inputTexture.PixelData.Length;
// Allocate NativeArrays for the pixel data and the individual RGBA channels
NativeArray<Color32> pixelData = new NativeArray<Color32>(inputTexture.PixelData, Allocator.Persistent);
NativeArray<byte> rChannel = new NativeArray<byte>(length, Allocator.Persistent);
NativeArray<byte> gChannel = new NativeArray<byte>(length, Allocator.Persistent);
NativeArray<byte> bChannel = new NativeArray<byte>(length, Allocator.Persistent);
NativeArray<byte> aChannel = new NativeArray<byte>(length, Allocator.Persistent);
// Create the job and set up the input/output data
RGBAJob rgbaJob = new RGBAJob
{
pixelData = pixelData,
rData = rChannel,
gData = gChannel,
bData = bChannel,
aData = aChannel,
length = length
};
rgbaJob.Run();
// Store the results in the SplitChannelData objects
r = new SplitChannelData(rChannel.ToArray(), (inputTexture.Width, inputTexture.Height));
g = new SplitChannelData(gChannel.ToArray(), (inputTexture.Width, inputTexture.Height));
b = new SplitChannelData(bChannel.ToArray(), (inputTexture.Width, inputTexture.Height));
a = new SplitChannelData(aChannel.ToArray(), (inputTexture.Width, inputTexture.Height));
}
}
// Job struct for processing the image data
struct RGBAJob : IJob
{
public NativeArray<Color32> pixelData;
public NativeArray<byte> rData;
public NativeArray<byte> gData;
public NativeArray<byte> bData;
public NativeArray<byte> aData;
public int length;
// Execute method that splits the RGBA channels
public void Execute()
{
for (int i = 0; i < length; i++)
{
Color32 pixel = pixelData[i];
rData[i] = pixel.r;
gData[i] = pixel.g;
bData[i] = pixel.b;
aData[i] = pixel.a;
}
}
}
}