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 pixelData = new NativeArray(inputTexture.PixelData, Allocator.Persistent); NativeArray rChannel = new NativeArray(length, Allocator.Persistent); NativeArray gChannel = new NativeArray(length, Allocator.Persistent); NativeArray bChannel = new NativeArray(length, Allocator.Persistent); NativeArray aChannel = new NativeArray(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 pixelData; public NativeArray rData; public NativeArray gData; public NativeArray bData; public NativeArray 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; } } } }