rj-action-library/Runtime/Rendering/RenderingManager.cs

173 lines
3.8 KiB
C#
Raw Normal View History

2025-08-31 06:05:39 +00:00
using System.Diagnostics;
2024-12-01 17:07:41 +00:00
using System.Collections;
using System.Collections.Generic;
2025-08-31 06:05:39 +00:00
using System;
using Godot;
2025-10-24 11:38:51 +00:00
using System.Linq;
2025-08-31 06:05:39 +00:00
2024-12-01 17:07:41 +00:00
namespace Rokojori
{
2025-10-24 11:38:51 +00:00
[Tool]
2025-09-21 07:35:17 +00:00
[GlobalClass, Icon("res://addons/rokojori_action_library/Icons/RenderingManager.svg") ]
2025-08-31 06:05:39 +00:00
public partial class RenderingManager:Node
{
2025-10-24 11:38:51 +00:00
[ExportToolButton( "Ensure Global Shader Properties")]
public Callable ensureGlobalShaderPropertiesButton => Callable.From(
()=>
{
EnsureGlobalShaderProperties();
}
);
void EnsureGlobalShaderProperties()
{
2025-12-11 14:47:57 +00:00
var globalProperties = RenderingServer.GlobalShaderParameterGetList();
var map = new HashSet<string>();
foreach ( var g in globalProperties )
{
map.Add( g );
}
2025-10-24 11:38:51 +00:00
for ( int i = 0; i < data.numGlobalShaderProperties; i++ )
{
var p = data.GetGlobalShaderPropertyAt( i );
2025-12-10 14:17:07 +00:00
this.LogInfo( "Prop:", i );
2025-10-24 11:38:51 +00:00
2025-12-11 14:47:57 +00:00
if ( p == null || map.Contains( p.GetPropertyName().propertyName ) )
2025-10-24 11:38:51 +00:00
{
continue;
2025-12-10 14:17:07 +00:00
}
2025-12-11 14:47:57 +00:00
2025-12-10 14:17:07 +00:00
this.LogInfo( i, ">>", p.GetPropertyName().propertyName );
2025-10-24 11:38:51 +00:00
p.AddAsGlobalUniform();
}
}
2025-12-10 14:17:07 +00:00
public T GetShaderPropertyByName<T>( ShaderPropertyName name ) where T:ShaderProperty
{
if ( data == null )
{
return null;
}
if ( data.globalShaderPropertyBlocks != null )
{
foreach ( var b in data.globalShaderPropertyBlocks )
{
var p = GetShaderPropertyByName( b.shaderProperties, name );
if ( p != null )
{
return p as T;
}
}
}
return GetShaderPropertyByName( data.globalShaderProperties, name ) as T;
}
public ShaderProperty GetShaderPropertyByName( ShaderProperty[] properties, ShaderPropertyName name )
{
if ( properties == null )
{
return null;
}
return properties.Find( p => p.GetPropertyName().propertyName == name.propertyName );
}
2025-10-24 11:38:51 +00:00
2024-12-01 17:07:41 +00:00
[Export]
2025-10-24 11:38:51 +00:00
public bool ensureGlobalShaderPropertiesOnReady = true;
[Export]
public RenderingManagerData data = new RenderingManagerData();
List<AssignedShaderProperty> _assignedProperties = new List<AssignedShaderProperty>();
public override void _Ready()
{
if ( ! ensureGlobalShaderPropertiesOnReady )
{
return;
}
EnsureGlobalShaderProperties();
}
public override void _Process( double delta )
{
UpdateProperties();
}
void UpdateProperties()
{
if ( data == null )
{
return;
}
if ( _assignedProperties.Count != data.numGlobalShaderProperties )
{
CreatePropertyList();
}
for ( int i = 0; i < data.numGlobalShaderProperties; i++ )
{
var p = data.GetGlobalShaderPropertyAt( i );
if ( p != _assignedProperties[ i ].property )
{
_assignedProperties.Clear();
CreatePropertyList();
UpdateProperties();
return;
}
_assignedProperties[ i ].Update();
}
}
void CreatePropertyList()
{
_assignedProperties.Clear();
for ( int i = 0; i < data.numGlobalShaderProperties; i++ )
{
_assignedProperties.Add( AssignedShaderProperty.From( data.GetGlobalShaderPropertyAt( i ) ) );
}
}
2025-09-17 08:25:03 +00:00
public static int GetStencilReferenceIndex( StencilLayer stencilLayer )
{
if ( stencilLayer == null )
{
return -1;
}
var rm = Unique<RenderingManager>.Get();
if ( rm == null )
{
return stencilLayer.fallBackIndex;
}
var rmIndex = rm.data.stencilLayers.IndexOf( stencilLayer );
return rmIndex == -1 ? stencilLayer.fallBackIndex : ( rmIndex + 1 );
}
2024-12-01 17:07:41 +00:00
}
}