rj-action-library/Runtime/Procedural/Baking/MultiBaker/MultiBaker.cs

582 lines
14 KiB
C#
Raw Normal View History

2024-12-01 17:07:41 +00:00
using System.Collections;
using System.Collections.Generic;
using Godot;
using System;
2025-01-03 12:09:23 +00:00
using System.Threading.Tasks;
2024-12-01 17:07:41 +00:00
namespace Rokojori
{
[Tool]
[GlobalClass]
public partial class MultiBaker:Node
{
2025-04-06 16:39:24 +00:00
[ExportToolButton( "Bake")]
2025-04-08 09:07:02 +00:00
public Callable BakeButton => Callable.From( () => StartBaking() );
[Export]
public bool cleanUpAfterBaking = true;
[Export]
public _XX_MultiBakeMode bakeMode;
2025-01-03 12:09:23 +00:00
public enum MaterialMode
{
Full_Seperated,
Simple_Prebaked
}
[ExportGroup("Material")]
[Export]
public MaterialMode materialMode;
2025-04-08 09:07:02 +00:00
[Export]
public int dilationRadius = 64;
2025-01-03 12:09:23 +00:00
[ExportGroup("Material/Full Seperated")]
[Export]
public bool mmfs_Normals = true;
[Export]
public bool mmfs_Depth = true;
[Export]
public bool mmfs_ORM = true;
2024-12-01 17:07:41 +00:00
2025-04-08 09:07:02 +00:00
[ExportGroup( "Object")]
2025-01-03 12:09:23 +00:00
2024-12-01 17:07:41 +00:00
[Export]
2025-04-08 09:07:02 +00:00
public Node3D target;
2025-01-03 12:09:23 +00:00
[Export]
2025-04-08 09:07:02 +00:00
public bool autoTargetPivot = false;
2025-01-03 12:09:23 +00:00
[Export]
2025-04-08 09:07:02 +00:00
public Vector3 targetPivot;
2025-01-03 12:09:23 +00:00
2025-04-08 09:07:02 +00:00
[ExportGroup( "Camera")]
2025-01-03 12:09:23 +00:00
[Export]
2025-04-08 09:07:02 +00:00
public BakingViewSettings viewSettings;
2025-01-03 12:09:23 +00:00
2025-04-08 09:07:02 +00:00
public float cameraZoom
{
get
{
if ( viewSettings.fovDistance is AutoDistance_BakingFDSettings ad )
{
return ad.zoom;
}
if ( viewSettings.fovDistance is AutoDistance_BakingFDSettings afd )
{
return afd.zoom;
}
2024-12-01 17:07:41 +00:00
2025-01-03 12:09:23 +00:00
2025-04-08 09:07:02 +00:00
return 1;
}
}
2024-12-01 17:07:41 +00:00
2025-04-08 09:07:02 +00:00
/*
2024-12-01 17:07:41 +00:00
[Export]
public Baker.CameraDistanceDetectionType distanceDetectionType = Baker.CameraDistanceDetectionType.Automatic_Distance_Detection;
[Export]
public float customDistance = 50;
[Export]
public float cameraZoom = 1;
[Export]
public Baker.CameraFOVMode fovMode = Baker.CameraFOVMode.Compute_Fov_With_Distance;
[Export]
public float originalFOV = 75;
[Export]
public float fovPlacingDistance = 200;
[Export]
public float customFOV = 75;
2025-04-08 09:07:02 +00:00
*/
2025-01-03 12:09:23 +00:00
[ExportGroup( "Output")]
2024-12-01 17:07:41 +00:00
[Export]
2025-01-03 12:09:23 +00:00
public string outputDirectory;
2024-12-01 17:07:41 +00:00
2025-01-03 12:09:23 +00:00
[Export]
public string outputFileName;
2024-12-01 17:07:41 +00:00
2025-01-03 12:09:23 +00:00
[Export]
public float outputQuality = 1f;
2024-12-01 17:07:41 +00:00
[Export]
2025-01-03 12:09:23 +00:00
public Vector2 outputTextureSize = new Vector2( 2048, 2048 );
2024-12-01 17:07:41 +00:00
[Export]
2025-01-03 12:09:23 +00:00
public bool showOutputTexture = false;
[ExportGroup("Read Only")]
2024-12-01 17:07:41 +00:00
[Export]
2025-01-03 12:09:23 +00:00
public SubViewport X_bakingViewport;
2024-12-01 17:07:41 +00:00
[Export]
2025-01-03 12:09:23 +00:00
public Node3D X_bakingTargetContainer;
2024-12-01 17:07:41 +00:00
[Export]
2025-01-03 12:09:23 +00:00
public Node X_views;
2024-12-01 17:07:41 +00:00
[Export]
2025-01-03 12:09:23 +00:00
public WorldEnvironment X_worldEnvironment;
[Export]
public MeshInstance3D X_outputMesh;
2024-12-01 17:07:41 +00:00
2025-01-03 12:09:23 +00:00
[Export]
public TextureMerger X_textureMerger;
2024-12-01 17:07:41 +00:00
[Export]
2025-01-03 12:09:23 +00:00
public SetBakingMaterials X_setBakingMaterials;
2024-12-01 17:07:41 +00:00
2025-01-03 12:09:23 +00:00
[Export]
public MeshInstance3D X_texturePreview;
2024-12-01 17:07:41 +00:00
[Export]
2025-01-03 12:09:23 +00:00
public CsgMesh3D X_depthMapper;
2024-12-01 17:07:41 +00:00
[Export]
2025-01-03 12:09:23 +00:00
public Texture2D X_bakedTextureAlbedo;
2024-12-01 17:07:41 +00:00
[Export]
2025-01-03 12:09:23 +00:00
public Texture2D X_bakedTextureNormal;
2024-12-01 17:07:41 +00:00
[Export]
2025-01-03 12:09:23 +00:00
public Texture2D X_bakedTextureORM;
[Export]
public Texture2D X_bakedTextureDepth;
2025-04-08 09:07:02 +00:00
[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<SubViewport>( "Multi Baker Viewport" );
X_bakingViewport.Size = (Vector2I) outputTextureSize;
X_bakingViewport.OwnWorld3D = true;
X_bakingViewport.TransparentBg = true;
X_worldEnvironment = X_bakingViewport.CreateChild<WorldEnvironment>( "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<Node3D>( "Target Container" );
X_views = X_bakingViewport.CreateChild<Node>( "Views" );
X_textureMerger = this.CreateChild<TextureMerger>( "Texture Merger" );
X_textureMerger.multiBaker = this;
X_textureMerger.dilationRadius = dilationRadius;
X_textureMerger.Initialize();
X_outputMesh = this.CreateChild<MeshInstance3D>( "Output Mesh" );
X_texturePreview = this.CreateChild<MeshInstance3D>( "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<SetBakingMaterials>( "Set Baking Materials" );
Materials.Set( X_texturePreview, pm );
}
2025-04-06 16:39:24 +00:00
public async Task Bake()
{
2025-04-08 09:07:02 +00:00
if ( X_baking )
2025-01-03 12:09:23 +00:00
{
return;
}
2025-04-08 09:07:02 +00:00
X_baking = true;
2025-01-03 12:09:23 +00:00
try
{
2025-04-08 09:07:02 +00:00
bakeMode.multiBaker = this;
2025-01-03 12:09:23 +00:00
this.LogInfo( "Started baking" );
Nodes.RemoveAndDeleteChildren( X_bakingTargetContainer );
2025-04-08 09:07:02 +00:00
target.DeepCopyTo( X_bakingTargetContainer );
2025-01-03 12:09:23 +00:00
if ( _bakers == null || _bakers.Count != GetNumViews() )
{
CreateViews();
await this.RequestNextFrame();
}
SetupViews();
this.LogInfo( "Views set up" );
await this.RequestNextFrame();
2025-04-08 09:07:02 +00:00
X_setBakingMaterials.SetTarget( X_bakingTargetContainer );
2025-01-03 12:09:23 +00:00
var bakingMaterialModes = new List<BakingMaterialMode>();
var preview_QuickMaterial = MaterialMode.Simple_Prebaked == materialMode;
2025-04-08 09:07:02 +00:00
bakeMode.CreateMaterial( preview_QuickMaterial );
2025-01-03 12:09:23 +00:00
if ( preview_QuickMaterial )
{
bakingMaterialModes.Add( BakingMaterialMode.Preview );
}
else
{
bakingMaterialModes.Add( BakingMaterialMode.Albedo );
if ( mmfs_Normals )
{
bakingMaterialModes.Add( BakingMaterialMode.Normals );
}
if ( mmfs_Depth )
{
bakingMaterialModes.Add( BakingMaterialMode.Depth );
}
if ( mmfs_ORM )
{
bakingMaterialModes.Add( BakingMaterialMode.ORM );
}
}
this.LogInfo( "Prepared baking modes" );
X_textureMerger.textureSize = outputTextureSize;
X_textureMerger.Initialize();
X_textureMerger.CreateLayout();
this.LogInfo( "Prepared texture merger" );
2025-04-08 09:07:02 +00:00
// var fovDistance = viewSettings.fovDistance.ComputeFOVDistance(
2025-01-03 12:09:23 +00:00
var objectDistance = GetCameraDistance();
2025-04-08 09:07:02 +00:00
this.LogInfo( "Set Camera Distance", objectDistance );
2025-01-03 12:09:23 +00:00
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 ] );
2025-04-08 09:07:02 +00:00
_bakers.ForEach( b => b.Bake() );
2025-01-03 12:09:23 +00:00
await this.RequestNextFrame();
Texture2D texture = X_textureMerger.X_textureMergerViewport.GetTexture();
this.LogInfo( "Texture created:", bakingMaterialModes[ i ] );
2025-04-06 16:39:24 +00:00
texture = Textures.Copy( texture );
await this.RequestNextFrame();
await this.RequestNextFrame();
2025-01-03 12:09:23 +00:00
this.LogInfo( "Assigning Texture", bakingMaterialModes[ i ] );
2025-04-08 09:07:02 +00:00
bakeMode.AssignMaterial( bakingMaterialModes[ i ], texture );
2025-01-03 12:09:23 +00:00
this.LogInfo( "Baking done:", bakingMaterialModes[ i ] );
await this.RequestNextFrame();
}
await this.RequestNextFrame();
2025-04-08 09:07:02 +00:00
X_outputMesh.GlobalPosition = target.GlobalPosition - targetPivot;
X_outputMesh.Scale = Vector3.One * cameraZoom;
if ( cleanUpAfterBaking )
{
this.LogInfo( "Cleaning Up" );
CleanUp();
}
this.LogInfo( "All Baking done" );
2025-01-03 12:09:23 +00:00
}
catch ( System.Exception e )
{
this.LogError( "Baking failed" );
this.LogError( e );
}
2025-04-08 09:07:02 +00:00
X_baking = false;
2025-01-03 12:09:23 +00:00
return;
}
public List<SubViewport> GetAllViewports()
2024-12-01 17:07:41 +00:00
{
2025-01-03 12:09:23 +00:00
return Nodes.AllIn<SubViewport>( X_views, null, false );
}
public void CacheTexture( BakingMaterialMode mode, Texture2D texture )
{
if ( BakingMaterialMode.Albedo == mode )
{
X_bakedTextureAlbedo = texture;
}
else if ( BakingMaterialMode.Normals == mode )
2024-12-01 17:07:41 +00:00
{
2025-01-03 12:09:23 +00:00
X_bakedTextureNormal = texture;
2024-12-01 17:07:41 +00:00
}
2025-01-03 12:09:23 +00:00
else if ( BakingMaterialMode.ORM == mode )
{
X_bakedTextureORM = texture;
}
else if ( BakingMaterialMode.Depth == mode )
{
X_bakedTextureDepth = texture;
}
2025-04-08 09:07:02 +00:00
}
2024-12-01 17:07:41 +00:00
2025-04-08 09:07:02 +00:00
public void CleanUp()
{
var mesh = X_outputMesh;
mesh.Reparent( GetParent(), true );
2024-12-01 17:07:41 +00:00
2025-04-08 09:07:02 +00:00
X_bakingViewport = null;
2024-12-01 17:07:41 +00:00
2025-04-08 09:07:02 +00:00
X_bakingTargetContainer = null;
2024-12-01 17:07:41 +00:00
2025-04-08 09:07:02 +00:00
X_views = null;
2024-12-01 17:07:41 +00:00
2025-04-08 09:07:02 +00:00
X_worldEnvironment = null;
X_outputMesh = null;
2024-12-01 17:07:41 +00:00
2025-04-08 09:07:02 +00:00
X_textureMerger = null;
2024-12-01 17:07:41 +00:00
2025-04-08 09:07:02 +00:00
X_setBakingMaterials = null;
2024-12-01 17:07:41 +00:00
2025-04-08 09:07:02 +00:00
X_texturePreview = null;
2024-12-01 17:07:41 +00:00
2025-04-08 09:07:02 +00:00
X_depthMapper = null;
2024-12-01 17:07:41 +00:00
2025-04-08 09:07:02 +00:00
X_bakedTextureAlbedo = null;
2024-12-01 17:07:41 +00:00
2025-04-08 09:07:02 +00:00
X_bakedTextureNormal = null;
2024-12-01 17:07:41 +00:00
2025-04-08 09:07:02 +00:00
X_bakedTextureORM = null;
2024-12-01 17:07:41 +00:00
2025-04-08 09:07:02 +00:00
X_bakedTextureDepth = null;
2025-01-03 12:09:23 +00:00
2025-04-08 09:07:02 +00:00
Nodes.RemoveAndDeleteChildren( this );
2025-01-03 12:09:23 +00:00
2025-04-08 09:07:02 +00:00
mesh.Reparent( this, true );
2025-01-03 12:09:23 +00:00
}
2025-04-08 09:07:02 +00:00
2025-01-03 12:09:23 +00:00
public int GetNumViews()
{
2025-04-08 09:07:02 +00:00
return bakeMode.GetNumViews();
2024-12-01 17:07:41 +00:00
}
List<Baker> _bakers;
2025-01-03 12:09:23 +00:00
public List<Baker> bakers => _bakers;
2024-12-01 17:07:41 +00:00
public void CreateViews()
2025-01-03 12:09:23 +00:00
{
2024-12-01 17:07:41 +00:00
Nodes.RemoveAndDeleteChildren( X_views );
var numViews = GetNumViews();
_bakers = new List<Baker>();
2025-01-03 12:09:23 +00:00
var minViewsPerAxis = TextureMerger.ComputeTextureAlignment( numViews ).Y;
2024-12-01 17:07:41 +00:00
for ( int i = 0; i < numViews; i++ )
{
var userIndex = ( i + 1 );
var bakingView = X_views.CreateChild<SubViewport>( "Baking View " + userIndex );
bakingView.TransparentBg = true;
2025-01-03 12:09:23 +00:00
bakingView.Size = (Vector2I) ( outputTextureSize / minViewsPerAxis );
2024-12-01 17:07:41 +00:00
var bakingCamera = bakingView.CreateChild<Camera3D>( "Camera View " + userIndex );
var baker = bakingView.CreateChild<Baker>( "Baker " + userIndex );
baker.camera = bakingCamera;
baker.target = X_bakingTargetContainer;
baker.viewport = bakingView;
2025-01-03 12:09:23 +00:00
2025-04-08 09:07:02 +00:00
2024-12-01 17:07:41 +00:00
_bakers.Add( baker );
}
}
public void SetupViews()
{
2025-01-03 12:09:23 +00:00
var numViews = GetNumViews();
var minViewsPerAxis = TextureMerger.ComputeTextureAlignment( numViews ).Y;
X_bakingViewport.Size = (Vector2I) outputTextureSize;
for ( int i = 0; i < numViews; i++ )
{
var baker = _bakers[ i ];
var bakingView = baker.viewport as SubViewport;
bakingView.Size = (Vector2I) ( outputTextureSize / minViewsPerAxis );
}
_targetBoundingSphere = null;
2025-04-08 09:07:02 +00:00
_targetBoundingBox = null;
2025-01-03 12:09:23 +00:00
ComputeBoundingSphere();
if ( _targetBoundingSphere == null )
{
this.LogError( "No bounding sphere created, ensure there are visible targets" );
return;
}
2025-04-08 09:07:02 +00:00
ComputeCameraViewSettings();
2025-01-03 12:09:23 +00:00
2025-04-08 09:07:02 +00:00
bakeMode.CreateBakers();
2025-01-03 12:09:23 +00:00
2024-12-01 17:07:41 +00:00
}
Sphere _targetBoundingSphere;
2025-04-08 09:07:02 +00:00
Box3 _targetBoundingBox;
2024-12-01 17:07:41 +00:00
Sphere targetBoundingSphere
{
get
{
if ( _targetBoundingSphere == null )
{
ComputeBoundingSphere();
}
return _targetBoundingSphere;
}
}
void ComputeBoundingSphere()
{
2025-01-03 12:09:23 +00:00
if ( X_bakingTargetContainer.GetChildCount() == 0 )
{
_targetBoundingSphere = null;
return;
}
var firstChild = X_bakingTargetContainer.GetChild( 0 ) as Node3D;
if ( firstChild == null )
{
_targetBoundingSphere = null;
return;
}
if ( ! firstChild.Visible )
{
firstChild.Visible = true;
}
2025-04-08 09:07:02 +00:00
_targetBoundingBox = X_bakingTargetContainer.GetWorldBounds();
2025-01-03 12:09:23 +00:00
2025-04-08 09:07:02 +00:00
if ( autoTargetPivot )
{
Box3 box = target.GetWorldBounds();
targetPivot = new Vector3( 0, -box.center.Y, 0 );
}
2025-01-03 12:09:23 +00:00
2025-04-08 09:07:02 +00:00
_targetBoundingSphere = Sphere.ContainingBox( _targetBoundingBox );
2024-12-01 17:07:41 +00:00
}
2025-04-08 09:07:02 +00:00
float _cameraFOV = 0;
float _cameraDistance = 0;
float _outputScale = 1;
void ComputeCameraViewSettings()
2024-12-01 17:07:41 +00:00
{
2025-04-08 09:07:02 +00:00
var fovDistance = viewSettings.fovDistance;
var size = targetBoundingSphere.radius;
var computeScale = false;
if ( fovDistance is AutoDistance_BakingFDSettings ad )
2024-12-01 17:07:41 +00:00
{
2025-04-08 09:07:02 +00:00
size = ad.sizeEstimation == _XX_BakingFDSettings.SizeEstimationType.Bounding_Sphere ?
targetBoundingSphere.radius : ( _targetBoundingBox.size.Y / 2f );
computeScale = true;
}
2024-12-01 17:07:41 +00:00
2025-04-08 09:07:02 +00:00
if ( fovDistance is AutoFOVDistance_BakingFDSettings afd )
2024-12-01 17:07:41 +00:00
{
2025-04-08 09:07:02 +00:00
size = afd.sizeEstimation == _XX_BakingFDSettings.SizeEstimationType.Bounding_Sphere ?
targetBoundingSphere.radius : ( _targetBoundingBox.size.Y / 2f );
computeScale = true;
}
var fd = fovDistance.ComputeFOVDistance( size / 2f );
2024-12-01 17:07:41 +00:00
2025-04-08 09:07:02 +00:00
_cameraFOV = fd.X;
_cameraDistance = fd.Y;
_outputScale = computeScale ? Cameras.ComputeCameraFittingScale( _cameraFOV, _cameraDistance ) : 1;
}
public float GetCameraFOV()
{
return _cameraFOV;
2024-12-01 17:07:41 +00:00
}
2025-01-03 12:09:23 +00:00
public float GetCameraDistance()
2024-12-01 17:07:41 +00:00
{
2025-04-08 09:07:02 +00:00
return _cameraDistance;
2024-12-01 17:07:41 +00:00
}
2025-01-03 12:09:23 +00:00
public float GetOutputScale()
2024-12-01 17:07:41 +00:00
{
2025-04-08 09:07:02 +00:00
return _outputScale;
2024-12-01 17:07:41 +00:00
}
}
}