From 24e6ca84c30ed3549c1f06235ebf4361c92facb5 Mon Sep 17 00:00:00 2001 From: Josef Date: Sun, 6 Apr 2025 07:48:27 +0200 Subject: [PATCH 1/4] Removed Unneeded Materials, Added Default Actions, Plugin AutoLoad --- External/Imposter/materials/dilatate.gdshader | 8 +- RokojoriPlugin.cs | 13 +++ Runtime/Actions/ActionList.cs | 1 + Runtime/Actions/Node3D/OnCollision.cs | 100 +++++++++++++++++ Runtime/Actions/Node3D/OnCollision.cs.uid | 1 + Runtime/Actions/Node3D/PlayParticles.cs | 17 +++ Runtime/Actions/Node3D/PlayParticles.cs.uid | 1 + Runtime/Actions/Node3D/PlaySound.cs | 17 +++ Runtime/Actions/Node3D/PlaySound.cs.uid | 1 + Runtime/Godot/Nodes.cs | 28 +++++ Runtime/Math/Math3D.cs | 21 +++- Runtime/Procedural/Assets/Grass/GrassPatch.cs | 105 ++++++++++++------ Runtime/Procedural/Baking/Baker.cs | 16 +++ Runtime/Procedural/Baking/MultiBaker.cs | 95 ++++++++-------- Runtime/Procedural/Mesh/MeshGeometry.cs | 12 ++ .../Scatter/Generators/GenerateInBox.cs | 8 +- Runtime/Procedural/Scatter/Scatterer.cs | 9 ++ .../Scatter/Transform/RandomizeTransform.cs | 2 +- Runtime/Random/RandomEngine.cs | 24 ++++ Runtime/Selectors/Selector.cs | 5 + Runtime/Shading/Library/Transform.gdshaderinc | 10 ++ Runtime/Shading/Materials/CustomMaterial.cs | 1 - .../Shading/Shaders/Baking/Dilation.gdshader | 35 ++++++ .../Shaders/Baking/Dilation.gdshader.uid | 1 + .../QuadBillboard/QuadBillboardMaterial.cs | 1 - .../FancyOutline/FancyOutlineMaterial.cs | 1 - .../FresnelOverlay/FresnelOverlayMaterial.cs | 1 - .../Effects/Outline/OutlineMaterial.cs | 1 - .../Effects/Overlay/OverlayMaterial.cs | 1 - .../ScanGradient/ScanGradientMaterial.cs | 1 - .../TriPlanarOverlayMaterial.cs | 1 - .../Wipes/FadeWipe/FadeWipeMaterial.cs | 1 - .../CSShaderClassTemplate.txt | 1 - Runtime/Structures/QueueList.cs | 54 +++++++++ Runtime/Time/TimeLineManager.cs | 19 +++- .../Shaders/NinePatch/UINinePatchMaterial.cs | 1 - .../RoundedRectangleMaterial.cs | 1 - 37 files changed, 508 insertions(+), 107 deletions(-) create mode 100644 Runtime/Actions/Node3D/OnCollision.cs create mode 100644 Runtime/Actions/Node3D/OnCollision.cs.uid create mode 100644 Runtime/Actions/Node3D/PlayParticles.cs create mode 100644 Runtime/Actions/Node3D/PlayParticles.cs.uid create mode 100644 Runtime/Actions/Node3D/PlaySound.cs create mode 100644 Runtime/Actions/Node3D/PlaySound.cs.uid create mode 100644 Runtime/Shading/Shaders/Baking/Dilation.gdshader create mode 100644 Runtime/Shading/Shaders/Baking/Dilation.gdshader.uid create mode 100644 Runtime/Structures/QueueList.cs diff --git a/External/Imposter/materials/dilatate.gdshader b/External/Imposter/materials/dilatate.gdshader index ab74aed..42a2342 100644 --- a/External/Imposter/materials/dilatate.gdshader +++ b/External/Imposter/materials/dilatate.gdshader @@ -6,7 +6,7 @@ uniform int u_distance = 16; uniform bool u_alpha_overwrite = true; -vec4 dilate(sampler2D a_tex, sampler2D tex, vec2 coords, vec2 texel_size, int dist, float alpha_cutoff) +vec4 dilate( sampler2D a_tex, sampler2D tex, vec2 coords, vec2 texel_size, int dist, float alpha_cutoff ) { vec2 offsets[8] = { @@ -40,7 +40,7 @@ vec4 dilate(sampler2D a_tex, sampler2D tex, vec2 coords, vec2 texel_size, int di return sample; } -void fragment() { - COLOR = dilate(u_alpha_tex, TEXTURE, UV, TEXTURE_PIXEL_SIZE, u_distance, 0.95); - //COLOR.a = 1.0; +void fragment() +{ + COLOR = dilate(u_alpha_tex, TEXTURE, UV, TEXTURE_PIXEL_SIZE, u_distance, 0.95); } diff --git a/RokojoriPlugin.cs b/RokojoriPlugin.cs index 3d4f313..e2ffb80 100644 --- a/RokojoriPlugin.cs +++ b/RokojoriPlugin.cs @@ -10,6 +10,19 @@ namespace Rokojori { GizmoDrawerPlugin gizmoDrawerPlugin = new GizmoDrawerPlugin(); + static readonly string RokojoriRootAutoLoad = "RokojoriRootAutoLoad"; + static readonly string RokojoriRootAutoLoadPath = "res://addons/rokojori_action_library/Runtime/Godot/Root.cs"; + + public override void _EnablePlugin() + { + AddAutoloadSingleton( RokojoriRootAutoLoad, RokojoriRootAutoLoadPath ); + } + + public override void _DisablePlugin() + { + RemoveAutoloadSingleton( RokojoriRootAutoLoad ); + } + public override void _EnterTree() { AddNode3DGizmoPlugin( gizmoDrawerPlugin ); diff --git a/Runtime/Actions/ActionList.cs b/Runtime/Actions/ActionList.cs index 2d439a6..e21a0e2 100644 --- a/Runtime/Actions/ActionList.cs +++ b/Runtime/Actions/ActionList.cs @@ -19,6 +19,7 @@ namespace Rokojori */ + [Tool] [GlobalClass, Icon("res://addons/rokojori_action_library/Icons/ActionList.svg") ] public partial class ActionList : Action { diff --git a/Runtime/Actions/Node3D/OnCollision.cs b/Runtime/Actions/Node3D/OnCollision.cs new file mode 100644 index 0000000..0460023 --- /dev/null +++ b/Runtime/Actions/Node3D/OnCollision.cs @@ -0,0 +1,100 @@ + +using System.Collections.Generic; +using Godot; +using Rokojori; + +namespace Rokojori +{ + [Tool] + [GlobalClass, Icon("res://addons/rokojori_action_library/Icons/OnEvent.svg") ] + public partial class OnCollision:Node + { + [Export] + public Area3D area; + + [Export] + public Selector selector; + + [Export] + public Action onEntered; + + [Export] + public Action onInside; + + [Export] + public Action onExit; + + + + Dictionary _inside = new Dictionary(); + + + public override void _Ready() + { + if ( area == null ) + { + return; + } + + area.AreaEntered += TriggerOnEnter; + area.BodyEntered += TriggerOnEnter; + + area.AreaExited += TriggerOnExited; + area.BodyExited += TriggerOnExited; + } + + void TriggerOnEnter( Node n ) + { + if ( ! Selector.IsSelecting( selector, n ) ) + { + return; + } + + Action.Trigger( onEntered ); + + if ( onInside == null ) + { + return; + } + + var tm = Unique.Get(); + + if ( tm == null ) + { + return; + } + + var callback = ()=>{ Action.Trigger( onInside ); }; + _inside[ n ] = callback; + tm.AddProcessCallback( callback ); + + } + + void TriggerOnExited( Node n ) + { + if ( ! Selector.IsSelecting( selector, n ) ) + { + return; + } + + Action.Trigger( onExit ); + + if ( ! _inside.ContainsKey( n ) ) + { + return; + } + + var tm = Unique.Get(); + + if ( tm == null ) + { + return; + } + + var callback = _inside[ n ]; + tm.RemoveProcessCallback( callback ); + + _inside.Remove( n ); + } + } +} \ No newline at end of file diff --git a/Runtime/Actions/Node3D/OnCollision.cs.uid b/Runtime/Actions/Node3D/OnCollision.cs.uid new file mode 100644 index 0000000..82e5321 --- /dev/null +++ b/Runtime/Actions/Node3D/OnCollision.cs.uid @@ -0,0 +1 @@ +uid://c8gcunaffcaww diff --git a/Runtime/Actions/Node3D/PlayParticles.cs b/Runtime/Actions/Node3D/PlayParticles.cs new file mode 100644 index 0000000..67c93af --- /dev/null +++ b/Runtime/Actions/Node3D/PlayParticles.cs @@ -0,0 +1,17 @@ + +using Godot; + +namespace Rokojori +{ + [GlobalClass] + public partial class PlayParticles:Action + { + [Export] + public GpuParticles3D particles3D; + + protected override void _OnTrigger() + { + particles3D.Restart(); + } + } +} \ No newline at end of file diff --git a/Runtime/Actions/Node3D/PlayParticles.cs.uid b/Runtime/Actions/Node3D/PlayParticles.cs.uid new file mode 100644 index 0000000..22a9db9 --- /dev/null +++ b/Runtime/Actions/Node3D/PlayParticles.cs.uid @@ -0,0 +1 @@ +uid://dnstanbmrqthf diff --git a/Runtime/Actions/Node3D/PlaySound.cs b/Runtime/Actions/Node3D/PlaySound.cs new file mode 100644 index 0000000..212fee9 --- /dev/null +++ b/Runtime/Actions/Node3D/PlaySound.cs @@ -0,0 +1,17 @@ + +using Godot; + +namespace Rokojori +{ + [GlobalClass] + public partial class PlaySound:Action + { + [Export] + public AudioStreamPlayer player; + + protected override void _OnTrigger() + { + player.Play(); + } + } +} \ No newline at end of file diff --git a/Runtime/Actions/Node3D/PlaySound.cs.uid b/Runtime/Actions/Node3D/PlaySound.cs.uid new file mode 100644 index 0000000..36b7804 --- /dev/null +++ b/Runtime/Actions/Node3D/PlaySound.cs.uid @@ -0,0 +1 @@ +uid://ddgf2mfdmqywc diff --git a/Runtime/Godot/Nodes.cs b/Runtime/Godot/Nodes.cs index 60096bb..bde3bf2 100644 --- a/Runtime/Godot/Nodes.cs +++ b/Runtime/Godot/Nodes.cs @@ -226,6 +226,34 @@ namespace Rokojori return CreateChildIn( parent, name ); } + public static T CreateChildLocal3D( this Node parent, Vector3 position, Quaternion? rotation, string name = null ) where T:Node3D,new() + { + var c = CreateChildIn( parent, name ); + + c.Position = position; + + if ( rotation != null ) + { + Math3D.SetLocalQuaternion( c, (Quaternion)rotation ); + } + + return c; + } + + public static T CreateChildGlobal3D( this Node parent, Vector3 position, Quaternion? rotation, string name = null ) where T:Node3D,new() + { + var c = CreateChildIn( parent, name ); + + c.GlobalPosition = position; + + if ( rotation != null ) + { + Math3D.SetGlobalQuaternion( c, (Quaternion)rotation ); + } + + return c; + } + public static Node CreateChildWithType( this Node parent, Type type, string name = null ) { return CreateChildInWithType( parent, type, name ); diff --git a/Runtime/Math/Math3D.cs b/Runtime/Math/Math3D.cs index 3ffa643..2d445b8 100644 --- a/Runtime/Math/Math3D.cs +++ b/Runtime/Math/Math3D.cs @@ -242,16 +242,26 @@ namespace Rokojori return quaternion; } - public static Vector3 MinPosition( Node3D a, Node3D b ) + public static Vector3 MinGlobalPosition( Node3D a, Node3D b ) { return a.GlobalPosition.Min( b.GlobalPosition ); } - public static Vector3 MaxPosition( Node3D a, Node3D b ) + public static Vector3 MaxGlobalPosition( Node3D a, Node3D b ) { return a.GlobalPosition.Max( b.GlobalPosition ); } + public static Vector3 MinLocalPosition( Node3D a, Node3D b ) + { + return a.Position.Min( b.Position ); + } + + public static Vector3 MaxLocalPosition( Node3D a, Node3D b ) + { + return a.Position.Max( b.Position ); + } + public static Vector3 SnapRounded( Vector3 v, Vector3 snapping ) { v.X = MathX.SnapRounded( v.X, snapping.X ); @@ -443,6 +453,13 @@ namespace Rokojori node.Scale = localScale; } + public static void SetLocalQuaternion( this Node3D node, Quaternion quaternion ) + { + var localScale = node.Scale; + node.Basis = new Basis( quaternion ); + node.Scale = localScale; + } + public static void SetGlobalRotationTo( Node3D node, Quaternion quaternion ) { var forward = quaternion * Vector3.Forward; diff --git a/Runtime/Procedural/Assets/Grass/GrassPatch.cs b/Runtime/Procedural/Assets/Grass/GrassPatch.cs index ae25d95..8ab4424 100644 --- a/Runtime/Procedural/Assets/Grass/GrassPatch.cs +++ b/Runtime/Procedural/Assets/Grass/GrassPatch.cs @@ -24,6 +24,26 @@ namespace Rokojori [Export] public bool updateAlways; + [ExportGroup( "Patch")] + [Export] + public float patchSize = 2; + + [Export] + public float patchSizeX = 0; + + [Export] + public float patchSizeZ = 0; + + [Export] + public Vector2 patchOffsetPosition = new Vector2( 0.5f, 0.5f ); + + [Export] + public bool centerPatch = false; + + [Export] + public float centerPatchComputationHeightTreshold = 0.1f; + + [ExportGroup( "Blades")] [Export( PropertyHint.Range, "0,100")] @@ -38,27 +58,10 @@ namespace Rokojori [Export] public int X_numBlades; - [ExportGroup( "Patch")] - [Export] - public float patchSize = 2; - - [Export] - public float patchSizeX = 0; - - [Export] - public float patchSizeZ = 0; - - [Export] - public Vector2 patchOffsetPosition = new Vector2( 0.5f, 0.5f ); - - [Export] - public bool centerPatch = false; - - [Export] - public float centerPatchComputationHeightTreshold = 0.1f; - [ExportGroup( "Blade Triangles")] + + [ExportGroup( "Triangles")] [Export( PropertyHint.Range, "1,256")] public int bladeSegments = 3; @@ -75,7 +78,7 @@ namespace Rokojori [Export] public Curve bladeSegmentMapping = MathX.Curve( 0, 1 ); - [ExportGroup( "Blade Segmentation")] + [ExportGroup( "Segmentation")] [Export] public int uvSegmentColumns = 1; @@ -95,7 +98,7 @@ namespace Rokojori public float uvSegmentMaxRange = 0.3f; - [ExportGroup( "Blade Shape")] + [ExportGroup( "Shape")] [Export] public Curve bladeHeight = MathX.Curve( 0.4f ); @@ -109,19 +112,14 @@ namespace Rokojori [Export] public Curve bladeWidth2 = null; + [ExportGroup( "Curve")] + [Export] public Curve bladeBending = MathX.Curve( 0f ); [Export] public Curve bladeBending2 = null; - [Export] - public Curve bladeArching = MathX.Curve( 0f ); - - [Export] - public Curve bladeArching2 = null; - - [Export] public Curve bladeTwisting = null; @@ -135,7 +133,7 @@ namespace Rokojori public Curve rolling2 = null; - [ExportGroup( "Blade Offset")] + [ExportGroup( "Offset")] [Export] public Curve positionJitter = MathX.Curve( 0.05f ); @@ -148,7 +146,7 @@ namespace Rokojori - [ExportGroup( "Blade Scale")] + [ExportGroup( "Scale")] [Export] public Curve bladeScale = MathX.Curve( 1f ); @@ -178,7 +176,7 @@ namespace Rokojori public Curve scaleZForY = null; - [ExportGroup( "Blade Rotation")] + [ExportGroup( "Rotation")] [Export] public Curve yawRotation = MathX.Curve( 0f, 1f ); @@ -186,8 +184,24 @@ namespace Rokojori [Export] public Curve randomRotation = MathX.Curve( 0f, 20f ); + [ExportGroup( "Noise")] + [Export] + public Curve vertexTurbulenceAmount = MathX.Curve( 0f, 0f ); - [ExportGroup( "Blade Normals")] + [Export] + public Curve vertexTurbulenceScale = MathX.Curve( 1f, 1f ); + + [Export] + public Curve vertexTurbulenceScaleX = MathX.Curve( 1f, 1f ); + + [Export] + public Curve vertexTurbulenceScaleY = MathX.Curve( 1f, 1f ); + + [Export] + public Curve vertexTurbulenceScaleZ = MathX.Curve( 1f, 1f ); + + + [ExportGroup( "Normals")] [Export] public Curve normalBlending = MathX.Curve( 0.5f ); @@ -199,7 +213,7 @@ namespace Rokojori - [ExportGroup( "Blade Filter")] + [ExportGroup( "Filter")] [Export (PropertyHint.Range, "0,1")] public float filterTreshold = 1; @@ -412,6 +426,29 @@ namespace Rokojori mg.CenterMesh( true, false, true, box ); } + if ( vertexTurbulenceAmount != null ) + { + var turbulenceAmount = random.Sample( vertexTurbulenceAmount ); + var turbulenceScale = Vector3.One * ( vertexTurbulenceScale == null ? 1 : random.Sample( vertexTurbulenceScale ) ); + + if ( vertexTurbulenceScaleX != null ) + { + turbulenceScale.X *= random.Sample( vertexTurbulenceScaleX ); + } + + if ( vertexTurbulenceScaleY != null ) + { + turbulenceScale.Y *= random.Sample( vertexTurbulenceScaleY ); + } + + if ( vertexTurbulenceScaleZ != null ) + { + turbulenceScale.Z *= random.Sample( vertexTurbulenceScaleX ); + } + + mg.Turbulence( turbulenceAmount, turbulenceScale, Vector3.Zero ); + } + if ( xRemapper != null ) { mg.RemapX( xRemapper ); @@ -441,6 +478,8 @@ namespace Rokojori { mg.ScaleZForY( scaleZForY ); } + + X_numTriangles = mg.indices.Count / 3; diff --git a/Runtime/Procedural/Baking/Baker.cs b/Runtime/Procedural/Baking/Baker.cs index 6f42829..fb2fb1f 100644 --- a/Runtime/Procedural/Baking/Baker.cs +++ b/Runtime/Procedural/Baking/Baker.cs @@ -111,6 +111,22 @@ namespace Rokojori [Export] public float distance = 1; + public static Baker Create( Node parent, Node3D target, Vector2I size, string name ) + { + var bakingView = parent.CreateChild( "Viewport " + name ); + bakingView.TransparentBg = true; + bakingView.Size = size; + + var bakingCamera = bakingView.CreateChild( "Camera View " + name ); + var baker = bakingView.CreateChild( "Baker " + name ); + + baker.camera = bakingCamera; + baker.target = target; + baker.viewport = bakingView; + + return baker; + } + public override void _Process( double delta ) { if ( ! ( update || updateAlways ) ) diff --git a/Runtime/Procedural/Baking/MultiBaker.cs b/Runtime/Procedural/Baking/MultiBaker.cs index 09eb51a..cd5eb91 100644 --- a/Runtime/Procedural/Baking/MultiBaker.cs +++ b/Runtime/Procedural/Baking/MultiBaker.cs @@ -180,8 +180,8 @@ namespace Rokojori [Export] public WorldEnvironment X_worldEnvironment; - [Export] - public DilateTexture X_dilateTexture; + // [Export] + // public DilateTexture X_dilateTexture; [Export] public MeshInstance3D X_outputMesh; @@ -211,22 +211,23 @@ namespace Rokojori public Texture2D X_bakedTextureDepth; - - bool _initialized = false; bool _baking = false; - - - SerializedGodotObject _cached; - public override void _Ready() { - Initialize(); + } - + + public bool preventProcessing = true; + public override void _Process( double delta ) - { + { + if ( preventProcessing ) + { + return; + } + X_texturePreview.Visible = showOutputTexture; if ( _baking ) @@ -234,20 +235,14 @@ namespace Rokojori return; } - if ( initialize || ! _initialized ) + if ( initialize ) { initialize = false; Initialize(); } - var current = SerializedGodotObject.Create( this ); - - var changed = _cached != null && ! _cached.Equals( current ); - - _cached = current; - - if ( bake || changed && preview_UpdateAlways ) + if ( bake || preview_UpdateAlways ) { bake = false; Bake(); @@ -338,19 +333,19 @@ namespace Rokojori this.LogInfo( "Texture created:", bakingMaterialModes[ i ] ); - if ( preview_DilateTextures ) - { - this.LogInfo( "Dilating:", bakingMaterialModes[ i ] ); - texture = await CreateDilatedTexture(); + // if ( preview_DilateTextures ) + // { + // this.LogInfo( "Dilating:", bakingMaterialModes[ i ] ); + // texture = await CreateDilatedTexture(); - this.LogInfo( "Dilating done:", bakingMaterialModes[ i ] ); - } - else - { + // this.LogInfo( "Dilating done:", bakingMaterialModes[ i ] ); + // } + // else + // { texture = Textures.Copy( texture ); await this.RequestNextFrame(); await this.RequestNextFrame(); - } + // } this.LogInfo( "Assigning Texture", bakingMaterialModes[ i ] ); @@ -379,37 +374,37 @@ namespace Rokojori } - public async Task CreateDilatedTexture() - { - var viewports = GetAllViewports(); - var textures = Lists.Map( viewports, v => v.GetTexture() as Texture2D ); - var dilatedTextures = new List(); + // public async Task CreateDilatedTexture() + // { + // var viewports = GetAllViewports(); + // var textures = Lists.Map( viewports, v => v.GetTexture() as Texture2D ); + // var dilatedTextures = new List(); - var index = 0; + // var index = 0; - foreach ( var t in textures ) - { - index ++; + // foreach ( var t in textures ) + // { + // index ++; - var dilatedTexture = await X_dilateTexture.Create( t ); - dilatedTextures.Add( dilatedTexture ); - } + // var dilatedTexture = await X_dilateTexture.Create( t ); + // dilatedTextures.Add( dilatedTexture ); + // } - X_textureMerger.sourceTextures = dilatedTextures.ToArray(); - X_textureMerger.sourceMode = TextureMerger.SourceMode.Textures; + // X_textureMerger.sourceTextures = dilatedTextures.ToArray(); + // X_textureMerger.sourceMode = TextureMerger.SourceMode.Textures; - X_textureMerger.Initialize(); + // X_textureMerger.Initialize(); - await this.RequestNextFrame(); + // await this.RequestNextFrame(); - X_textureMerger.CreateLayout(); + // X_textureMerger.CreateLayout(); - await this.RequestNextFrame(); + // await this.RequestNextFrame(); - var finalTexture = await X_dilateTexture.Create( X_textureMerger.X_textureMergerViewport.GetTexture() ); + // var finalTexture = await X_dilateTexture.Create( X_textureMerger.X_textureMergerViewport.GetTexture() ); - return finalTexture; - } + // return finalTexture; + // } public List GetAllViewports() { @@ -474,7 +469,7 @@ namespace Rokojori X_views = X_bakingViewport.CreateChild( "Views" ); - X_dilateTexture = this.CreateChild( "Dilate Texture" ); + // X_dilateTexture = this.CreateChild( "Dilate Texture" ); X_textureMerger = this.CreateChild( "Texture Merger" ); X_textureMerger.multiBaker = this; diff --git a/Runtime/Procedural/Mesh/MeshGeometry.cs b/Runtime/Procedural/Mesh/MeshGeometry.cs index 36e7cf5..04c2e5f 100644 --- a/Runtime/Procedural/Mesh/MeshGeometry.cs +++ b/Runtime/Procedural/Mesh/MeshGeometry.cs @@ -791,6 +791,16 @@ namespace Rokojori } } + public void Turbulence( float amount, Vector3 noiseScale, Vector3 noiseOffset ) + { + for ( int i = 0; i < vertices.Count; i++ ) + { + var v = vertices[ i ]; + var offset = Noise.PerlinPolar3( v * noiseScale + noiseOffset ) * amount; + vertices[ i ] = v + offset; + } + } + public void RemapX( Curve curve ) { for ( int i = 0; i < vertices.Count; i++ ) @@ -821,6 +831,8 @@ namespace Rokojori } } + + public void ScaleXForY( Curve curve ) { for ( int i = 0; i < vertices.Count; i++ ) diff --git a/Runtime/Procedural/Scatter/Generators/GenerateInBox.cs b/Runtime/Procedural/Scatter/Generators/GenerateInBox.cs index 04ed5d8..6fe0536 100644 --- a/Runtime/Procedural/Scatter/Generators/GenerateInBox.cs +++ b/Runtime/Procedural/Scatter/Generators/GenerateInBox.cs @@ -28,8 +28,8 @@ namespace Rokojori { CreateWeights(); - var minPosition = Math3D.MinPosition( cornerA, cornerB ); - var maxPosition = Math3D.MaxPosition( cornerA, cornerB ); + var minPosition = Math3D.MinLocalPosition( cornerA, cornerB ); + var maxPosition = Math3D.MaxLocalPosition( cornerA, cornerB ); if ( snapToWorldGrid ) { @@ -46,7 +46,7 @@ namespace Rokojori if ( xzOnly ) { - var y = ( cornerA.GlobalPosition.Y + cornerB.GlobalPosition.Y ) / 2f; + var y = ( cornerA.Position.Y + cornerB.Position.Y ) / 2f; for ( int x = 0; x < pointsX; x++ ) { @@ -89,6 +89,8 @@ namespace Rokojori var p = new ScatterPoint( this, id++ ); p.position = new Vector3( x, y, z ); + p.useGlobalPosition = false; + p.visible = ! setDiscarded; p.seed = Noise.CreateSeed( p.position ); diff --git a/Runtime/Procedural/Scatter/Scatterer.cs b/Runtime/Procedural/Scatter/Scatterer.cs index 8a9e60a..6e5f794 100644 --- a/Runtime/Procedural/Scatter/Scatterer.cs +++ b/Runtime/Procedural/Scatter/Scatterer.cs @@ -103,6 +103,15 @@ namespace Rokojori Dictionary _scattererIDs = new Dictionary(); + public enum PositionMode + { + Local_As_Global, + Local_Plus_ScattererGlobal + } + + [Export] + public PositionMode positionMode; + public void ClearCache() { _instantiatedPoints.Clear(); diff --git a/Runtime/Procedural/Scatter/Transform/RandomizeTransform.cs b/Runtime/Procedural/Scatter/Transform/RandomizeTransform.cs index a023bc2..c6727c3 100644 --- a/Runtime/Procedural/Scatter/Transform/RandomizeTransform.cs +++ b/Runtime/Procedural/Scatter/Transform/RandomizeTransform.cs @@ -54,7 +54,7 @@ namespace Rokojori [Export] public Vector3 scaleNoiseOffset = Vector3.Zero; - + protected override List _Scatter( List points, Scatterer root ) { var output = points; diff --git a/Runtime/Random/RandomEngine.cs b/Runtime/Random/RandomEngine.cs index fe2ddb8..b31d409 100644 --- a/Runtime/Random/RandomEngine.cs +++ b/Runtime/Random/RandomEngine.cs @@ -377,6 +377,30 @@ namespace Rokojori return IndexFromUnnormalizedWeights( weights, 1 ); } + public List Numbers( int numValues, int offset = 0 ) + { + var list = new List(); + + for ( int i = 0; i < numValues; i++ ) + { + list.Add( i + offset ); + } + + return new RandomList( list, this ).GetList(); + } + + public List Selection( int possibleValues, int selection ) + { + var list = new List(); + + for ( int i = 0; i < possibleValues; i++ ) + { + list.Add( i ); + } + + return new RandomList( list, this ).GetList().GetRange( 0, selection ); + } + int _FindElementIndexWithWeights( List weights, float value ) { var limit = 0f; diff --git a/Runtime/Selectors/Selector.cs b/Runtime/Selectors/Selector.cs index 8b75b72..03768ef 100644 --- a/Runtime/Selectors/Selector.cs +++ b/Runtime/Selectors/Selector.cs @@ -16,5 +16,10 @@ namespace Rokojori { return true; } + + public static bool IsSelecting( Selector selector, Node node ) + { + return selector == null || selector.Selects( node ); + } } } \ No newline at end of file diff --git a/Runtime/Shading/Library/Transform.gdshaderinc b/Runtime/Shading/Library/Transform.gdshaderinc index b69d55d..959eb6c 100644 --- a/Runtime/Shading/Library/Transform.gdshaderinc +++ b/Runtime/Shading/Library/Transform.gdshaderinc @@ -15,6 +15,16 @@ vec3 applyMatrixWithTranslation( vec3 v, mat4 m ) return ( mw * vec4( v, 1.0 ) ).xyz; } +// L W V +// L +// W +// V + +// LD WD VD +// LD +// WD +// VD + vec3 localToWorld( vec3 _VERTEX, mat4 _MODEL_MATRIX ) { return ( _MODEL_MATRIX * vec4( _VERTEX, 1.0 ) ).xyz; diff --git a/Runtime/Shading/Materials/CustomMaterial.cs b/Runtime/Shading/Materials/CustomMaterial.cs index 9b536d9..a3725c4 100644 --- a/Runtime/Shading/Materials/CustomMaterial.cs +++ b/Runtime/Shading/Materials/CustomMaterial.cs @@ -5,7 +5,6 @@ using System.Collections.Generic; namespace Rokojori { [Tool] - [GlobalClass] public partial class CustomMaterial:ShaderMaterial { public void CopyUniformsFrom( ShaderMaterial material ) diff --git a/Runtime/Shading/Shaders/Baking/Dilation.gdshader b/Runtime/Shading/Shaders/Baking/Dilation.gdshader new file mode 100644 index 0000000..51535c6 --- /dev/null +++ b/Runtime/Shading/Shaders/Baking/Dilation.gdshader @@ -0,0 +1,35 @@ +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 new file mode 100644 index 0000000..fd6a5f2 --- /dev/null +++ b/Runtime/Shading/Shaders/Baking/Dilation.gdshader.uid @@ -0,0 +1 @@ +uid://djf46k1sq5eks diff --git a/Runtime/Shading/Shaders/Billboards/QuadBillboard/QuadBillboardMaterial.cs b/Runtime/Shading/Shaders/Billboards/QuadBillboard/QuadBillboardMaterial.cs index de18579..da7d0c7 100644 --- a/Runtime/Shading/Shaders/Billboards/QuadBillboard/QuadBillboardMaterial.cs +++ b/Runtime/Shading/Shaders/Billboards/QuadBillboard/QuadBillboardMaterial.cs @@ -49,7 +49,6 @@ namespace Rokojori } [Tool] - [GlobalClass] public partial class QuadBillboardMaterial:CustomMaterial { diff --git a/Runtime/Shading/Shaders/Effects/FancyOutline/FancyOutlineMaterial.cs b/Runtime/Shading/Shaders/Effects/FancyOutline/FancyOutlineMaterial.cs index b4a4d84..45581f8 100644 --- a/Runtime/Shading/Shaders/Effects/FancyOutline/FancyOutlineMaterial.cs +++ b/Runtime/Shading/Shaders/Effects/FancyOutline/FancyOutlineMaterial.cs @@ -25,7 +25,6 @@ namespace Rokojori } [Tool] - [GlobalClass] public partial class FancyOutlineMaterial:CustomMaterial { public static readonly CachedResource ScanWhite500Ms20x = CustomMaterial.Cached( diff --git a/Runtime/Shading/Shaders/Effects/FresnelOverlay/FresnelOverlayMaterial.cs b/Runtime/Shading/Shaders/Effects/FresnelOverlay/FresnelOverlayMaterial.cs index 6ff2d81..2ecd445 100644 --- a/Runtime/Shading/Shaders/Effects/FresnelOverlay/FresnelOverlayMaterial.cs +++ b/Runtime/Shading/Shaders/Effects/FresnelOverlay/FresnelOverlayMaterial.cs @@ -20,7 +20,6 @@ namespace Rokojori } [Tool] - [GlobalClass] public partial class FresnelOverlayMaterial:CustomMaterial { diff --git a/Runtime/Shading/Shaders/Effects/Outline/OutlineMaterial.cs b/Runtime/Shading/Shaders/Effects/Outline/OutlineMaterial.cs index 7dec3b7..12b352c 100644 --- a/Runtime/Shading/Shaders/Effects/Outline/OutlineMaterial.cs +++ b/Runtime/Shading/Shaders/Effects/Outline/OutlineMaterial.cs @@ -22,7 +22,6 @@ namespace Rokojori } [Tool] - [GlobalClass] public partial class OutlineMaterial:CustomMaterial { public static readonly CachedResource BoldYellow = CustomMaterial.Cached( diff --git a/Runtime/Shading/Shaders/Effects/Overlay/OverlayMaterial.cs b/Runtime/Shading/Shaders/Effects/Overlay/OverlayMaterial.cs index 4291975..5047b44 100644 --- a/Runtime/Shading/Shaders/Effects/Overlay/OverlayMaterial.cs +++ b/Runtime/Shading/Shaders/Effects/Overlay/OverlayMaterial.cs @@ -17,7 +17,6 @@ namespace Rokojori } [Tool] - [GlobalClass] public partial class OverlayMaterial:CustomMaterial { diff --git a/Runtime/Shading/Shaders/Effects/ScanGradient/ScanGradientMaterial.cs b/Runtime/Shading/Shaders/Effects/ScanGradient/ScanGradientMaterial.cs index d0e4739..8e66401 100644 --- a/Runtime/Shading/Shaders/Effects/ScanGradient/ScanGradientMaterial.cs +++ b/Runtime/Shading/Shaders/Effects/ScanGradient/ScanGradientMaterial.cs @@ -29,7 +29,6 @@ namespace Rokojori } [Tool] - [GlobalClass] public partial class ScanGradientMaterial:CustomMaterial { public static readonly CachedResource White = CustomMaterial.Cached( diff --git a/Runtime/Shading/Shaders/Effects/TriPlanarOverlay/TriPlanarOverlayMaterial.cs b/Runtime/Shading/Shaders/Effects/TriPlanarOverlay/TriPlanarOverlayMaterial.cs index 90001b4..f354761 100644 --- a/Runtime/Shading/Shaders/Effects/TriPlanarOverlay/TriPlanarOverlayMaterial.cs +++ b/Runtime/Shading/Shaders/Effects/TriPlanarOverlay/TriPlanarOverlayMaterial.cs @@ -20,7 +20,6 @@ namespace Rokojori } [Tool] - [GlobalClass] public partial class TriPlanarOverlayMaterial:CustomMaterial { public static readonly CachedResource BlueShield = CustomMaterial.Cached( diff --git a/Runtime/Shading/Shaders/Wipes/FadeWipe/FadeWipeMaterial.cs b/Runtime/Shading/Shaders/Wipes/FadeWipe/FadeWipeMaterial.cs index a5f1483..b02ee65 100644 --- a/Runtime/Shading/Shaders/Wipes/FadeWipe/FadeWipeMaterial.cs +++ b/Runtime/Shading/Shaders/Wipes/FadeWipe/FadeWipeMaterial.cs @@ -24,7 +24,6 @@ namespace Rokojori } [Tool] - [GlobalClass] public partial class FadeWipeMaterial:CustomMaterial { diff --git a/Runtime/Shading/Tools/CSShaderClassGenerator/CSShaderClassTemplate.txt b/Runtime/Shading/Tools/CSShaderClassGenerator/CSShaderClassTemplate.txt index a95388c..a20677e 100644 --- a/Runtime/Shading/Tools/CSShaderClassGenerator/CSShaderClassTemplate.txt +++ b/Runtime/Shading/Tools/CSShaderClassGenerator/CSShaderClassTemplate.txt @@ -15,7 +15,6 @@ namespace Rokojori } [Tool] - [GlobalClass] public partial class ${ShaderName}Material:CustomMaterial { ${MaterialPresets} diff --git a/Runtime/Structures/QueueList.cs b/Runtime/Structures/QueueList.cs new file mode 100644 index 0000000..1f2eafd --- /dev/null +++ b/Runtime/Structures/QueueList.cs @@ -0,0 +1,54 @@ + +using System.Collections.Generic; + +namespace Rokojori +{ + public class QueueList:List + { + List _queuedInsertions = new List(); + List _queuedRemovals = new List(); + + public void QueueInsert( T t ) + { + _queuedInsertions.Add( t ); + } + + public void QueueRemoval( T t ) + { + _queuedRemovals.Add( t ); + } + + public void IterateAndResolve( System.Action callback ) + { + ResolveRemovalsQueue(); + ResolveInsertionQueue(); + + this.ForEach( t => callback( t ) ); + + ResolveRemovalsQueue(); + } + + protected void ResolveInsertionQueue() + { + if ( _queuedInsertions.Count == 0 ) + { + return; + } + + AddRange( _queuedInsertions ); + _queuedInsertions.Clear(); + } + + protected void ResolveRemovalsQueue() + { + if ( _queuedRemovals.Count == 0 ) + { + return; + } + + Lists.RemoveRange( this, _queuedRemovals ); + _queuedRemovals.Clear(); + } + + } +} \ No newline at end of file diff --git a/Runtime/Time/TimeLineManager.cs b/Runtime/Time/TimeLineManager.cs index 8b22b4a..8ea6013 100644 --- a/Runtime/Time/TimeLineManager.cs +++ b/Runtime/Time/TimeLineManager.cs @@ -50,16 +50,31 @@ namespace Rokojori float _lastTimeScale = 1; + QueueList _processCallbacks = new QueueList(); + public override void _Process( double delta ) { UpdateRealTime( delta ); if ( ! Engine.IsEditorHint() && gametimeTimeline != null ) { - Engine.TimeScale = gametimeTimeline.runner.modulatedSpeed; + Engine.TimeScale = GetRunner( gametimeTimeline ).modulatedSpeed; + } - _runners.ForEach( r => r.UpdateTimeLine( unscaledTimeDelta ) ); + _runners.ForEach( r => r.UpdateTimeLine( unscaledTimeDelta ) ); + + _processCallbacks.IterateAndResolve( t => t() ); + } + + public void AddProcessCallback( System.Action a ) + { + _processCallbacks.QueueInsert( a ); + } + + public void RemoveProcessCallback( System.Action a ) + { + _processCallbacks.QueueRemoval( a ); } public int GetTimeLineIndex( TimeLine timeLine ) diff --git a/Runtime/UI/Shaders/NinePatch/UINinePatchMaterial.cs b/Runtime/UI/Shaders/NinePatch/UINinePatchMaterial.cs index d74804c..729ca3c 100644 --- a/Runtime/UI/Shaders/NinePatch/UINinePatchMaterial.cs +++ b/Runtime/UI/Shaders/NinePatch/UINinePatchMaterial.cs @@ -16,7 +16,6 @@ namespace Rokojori } [Tool] - [GlobalClass] public partial class UINinePatchMaterial:CustomMaterial { diff --git a/Runtime/UI/Shaders/RoundedRectangle/RoundedRectangleMaterial.cs b/Runtime/UI/Shaders/RoundedRectangle/RoundedRectangleMaterial.cs index 4800576..9cfba3a 100644 --- a/Runtime/UI/Shaders/RoundedRectangle/RoundedRectangleMaterial.cs +++ b/Runtime/UI/Shaders/RoundedRectangle/RoundedRectangleMaterial.cs @@ -32,7 +32,6 @@ namespace Rokojori } [Tool] - [GlobalClass] public partial class RoundedRectangleMaterial:CustomMaterial { From e103f0ab567b1db27e0d8280d5841eb5bb44b482 Mon Sep 17 00:00:00 2001 From: Josef Date: Sun, 6 Apr 2025 18:39:24 +0200 Subject: [PATCH 2/4] Before MultiBaker Update --- Runtime/Procedural/Baking/MultiBaker.cs | 105 ++---------------- Runtime/Random/RandomEngine.cs | 2 + .../Shading/Shaders/Baking/Dilation.gdshader | 35 ------ .../Shaders/Baking/Dilation.gdshader.uid | 1 - .../Shaders/Baking/DilationDrawer.gdshader | 93 ++++++++++++++++ .../Baking/DilationDrawer.gdshader.uid | 1 + .../Shaders/Baking/DilationDrawerMaterial.cs | 53 +++++++++ .../Baking/DilationDrawerMaterial.cs.uid | 1 + Runtime/Structures/QueueList.cs.uid | 1 + 9 files changed, 160 insertions(+), 132 deletions(-) delete mode 100644 Runtime/Shading/Shaders/Baking/Dilation.gdshader delete mode 100644 Runtime/Shading/Shaders/Baking/Dilation.gdshader.uid create mode 100644 Runtime/Shading/Shaders/Baking/DilationDrawer.gdshader create mode 100644 Runtime/Shading/Shaders/Baking/DilationDrawer.gdshader.uid create mode 100644 Runtime/Shading/Shaders/Baking/DilationDrawerMaterial.cs create mode 100644 Runtime/Shading/Shaders/Baking/DilationDrawerMaterial.cs.uid create mode 100644 Runtime/Structures/QueueList.cs.uid 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 From 523db80daeaeb95a35e29fe8e31dd0dc91463193 Mon Sep 17 00:00:00 2001 From: Josef Date: Tue, 8 Apr 2025 11:07:02 +0200 Subject: [PATCH 3/4] Baking Rewrite --- Runtime/Godot/Nodes.cs | 1 + Runtime/Math/Geometry/Box3.cs | 13 +- Runtime/Procedural/Baking/Baker.cs | 149 ++----- .../Depth/Depth From Standard.tres | 10 +- .../Normal/Normal From Standard.tres | 10 +- .../ORM/ORM From Standard.tres | 12 +- Runtime/Procedural/Baking/MultiBakeMode.cs | 20 - .../Baking/MultiBakeModeCrossBraces.cs | 142 ------- .../Baking/MultiBakeModeCylinder.cs | 155 ------- .../Baking/MultiBakeModeSpherical.cs | 42 -- .../Baking/MultiBaker/CrossBraces_Baker.cs | 150 +++++++ .../CrossBraces_Baker.cs.uid} | 0 .../Baking/MultiBaker/Cylinder_Baker.cs | 167 ++++++++ .../Cylinder_Baker.cs.uid} | 0 .../Baking/{ => MultiBaker}/MultiBaker.cs | 401 +++++++++--------- .../Baking/{ => MultiBaker}/MultiBaker.cs.uid | 0 .../MultiBaker/MultiBakerCameraTools.cs | 14 + .../{ => MultiBaker}/OctahedralMapping.cs | 0 .../{ => MultiBaker}/OctahedralMapping.cs.uid | 0 .../Octahedral_Baker.cs} | 51 +-- .../Baking/MultiBaker/Octahedral_Baker.cs.uid | 1 + .../Octahedral_Baker.uid} | 0 .../Baking/MultiBaker/_XX_MultiBakeMode.cs | 36 ++ .../_XX_MultiBakeMode.cs.uid} | 0 .../_XX_MultiBakeModeBillboardBase.cs} | 3 +- .../_XX_MultiBakeModeBillboardBase.cs.uid} | 0 .../Baking/MultiBaker/_XX_Spherical_Baker.cs | 48 +++ .../MultiBaker/_XX_Spherical_Baker.cs.uid | 1 + .../_XX_Spherical_Baker.uid} | 0 Runtime/Procedural/Baking/TextureMerger.cs | 47 +- .../AutoDistance_BakingFDSettings.cs | 40 ++ .../AutoDistance_BakingFDSettings.cs.uid | 1 + .../AutoFOVDistance_BakingFDSettings.cs | 45 ++ .../AutoFOVDistance_BakingFDSettings.cs.uid | 1 + .../Baking/ViewSettings/BakingViewSettings.cs | 154 +++++++ .../ViewSettings/BakingViewSettings.cs.uid | 1 + .../ViewSettings/Manual_BakingFDSettings.cs | 33 ++ .../Manual_BakingFDSettings.cs.uid | 1 + .../ViewSettings/_XX_BakingFDSettings.cs | 27 ++ .../ViewSettings/_XX_BakingFDSettings.cs.uid | 1 + 40 files changed, 1048 insertions(+), 729 deletions(-) delete mode 100644 Runtime/Procedural/Baking/MultiBakeMode.cs delete mode 100644 Runtime/Procedural/Baking/MultiBakeModeCrossBraces.cs delete mode 100644 Runtime/Procedural/Baking/MultiBakeModeCylinder.cs delete mode 100644 Runtime/Procedural/Baking/MultiBakeModeSpherical.cs create mode 100644 Runtime/Procedural/Baking/MultiBaker/CrossBraces_Baker.cs rename Runtime/Procedural/Baking/{MultiBakeModeCrossBraces.cs.uid => MultiBaker/CrossBraces_Baker.cs.uid} (100%) create mode 100644 Runtime/Procedural/Baking/MultiBaker/Cylinder_Baker.cs rename Runtime/Procedural/Baking/{MultiBakeModeCylinder.cs.uid => MultiBaker/Cylinder_Baker.cs.uid} (100%) rename Runtime/Procedural/Baking/{ => MultiBaker}/MultiBaker.cs (71%) rename Runtime/Procedural/Baking/{ => MultiBaker}/MultiBaker.cs.uid (100%) create mode 100644 Runtime/Procedural/Baking/MultiBaker/MultiBakerCameraTools.cs rename Runtime/Procedural/Baking/{ => MultiBaker}/OctahedralMapping.cs (100%) rename Runtime/Procedural/Baking/{ => MultiBaker}/OctahedralMapping.cs.uid (100%) rename Runtime/Procedural/Baking/{MultiBakeModeOctahedral.cs => MultiBaker/Octahedral_Baker.cs} (66%) create mode 100644 Runtime/Procedural/Baking/MultiBaker/Octahedral_Baker.cs.uid rename Runtime/Procedural/Baking/{MultiBakeModeOctahedral.cs.uid => MultiBaker/Octahedral_Baker.uid} (100%) create mode 100644 Runtime/Procedural/Baking/MultiBaker/_XX_MultiBakeMode.cs rename Runtime/Procedural/Baking/{MultiBakeMode.cs.uid => MultiBaker/_XX_MultiBakeMode.cs.uid} (100%) rename Runtime/Procedural/Baking/{MultiBakeModeBillboardBase.cs => MultiBaker/_XX_MultiBakeModeBillboardBase.cs} (93%) rename Runtime/Procedural/Baking/{MultiBakeModeBillboardBase.cs.uid => MultiBaker/_XX_MultiBakeModeBillboardBase.cs.uid} (100%) create mode 100644 Runtime/Procedural/Baking/MultiBaker/_XX_Spherical_Baker.cs create mode 100644 Runtime/Procedural/Baking/MultiBaker/_XX_Spherical_Baker.cs.uid rename Runtime/Procedural/Baking/{MultiBakeModeSpherical.cs.uid => MultiBaker/_XX_Spherical_Baker.uid} (100%) create mode 100644 Runtime/Procedural/Baking/ViewSettings/AutoDistance_BakingFDSettings.cs create mode 100644 Runtime/Procedural/Baking/ViewSettings/AutoDistance_BakingFDSettings.cs.uid create mode 100644 Runtime/Procedural/Baking/ViewSettings/AutoFOVDistance_BakingFDSettings.cs create mode 100644 Runtime/Procedural/Baking/ViewSettings/AutoFOVDistance_BakingFDSettings.cs.uid create mode 100644 Runtime/Procedural/Baking/ViewSettings/BakingViewSettings.cs create mode 100644 Runtime/Procedural/Baking/ViewSettings/BakingViewSettings.cs.uid create mode 100644 Runtime/Procedural/Baking/ViewSettings/Manual_BakingFDSettings.cs create mode 100644 Runtime/Procedural/Baking/ViewSettings/Manual_BakingFDSettings.cs.uid create mode 100644 Runtime/Procedural/Baking/ViewSettings/_XX_BakingFDSettings.cs create mode 100644 Runtime/Procedural/Baking/ViewSettings/_XX_BakingFDSettings.cs.uid diff --git a/Runtime/Godot/Nodes.cs b/Runtime/Godot/Nodes.cs index bde3bf2..f47d976 100644 --- a/Runtime/Godot/Nodes.cs +++ b/Runtime/Godot/Nodes.cs @@ -400,6 +400,7 @@ namespace Rokojori } } + public static T GetDirectChild( Node parent ) where T:Node { if ( parent == null ) diff --git a/Runtime/Math/Geometry/Box3.cs b/Runtime/Math/Geometry/Box3.cs index 78eeb57..7d3ce8b 100644 --- a/Runtime/Math/Geometry/Box3.cs +++ b/Runtime/Math/Geometry/Box3.cs @@ -21,6 +21,12 @@ namespace Rokojori return new Aabb( box.min, box.size ); } + public void Translate( Vector3 translation ) + { + min += translation; + max += translation; + } + public static Box3 FromPositionAndScale( Vector3 position, Vector3 scale ) { var max = scale * 0.5f; @@ -40,7 +46,12 @@ namespace Rokojori public Vector3 size => max - min; - + public void IncludePoint( Vector3 p ) + { + min = min.Min( p ); + max = max.Max( p ); + } + public Box2 AsXZBox2() { var b = new Box2(); diff --git a/Runtime/Procedural/Baking/Baker.cs b/Runtime/Procedural/Baking/Baker.cs index fb2fb1f..dfd30f9 100644 --- a/Runtime/Procedural/Baking/Baker.cs +++ b/Runtime/Procedural/Baking/Baker.cs @@ -11,105 +11,48 @@ namespace Rokojori [GlobalClass] public partial class Baker:Node { - public enum CameraDistanceDetectionType - { - Automatic_Distance_Detection, - Custom_Distance - } - - public enum CameraFOVMode - { - Keep_Fov, - Compute_Fov_With_Distance, - Custom_Fov - } - public enum MeshMode - { - World_Scale, - Custom_Scale - } - - [Export] - public bool update = false; + [ExportToolButton( "Update")] + public Callable BakeButton => Callable.From( () => Bake() ); [Export] public bool updateAlways = false; + [ExportGroup( "View") ] + + [Export] + public BakingViewSettings viewSettings = new BakingViewSettings(); + [Export] public Viewport viewport; + + [Export] + public Camera3D camera; + + + [ExportGroup( "Target")] [Export] public Node3D target; [Export] - public Camera3D camera; - + public Vector3 targetPivot; [Export] - public float originalFOV = 75; + public bool autoTargetPivot = true; - - [Export] - public bool assignFOV = false; - [Export] - public float placingDistance = 500; - - [Export] - public float computedFOV = 75; - - [Export] - public bool useCustomFOV = false; - - [Export] - public float customFOV = 75; - - [Export] - public bool useCustomDistance = false; - - [Export] - public float customDistance = 50; - - [Export] - public float outputScale = 1; + [ExportGroup( "Output")] [Export] public Node3D outputTexture; + [Export] + public float outputScale = 1; + [Export] public float outputTextureSize = 1; - public enum RotationMode - { - Yaw_Pitch, - Quaternion - } - - [ExportGroup("Rotation")] - - [Export] - public RotationMode rotationMode = RotationMode.Yaw_Pitch; - - [Export( PropertyHint.Range, "-180,180")] - public float yaw = 0; - - [Export( PropertyHint.Range, "-180,180")] - public float pitch = 0; - - [Export] - public Quaternion rotationQuaternion; - - - public Quaternion bakingRotation => RotationMode.Yaw_Pitch == rotationMode ? - Math3D.YawPitchRotation( yaw, pitch ) : - rotationQuaternion; - - [Export] - public float zoom = 1; - - [Export] - public float distance = 1; public static Baker Create( Node parent, Node3D target, Vector2I size, string name ) { @@ -129,72 +72,36 @@ namespace Rokojori public override void _Process( double delta ) { - if ( ! ( update || updateAlways ) ) + if ( ! updateAlways ) { return; } - update = false; - Bake(); } - void Bake() + public void Bake() { if ( viewport == null || target == null || camera == null ) { return; } - - var box = target.GetWorldBounds(); - - if ( box == null ) - { - RJLog.Log( "No target" ); - return; - } - - var sphere = Sphere.ContainingBox( box ); - camera.Fov = originalFOV; - - var billboardFOV = camera.Fov; - - if ( assignFOV ) + if ( autoTargetPivot ) { - computedFOV = Cameras.ComputeFOVForBillboard( originalFOV, sphere.radius, placingDistance ); - billboardFOV = computedFOV; - camera.Fov = billboardFOV; + Box3 box = target.GetWorldBounds(); + targetPivot = new Vector3( 0, -box.center.Y, 0 ); } - if ( useCustomFOV ) - { - billboardFOV = customFOV; - camera.Fov = billboardFOV; - } - - var newDistance = useCustomDistance ? customDistance : Cameras.ComputeCameraFrameFittingDistance( billboardFOV, sphere.radius / zoom ); - - if ( newDistance != distance ) - { - distance = newDistance; - // RJLog.Log( "New Distance:", box, sphere, distance ); - } - - var cameraRotation = bakingRotation; - var offset = ( Vector3.Back * distance ) * cameraRotation ; - camera.GlobalPosition = target.GlobalPosition + offset; - - camera.SetGlobalQuaternion( cameraRotation.Inverse() ); - - // RJLog.Log( "Set Rotation", cameraRotation, ">>", camera.GetGlobalQuaternion() ); - - outputScale = Cameras.ComputeCameraFittingScale( camera.Fov, distance ); + viewSettings.ApplySettings( camera, target, targetPivot ); + + var outputScale = Cameras.ComputeCameraFittingScale( camera.Fov, viewSettings._XX_ComputedDistance ); if ( outputTexture != null ) { outputTexture.Scale = Vector3.One * outputScale; + outputTexture.GlobalPosition = target.GlobalPosition - targetPivot; } } diff --git a/Runtime/Procedural/Baking/BakingMaterials/Depth/Depth From Standard.tres b/Runtime/Procedural/Baking/BakingMaterials/Depth/Depth From Standard.tres index 4e357a8..a97a411 100644 --- a/Runtime/Procedural/Baking/BakingMaterials/Depth/Depth From Standard.tres +++ b/Runtime/Procedural/Baking/BakingMaterials/Depth/Depth From Standard.tres @@ -1,14 +1,14 @@ [gd_resource type="Resource" script_class="SubMaterialTransfer" load_steps=16 format=3 uid="uid://crxw8r6q5uhpe"] [ext_resource type="Resource" uid="uid://cwbo0avyyq0t" path="res://addons/rokojori_action_library/Runtime/Shading/Properties/Property Library/Color/Albedo Color.tres" id="1_vhp84"] -[ext_resource type="Script" path="res://addons/rokojori_action_library/Runtime/Shading/Materials/Transfers/ColorPropertyTransfer.cs" id="2_awr2w"] +[ext_resource type="Script" uid="uid://c4kkc55ia5udl" path="res://addons/rokojori_action_library/Runtime/Shading/Materials/Transfers/ColorPropertyTransfer.cs" id="2_awr2w"] [ext_resource type="Resource" uid="uid://bja8pltfjyt3x" path="res://addons/rokojori_action_library/Runtime/Shading/Properties/Property Library/Float/Alpha Scissor Threshold.tres" id="3_baip0"] -[ext_resource type="Script" path="res://addons/rokojori_action_library/Runtime/Shading/Materials/Transfers/FloatPropertyTransfer.cs" id="4_r0n2g"] -[ext_resource type="Script" path="res://addons/rokojori_action_library/Runtime/Shading/Materials/SubMaterialTransfer.cs" id="5_iuqjk"] +[ext_resource type="Script" uid="uid://dbjd6oaknq055" path="res://addons/rokojori_action_library/Runtime/Shading/Materials/Transfers/FloatPropertyTransfer.cs" id="4_r0n2g"] +[ext_resource type="Script" uid="uid://cwp8to3eakdrm" path="res://addons/rokojori_action_library/Runtime/Shading/Materials/SubMaterialTransfer.cs" id="5_iuqjk"] [ext_resource type="Resource" uid="uid://dldbju3x0x2ow" path="res://addons/rokojori_action_library/Runtime/Shading/Properties/Property Library/Texture2D/Albedo Texture.tres" id="6_tcnva"] -[ext_resource type="Script" path="res://addons/rokojori_action_library/Runtime/Shading/Materials/Transfers/Texture2DPropertyTransfer.cs" id="7_pf381"] +[ext_resource type="Script" uid="uid://c55j0ppclm0k" path="res://addons/rokojori_action_library/Runtime/Shading/Materials/Transfers/Texture2DPropertyTransfer.cs" id="7_pf381"] [ext_resource type="Resource" uid="uid://cn7nxc0qw7pan" path="res://addons/rokojori_action_library/Runtime/Shading/Properties/Property Library/Vector3/UV1 Offset.tres" id="8_00usm"] -[ext_resource type="Script" path="res://addons/rokojori_action_library/Runtime/Shading/Materials/Transfers/Vector3PropertyTransfer.cs" id="9_sn3qs"] +[ext_resource type="Script" uid="uid://sm7n01wb77f1" path="res://addons/rokojori_action_library/Runtime/Shading/Materials/Transfers/Vector3PropertyTransfer.cs" id="9_sn3qs"] [ext_resource type="Resource" uid="uid://douianw6mx4p5" path="res://addons/rokojori_action_library/Runtime/Shading/Properties/Property Library/Vector3/UV1 Scale.tres" id="10_5fqi2"] [sub_resource type="Resource" id="Resource_7u5wc"] diff --git a/Runtime/Procedural/Baking/BakingMaterials/Normal/Normal From Standard.tres b/Runtime/Procedural/Baking/BakingMaterials/Normal/Normal From Standard.tres index cf8a475..fc42976 100644 --- a/Runtime/Procedural/Baking/BakingMaterials/Normal/Normal From Standard.tres +++ b/Runtime/Procedural/Baking/BakingMaterials/Normal/Normal From Standard.tres @@ -1,16 +1,16 @@ [gd_resource type="Resource" script_class="SubMaterialTransfer" load_steps=20 format=3 uid="uid://dgyihly620ymm"] [ext_resource type="Resource" uid="uid://cwbo0avyyq0t" path="res://addons/rokojori_action_library/Runtime/Shading/Properties/Property Library/Color/Albedo Color.tres" id="1_8s4ko"] -[ext_resource type="Script" path="res://addons/rokojori_action_library/Runtime/Shading/Materials/Transfers/ColorPropertyTransfer.cs" id="2_edgft"] +[ext_resource type="Script" uid="uid://c4kkc55ia5udl" path="res://addons/rokojori_action_library/Runtime/Shading/Materials/Transfers/ColorPropertyTransfer.cs" id="2_edgft"] [ext_resource type="Resource" uid="uid://bja8pltfjyt3x" path="res://addons/rokojori_action_library/Runtime/Shading/Properties/Property Library/Float/Alpha Scissor Threshold.tres" id="3_8a5am"] -[ext_resource type="Script" path="res://addons/rokojori_action_library/Runtime/Shading/Materials/Transfers/FloatPropertyTransfer.cs" id="4_wnydu"] -[ext_resource type="Script" path="res://addons/rokojori_action_library/Runtime/Shading/Materials/SubMaterialTransfer.cs" id="5_atmxl"] +[ext_resource type="Script" uid="uid://dbjd6oaknq055" path="res://addons/rokojori_action_library/Runtime/Shading/Materials/Transfers/FloatPropertyTransfer.cs" id="4_wnydu"] +[ext_resource type="Script" uid="uid://cwp8to3eakdrm" path="res://addons/rokojori_action_library/Runtime/Shading/Materials/SubMaterialTransfer.cs" id="5_atmxl"] [ext_resource type="Resource" uid="uid://c6actnjj8i8o5" path="res://addons/rokojori_action_library/Runtime/Shading/Properties/Property Library/Float/Normal Scale.tres" id="5_iyqth"] [ext_resource type="Resource" uid="uid://dldbju3x0x2ow" path="res://addons/rokojori_action_library/Runtime/Shading/Properties/Property Library/Texture2D/Albedo Texture.tres" id="6_l0r8p"] -[ext_resource type="Script" path="res://addons/rokojori_action_library/Runtime/Shading/Materials/Transfers/Texture2DPropertyTransfer.cs" id="7_l54r2"] +[ext_resource type="Script" uid="uid://c55j0ppclm0k" path="res://addons/rokojori_action_library/Runtime/Shading/Materials/Transfers/Texture2DPropertyTransfer.cs" id="7_l54r2"] [ext_resource type="Resource" uid="uid://bmt10lgwm8ckx" path="res://addons/rokojori_action_library/Runtime/Shading/Properties/Property Library/Texture2D/Normal Texture.tres" id="9_fmy5n"] [ext_resource type="Resource" uid="uid://cn7nxc0qw7pan" path="res://addons/rokojori_action_library/Runtime/Shading/Properties/Property Library/Vector3/UV1 Offset.tres" id="10_dw87i"] -[ext_resource type="Script" path="res://addons/rokojori_action_library/Runtime/Shading/Materials/Transfers/Vector3PropertyTransfer.cs" id="11_mpsiy"] +[ext_resource type="Script" uid="uid://sm7n01wb77f1" path="res://addons/rokojori_action_library/Runtime/Shading/Materials/Transfers/Vector3PropertyTransfer.cs" id="11_mpsiy"] [ext_resource type="Resource" uid="uid://douianw6mx4p5" path="res://addons/rokojori_action_library/Runtime/Shading/Properties/Property Library/Vector3/UV1 Scale.tres" id="12_xnkjm"] [sub_resource type="Resource" id="Resource_7u5wc"] diff --git a/Runtime/Procedural/Baking/BakingMaterials/ORM/ORM From Standard.tres b/Runtime/Procedural/Baking/BakingMaterials/ORM/ORM From Standard.tres index cc67549..fe5dcbf 100644 --- a/Runtime/Procedural/Baking/BakingMaterials/ORM/ORM From Standard.tres +++ b/Runtime/Procedural/Baking/BakingMaterials/ORM/ORM From Standard.tres @@ -1,22 +1,22 @@ [gd_resource type="Resource" script_class="SubMaterialTransfer" load_steps=36 format=3 uid="uid://kwk45f3p6ic4"] [ext_resource type="Resource" uid="uid://cwbo0avyyq0t" path="res://addons/rokojori_action_library/Runtime/Shading/Properties/Property Library/Color/Albedo Color.tres" id="1_b7tdx"] -[ext_resource type="Script" path="res://addons/rokojori_action_library/Runtime/Shading/Materials/Transfers/ColorPropertyTransfer.cs" id="2_vwrue"] +[ext_resource type="Script" uid="uid://c4kkc55ia5udl" path="res://addons/rokojori_action_library/Runtime/Shading/Materials/Transfers/ColorPropertyTransfer.cs" id="2_vwrue"] [ext_resource type="Resource" uid="uid://txmti1ghef1" path="res://addons/rokojori_action_library/Runtime/Shading/Properties/Property Library/TextureChannels/AO Texture Channel.tres" id="3_rkwtk"] [ext_resource type="Resource" uid="uid://bja8pltfjyt3x" path="res://addons/rokojori_action_library/Runtime/Shading/Properties/Property Library/Float/Alpha Scissor Threshold.tres" id="3_u377f"] -[ext_resource type="Script" path="res://addons/rokojori_action_library/Runtime/Shading/Materials/Transfers/FloatPropertyTransfer.cs" id="4_ed7jn"] -[ext_resource type="Script" path="res://addons/rokojori_action_library/Runtime/Shading/Materials/Transfers/TextureChannelToVector4Transfer.cs" id="4_ubpsj"] +[ext_resource type="Script" uid="uid://dbjd6oaknq055" path="res://addons/rokojori_action_library/Runtime/Shading/Materials/Transfers/FloatPropertyTransfer.cs" id="4_ed7jn"] +[ext_resource type="Script" uid="uid://hged4kw2qsv8" path="res://addons/rokojori_action_library/Runtime/Shading/Materials/Transfers/TextureChannelToVector4Transfer.cs" id="4_ubpsj"] [ext_resource type="Resource" uid="uid://dy4yrg7cbx84g" path="res://addons/rokojori_action_library/Runtime/Shading/Properties/Property Library/Float/Metallic.tres" id="5_hkpx0"] -[ext_resource type="Script" path="res://addons/rokojori_action_library/Runtime/Shading/Materials/SubMaterialTransfer.cs" id="6_7dw2g"] +[ext_resource type="Script" uid="uid://cwp8to3eakdrm" path="res://addons/rokojori_action_library/Runtime/Shading/Materials/SubMaterialTransfer.cs" id="6_7dw2g"] [ext_resource type="Resource" uid="uid://dn4k668h72myc" path="res://addons/rokojori_action_library/Runtime/Shading/Properties/Property Library/Float/Roughness.tres" id="6_8likw"] [ext_resource type="Resource" uid="uid://xoj2836knmix" path="res://addons/rokojori_action_library/Runtime/Shading/Properties/Property Library/TextureChannels/Metallic Texture Channel.tres" id="6_v4lhi"] [ext_resource type="Resource" uid="uid://dldbju3x0x2ow" path="res://addons/rokojori_action_library/Runtime/Shading/Properties/Property Library/Texture2D/Albedo Texture.tres" id="7_yqc2y"] [ext_resource type="Resource" uid="uid://d2x1ijwsv2urr" path="res://addons/rokojori_action_library/Runtime/Shading/Properties/Property Library/TextureChannels/Roughness Texture Channel.tres" id="8_gljck"] -[ext_resource type="Script" path="res://addons/rokojori_action_library/Runtime/Shading/Materials/Transfers/Texture2DPropertyTransfer.cs" id="8_iwxjk"] +[ext_resource type="Script" uid="uid://c55j0ppclm0k" path="res://addons/rokojori_action_library/Runtime/Shading/Materials/Transfers/Texture2DPropertyTransfer.cs" id="8_iwxjk"] [ext_resource type="Resource" uid="uid://cow6rei03x5bs" path="res://addons/rokojori_action_library/Runtime/Shading/Properties/Property Library/Texture2D/AO Texture.tres" id="10_8mfy8"] [ext_resource type="Resource" uid="uid://cn7nxc0qw7pan" path="res://addons/rokojori_action_library/Runtime/Shading/Properties/Property Library/Vector3/UV1 Offset.tres" id="10_y3nsy"] [ext_resource type="Resource" uid="uid://csiwpeupf761o" path="res://addons/rokojori_action_library/Runtime/Shading/Properties/Property Library/Texture2D/Metallic Texture.tres" id="11_i36mp"] -[ext_resource type="Script" path="res://addons/rokojori_action_library/Runtime/Shading/Materials/Transfers/Vector3PropertyTransfer.cs" id="11_on6ta"] +[ext_resource type="Script" uid="uid://sm7n01wb77f1" path="res://addons/rokojori_action_library/Runtime/Shading/Materials/Transfers/Vector3PropertyTransfer.cs" id="11_on6ta"] [ext_resource type="Resource" uid="uid://douianw6mx4p5" path="res://addons/rokojori_action_library/Runtime/Shading/Properties/Property Library/Vector3/UV1 Scale.tres" id="12_a173h"] [ext_resource type="Resource" uid="uid://bdjlvcpc13hl6" path="res://addons/rokojori_action_library/Runtime/Shading/Properties/Property Library/Texture2D/Roughness Texture.tres" id="12_u6pwu"] [ext_resource type="Resource" uid="uid://bprgfki4joe6x" path="res://addons/rokojori_action_library/Runtime/Shading/Properties/Property Library/Vector4/AO Texture Channel.tres" id="16_56ntu"] diff --git a/Runtime/Procedural/Baking/MultiBakeMode.cs b/Runtime/Procedural/Baking/MultiBakeMode.cs deleted file mode 100644 index a54ddd5..0000000 --- a/Runtime/Procedural/Baking/MultiBakeMode.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using Godot; -using System; - -using System.Threading.Tasks; - -namespace Rokojori -{ - public abstract class MultiBakeModeImplementation - { - public MultiBaker multiBaker; - public abstract MultiBaker.BakeMode GetBakeMode(); - public abstract int GetNumViews(); - public abstract void CreateBakes(); - public abstract void AssignMaterial( BakingMaterialMode mode, Texture2D texture ); - public abstract void CreateMaterial( bool preview ); - - } -} \ No newline at end of file diff --git a/Runtime/Procedural/Baking/MultiBakeModeCrossBraces.cs b/Runtime/Procedural/Baking/MultiBakeModeCrossBraces.cs deleted file mode 100644 index 0a1627c..0000000 --- a/Runtime/Procedural/Baking/MultiBakeModeCrossBraces.cs +++ /dev/null @@ -1,142 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using Godot; -using System; - -using System.Threading.Tasks; - -namespace Rokojori -{ - public class MultiBakeModeCrossBraces:MultiBakeModeBillboardBase - { - public override MultiBaker.BakeMode GetBakeMode() - { - return MultiBaker.BakeMode.Cross_Braces; - } - - public override int GetNumViews() - { - var views = multiBaker.crossAngles * 2; - - if ( multiBaker.crossBottom ) - { - views ++; - } - - if ( multiBaker.crossTop ) - { - views ++; - } - - return views; - } - - public override void CreateBakes() - { - var fov = multiBaker.GetCameraFOV(); - var distance = multiBaker.GetCameraDistance(); - var outputScale = multiBaker.GetOutputScale(); - - var _bakers = multiBaker.bakers; - var mb = multiBaker; - - - _bakers.ForEach( - b => - { - b.useCustomFOV = true; - b.customFOV = fov; - - b.useCustomDistance = true; - b.customDistance = distance; - - b.rotationMode = Baker.RotationMode.Yaw_Pitch; - } - ); - - var index = 0; - var mg = new MeshGeometry(); - - var numTextures = GetNumViews(); - var textureAlignment = TextureMerger.ComputeTextureAlignment( numTextures ); - - - if ( mb.crossTop ) - { - _bakers[ index ].yaw = 0 + mb.crossAngleOffset; - _bakers[ index ].pitch = 90f; - - var uv = TextureMerger.GetUVRectangle( textureAlignment, index, true ); - - var q = new MeshGeometry(); - q.AddQuad( _bakers[ index ].bakingRotation, outputScale, uv.min, uv.max ); - - mg.Add( q ); - - index ++; - } - - if ( mb.crossBottom ) - { - _bakers[ index ].yaw = 0 + mb.crossAngleOffset; - _bakers[ index ].pitch = -90f; - - var uv = TextureMerger.GetUVRectangle( textureAlignment, index, true ); - var q = new MeshGeometry(); - q.AddQuad( _bakers[ index ].bakingRotation, outputScale, uv.min, uv.max ); - - mg.Add( q ); - - index ++; - } - - - for ( int i = 0; i < mb.crossAngles; i++ ) - { - for ( int side = 0; side < 2; side ++ ) - { - var angle = ( 180f * i ) / (float) mb.crossAngles; - - _bakers[ index ].yaw = side == 0 ? angle : ( MathX.Repeat( angle + 180f, 360f ) ); - _bakers[ index ].yaw += mb.crossAngleOffset; - _bakers[ index ].pitch = 0; - - var uv = TextureMerger.GetUVRectangle( textureAlignment, index, true ); - - var q = new MeshGeometry(); - q.AddQuad( _bakers[ index ].bakingRotation, outputScale, uv.min, uv.max ); - - if ( mb.crossBraces > 1 ) - { - var normal = Vector3.Forward * _bakers[ index ].bakingRotation; - var startOffset = normal * mb.crossSpreadDistance * 0.5f; - var endOffset = -startOffset; - - for ( int brace = 0; brace < mb.crossBraces; brace ++ ) - { - var bq = q.Clone(); - var lerpAmount = (float)brace / (float)( mb.crossBraces - 1 ); - var offset = startOffset.Lerp( endOffset, lerpAmount ); - bq.ApplyTranslation( offset ); - mg.Add( bq ); - } - } - else - { - mg.Add( q ); - } - - - index ++; - - } - - - - } - - - mb.X_outputMesh.Mesh = mg.GenerateMesh(); - } - } -} \ No newline at end of file diff --git a/Runtime/Procedural/Baking/MultiBakeModeCylinder.cs b/Runtime/Procedural/Baking/MultiBakeModeCylinder.cs deleted file mode 100644 index d6247a9..0000000 --- a/Runtime/Procedural/Baking/MultiBakeModeCylinder.cs +++ /dev/null @@ -1,155 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using Godot; -using System; - -using System.Threading.Tasks; - -namespace Rokojori -{ - public class MultiBakeModeCylinder:MultiBakeModeBillboardBase - { - public override MultiBaker.BakeMode GetBakeMode() - { - return MultiBaker.BakeMode.Cylinder; - } - - public override int GetNumViews() - { - var cylinderViews = multiBaker.cylinderSides; - - if ( multiBaker.cylinderBottom ) - { - cylinderViews ++; - } - - if ( multiBaker.cylinderTop ) - { - cylinderViews ++; - } - - return cylinderViews; - } - - public override void CreateBakes() - { - var fov = multiBaker.GetCameraFOV(); - var distance = multiBaker.GetCameraDistance(); - var outputScale = multiBaker.GetOutputScale(); - - var _bakers = multiBaker.bakers; - var mb = multiBaker; - - - _bakers.ForEach( - b => - { - b.useCustomFOV = true; - b.customFOV = fov; - - b.useCustomDistance = true; - b.customDistance = distance; - - b.rotationMode = Baker.RotationMode.Yaw_Pitch; - } - ); - - var index = 0; - var mg = new MeshGeometry(); - - var numTextures = GetNumViews(); - var textureAlignment = TextureMerger.ComputeTextureAlignment( numTextures ); - - - if ( mb.cylinderTop ) - { - _bakers[ index ].yaw = 0; - _bakers[ index ].pitch = 90f; - - var uv = TextureMerger.GetUVRectangle( textureAlignment, index, true ); - - var q = new MeshGeometry(); - q.AddQuad( _bakers[ index ].bakingRotation, outputScale, uv.min, uv.max ); - - - if ( mb.cylinderTopOffset != 0 ) - { - var topOffset = Vector3.Up * mb.cylinderTopOffset * outputScale * 0.5f; - q.ApplyTranslation( topOffset, -4 ); - } - - mg.Add( q ); - - if ( mb.createBackFacesForTop ) - { - q.FlipNormalDirection(); - mg.Add( q ); - } - - index ++; - } - - if ( mb.cylinderBottom ) - { - _bakers[ index ].yaw = 0; - _bakers[ index ].pitch = -90f; - - var uv = TextureMerger.GetUVRectangle( textureAlignment, index, true ); - - var q = new MeshGeometry(); - q.AddQuad( _bakers[ index ].bakingRotation, outputScale, uv.min, uv.max ); - - if ( mb.cylinderBottomOffset != 0 ) - { - var bottomOffset = Vector3.Down * mb.cylinderBottomOffset * outputScale * 0.5f; - q.ApplyTranslation( bottomOffset, -4 ); - } - - mg.Add( q ); - - if ( mb.createBackFacesForBottom ) - { - q.FlipNormalDirection(); - mg.Add( q ); - } - - index ++; - } - - - for ( int i = 0; i < mb.cylinderSides; i++ ) - { - var angle = ( 360f * i ) / (float) mb.cylinderSides; - _bakers[ index ].yaw = angle; - _bakers[ index ].pitch = 0; - - var uv = TextureMerger.GetUVRectangle( textureAlignment, index, true ); - - - var q = new MeshGeometry(); - q.AddQuad( _bakers[ index ].bakingRotation, outputScale, uv.min, uv.max ); - - if ( mb.cylinderSideOffset != 0 ) - { - var sideOffset = Vector3.Forward *_bakers[ index ].bakingRotation * mb.cylinderSideOffset * outputScale * -0.5f; - q.ApplyTranslation( sideOffset, -4 ); - } - - mg.Add( q ); - - if ( mb.createBackFacesForSides ) - { - q.FlipNormalDirection(); - mg.Add( q ); - } - - index++; - - } - - - mb.X_outputMesh.Mesh = mg.GenerateMesh(); - - } - } -} \ No newline at end of file diff --git a/Runtime/Procedural/Baking/MultiBakeModeSpherical.cs b/Runtime/Procedural/Baking/MultiBakeModeSpherical.cs deleted file mode 100644 index 5d5bf6d..0000000 --- a/Runtime/Procedural/Baking/MultiBakeModeSpherical.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using Godot; -using System; - -using System.Threading.Tasks; - -namespace Rokojori -{ - public class MultiBakeModeSpherical:MultiBakeModeImplementation - { - public readonly string sphericalShader = "res://addons/rokojori_action_library/External/Imposter/materials/shaders/ImpostorShader.gdshader"; - - public override MultiBaker.BakeMode GetBakeMode() - { - return MultiBaker.BakeMode.Spherical; - } - - public override int GetNumViews() - { - var views = multiBaker.sphericalSides * multiBaker.sphericalRows; - - return views; - } - - public override void CreateMaterial( bool preview ) - { - - } - - public override void AssignMaterial( BakingMaterialMode mode, Texture2D texture ) - { - - } - - public override void CreateBakes() - { - - } - - } -} \ No newline at end of file diff --git a/Runtime/Procedural/Baking/MultiBaker/CrossBraces_Baker.cs b/Runtime/Procedural/Baking/MultiBaker/CrossBraces_Baker.cs new file mode 100644 index 0000000..17f842e --- /dev/null +++ b/Runtime/Procedural/Baking/MultiBaker/CrossBraces_Baker.cs @@ -0,0 +1,150 @@ +using System.Collections; +using System.Collections.Generic; +using Godot; +using System; + +using System.Threading.Tasks; + +namespace Rokojori +{ + [Tool][GlobalClass] + public partial class CrossBraces_Baker:_XX_MultiBakeModeBillboardBase + { + [Export] + public int crossAngles = 2; + [Export] + public int crossBraces = 3; + [Export] + public float crossSpreadDistance = 1; + [Export] + public float crossAngleOffset = 0; + + [Export] + public bool crossTop = false; + + [Export] + public bool crossBottom = false; + + public override int GetNumViews() + { + var views = crossAngles * 2; + + if ( crossBottom ) + { + views ++; + } + + if ( crossTop ) + { + views ++; + } + + return views; + } + + public override void CreateBakers() + { + var fov = multiBaker.GetCameraFOV(); + var distance = multiBaker.GetCameraDistance(); + var outputScale = multiBaker.GetOutputScale(); + + RJLog.Log( "outputScale", outputScale ); + var _bakers = multiBaker.bakers; + var mb = multiBaker; + + + _bakers.ForEach( + bk => + { + var vs = bk.viewSettings; + vs.fovDistance = Manual_BakingFDSettings.Create( fov, distance ); + vs.rotationMode = BakingViewSettings.RotationMode.Yaw_Pitch; + } + ); + + var index = 0; + var mg = new MeshGeometry(); + + var numTextures = GetNumViews(); + var textureAlignment = TextureMerger.ComputeTextureAlignment( numTextures ); + + + if ( crossTop ) + { + _bakers[ index ].viewSettings.yaw = 0 + crossAngleOffset; + _bakers[ index ].viewSettings.pitch = 90f; + + var uv = TextureMerger.GetUVRectangle( textureAlignment, index, true ); + + var q = new MeshGeometry(); + q.AddQuad( _bakers[ index ].viewSettings.bakingRotation, outputScale, uv.min, uv.max ); + + mg.Add( q ); + + index ++; + } + + if ( crossBottom ) + { + _bakers[ index ].viewSettings.yaw = 0 + crossAngleOffset; + _bakers[ index ].viewSettings.pitch = -90f; + + var uv = TextureMerger.GetUVRectangle( textureAlignment, index, true ); + var q = new MeshGeometry(); + q.AddQuad( _bakers[ index ].viewSettings.bakingRotation, outputScale, uv.min, uv.max ); + + mg.Add( q ); + + index ++; + } + + + for ( int i = 0; i < crossAngles; i++ ) + { + for ( int side = 0; side < 2; side ++ ) + { + var angle = ( 180f * i ) / (float) crossAngles; + + _bakers[ index ].viewSettings.yaw = side == 0 ? angle : ( MathX.Repeat( angle + 180f, 360f ) ); + _bakers[ index ].viewSettings.yaw += crossAngleOffset; + _bakers[ index ].viewSettings.pitch = 0; + + var uv = TextureMerger.GetUVRectangle( textureAlignment, index, true ); + + var q = new MeshGeometry(); + q.AddQuad( _bakers[ index ].viewSettings.bakingRotation, outputScale, uv.min, uv.max ); + + if ( crossBraces > 1 ) + { + var normal = Vector3.Forward * _bakers[ index ].viewSettings.bakingRotation; + var startOffset = normal * crossSpreadDistance * 0.5f; + var endOffset = -startOffset; + + for ( int brace = 0; brace < crossBraces; brace ++ ) + { + var bq = q.Clone(); + var lerpAmount = (float)brace / (float)( crossBraces - 1 ); + var offset = startOffset.Lerp( endOffset, lerpAmount ); + bq.ApplyTranslation( offset ); + mg.Add( bq ); + } + } + else + { + mg.Add( q ); + } + + + index ++; + + } + + + + } + + + mb.X_outputMesh.Mesh = mg.GenerateMesh(); + } + } +} \ No newline at end of file diff --git a/Runtime/Procedural/Baking/MultiBakeModeCrossBraces.cs.uid b/Runtime/Procedural/Baking/MultiBaker/CrossBraces_Baker.cs.uid similarity index 100% rename from Runtime/Procedural/Baking/MultiBakeModeCrossBraces.cs.uid rename to Runtime/Procedural/Baking/MultiBaker/CrossBraces_Baker.cs.uid diff --git a/Runtime/Procedural/Baking/MultiBaker/Cylinder_Baker.cs b/Runtime/Procedural/Baking/MultiBaker/Cylinder_Baker.cs new file mode 100644 index 0000000..0eaa76b --- /dev/null +++ b/Runtime/Procedural/Baking/MultiBaker/Cylinder_Baker.cs @@ -0,0 +1,167 @@ +using System.Collections; +using System.Collections.Generic; +using Godot; +using System; + +using System.Threading.Tasks; + +namespace Rokojori +{ + [Tool][GlobalClass] + public partial class Cylinder_Baker:_XX_MultiBakeModeBillboardBase + { + [Export] + public int cylinderSides = 4; + [Export] + public bool createBackFacesForSides = false; + [Export] + public bool cylinderTop = false; + [Export] + public bool createBackFacesForTop = false; + [Export] + public bool cylinderBottom = false; + [Export] + public bool createBackFacesForBottom = false; + [Export( PropertyHint.Range, "-1,1" )] + public float cylinderSideOffset = 0; + [Export( PropertyHint.Range, "-1,1" )] + public float cylinderTopOffset = 0f; + [Export( PropertyHint.Range, "-1,1" )] + public float cylinderBottomOffset = 0f; + + + public override int GetNumViews() + { + var cylinderViews = cylinderSides; + + if ( cylinderBottom ) + { + cylinderViews ++; + } + + if ( cylinderTop ) + { + cylinderViews ++; + } + + return cylinderViews; + } + + public override void CreateBakers() + { + var fov = multiBaker.GetCameraFOV(); + var distance = multiBaker.GetCameraDistance(); + var outputScale = multiBaker.GetOutputScale(); + + var _bakers = multiBaker.bakers; + var mb = multiBaker; + + + _bakers.ForEach( + bk => + { + var vs = bk.viewSettings; + vs.fovDistance = Manual_BakingFDSettings.Create( fov, distance ); + vs.rotationMode = BakingViewSettings.RotationMode.Yaw_Pitch; + } + ); + + var index = 0; + var mg = new MeshGeometry(); + + var numTextures = GetNumViews(); + var textureAlignment = TextureMerger.ComputeTextureAlignment( numTextures ); + + + if ( cylinderTop ) + { + _bakers[ index ].viewSettings.yaw = 0; + _bakers[ index ].viewSettings.pitch = 90f; + + var uv = TextureMerger.GetUVRectangle( textureAlignment, index, true ); + + var q = new MeshGeometry(); + q.AddQuad( _bakers[ index ].viewSettings.bakingRotation, outputScale, uv.min, uv.max ); + + + if ( cylinderTopOffset != 0 ) + { + var topOffset = Vector3.Up * cylinderTopOffset * outputScale * 0.5f; + q.ApplyTranslation( topOffset, -4 ); + } + + mg.Add( q ); + + if ( createBackFacesForTop ) + { + q.FlipNormalDirection(); + mg.Add( q ); + } + + index ++; + } + + if ( cylinderBottom ) + { + _bakers[ index ].viewSettings.yaw = 0; + _bakers[ index ].viewSettings.pitch = -90f; + + var uv = TextureMerger.GetUVRectangle( textureAlignment, index, true ); + + var q = new MeshGeometry(); + q.AddQuad( _bakers[ index ].viewSettings.bakingRotation, outputScale, uv.min, uv.max ); + + if ( cylinderBottomOffset != 0 ) + { + var bottomOffset = Vector3.Down * cylinderBottomOffset * outputScale * 0.5f; + q.ApplyTranslation( bottomOffset, -4 ); + } + + mg.Add( q ); + + if ( createBackFacesForBottom ) + { + q.FlipNormalDirection(); + mg.Add( q ); + } + + index ++; + } + + + for ( int i = 0; i < cylinderSides; i++ ) + { + var angle = ( 360f * i ) / (float) cylinderSides; + _bakers[ index ].viewSettings.yaw = angle; + _bakers[ index ].viewSettings.pitch = 0; + + var uv = TextureMerger.GetUVRectangle( textureAlignment, index, true ); + + + var q = new MeshGeometry(); + q.AddQuad( _bakers[ index ].viewSettings.bakingRotation, outputScale, uv.min, uv.max ); + + if ( cylinderSideOffset != 0 ) + { + var sideOffset = Vector3.Forward *_bakers[ index ].viewSettings.bakingRotation * cylinderSideOffset * outputScale * -0.5f; + q.ApplyTranslation( sideOffset, -4 ); + } + + mg.Add( q ); + + if ( createBackFacesForSides ) + { + q.FlipNormalDirection(); + mg.Add( q ); + } + + index++; + + } + + + mb.X_outputMesh.Mesh = mg.GenerateMesh(); + + } + } +} \ No newline at end of file diff --git a/Runtime/Procedural/Baking/MultiBakeModeCylinder.cs.uid b/Runtime/Procedural/Baking/MultiBaker/Cylinder_Baker.cs.uid similarity index 100% rename from Runtime/Procedural/Baking/MultiBakeModeCylinder.cs.uid rename to Runtime/Procedural/Baking/MultiBaker/Cylinder_Baker.cs.uid diff --git a/Runtime/Procedural/Baking/MultiBaker.cs b/Runtime/Procedural/Baking/MultiBaker/MultiBaker.cs similarity index 71% rename from Runtime/Procedural/Baking/MultiBaker.cs rename to Runtime/Procedural/Baking/MultiBaker/MultiBaker.cs index 5de3fba..1c7e7d7 100644 --- a/Runtime/Procedural/Baking/MultiBaker.cs +++ b/Runtime/Procedural/Baking/MultiBaker/MultiBaker.cs @@ -11,11 +11,14 @@ namespace Rokojori [GlobalClass] public partial class MultiBaker:Node { - [ExportToolButton( "Initialize")] - public Callable InitializeButton => Callable.From( () => Initialize() ); - [ExportToolButton( "Bake")] - public Callable BakeButton => Callable.From( () => Bake() ); + public Callable BakeButton => Callable.From( () => StartBaking() ); + + [Export] + public bool cleanUpAfterBaking = true; + + [Export] + public _XX_MultiBakeMode bakeMode; public enum MaterialMode { @@ -27,6 +30,8 @@ namespace Rokojori [Export] public MaterialMode materialMode; + [Export] + public int dilationRadius = 64; [ExportGroup("Material/Full Seperated")] [Export] public bool mmfs_Normals = true; @@ -36,89 +41,44 @@ namespace Rokojori public bool mmfs_ORM = true; - public enum BakeMode - { - Cylinder, - Cross_Braces, - Octahedral, - Spherical, - Cloud - } - - [ExportGroup("BakeMode")] - - [Export] - public BakeMode bakeMode; - - - - [ExportGroup("BakeMode/Cylinder")] - [Export] - public int cylinderSides = 4; - [Export] - public bool createBackFacesForSides = false; - [Export] - public bool cylinderTop = false; - [Export] - public bool createBackFacesForTop = false; - [Export] - public bool cylinderBottom = false; - [Export] - public bool createBackFacesForBottom = false; - [Export( PropertyHint.Range, "-1,1" )] - public float cylinderSideOffset = 0; - [Export( PropertyHint.Range, "-1,1" )] - public float cylinderTopOffset = 0f; - [Export( PropertyHint.Range, "-1,1" )] - public float cylinderBottomOffset = 0f; - - [ExportGroup("BakeMode/Cross Braces")] - [Export] - public int crossAngles = 2; - [Export] - public int crossBraces = 3; - [Export] - public float crossSpreadDistance = 1; - [Export] - public float crossAngleOffset = 0; - - [Export] - public bool crossTop = false; - - [Export] - public bool crossBottom = false; - - - [ExportGroup("BakeMode/Octahedral")] - [Export] - public int octahedralSides = 4; - [Export] - public bool octahedralFullSphere = false; - - [ExportGroup("BakeMode/Spherical")] - [Export] - public int sphericalSides = 4; - [Export] - public int sphericalRows = 3; - [Export] - public float minPitch = -30; - [Export] - public float maxPitch = 30; - [ExportGroup( "Object")] [Export] - public Node3D sourceTarget; + public Node3D target; [Export] - public bool autoCenter = false; + public bool autoTargetPivot = false; [Export] - public Vector3 sourceOffset; - + public Vector3 targetPivot; + [ExportGroup( "Camera")] + + [Export] + public BakingViewSettings viewSettings; + + public float cameraZoom + { + get + { + if ( viewSettings.fovDistance is AutoDistance_BakingFDSettings ad ) + { + return ad.zoom; + } + + if ( viewSettings.fovDistance is AutoDistance_BakingFDSettings afd ) + { + return afd.zoom; + } + + + return 1; + } + } + + /* [Export] public Baker.CameraDistanceDetectionType distanceDetectionType = Baker.CameraDistanceDetectionType.Automatic_Distance_Detection; @@ -136,7 +96,7 @@ namespace Rokojori public float fovPlacingDistance = 200; [Export] public float customFOV = 75; - + */ [ExportGroup( "Output")] @@ -199,25 +159,82 @@ namespace Rokojori public Texture2D X_bakedTextureDepth; - bool _initialized = false; - bool _baking = false; - + [Export] + public bool X_baking = false; + + public void StartBaking() + { + Initialize(); + Bake(); + } + + public void Initialize() + { + this.LogInfo( "Initializing" ); + + Nodes.RemoveAndDeleteChildren( this ); + + _bakers = null; + + X_bakingViewport = this.CreateChild( "Multi Baker Viewport" ); + + X_bakingViewport.Size = (Vector2I) outputTextureSize; + X_bakingViewport.OwnWorld3D = true; + X_bakingViewport.TransparentBg = true; + + X_worldEnvironment = X_bakingViewport.CreateChild( "Multi Baker Environment" ); + X_worldEnvironment.Environment = new Godot.Environment(); + X_worldEnvironment.Environment.AmbientLightSource = Godot.Environment.AmbientSource.Color; + X_worldEnvironment.Environment.AmbientLightColor = HSLColor.white; + + X_bakingTargetContainer = X_bakingViewport.CreateChild( "Target Container" ); + + X_views = X_bakingViewport.CreateChild( "Views" ); + + X_textureMerger = this.CreateChild( "Texture Merger" ); + X_textureMerger.multiBaker = this; + X_textureMerger.dilationRadius = dilationRadius; + X_textureMerger.Initialize(); + + X_outputMesh = this.CreateChild( "Output Mesh" ); + + X_texturePreview = this.CreateChild( "Texture Preview" ); + X_texturePreview.Mesh = new QuadMesh(); + X_texturePreview.Scale = Vector3.One * 100; + + var pm = new StandardMaterial3D(); + pm.Transparency = BaseMaterial3D.TransparencyEnum.AlphaScissor; + pm.ResourceLocalToScene = true; + + var vt = new ViewportTexture(); + vt.ViewportPath = X_textureMerger.X_textureMergerViewport.GetPath(); + pm.AlbedoTexture = vt; + + X_setBakingMaterials = this.CreateChild( "Set Baking Materials" ); + + Materials.Set( X_texturePreview, pm ); + + } + + public async Task Bake() { - if ( _baking ) + if ( X_baking ) { return; } - _baking = true; + X_baking = true; + try { + bakeMode.multiBaker = this; this.LogInfo( "Started baking" ); Nodes.RemoveAndDeleteChildren( X_bakingTargetContainer ); - sourceTarget.DeepCopyTo( X_bakingTargetContainer ); + target.DeepCopyTo( X_bakingTargetContainer ); if ( _bakers == null || _bakers.Count != GetNumViews() ) { @@ -225,20 +242,20 @@ namespace Rokojori await this.RequestNextFrame(); } - SetupViews(); this.LogInfo( "Views set up" ); await this.RequestNextFrame(); - X_setBakingMaterials.SetTarget( X_bakingTargetContainer ); + X_setBakingMaterials.SetTarget( X_bakingTargetContainer ); var bakingMaterialModes = new List(); var preview_QuickMaterial = MaterialMode.Simple_Prebaked == materialMode; - GetBakeModeImplementation().CreateMaterial( preview_QuickMaterial ); + bakeMode.CreateMaterial( preview_QuickMaterial ); + if ( preview_QuickMaterial ) { @@ -265,24 +282,25 @@ namespace Rokojori } this.LogInfo( "Prepared baking modes" ); - X_textureMerger.textureSize = outputTextureSize; X_textureMerger.Initialize(); X_textureMerger.CreateLayout(); this.LogInfo( "Prepared texture merger" ); - + // var fovDistance = viewSettings.fovDistance.ComputeFOVDistance( var objectDistance = GetCameraDistance(); + this.LogInfo( "Set Camera Distance", objectDistance ); for ( int i = 0; i < bakingMaterialModes.Count; i++ ) { this.LogInfo( "Baking mode:", bakingMaterialModes[ i ] ); - X_setBakingMaterials.mode = bakingMaterialModes[ i ]; X_setBakingMaterials.ApplyBakingMaterials( objectDistance, _targetBoundingSphere.radius ); this.LogInfo( "Materials changed:", bakingMaterialModes[ i ] ); + _bakers.ForEach( b => b.Bake() ); + await this.RequestNextFrame(); Texture2D texture = X_textureMerger.X_textureMergerViewport.GetTexture(); @@ -294,7 +312,7 @@ namespace Rokojori await this.RequestNextFrame(); this.LogInfo( "Assigning Texture", bakingMaterialModes[ i ] ); - GetBakeModeImplementation().AssignMaterial( bakingMaterialModes[ i ], texture ); + bakeMode.AssignMaterial( bakingMaterialModes[ i ], texture ); this.LogInfo( "Baking done:", bakingMaterialModes[ i ] ); @@ -304,7 +322,17 @@ namespace Rokojori await this.RequestNextFrame(); - // this.LogInfo( "Baking done" ); + X_outputMesh.GlobalPosition = target.GlobalPosition - targetPivot; + + X_outputMesh.Scale = Vector3.One * cameraZoom; + + if ( cleanUpAfterBaking ) + { + this.LogInfo( "Cleaning Up" ); + CleanUp(); + } + + this.LogInfo( "All Baking done" ); } catch ( System.Exception e ) @@ -313,31 +341,16 @@ namespace Rokojori this.LogError( e ); } - _baking = false; + X_baking = false; return; } - public List GetAllViewports() { return Nodes.AllIn( X_views, null, false ); } - List _bakeModeImplementations = new List() - { - new MultiBakeModeCylinder(), - new MultiBakeModeCrossBraces(), - new MultiBakeModeOctahedral() - }; - - public MultiBakeModeImplementation GetBakeModeImplementation() - { - var bm = _bakeModeImplementations.Find( bm => bm.GetBakeMode() == bakeMode ); - bm.multiBaker = this; - return bm; - } - public void CacheTexture( BakingMaterialMode mode, Texture2D texture ) { if ( BakingMaterialMode.Albedo == mode ) @@ -357,62 +370,49 @@ namespace Rokojori X_bakedTextureDepth = texture; } - } + } + + public void CleanUp() + { + var mesh = X_outputMesh; + mesh.Reparent( GetParent(), true ); + + X_bakingViewport = null; + + X_bakingTargetContainer = null; + + X_views = null; + + X_worldEnvironment = null; + + X_outputMesh = null; + + X_textureMerger = null; + + X_setBakingMaterials = null; + + X_texturePreview = null; + + X_depthMapper = null; + + X_bakedTextureAlbedo = null; + + X_bakedTextureNormal = null; + + X_bakedTextureORM = null; + + X_bakedTextureDepth = null; - public void Initialize() - { - this.LogInfo( "Initializing" ); - Nodes.RemoveAndDeleteChildren( this ); - _bakers = null; - - X_bakingViewport = this.CreateChild( "Multi Baker Viewport" ); - - X_bakingViewport.Size = (Vector2I) outputTextureSize; - X_bakingViewport.OwnWorld3D = true; - X_bakingViewport.TransparentBg = true; - - X_worldEnvironment = X_bakingViewport.CreateChild( "Multi Baker Environment" ); - X_worldEnvironment.Environment = new Godot.Environment(); - X_worldEnvironment.Environment.AmbientLightSource = Godot.Environment.AmbientSource.Color; - X_worldEnvironment.Environment.AmbientLightColor = HSLColor.white; - - X_bakingTargetContainer = X_bakingViewport.CreateChild( "Target Container" ); - - X_views = X_bakingViewport.CreateChild( "Views" ); - - // X_dilateTexture = this.CreateChild( "Dilate Texture" ); - - X_textureMerger = this.CreateChild( "Texture Merger" ); - X_textureMerger.multiBaker = this; - X_textureMerger.Initialize(); - - X_outputMesh = this.CreateChild( "Output Mesh" ); - - X_texturePreview = this.CreateChild( "Texture Preview" ); - X_texturePreview.Mesh = new QuadMesh(); - X_texturePreview.Scale = Vector3.One * 100; - - var pm = new StandardMaterial3D(); - pm.Transparency = BaseMaterial3D.TransparencyEnum.AlphaScissor; - pm.ResourceLocalToScene = true; - - var vt = new ViewportTexture(); - vt.ViewportPath = X_textureMerger.X_textureMergerViewport.GetPath(); - pm.AlbedoTexture = vt; - - X_setBakingMaterials = this.CreateChild( "Set Baking Materials" ); - - Materials.Set( X_texturePreview, pm ); - - _initialized = true; - + mesh.Reparent( this, true ); } + + public int GetNumViews() { - return GetBakeModeImplementation().GetNumViews(); + return bakeMode.GetNumViews(); } List _bakers; @@ -445,7 +445,7 @@ namespace Rokojori baker.viewport = bakingView; - baker.update = true; + _bakers.Add( baker ); } @@ -466,6 +466,8 @@ namespace Rokojori } _targetBoundingSphere = null; + _targetBoundingBox = null; + ComputeBoundingSphere(); if ( _targetBoundingSphere == null ) @@ -474,22 +476,16 @@ namespace Rokojori return; } - var bmi = GetBakeModeImplementation(); + ComputeCameraViewSettings(); + + bakeMode.CreateBakers(); + - if ( bmi == null ) - { - return; - } - bmi.CreateBakes(); - - /*if ( X_bakedTextureAlbedo != null ) - { - bmi.AssignMaterial( BakingMaterialMode.Albedo, X_bakedTextureAlbedo ); - }*/ } Sphere _targetBoundingSphere; + Box3 _targetBoundingBox; Sphere targetBoundingSphere { @@ -525,45 +521,62 @@ namespace Rokojori firstChild.Visible = true; } - var worldBounds = X_bakingTargetContainer.GetWorldBounds(); + _targetBoundingBox = X_bakingTargetContainer.GetWorldBounds(); + if ( autoTargetPivot ) + { + Box3 box = target.GetWorldBounds(); + targetPivot = new Vector3( 0, -box.center.Y, 0 ); + } - _targetBoundingSphere = Sphere.ContainingBox( worldBounds ); + _targetBoundingSphere = Sphere.ContainingBox( _targetBoundingBox ); } + float _cameraFOV = 0; + float _cameraDistance = 0; + float _outputScale = 1; + + void ComputeCameraViewSettings() + { + var fovDistance = viewSettings.fovDistance; + var size = targetBoundingSphere.radius; + + var computeScale = false; + + if ( fovDistance is AutoDistance_BakingFDSettings ad ) + { + size = ad.sizeEstimation == _XX_BakingFDSettings.SizeEstimationType.Bounding_Sphere ? + targetBoundingSphere.radius : ( _targetBoundingBox.size.Y / 2f ); + computeScale = true; + } + + if ( fovDistance is AutoFOVDistance_BakingFDSettings afd ) + { + size = afd.sizeEstimation == _XX_BakingFDSettings.SizeEstimationType.Bounding_Sphere ? + targetBoundingSphere.radius : ( _targetBoundingBox.size.Y / 2f ); + computeScale = true; + } + + var fd = fovDistance.ComputeFOVDistance( size / 2f ); + + _cameraFOV = fd.X; + _cameraDistance = fd.Y; + _outputScale = computeScale ? Cameras.ComputeCameraFittingScale( _cameraFOV, _cameraDistance ) : 1; + } + public float GetCameraFOV() { - if ( Baker.CameraFOVMode.Custom_Fov == fovMode ) - { - return customFOV; - } - - if ( Baker.CameraFOVMode.Keep_Fov == fovMode ) - { - return originalFOV; - } - - return Cameras.ComputeFOVForBillboard( originalFOV, targetBoundingSphere.radius, fovPlacingDistance ); + return _cameraFOV; } public float GetCameraDistance() { - if ( Baker.CameraDistanceDetectionType.Custom_Distance == distanceDetectionType ) - { - return customDistance; - } - - var fov = GetCameraFOV(); - - return Cameras.ComputeCameraFrameFittingDistance( fov, targetBoundingSphere.radius / cameraZoom ); + return _cameraDistance; } public float GetOutputScale() { - var fov = GetCameraFOV(); - var distance = GetCameraDistance(); - - return Cameras.ComputeCameraFittingScale( fov, distance ); + return _outputScale; } } } \ No newline at end of file diff --git a/Runtime/Procedural/Baking/MultiBaker.cs.uid b/Runtime/Procedural/Baking/MultiBaker/MultiBaker.cs.uid similarity index 100% rename from Runtime/Procedural/Baking/MultiBaker.cs.uid rename to Runtime/Procedural/Baking/MultiBaker/MultiBaker.cs.uid diff --git a/Runtime/Procedural/Baking/MultiBaker/MultiBakerCameraTools.cs b/Runtime/Procedural/Baking/MultiBaker/MultiBakerCameraTools.cs new file mode 100644 index 0000000..384bb3a --- /dev/null +++ b/Runtime/Procedural/Baking/MultiBaker/MultiBakerCameraTools.cs @@ -0,0 +1,14 @@ +using System.Collections; +using System.Collections.Generic; +using Godot; +using System; + +using System.Threading.Tasks; + +namespace Rokojori +{ + public class MultiBakerCameraTools + { + + } +} \ No newline at end of file diff --git a/Runtime/Procedural/Baking/OctahedralMapping.cs b/Runtime/Procedural/Baking/MultiBaker/OctahedralMapping.cs similarity index 100% rename from Runtime/Procedural/Baking/OctahedralMapping.cs rename to Runtime/Procedural/Baking/MultiBaker/OctahedralMapping.cs diff --git a/Runtime/Procedural/Baking/OctahedralMapping.cs.uid b/Runtime/Procedural/Baking/MultiBaker/OctahedralMapping.cs.uid similarity index 100% rename from Runtime/Procedural/Baking/OctahedralMapping.cs.uid rename to Runtime/Procedural/Baking/MultiBaker/OctahedralMapping.cs.uid diff --git a/Runtime/Procedural/Baking/MultiBakeModeOctahedral.cs b/Runtime/Procedural/Baking/MultiBaker/Octahedral_Baker.cs similarity index 66% rename from Runtime/Procedural/Baking/MultiBakeModeOctahedral.cs rename to Runtime/Procedural/Baking/MultiBaker/Octahedral_Baker.cs index c992ec2..cd90ca9 100644 --- a/Runtime/Procedural/Baking/MultiBakeModeOctahedral.cs +++ b/Runtime/Procedural/Baking/MultiBaker/Octahedral_Baker.cs @@ -7,18 +7,22 @@ using System.Threading.Tasks; namespace Rokojori { - public class MultiBakeModeOctahedral:MultiBakeModeImplementation + [Tool] + [GlobalClass] + public partial class Octahedral_Baker:_XX_MultiBakeMode { - public readonly string octahedralShader = "res://addons/rokojori_action_library/External/Imposter/materials/shaders/ImpostorShader.gdshader"; + public static readonly string octahedralShader = "res://addons/rokojori_action_library/External/Imposter/materials/shaders/ImpostorShader.gdshader"; - public override MultiBaker.BakeMode GetBakeMode() - { - return MultiBaker.BakeMode.Octahedral; - } + + [Export] + public int octahedralSides = 4; + [Export] + public bool octahedralFullSphere = false; + public override int GetNumViews() { - var views = multiBaker.octahedralSides * multiBaker.octahedralSides; + var views = octahedralSides * octahedralSides; return views; } @@ -29,8 +33,8 @@ namespace Rokojori var material = new ShaderMaterial(); material.Shader = ResourceLoader.Load( octahedralShader ) as Shader; - material.SetShaderParameter( "isFullSphere", mb.octahedralFullSphere ); - material.SetShaderParameter( "imposterFrames", Vector2.One * mb.octahedralSides ); + material.SetShaderParameter( "isFullSphere", octahedralFullSphere ); + material.SetShaderParameter( "imposterFrames", Vector2.One * octahedralSides ); Materials.Set( mb.X_outputMesh, material ); } @@ -65,11 +69,10 @@ namespace Rokojori material.SetShaderParameter( "imposterTextureDepth", texture ); } - } - public override void CreateBakes() + public override void CreateBakers() { var fov = multiBaker.GetCameraFOV(); var distance = multiBaker.GetCameraDistance(); @@ -78,17 +81,15 @@ namespace Rokojori var _bakers = multiBaker.bakers; var mb = multiBaker; + _bakers.ForEach( - b => + bk => { - b.useCustomFOV = true; - b.customFOV = fov; + var vs = bk.viewSettings; + vs.fovDistance = Manual_BakingFDSettings.Create( fov, distance ); - b.useCustomDistance = true; - b.customDistance = distance; - - b.rotationMode = Baker.RotationMode.Quaternion; + vs.rotationMode = BakingViewSettings.RotationMode.Quaternion; } ); @@ -96,20 +97,20 @@ namespace Rokojori var numTextures = GetNumViews(); var textureAlignment = TextureMerger.ComputeTextureAlignment( numTextures ); - var toOP = 1f / ( mb.octahedralSides - 1 ); + var toOP = 1f / ( octahedralSides - 1 ); var index = 0; - for ( int x = 0; x < mb.octahedralSides; x++ ) + for ( int x = 0; x < octahedralSides; x++ ) { - for ( int y = 0; y < mb.octahedralSides; y++ ) + for ( int y = 0; y < octahedralSides; y++ ) { - var inverseX = ( mb.octahedralSides - 1 ) - x; - var inverseY = ( mb.octahedralSides - 1 ) - y; + var inverseX = ( octahedralSides - 1 ) - x; + var inverseY = ( octahedralSides - 1 ) - y; var octahedralPosition = new Vector2( y, inverseX ) * toOP; - var normal = OctahedralMapping.Map( octahedralPosition, mb.octahedralFullSphere ); + var normal = OctahedralMapping.Map( octahedralPosition, octahedralFullSphere ); - _bakers[ index ].rotationQuaternion = Math3D.LookRotation( normal, true ).Normalized().Inverse(); + _bakers[ index ].viewSettings.rotationQuaternion = Math3D.LookRotation( normal, true ).Normalized().Inverse(); index ++; } diff --git a/Runtime/Procedural/Baking/MultiBaker/Octahedral_Baker.cs.uid b/Runtime/Procedural/Baking/MultiBaker/Octahedral_Baker.cs.uid new file mode 100644 index 0000000..61cbefc --- /dev/null +++ b/Runtime/Procedural/Baking/MultiBaker/Octahedral_Baker.cs.uid @@ -0,0 +1 @@ +uid://bdgrhysvuusyp diff --git a/Runtime/Procedural/Baking/MultiBakeModeOctahedral.cs.uid b/Runtime/Procedural/Baking/MultiBaker/Octahedral_Baker.uid similarity index 100% rename from Runtime/Procedural/Baking/MultiBakeModeOctahedral.cs.uid rename to Runtime/Procedural/Baking/MultiBaker/Octahedral_Baker.uid diff --git a/Runtime/Procedural/Baking/MultiBaker/_XX_MultiBakeMode.cs b/Runtime/Procedural/Baking/MultiBaker/_XX_MultiBakeMode.cs new file mode 100644 index 0000000..33b63ea --- /dev/null +++ b/Runtime/Procedural/Baking/MultiBaker/_XX_MultiBakeMode.cs @@ -0,0 +1,36 @@ +using System.Collections; +using System.Collections.Generic; +using Godot; +using System; + +using System.Threading.Tasks; + +namespace Rokojori +{ + [Tool][GlobalClass] + public partial class _XX_MultiBakeMode:Resource + { + public MultiBaker multiBaker; + + public virtual int GetNumViews() + { + return -1; + } + + public virtual void CreateBakers() + { + + } + + public virtual void AssignMaterial( BakingMaterialMode mode, Texture2D texture ) + { + + } + + public virtual void CreateMaterial( bool preview ) + { + + } + + } +} \ No newline at end of file diff --git a/Runtime/Procedural/Baking/MultiBakeMode.cs.uid b/Runtime/Procedural/Baking/MultiBaker/_XX_MultiBakeMode.cs.uid similarity index 100% rename from Runtime/Procedural/Baking/MultiBakeMode.cs.uid rename to Runtime/Procedural/Baking/MultiBaker/_XX_MultiBakeMode.cs.uid diff --git a/Runtime/Procedural/Baking/MultiBakeModeBillboardBase.cs b/Runtime/Procedural/Baking/MultiBaker/_XX_MultiBakeModeBillboardBase.cs similarity index 93% rename from Runtime/Procedural/Baking/MultiBakeModeBillboardBase.cs rename to Runtime/Procedural/Baking/MultiBaker/_XX_MultiBakeModeBillboardBase.cs index 9703402..b0a9786 100644 --- a/Runtime/Procedural/Baking/MultiBakeModeBillboardBase.cs +++ b/Runtime/Procedural/Baking/MultiBaker/_XX_MultiBakeModeBillboardBase.cs @@ -7,7 +7,8 @@ using System.Threading.Tasks; namespace Rokojori { - public abstract class MultiBakeModeBillboardBase:MultiBakeModeImplementation + [Tool][GlobalClass] + public partial class _XX_MultiBakeModeBillboardBase:_XX_MultiBakeMode { public override void CreateMaterial( bool preview ) { diff --git a/Runtime/Procedural/Baking/MultiBakeModeBillboardBase.cs.uid b/Runtime/Procedural/Baking/MultiBaker/_XX_MultiBakeModeBillboardBase.cs.uid similarity index 100% rename from Runtime/Procedural/Baking/MultiBakeModeBillboardBase.cs.uid rename to Runtime/Procedural/Baking/MultiBaker/_XX_MultiBakeModeBillboardBase.cs.uid diff --git a/Runtime/Procedural/Baking/MultiBaker/_XX_Spherical_Baker.cs b/Runtime/Procedural/Baking/MultiBaker/_XX_Spherical_Baker.cs new file mode 100644 index 0000000..e2eccd3 --- /dev/null +++ b/Runtime/Procedural/Baking/MultiBaker/_XX_Spherical_Baker.cs @@ -0,0 +1,48 @@ +using System.Collections; +using System.Collections.Generic; +using Godot; +using System; + +using System.Threading.Tasks; + +namespace Rokojori +{ + [Tool][GlobalClass] + public partial class _XX_Spherical_Baker:_XX_MultiBakeMode + { + public static readonly string sphericalShader = "res://addons/rokojori_action_library/External/Imposter/materials/shaders/ImpostorShader.gdshader"; + + [Export] + public int sphericalSides = 4; + [Export] + public int sphericalRows = 3; + [Export] + public float minPitch = -30; + [Export] + public float maxPitch = 30; + + + public override int GetNumViews() + { + var views = sphericalSides * sphericalRows; + + return views; + } + + public override void CreateMaterial( bool preview ) + { + + } + + public override void AssignMaterial( BakingMaterialMode mode, Texture2D texture ) + { + + } + + public override void CreateBakers() + { + + } + + } +} \ No newline at end of file diff --git a/Runtime/Procedural/Baking/MultiBaker/_XX_Spherical_Baker.cs.uid b/Runtime/Procedural/Baking/MultiBaker/_XX_Spherical_Baker.cs.uid new file mode 100644 index 0000000..7d3f9f9 --- /dev/null +++ b/Runtime/Procedural/Baking/MultiBaker/_XX_Spherical_Baker.cs.uid @@ -0,0 +1 @@ +uid://dc1xsvfdwtqbc diff --git a/Runtime/Procedural/Baking/MultiBakeModeSpherical.cs.uid b/Runtime/Procedural/Baking/MultiBaker/_XX_Spherical_Baker.uid similarity index 100% rename from Runtime/Procedural/Baking/MultiBakeModeSpherical.cs.uid rename to Runtime/Procedural/Baking/MultiBaker/_XX_Spherical_Baker.uid diff --git a/Runtime/Procedural/Baking/TextureMerger.cs b/Runtime/Procedural/Baking/TextureMerger.cs index 6519dcc..ae9c440 100644 --- a/Runtime/Procedural/Baking/TextureMerger.cs +++ b/Runtime/Procedural/Baking/TextureMerger.cs @@ -24,6 +24,9 @@ namespace Rokojori [Export] public bool createLayout; + [Export] + public int dilationRadius = 64; + [ExportGroup("Source")] [Export] @@ -217,19 +220,29 @@ namespace Rokojori for ( int i = 0; i < _textures.Count; i++ ) { - var mesh = X_textureMergerViewport.CreateChild( "Texture " + ( i + 1 ) ); + var meshInstance = X_textureMergerViewport.CreateChild( "Texture " + ( i + 1 ) ); var uvRectangle = GetUVRectangle( alignment, i ); - - SetMeshCoordinates( mesh, uvRectangle, i ); - + + RJLog.Log( "Set Texture" + ( i + 1 ), uvRectangle.min, uvRectangle.max ); + SetMeshCoordinates( meshInstance, uvRectangle, i ); + + var dilatedMaterial = new DilationDrawerMaterial(); + dilatedMaterial.textureAlbedo.Set( _textures[ i ] ); + dilatedMaterial.resolution.Set( _textures[ i ].GetSize() ); + dilatedMaterial.maxRadius.Set( dilationRadius ); + dilatedMaterial.alphaTreshold.Set( 1 ); + dilatedMaterial.assignedColorAlphaMinimum.Set( 254 ); + dilatedMaterial.genericAlphaOffset.Set( 0 ); + dilatedMaterial.amount.Set( 1 ); + var material = new StandardMaterial3D(); material.Transparency = transparencyMode; material.AlphaScissorThreshold = alphaThreshold; material.ShadingMode = BaseMaterial3D.ShadingModeEnum.Unshaded; - material.AlbedoTexture = _textures[ i ]; - mesh.Material = material; + + Materials.Set( meshInstance, dilatedMaterial ); } } @@ -237,26 +250,36 @@ namespace Rokojori { for ( int i = 0; i < _textures.Count; i++ ) { - var mesh = outputTarget.CreateChild( "Texture " + ( i + 1 ) ); + var meshInstance = outputTarget.CreateChild( "Texture " + ( i + 1 ) ); - SetMeshCoordinates( mesh, customPositions[ i ], customSizes[ i ], i ); + SetMeshCoordinates( meshInstance, customPositions[ i ], customSizes[ i ], i ); var material = new StandardMaterial3D(); material.Transparency = transparencyMode; material.ShadingMode = BaseMaterial3D.ShadingModeEnum.Unshaded; material.AlphaScissorThreshold = alphaThreshold; + var dilatedMaterial = new DilationDrawerMaterial(); + dilatedMaterial.textureAlbedo.Set( _textures[ i ] ); + dilatedMaterial.resolution.Set( _textures[ i ].GetSize() ); + dilatedMaterial.maxRadius.Set( dilationRadius ); + dilatedMaterial.alphaTreshold.Set( 10 ); + dilatedMaterial.assignedColorAlphaMinimum.Set( 10 ); + dilatedMaterial.genericAlphaOffset.Set( 50 ); + dilatedMaterial.amount.Set( 1 ); + + material.AlbedoTexture = _textures[ i ]; - mesh.Material = material; + Materials.Set( meshInstance, dilatedMaterial ); } } - void SetMeshCoordinates( CsgMesh3D mesh, Box2 rectangle, int index ) + void SetMeshCoordinates( MeshInstance3D mesh, Box2 rectangle, int index ) { SetMeshCoordinates( mesh, rectangle.min, rectangle.max, index ); } - void SetMeshCoordinates( CsgMesh3D mesh, Vector2 min, Vector2 max, int index ) + void SetMeshCoordinates( MeshInstance3D mesh, Vector2 min, Vector2 max, int index ) { var start = ConvertUVtoCameraSpace( min ); var end = ConvertUVtoCameraSpace( max ); @@ -269,7 +292,7 @@ namespace Rokojori mesh.GlobalPosition = new Vector3( start.X + size.X/2, start.Y + size.Y/2, -1 ); - // RJLog.Log( "Set Mesh", index, "Min:", min, ">>", start, "Max:", max, ">>", end ); + RJLog.Log( "Set Mesh", index, "Size:", size, "Min:", min, ">>", start, "Max:", max, ">>", end ); } public static Vector2I ComputeTextureAlignment( int numElements ) diff --git a/Runtime/Procedural/Baking/ViewSettings/AutoDistance_BakingFDSettings.cs b/Runtime/Procedural/Baking/ViewSettings/AutoDistance_BakingFDSettings.cs new file mode 100644 index 0000000..ddd5466 --- /dev/null +++ b/Runtime/Procedural/Baking/ViewSettings/AutoDistance_BakingFDSettings.cs @@ -0,0 +1,40 @@ +using System.Collections; +using System.Collections.Generic; +using Godot; +using System; + + + +namespace Rokojori +{ + [Tool] + [GlobalClass] + public partial class AutoDistance_BakingFDSettings:_XX_BakingFDSettings + { + [Export] + public float fov = 75; + + [Export] + public float zoom = 1; + + [Export] + public SizeEstimationType sizeEstimation = SizeEstimationType.Bounding_Sphere; + + [Export] + public float sizeAbsoluteOffset = 0; + + [Export] + public float sizeRelativeOffset = 0.1f; + + + public override Vector2 ComputeFOVDistance( float size ) + { + size += sizeAbsoluteOffset; + size += sizeRelativeOffset * size; + + var distance = Cameras.ComputeCameraFrameFittingDistance( fov, size / zoom ); + + return new Vector2( fov, distance ); + } + } +} \ No newline at end of file diff --git a/Runtime/Procedural/Baking/ViewSettings/AutoDistance_BakingFDSettings.cs.uid b/Runtime/Procedural/Baking/ViewSettings/AutoDistance_BakingFDSettings.cs.uid new file mode 100644 index 0000000..25a517f --- /dev/null +++ b/Runtime/Procedural/Baking/ViewSettings/AutoDistance_BakingFDSettings.cs.uid @@ -0,0 +1 @@ +uid://chqe2154cxijp diff --git a/Runtime/Procedural/Baking/ViewSettings/AutoFOVDistance_BakingFDSettings.cs b/Runtime/Procedural/Baking/ViewSettings/AutoFOVDistance_BakingFDSettings.cs new file mode 100644 index 0000000..44f0b1b --- /dev/null +++ b/Runtime/Procedural/Baking/ViewSettings/AutoFOVDistance_BakingFDSettings.cs @@ -0,0 +1,45 @@ +using System.Collections; +using System.Collections.Generic; +using Godot; +using System; + + + +namespace Rokojori +{ + [Tool] + [GlobalClass] + public partial class AutoFOVDistance_BakingFDSettings:_XX_BakingFDSettings + { + [Export] + public float gameCameraFOV = 75; + + [Export] + public float gameViewDistance = 10; + + [Export] + public float zoom = 1; + + [Export] + public SizeEstimationType sizeEstimation = SizeEstimationType.Bounding_Sphere; + + [Export] + public float sizeAbsoluteOffset = 0; + + [Export] + public float sizeRelativeOffset = 0.1f; + + + public override Vector2 ComputeFOVDistance( float size ) + { + size += sizeAbsoluteOffset; + size += sizeRelativeOffset * size; + + var fov = Cameras.ComputeFOVForBillboard( gameCameraFOV, size, gameViewDistance ); + var distance = Cameras.ComputeCameraFrameFittingDistance( fov, size / zoom ); + + + return new Vector2( fov, distance ); + } + } +} \ No newline at end of file diff --git a/Runtime/Procedural/Baking/ViewSettings/AutoFOVDistance_BakingFDSettings.cs.uid b/Runtime/Procedural/Baking/ViewSettings/AutoFOVDistance_BakingFDSettings.cs.uid new file mode 100644 index 0000000..8486201 --- /dev/null +++ b/Runtime/Procedural/Baking/ViewSettings/AutoFOVDistance_BakingFDSettings.cs.uid @@ -0,0 +1 @@ +uid://cf5kjcm27di0q diff --git a/Runtime/Procedural/Baking/ViewSettings/BakingViewSettings.cs b/Runtime/Procedural/Baking/ViewSettings/BakingViewSettings.cs new file mode 100644 index 0000000..29afeb3 --- /dev/null +++ b/Runtime/Procedural/Baking/ViewSettings/BakingViewSettings.cs @@ -0,0 +1,154 @@ +using System.Collections; +using System.Collections.Generic; +using Godot; +using System; + + + +namespace Rokojori +{ + [Tool] + [GlobalClass] + public partial class BakingViewSettings:Resource + { + /*public enum CameraDistanceDetectionType + { + Automatic_Distance_Detection, + Custom_Distance + } + + public enum CameraFOVMode + { + Keep_Fov, + Compute_Fov_With_Distance, + Custom_Fov + }*/ + + [Export] + public _XX_BakingFDSettings fovDistance; + + /* + [Export] + public float originalFOV = 75; + + [Export] + public bool assignFOV = false; + + [Export] + public float placingDistance = 500; + + + + [Export] + public float zoom = 1; + + */ + + [ExportGroup( "Debug Readonly" )] + + [Export] + public float _XX_ComputedFOV = 75; + + [Export] + public float _XX_ComputedDistance = 1; + + [Export] + public float _XX_ComputedOutputScale = 1; + + /* + + [ExportGroup( "Custom FOV" )] + + [Export] + public bool useCustomFOV = false; + + [Export] + public float customFOV = 75; + + [ExportGroup( "Custom Distance" )] + + [Export] + public bool useCustomDistance = false; + + [Export] + public float customDistance = 50; + */ + + public enum RotationMode + { + Yaw_Pitch, + Quaternion + } + + + + [ExportGroup("Rotation")] + + [Export] + public RotationMode rotationMode = RotationMode.Yaw_Pitch; + + [Export( PropertyHint.Range, "-180,180")] + public float yaw = 0; + + [Export( PropertyHint.Range, "-180,180")] + public float pitch = 0; + + [Export] + public Quaternion rotationQuaternion; + + + public Quaternion bakingRotation => RotationMode.Yaw_Pitch == rotationMode ? + Math3D.YawPitchRotation( yaw, pitch ) : + rotationQuaternion; + + + + + + public void ApplySettings( Camera3D camera, Node3D target, Vector3 pivot ) + { + var box = target.GetWorldBounds(); + + if ( box == null ) + { + RJLog.Log( "No target" ); + return; + } + + Box3 box3 = box; + box3.IncludePoint( target.ToGlobal( pivot ) ); + + var sphere = Sphere.ContainingBox( box3 ); + + var size = sphere.radius; + + if ( fovDistance is AutoDistance_BakingFDSettings ad ) + { + size = ad.sizeEstimation == _XX_BakingFDSettings.SizeEstimationType.Bounding_Sphere ? + sphere.radius : ( box3.size.Y / 2f ); + } + + if ( fovDistance is AutoFOVDistance_BakingFDSettings afd ) + { + size = afd.sizeEstimation == _XX_BakingFDSettings.SizeEstimationType.Bounding_Sphere ? + sphere.radius : ( box3.size.Y / 2f ); + } + + var fd = fovDistance.ComputeFOVDistance( size / 2f ); + _XX_ComputedDistance = fd.Y; + _XX_ComputedFOV = fd.X; + + camera.Fov = fd.X; + + var cameraRotation = bakingRotation; + var offset = ( Vector3.Back * _XX_ComputedDistance ) * cameraRotation ; + camera.GlobalPosition = target.GlobalPosition + offset - pivot; + + camera.SetGlobalQuaternion( cameraRotation.Inverse() ); + + _XX_ComputedOutputScale = Cameras.ComputeCameraFittingScale( camera.Fov, _XX_ComputedDistance ); + + + } + } +} diff --git a/Runtime/Procedural/Baking/ViewSettings/BakingViewSettings.cs.uid b/Runtime/Procedural/Baking/ViewSettings/BakingViewSettings.cs.uid new file mode 100644 index 0000000..6a83783 --- /dev/null +++ b/Runtime/Procedural/Baking/ViewSettings/BakingViewSettings.cs.uid @@ -0,0 +1 @@ +uid://d1mvjtf46vfip diff --git a/Runtime/Procedural/Baking/ViewSettings/Manual_BakingFDSettings.cs b/Runtime/Procedural/Baking/ViewSettings/Manual_BakingFDSettings.cs new file mode 100644 index 0000000..d768aaf --- /dev/null +++ b/Runtime/Procedural/Baking/ViewSettings/Manual_BakingFDSettings.cs @@ -0,0 +1,33 @@ +using System.Collections; +using System.Collections.Generic; +using Godot; +using System; + + + +namespace Rokojori +{ + [Tool] + [GlobalClass] + public partial class Manual_BakingFDSettings:_XX_BakingFDSettings + { + [Export] + public float cameraDistance = 10; + + [Export] + public float cameraFOV = 75; + + public override Vector2 ComputeFOVDistance( float size ) + { + return new Vector2( cameraFOV, cameraDistance ); + } + + public static Manual_BakingFDSettings Create( float fov, float distance ) + { + var m = new Manual_BakingFDSettings(); + m.cameraDistance = distance; + m.cameraFOV = fov; + return m; + } + } +} \ No newline at end of file diff --git a/Runtime/Procedural/Baking/ViewSettings/Manual_BakingFDSettings.cs.uid b/Runtime/Procedural/Baking/ViewSettings/Manual_BakingFDSettings.cs.uid new file mode 100644 index 0000000..bf2837a --- /dev/null +++ b/Runtime/Procedural/Baking/ViewSettings/Manual_BakingFDSettings.cs.uid @@ -0,0 +1 @@ +uid://dbfheg550pyqw diff --git a/Runtime/Procedural/Baking/ViewSettings/_XX_BakingFDSettings.cs b/Runtime/Procedural/Baking/ViewSettings/_XX_BakingFDSettings.cs new file mode 100644 index 0000000..73ee490 --- /dev/null +++ b/Runtime/Procedural/Baking/ViewSettings/_XX_BakingFDSettings.cs @@ -0,0 +1,27 @@ +using System.Collections; +using System.Collections.Generic; +using Godot; +using System; + + + +namespace Rokojori +{ + [Tool] + [GlobalClass] + public partial class _XX_BakingFDSettings:Resource + { + public enum SizeEstimationType + { + Bounding_Sphere, + Bounding_Box_Height + } + + + + public virtual Vector2 ComputeFOVDistance( float size ) + { + return Vector2.Zero; + } + } +} \ No newline at end of file diff --git a/Runtime/Procedural/Baking/ViewSettings/_XX_BakingFDSettings.cs.uid b/Runtime/Procedural/Baking/ViewSettings/_XX_BakingFDSettings.cs.uid new file mode 100644 index 0000000..6d03e36 --- /dev/null +++ b/Runtime/Procedural/Baking/ViewSettings/_XX_BakingFDSettings.cs.uid @@ -0,0 +1 @@ +uid://cy6khw6xl3qrg From 9854cd68d374794967965274b2d7d512ad576fed Mon Sep 17 00:00:00 2001 From: Josef Date: Sun, 13 Apr 2025 21:40:16 +0200 Subject: [PATCH 4/4] Small Update --- .../Procedural/Baking/MultiBaker/MultiBakerCameraTools.cs.uid | 1 + 1 file changed, 1 insertion(+) create mode 100644 Runtime/Procedural/Baking/MultiBaker/MultiBakerCameraTools.cs.uid diff --git a/Runtime/Procedural/Baking/MultiBaker/MultiBakerCameraTools.cs.uid b/Runtime/Procedural/Baking/MultiBaker/MultiBakerCameraTools.cs.uid new file mode 100644 index 0000000..1dc0807 --- /dev/null +++ b/Runtime/Procedural/Baking/MultiBaker/MultiBakerCameraTools.cs.uid @@ -0,0 +1 @@ +uid://bwqywi630pq3h