using System.Collections; using System.Collections.Generic; using Godot; using System; using System.Threading.Tasks; namespace Rokojori { [Tool] [GlobalClass] public partial class NormalsBakingPass: _XX_BakingPass { public NormalsBakingPass():base(){} protected override void CreateBakingOutputs() { bakingOutputs = [ BakingOutput.Normals() ]; } protected override bool KeepsOriginalState() { return false; } protected override bool KeepsCompositors() { return true; } protected override async Task _Bake() { Clear( BakingTargetType.Normals ); SetNormalMaterial(); await multiBaker.RequestNextFrame(); var texture = await GrabDilatedTexture( false, false ); Set( BakingTargetType.Normals, texture ); return; } public void SetNormalMaterial() { Dictionary materials = new Dictionary(); Nodes.ForEach( multiBaker.X_bakingTargetContainer, ( n )=> { SetMaterial( n, materials ); } ); } public static readonly CachedResource shader = new CachedResource( "res://addons/rokojori_action_library/External/Imposter/materials/normal_baker.gdshader" ); void SetMaterial( Node3D n, Dictionary materials ) { var material = Materials.Get( n ); if ( material == null ) { n.LogInfo( "No material found" ); return; } if ( ! materials.ContainsKey( material ) ) { var appliedMaterial = new ShaderMaterial(); appliedMaterial.Shader = shader.Get(); appliedMaterial.SetShaderParameter( "use_alpha_texture", true ); if ( material is StandardMaterial3D sm ) { n.LogInfo( "StandardMaterial3D found" ); appliedMaterial.SetShaderParameter( "use_normalmap", sm.NormalTexture != null ); appliedMaterial.SetShaderParameter( "normal_texture", sm.NormalTexture ); appliedMaterial.SetShaderParameter( "normal_scale", sm.NormalScale ); appliedMaterial.SetShaderParameter( "albedo_texture", sm.AlbedoTexture ); appliedMaterial.SetShaderParameter( "albedo_color", sm.AlbedoColor ); appliedMaterial.SetShaderParameter( "alpha_scissor_threshold", sm.AlphaScissorThreshold ); appliedMaterial.SetShaderParameter( "uv1_scale", sm.Uv1Scale ); appliedMaterial.SetShaderParameter( "uv1_offset", sm.Uv1Offset ); appliedMaterial.SetShaderParameter( "uncompressNormalMap", false ); appliedMaterial.SetShaderParameter( "useCustomNormals", true ); } else { n.LogInfo( "No StandardMaterial3D found", material.GetType().Name ); } materials[ material ] = appliedMaterial; } Materials.Set( n, materials[ material ] ); } } }