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 _customPasses = new Dictionary(); private readonly Dictionary _customPassesImplementations; private readonly HTraceWSGI _hTraceWsgi; public PassService(HTraceWSGI hTraceWsgi) { _hTraceWsgi = hTraceWsgi; _customPassesImplementations = new Dictionary() { [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}, }; } /// /// It's not flexible, only for HTrace /// /// /// /// /// /// /// public CustomPassObject GetOrCreateCustomPassObject(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(); CustomPassVolume volume = passGO.AddComponent(); 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() { 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(); } } }