Files
HauntedBloodlines/Assets/HTraceWSGI/Scripts/Infrastructure/PassService.cs
2025-05-29 22:31:40 +03:00

112 lines
3.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using HTraceWSGI.Scripts.Passes;
using HTraceWSGI.Scripts.Pipeline;
using UnityEngine;
using UnityEngine.Rendering.HighDefinition;
using Object = UnityEngine.Object;
namespace HTraceWSGI.Scripts.Infrastructure
{
internal class PassService
{
private readonly Dictionary<Type, CustomPassObject> _customPasses = new Dictionary<Type, CustomPassObject>();
private readonly Dictionary<Type, CustomPass> _customPassesImplementations;
private readonly HTraceWSGI _hTraceWsgi;
public PassService(HTraceWSGI hTraceWsgi)
{
_hTraceWsgi = hTraceWsgi;
_customPassesImplementations = new Dictionary<Type, CustomPass>()
{
[typeof(HTracePrePass)] = new HTracePrePass() {enabled = true},
[typeof(HTraceMainPass)] = new HTraceMainPass() {enabled = true},
[typeof(HTraceFinalPass)] = new HTraceFinalPass() {enabled = true},
[typeof(VoxelizationPassConstant)] = new VoxelizationPassConstant() {enabled = true},
[typeof(VoxelizationPassPartial)] = new VoxelizationPassPartial() {enabled = true},
};
}
/// <summary>
/// It's not flexible, only for HTrace
/// </summary>
/// <param name="injectionPoint"></param>
/// <param name="passName"></param>
/// <param name="priority"></param>
/// <param name="passes"></param>
/// <typeparam name="K"></typeparam>
/// <returns></returns>
public CustomPassObject GetOrCreateCustomPassObject<K>(CustomPassInjectionPoint injectionPoint, string passName, int priority = 0, int layer = 0, params Type[] passes)
where K : PassHandler
{
for (int index = 0; index < passes.Length; index++)
{
if (_customPasses.TryGetValue(passes[index], out var implementation))
return implementation;
}
GameObject passGO = new GameObject(passName);
passGO.layer = layer;
passGO.hideFlags = HResources.DebugData.ShowBowels ? HideFlags.None : HideFlags.HideInHierarchy;
PassHandler passHandler = null;
passHandler = passGO.AddComponent<K>();
CustomPassVolume volume = passGO.AddComponent<CustomPassVolume>();
volume.injectionPoint = injectionPoint;
volume.priority = priority;
CustomPass[] customPasses = new CustomPass[passes.Length];
for (int index = 0; index < customPasses.Length; index++)
{
customPasses[index] = _customPassesImplementations[passes[index]];
}
CustomPassObject passObject = new CustomPassObject(passHandler, volume, customPasses);
for (int index = 0; index < passes.Length; index++)
{
volume.customPasses.Add(customPasses[index]);
_customPasses.Add(passes[index], passObject);
}
passHandler.Initialize(_hTraceWsgi, _hTraceWsgi.transform, passObject);
return passObject;
}
public void DeletePass<T>()
{
if (_customPasses.TryGetValue(typeof(T), out CustomPassObject customPass))
{
Object.DestroyImmediate(customPass.Handler.gameObject);
_customPasses.Remove(typeof(T));
}
}
public bool CustomPassObjectContains(CustomPassObject customPassObject)
{
if (_customPasses.Values.Contains(customPassObject))
return true;
return false;
}
public void Cleanup()
{
while (_customPasses.Any())
{
var keyValuePair = _customPasses.First();
if (_customPasses[keyValuePair.Key].Handler != null)
Object.DestroyImmediate(_customPasses[keyValuePair.Key].Handler.gameObject);
_customPasses.Remove(keyValuePair.Key);
}
_customPasses.Clear();
_customPassesImplementations.Clear();
}
}
}