59 lines
1.8 KiB
C#
59 lines
1.8 KiB
C#
using ImageProcessingGraph.Editor.Nodes.NodeAttributes;
|
|
using Unity.Burst;
|
|
using Unity.Collections;
|
|
using Unity.Jobs;
|
|
using UnityEngine;
|
|
|
|
namespace ImageProcessingGraph.Editor.Nodes.Fun_Nodes.Texture
|
|
{
|
|
[NodeInfoAttribute("Invert", "Adjustments/Invert", true)]
|
|
public class Texture2DInvert : BaseImageNode
|
|
{
|
|
[NodeAttributes.Input("")]
|
|
public ImageData inputTexture; // Changed to ImageData
|
|
|
|
[NodeAttributes.Output("")]
|
|
public ImageData outputTexture; // Changed to ImageData
|
|
|
|
public override void Process()
|
|
{
|
|
// Create an empty NativeArray for the output
|
|
NativeArray<Color32> output = new NativeArray<Color32>(inputTexture.PixelData.Length, Allocator.Persistent);
|
|
|
|
// Create and run the InvertJob
|
|
InvertJob job = new InvertJob
|
|
{
|
|
pixels = inputTexture.PixelData,
|
|
outputPixels = output
|
|
};
|
|
|
|
job.Run();
|
|
|
|
// Store the result in the outputImage as an ImageData instance
|
|
outputTexture = new ImageData(output, (inputTexture.Width, inputTexture.Height), inputTexture.isRGBA);
|
|
}
|
|
}
|
|
|
|
[BurstCompile]
|
|
public struct InvertJob : IJob
|
|
{
|
|
[ReadOnly] public NativeArray<Color32> pixels; // Input pixels
|
|
[WriteOnly] public NativeArray<Color32> outputPixels; // Output pixels
|
|
|
|
public void Execute()
|
|
{
|
|
int length = pixels.Length;
|
|
|
|
// Invert each pixel color
|
|
for (int i = 0; i < length; i++)
|
|
{
|
|
Color32 pixel = pixels[i];
|
|
outputPixels[i] = new Color32(
|
|
(byte)(255 - pixel.r),
|
|
(byte)(255 - pixel.g),
|
|
(byte)(255 - pixel.b),
|
|
pixel.a);
|
|
}
|
|
}
|
|
}
|
|
} |