Texture Combiner

This commit is contained in:
Josef 2025-02-14 08:44:20 +01:00
parent 489199f37a
commit 756b1d2a9d
7 changed files with 228 additions and 1 deletions

View File

@ -2,8 +2,37 @@ using Godot;
namespace Rokojori
{
public class ColorX
public static class ColorX
{
public static float AlphaMultipliedR( this Color c )
{
return c.R * c.A;
}
public static float AlphaMultipliedG( this Color c )
{
return c.G * c.A;
}
public static float AlphaMultipliedB( this Color c )
{
return c.B * c.A;
}
public static Color Blend( Color bottom, Color top )
{
var fade = ( 1f - top.A );
var a0 = top.A + bottom.A * fade;
var xR = top.AlphaMultipliedR() + bottom.AlphaMultipliedR() * fade;
var xG = top.AlphaMultipliedG() + bottom.AlphaMultipliedG() * fade;
var xB = top.AlphaMultipliedB() + bottom.AlphaMultipliedB() * fade;
var normalizer = 1f / a0;
return new Color( xR, xG, xB, 1 ) * normalizer;
}
public static Color Lerp( Color a, Color b, float amount )
{
return a.Lerp( b, amount );
@ -40,6 +69,12 @@ namespace Rokojori
return color;
}
public static Color FadeAlpha( this Color color, float fade )
{
color.A = color.A * fade;
return color;
}
public static Color SetAlpha( Color color, float alpha )
{
color.A = alpha;

View File

@ -0,0 +1,17 @@
using System.Collections;
using System.Collections.Generic;
using Godot;
using System;
namespace Rokojori
{
[Tool]
[GlobalClass]
public partial class TextureCombiner:Resource
{
[Export]
public TextureCombinerLayer[] layers;
}
}

View File

@ -0,0 +1,53 @@
using System.Collections;
using System.Collections.Generic;
using Godot;
using System;
namespace Rokojori
{
public enum TextureCombinerBlendMode
{
Normal,
Add,
Multiply
}
public class TextureCombinerBlendModeAlgorithm
{
public static void Blend( TextureCombinerBlendMode blendMode, int x, int y, int w, int h, float topOpacity,
TextureBuffer<Color> bottom, TextureBuffer<Color> top, TextureBuffer<Color> output
)
{
if ( TextureCombinerBlendMode.Normal == blendMode )
{
BlendNormal( x, y, w, h, topOpacity, bottom, top, output );
}
}
static void BlendNormal( int x, int y, int w, int h, float topOpacity,
TextureBuffer<Color> bottom, TextureBuffer<Color> top, TextureBuffer<Color> output )
{
for ( int i = 0; i < w; i++ )
{
var rx = x + i;
for ( int j = 0; j < h; j++ )
{
var ry = y + j;
var index = output.ComputeIndexFromPosition( rx, ry );
var topColor = top.GetIndexed( index ).FadeAlpha( topOpacity );
var bottomColor = bottom.GetIndexed( index );
var outputColor = bottomColor.Blend( topColor );
output.SetIndexed( index, outputColor );
}
}
}
}
}

View File

@ -0,0 +1,57 @@
using System.Collections;
using System.Collections.Generic;
using Godot;
using System;
namespace Rokojori
{
public class TextureBuffer<T>
{
int _width;
public int width => _width;
int _height;
public int height => _height;
T[] _pixels;
public static TextureBuffer<T> Create( int w, int h )
{
var tb = new TextureBuffer<T>();
tb._height = h;
tb._width = w;
tb._pixels = new T[ w * h ];
return tb;
}
public int ComputeIndexFromPosition( int x, int y )
{
return x + y * _width;
}
public void SetAt( int x, int y, T value )
{
SetIndexed( ComputeIndexFromPosition( x, y ), value );
}
public void SetIndexed( int index, T value )
{
_pixels[ index ] = value;
}
public T GetAt( int x, int y )
{
return GetIndexed( ComputeIndexFromPosition( x, y ) );
}
public T GetIndexed( int index )
{
return _pixels[ index ];
}
}
}

View File

@ -0,0 +1,27 @@
using System.Collections;
using System.Collections.Generic;
using Godot;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Rokojori
{
[Tool]
[GlobalClass]
public partial class TextureCombinerLayer:Resource
{
[Export]
public TextureCombinerBlendMode blendMode;
[Export( PropertyHint.Range, "0,1")]
public float opacity = 1;
public async Task Process( TextureCombinerProcessingRect processingRect )
{
return;
}
}
}

View File

@ -0,0 +1,17 @@
using System.Collections;
using System.Collections.Generic;
using Godot;
using System;
namespace Rokojori
{
public class TextureCombinerContext
{
public TextureCombiner combiner;
public int imageWidth = 0;
public int imageHeight = 0;
}
}

View File

@ -0,0 +1,21 @@
using System.Collections;
using System.Collections.Generic;
using Godot;
using System;
namespace Rokojori
{
public class TextureCombinerProcessingRect
{
public TextureCombinerContext context;
public int processingX = 0;
public int processingY = 0;
public int processingWidht = 0;
public int processingHeight = 0;
public TextureBuffer<Color> output;
}
}