rj-action-library/Runtime/Shading/Generators/Spatial/SpatialShaderGenerator.cs

153 lines
3.6 KiB
C#
Raw Normal View History

using Godot;
using System.Reflection;
using System.Collections.Generic;
namespace Rokojori
{
[Tool]
[GlobalClass]
public partial class SpatialShaderGenerator:ShaderGenerator
{
[Export]
2025-09-17 08:25:03 +00:00
public SpatialShaderData data = new SpatialShaderData();
public override List<ShaderPhase> GetPhases()
{
return new List<ShaderPhase>()
{
ShaderPhase.Header,
ShaderPhase.RenderMode,
ShaderPhase.Includes,
ShaderPhase.Variables,
ShaderPhase.Functions,
ShaderPhase.Vertex,
ShaderPhase.Fragment,
ShaderPhase.Light
};
}
2025-09-17 08:25:03 +00:00
public string RokojoriInfo()
{
var allModules = GetAllModules();
var hashValues = allModules.Map( a => a.GetShaderCacheHash() );
hashValues.Sort();
var hash = hashValues.Join( "-" );
var allInfos = allModules.Map( a => a.GetShaderCacheData() );
return $"// Rokojori Shader @{hash}: \n/*\n" + allInfos.Join( "\n" ) + "\n*/\n";
}
List<ShaderGenerationModule> GetAllModules()
{
return GetHeaderModules().Concat( GetMainModules() );
}
List<ShaderGenerationModule> GetHeaderModules()
{
List<ShaderGenerationModule> list = [
data.transparency,
data.shading,
data.stencil
];
return list.FilterNulls();
}
List<ShaderGenerationModule> GetMainModules()
{
List<ShaderGenerationModule> list = [
data.geometry,
data.uv,
data.albedo,
data.alpha,
data.fading,
data.normalMap,
data.occlusion,
data.roughness,
data.metallic,
data.specular,
data.emission,
data.backlight,
data.subsurfaceScattering
];
return list.FilterNulls();
}
public override List<ShaderGenerationModule> GetShaderGenerationModules( ShaderPhase phase )
{
if ( ShaderPhase.Header == phase )
{
2025-09-17 08:25:03 +00:00
var info = RokojoriInfo();
return
[
2025-09-17 08:25:03 +00:00
new ShaderRawModule( "\n\nshader_type spatial;\n" ),
new ShaderRawModule( $"\n{info}\n")
];
}
if ( ShaderPhase.RenderMode == phase )
{
2025-09-17 08:25:03 +00:00
List<ShaderGenerationModule> modules =
[
new ShaderRawModule( "render_mode " ),
2025-09-17 08:25:03 +00:00
data.transparency,
new ShaderRawModule( ", " ),
2025-09-17 08:25:03 +00:00
data.shading,
new ShaderRawModule( ";\n" ),
];
2025-09-17 08:25:03 +00:00
if ( data.stencil != null )
{
modules.Add( new ShaderRawModule( data.stencil.ToShaderCode() + "\n" ) );
}
modules.Add( new ShaderRawModule( "\n" ) );
return modules;
}
2025-09-17 08:25:03 +00:00
var mainModules = GetMainModules();
if ( ShaderPhase.Includes == phase || ShaderPhase.Variables == phase || ShaderPhase.Functions == phase )
{
2025-09-17 08:25:03 +00:00
List<ShaderGenerationModule> preModules = [
data.transparency,
data.shading ]
;
return preModules.Concat( mainModules );
}
if ( ShaderPhase.Vertex == phase )
{
return new List<ShaderGenerationModule>()
{
new ShaderRawModule( "\nvoid vertex()\n{\n\n" )
}
2025-09-17 08:25:03 +00:00
.Concat( mainModules )
.Concat
(
new ShaderRawModule( "\n}\n" )
);
}
if ( ShaderPhase.Fragment == phase )
{
return new List<ShaderGenerationModule>()
{
new ShaderRawModule( "\nvoid fragment()\n{\n\n" )
}
2025-09-17 08:25:03 +00:00
.Concat( mainModules )
.Concat
(
new ShaderRawModule( "\n}\n" )
);
}
return null;
}
}
}