81 lines
1.7 KiB
C#
81 lines
1.7 KiB
C#
|
|
using Rokojori;
|
||
|
|
using Godot;
|
||
|
|
|
||
|
|
namespace Rokojori;
|
||
|
|
|
||
|
|
[GlobalClass,Tool]
|
||
|
|
public partial class AdjustmentsStack: Resource
|
||
|
|
{
|
||
|
|
[Export]
|
||
|
|
public AdjustmentsFilter[] filters = [];
|
||
|
|
|
||
|
|
[Export( PropertyHint.Range, "0,1" )]
|
||
|
|
public float amount = 1;
|
||
|
|
|
||
|
|
[Export]
|
||
|
|
public bool enabled = true;
|
||
|
|
|
||
|
|
[Export]
|
||
|
|
public int adjustmentSize = 33;
|
||
|
|
|
||
|
|
[Export]
|
||
|
|
public Texture3D adjustmentsTexture;
|
||
|
|
|
||
|
|
public void GenerateAdjustmentsTexture()
|
||
|
|
{
|
||
|
|
filters.ForEach( f => f.PrepareFilter() );
|
||
|
|
|
||
|
|
var luts = new Godot.Collections.Array<Image>();
|
||
|
|
|
||
|
|
for ( int i = 0; i < adjustmentSize; i++ )
|
||
|
|
{
|
||
|
|
luts.Add( Image.CreateEmpty( adjustmentSize, adjustmentSize, false, Image.Format.Rgb8 ) );
|
||
|
|
}
|
||
|
|
|
||
|
|
for ( int bi = 0; bi < adjustmentSize; bi++ )
|
||
|
|
{
|
||
|
|
var image = luts[ bi ];
|
||
|
|
|
||
|
|
var b = bi / ( adjustmentSize - 1f );
|
||
|
|
|
||
|
|
for ( int ri = 0; ri < adjustmentSize; ri++ )
|
||
|
|
{
|
||
|
|
var r = ri / ( adjustmentSize - 1f );
|
||
|
|
|
||
|
|
for ( int gi = 0; gi < adjustmentSize; gi++ )
|
||
|
|
{
|
||
|
|
var g = gi / ( adjustmentSize - 1f );
|
||
|
|
var filtered = GetFilteredColor( new Color( r, g, b ) );
|
||
|
|
image.SetPixel( ri, gi, filtered );
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
var texture = new ImageTexture3D();
|
||
|
|
|
||
|
|
if ( adjustmentsTexture is ImageTexture3D existing && existing.GetWidth() == adjustmentSize )
|
||
|
|
{
|
||
|
|
existing.Update( luts );
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
texture.Create( Image.Format.Rgb8, adjustmentSize, adjustmentSize, adjustmentSize, false, luts );
|
||
|
|
adjustmentsTexture = texture;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public Color GetFilteredColor( Color rgb )
|
||
|
|
{
|
||
|
|
if ( ! enabled )
|
||
|
|
{
|
||
|
|
return rgb;
|
||
|
|
}
|
||
|
|
|
||
|
|
var originalRGB = rgb;
|
||
|
|
|
||
|
|
filters.ForEach( f => { rgb = f.GetFiltered( rgb ); } );
|
||
|
|
|
||
|
|
|
||
|
|
return originalRGB.Lerp( rgb, amount );
|
||
|
|
}
|
||
|
|
}
|