53 lines
1.5 KiB
C#
53 lines
1.5 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("Desaturate", "Adjustments/Desaturate", true)]
|
|
public class Texture2DDesaturate : BaseImageNode
|
|
{
|
|
[NodeAttributes.Input("")]
|
|
public ImageData inputTexture; // Changed to ImageData
|
|
|
|
[NodeAttributes.Output("")]
|
|
public ImageData outputTexture; // Changed to ImageData
|
|
|
|
public override void Process()
|
|
{
|
|
NativeArray<Color32> output = new NativeArray<Color32>(inputTexture.PixelData.Length, Allocator.Persistent);
|
|
|
|
DesaturateJob job = new DesaturateJob
|
|
{
|
|
pixels = inputTexture.PixelData,
|
|
outputPixels = output
|
|
};
|
|
|
|
job.Run();
|
|
|
|
outputTexture = new ImageData(output, (inputTexture.Width, inputTexture.Height), inputTexture.isRGBA);
|
|
}
|
|
}
|
|
|
|
public struct DesaturateJob : IJob
|
|
{
|
|
[ReadOnly] public NativeArray<Color32> pixels;
|
|
public NativeArray<Color32> outputPixels;
|
|
|
|
public void Execute()
|
|
{
|
|
for (int i = 0; i < pixels.Length; i++)
|
|
{
|
|
Color32 pixel = pixels[i];
|
|
|
|
byte grayValue = (byte)(0.2989f * pixel.r + 0.5870f * pixel.g + 0.1140f * pixel.b);
|
|
|
|
outputPixels[i] = new Color32(grayValue, grayValue, grayValue, pixel.a);
|
|
}
|
|
}
|
|
}
|
|
} |