diff --git a/Runtime/Procedural/Baking/MultiBaker.cs b/Runtime/Procedural/Baking/MultiBaker.cs index cd5eb91..5de3fba 100644 --- a/Runtime/Procedural/Baking/MultiBaker.cs +++ b/Runtime/Procedural/Baking/MultiBaker.cs @@ -11,20 +11,11 @@ namespace Rokojori [GlobalClass] public partial class MultiBaker:Node { - [Export] - public bool initialize; + [ExportToolButton( "Initialize")] + public Callable InitializeButton => Callable.From( () => Initialize() ); - public bool bake; - - public bool saveTexture; - - [ExportGroup("Preview")] - - [Export] - public bool preview_UpdateAlways = true; - - [Export] - public bool preview_DilateTextures = true; + [ExportToolButton( "Bake")] + public Callable BakeButton => Callable.From( () => Bake() ); public enum MaterialMode { @@ -179,9 +170,6 @@ namespace Rokojori [Export] public WorldEnvironment X_worldEnvironment; - - // [Export] - // public DilateTexture X_dilateTexture; [Export] public MeshInstance3D X_outputMesh; @@ -213,46 +201,14 @@ namespace Rokojori bool _initialized = false; bool _baking = false; - - public override void _Ready() + + public async Task Bake() { - - } - - public bool preventProcessing = true; - - public override void _Process( double delta ) - { - if ( preventProcessing ) - { - return; - } - - X_texturePreview.Visible = showOutputTexture; - if ( _baking ) { return; } - - if ( initialize ) - { - initialize = false; - Initialize(); - } - - if ( bake || preview_UpdateAlways ) - { - bake = false; - Bake(); - } - - } - - - public async Task Bake() - { _baking = true; try @@ -333,20 +289,9 @@ namespace Rokojori this.LogInfo( "Texture created:", bakingMaterialModes[ i ] ); - // if ( preview_DilateTextures ) - // { - // this.LogInfo( "Dilating:", bakingMaterialModes[ i ] ); - // texture = await CreateDilatedTexture(); - - // this.LogInfo( "Dilating done:", bakingMaterialModes[ i ] ); - // } - // else - // { - texture = Textures.Copy( texture ); - await this.RequestNextFrame(); - await this.RequestNextFrame(); - // } - + texture = Textures.Copy( texture ); + await this.RequestNextFrame(); + await this.RequestNextFrame(); this.LogInfo( "Assigning Texture", bakingMaterialModes[ i ] ); GetBakeModeImplementation().AssignMaterial( bakingMaterialModes[ i ], texture ); @@ -373,38 +318,6 @@ namespace Rokojori } - - // public async Task CreateDilatedTexture() - // { - // var viewports = GetAllViewports(); - // var textures = Lists.Map( viewports, v => v.GetTexture() as Texture2D ); - // var dilatedTextures = new List(); - - // var index = 0; - - // foreach ( var t in textures ) - // { - // index ++; - - // var dilatedTexture = await X_dilateTexture.Create( t ); - // dilatedTextures.Add( dilatedTexture ); - // } - - // X_textureMerger.sourceTextures = dilatedTextures.ToArray(); - // X_textureMerger.sourceMode = TextureMerger.SourceMode.Textures; - - // X_textureMerger.Initialize(); - - // await this.RequestNextFrame(); - - // X_textureMerger.CreateLayout(); - - // await this.RequestNextFrame(); - - // var finalTexture = await X_dilateTexture.Create( X_textureMerger.X_textureMergerViewport.GetTexture() ); - - // return finalTexture; - // } public List GetAllViewports() { diff --git a/Runtime/Random/RandomEngine.cs b/Runtime/Random/RandomEngine.cs index b31d409..ad1db3f 100644 --- a/Runtime/Random/RandomEngine.cs +++ b/Runtime/Random/RandomEngine.cs @@ -391,6 +391,8 @@ namespace Rokojori public List Selection( int possibleValues, int selection ) { + selection = Mathf.Min( selection, possibleValues ); + var list = new List(); for ( int i = 0; i < possibleValues; i++ ) diff --git a/Runtime/Shading/Shaders/Baking/Dilation.gdshader b/Runtime/Shading/Shaders/Baking/Dilation.gdshader deleted file mode 100644 index 51535c6..0000000 --- a/Runtime/Shading/Shaders/Baking/Dilation.gdshader +++ /dev/null @@ -1,35 +0,0 @@ -shader_type canvas_item; - -uniform sampler2D screen_texture : hint_screen_texture, repeat_disable, filter_nearest; - -uniform float kernelOffset = 1; - -void fragment() -{ - vec2 pixelSize = SCREEN_PIXEL_SIZE * kernelOffset; - - vec4 blurred = vec4( 0, 0, 0, 0 ); - int kernel = 2; - - for ( int x = -kernel; x<= kernel; ++x ) - { - for ( int y = -kernel; y<= kernel; ++y ) - { - vec2 sampledUV = SCREEN_UV + pixelSize * vec2( float(x), float( y ) ); - vec4 value = textureLod( screen_texture, sampledUV, 0 ); - - if ( ( value.r + value.g + value.b ) * 255.0 < 10.0 ) - { - value.a = 0.0; - } - - blurred += value; - } - } - - blurred /= float( kernel * kernel ); - - - COLOR = blurred; - ; -} diff --git a/Runtime/Shading/Shaders/Baking/Dilation.gdshader.uid b/Runtime/Shading/Shaders/Baking/Dilation.gdshader.uid deleted file mode 100644 index fd6a5f2..0000000 --- a/Runtime/Shading/Shaders/Baking/Dilation.gdshader.uid +++ /dev/null @@ -1 +0,0 @@ -uid://djf46k1sq5eks diff --git a/Runtime/Shading/Shaders/Baking/DilationDrawer.gdshader b/Runtime/Shading/Shaders/Baking/DilationDrawer.gdshader new file mode 100644 index 0000000..c343fb1 --- /dev/null +++ b/Runtime/Shading/Shaders/Baking/DilationDrawer.gdshader @@ -0,0 +1,93 @@ +// NOTE: Shader automatically converted from Godot Engine 4.4.stable.mono's StandardMaterial3D. + +shader_type spatial; +render_mode blend_mix, depth_draw_opaque, cull_disabled, unshaded; + +uniform sampler2D texture_albedo : source_color, filter_linear_mipmap, repeat_enable; +uniform vec2 resolution; +uniform int maxRadius = 32; +uniform int alphaTreshold:hint_range(1,255) = 1; +uniform int genericAlphaOffset:hint_range(0,255) = 9; +uniform int assignedColorAlphaMinimum:hint_range(0,255) = 0; +uniform float amount:hint_range(0,1)=1; + + +vec4 getPaddingColor( vec2 uv, vec2 pixelSize, int closestDistance, float t ) +{ + vec4 closestPixel = texture( texture_albedo, uv ); + + if ( closestPixel.a >= t ) + { + return closestPixel / closestPixel.a;; + } + + for ( int x = -maxRadius; x <= maxRadius; ++x ) + { + for ( int y = -maxRadius; y <= maxRadius; ++y ) + { + int d = x * x + y * y; + + if ( d >= closestDistance ) + { + continue; + } + + vec2 pUV = uv + pixelSize * vec2( float( x ), float( y ) ); + vec4 pixel = texture( texture_albedo, pUV ); + + if ( pixel.a >= t ) + { + closestPixel = pixel / pixel.a; + closestPixel.a = pixel.a; + closestDistance = d; + } + + } + } + + return closestPixel; +} + +varying vec2 pixelSize; +varying float closestDistance; +const float halfSquare = pow( 2.0, 0.5 ) * 0.5; +varying float tresholdFloat; +varying float assignedColorAlphaMinimumFloat; +varying float genericAlphaOffsetFloat; + +void vertex() +{ + pixelSize = vec2( 1.0 / resolution.x, 1.0 / resolution.y ); + + float range = float( maxRadius ) * halfSquare; + closestDistance = ( range * range + range * range ); + + tresholdFloat = float( alphaTreshold ) / 255.0; + assignedColorAlphaMinimumFloat = float( assignedColorAlphaMinimum ) / 255.0; + genericAlphaOffsetFloat = float( genericAlphaOffset ) / 255.0; +} + +void fragment() +{ + + vec2 pixelPerfectUV = round( UV * resolution ) / resolution; + vec4 original = texture( texture_albedo, pixelPerfectUV ); + + if ( original.a == 1.0 ) + { + ALBEDO = original.rgb; + ALPHA = original.a; + } + else + { + int closestDistanceInteger = int( closestDistance ); + vec4 outputColor = getPaddingColor( pixelPerfectUV, pixelSize, closestDistanceInteger, tresholdFloat ); + + outputColor.a = max( original.a, outputColor.a > 0.0 ? assignedColorAlphaMinimumFloat : 0.0 ); + + ALBEDO = mix( original.rgb, outputColor.rgb, amount ); + ALPHA = mix( original.a, min( 1, outputColor.a + genericAlphaOffsetFloat ), amount ); + } + + +} diff --git a/Runtime/Shading/Shaders/Baking/DilationDrawer.gdshader.uid b/Runtime/Shading/Shaders/Baking/DilationDrawer.gdshader.uid new file mode 100644 index 0000000..f22e140 --- /dev/null +++ b/Runtime/Shading/Shaders/Baking/DilationDrawer.gdshader.uid @@ -0,0 +1 @@ +uid://cpe2qaooyu4rx diff --git a/Runtime/Shading/Shaders/Baking/DilationDrawerMaterial.cs b/Runtime/Shading/Shaders/Baking/DilationDrawerMaterial.cs new file mode 100644 index 0000000..b315695 --- /dev/null +++ b/Runtime/Shading/Shaders/Baking/DilationDrawerMaterial.cs @@ -0,0 +1,53 @@ +using Godot; + +namespace Rokojori +{ + // Generated by ShaderClassGenerator + + public class DilationDrawerShader + { + public static readonly CachedResource shader = new CachedResource( + "res://addons/rokojori_action_library/Runtime/Shading/Shaders/Baking/DilationDrawer.gdshader" + ); + + public static readonly Texture2DPropertyName textureAlbedo = Texture2DPropertyName.Create( "texture_albedo" ); + public static readonly Vector2PropertyName resolution = Vector2PropertyName.Create( "resolution" ); + public static readonly IntPropertyName maxRadius = IntPropertyName.Create( "maxRadius" ); + public static readonly IntPropertyName alphaTreshold = IntPropertyName.Create( "alphaTreshold" ); + public static readonly IntPropertyName genericAlphaOffset = IntPropertyName.Create( "genericAlphaOffset" ); + public static readonly IntPropertyName assignedColorAlphaMinimum = IntPropertyName.Create( "assignedColorAlphaMinimum" ); + public static readonly FloatPropertyName amount = FloatPropertyName.Create( "amount" ); + + } + + [Tool] + public partial class DilationDrawerMaterial:CustomMaterial + { + public static readonly CachedResource DepthMap = CustomMaterial.Cached( + "res://addons/rokojori_action_library/Runtime/Shading/Shaders/Baking/DepthMap.material" + ); + + public readonly CustomMaterialProperty textureAlbedo; + public readonly CustomMaterialProperty resolution; + public readonly CustomMaterialProperty maxRadius; + public readonly CustomMaterialProperty alphaTreshold; + public readonly CustomMaterialProperty genericAlphaOffset; + public readonly CustomMaterialProperty assignedColorAlphaMinimum; + public readonly CustomMaterialProperty amount; + + public DilationDrawerMaterial() + { + Shader = DilationDrawerShader.shader.Get(); + + textureAlbedo = new CustomMaterialProperty( this, DilationDrawerShader.textureAlbedo ); + resolution = new CustomMaterialProperty( this, DilationDrawerShader.resolution ); + maxRadius = new CustomMaterialProperty( this, DilationDrawerShader.maxRadius ); + alphaTreshold = new CustomMaterialProperty( this, DilationDrawerShader.alphaTreshold ); + genericAlphaOffset = new CustomMaterialProperty( this, DilationDrawerShader.genericAlphaOffset ); + assignedColorAlphaMinimum = new CustomMaterialProperty( this, DilationDrawerShader.assignedColorAlphaMinimum ); + amount = new CustomMaterialProperty( this, DilationDrawerShader.amount ); + } + + } + +} \ No newline at end of file diff --git a/Runtime/Shading/Shaders/Baking/DilationDrawerMaterial.cs.uid b/Runtime/Shading/Shaders/Baking/DilationDrawerMaterial.cs.uid new file mode 100644 index 0000000..55921c1 --- /dev/null +++ b/Runtime/Shading/Shaders/Baking/DilationDrawerMaterial.cs.uid @@ -0,0 +1 @@ +uid://dq3gkhoapw0ad diff --git a/Runtime/Structures/QueueList.cs.uid b/Runtime/Structures/QueueList.cs.uid new file mode 100644 index 0000000..a338123 --- /dev/null +++ b/Runtime/Structures/QueueList.cs.uid @@ -0,0 +1 @@ +uid://cbec0e07mfxps