168 lines
3.4 KiB
C#
168 lines
3.4 KiB
C#
//pipelinedefine
|
|
#define H_URP
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using HTraceWSGI.Scripts.Extensions;
|
|
using HTraceWSGI.Scripts.Globals;
|
|
using UnityEngine;
|
|
using UnityEngine.Experimental.Rendering;
|
|
using UnityEngine.Rendering;
|
|
|
|
namespace HTraceWSGI.Scripts.Services.LightsCluster
|
|
{
|
|
internal sealed class LightService : IService
|
|
{
|
|
internal struct LightInfo
|
|
{
|
|
public Vector3 Position;
|
|
}
|
|
|
|
private static readonly int _MaxCookieResolution = Shader.PropertyToID("_MaxCookieResolution");
|
|
private static readonly int _LightPointCookies_Output = Shader.PropertyToID("_LightPointCookies_Output");
|
|
private static readonly int _LightSpotCookies_Output = Shader.PropertyToID("_LightSpotCookies_Output");
|
|
|
|
private static LightService _instance;
|
|
|
|
public static LightService Instance
|
|
{
|
|
get
|
|
{
|
|
if (_instance == null)
|
|
_instance = new LightService();
|
|
return _instance;
|
|
}
|
|
}
|
|
|
|
private readonly Dictionary<Light, LightInfo> _lights = new Dictionary<Light, LightInfo>(10);
|
|
private readonly List<Light> _lightsToRemove = new List<Light>(20);
|
|
private readonly List<Light> _lightsToUpdate = new List<Light>(20);
|
|
|
|
public void Initialize()
|
|
{
|
|
|
|
HPunctualLight[] findObjectsByType = Object.FindObjectsByType<HPunctualLight>(FindObjectsSortMode.None);
|
|
foreach (var light in findObjectsByType)
|
|
{
|
|
if (light.enabled == true)
|
|
AddLight(light.Light);
|
|
}
|
|
}
|
|
|
|
public void AddLight(Light light)
|
|
{
|
|
if (light == null || !light.enabled)
|
|
return;
|
|
|
|
if (light.type != LightType.Point && light.type != LightType.Spot)
|
|
return;
|
|
|
|
if (_lights.ContainsKey(light))
|
|
return;
|
|
|
|
|
|
var lightInfo = new LightInfo
|
|
{
|
|
Position = light.transform.position,
|
|
};
|
|
|
|
_lights[light] = lightInfo;
|
|
}
|
|
|
|
public void RemoveLight(Light light)
|
|
{
|
|
if (object.ReferenceEquals(light, null))
|
|
return;
|
|
|
|
RemoveLightData(light);
|
|
}
|
|
|
|
private void RemoveLightData(Light light)
|
|
{
|
|
if (_lights.TryGetValue(light, out var info))
|
|
{
|
|
_lights.Remove(light);
|
|
}
|
|
}
|
|
|
|
public void LateUpdate()
|
|
{
|
|
_lightsToRemove.Clear();
|
|
|
|
foreach (var kvp in _lights)
|
|
{
|
|
var key = kvp.Key;
|
|
|
|
if (!key || !key.enabled || (key.type != LightType.Point && key.type != LightType.Spot))
|
|
{
|
|
_lightsToRemove.Add(key);
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < _lightsToRemove.Count; i++)
|
|
{
|
|
RemoveLightData(_lightsToRemove[i]);
|
|
}
|
|
|
|
_lightsToUpdate.Clear();
|
|
foreach (var kvp in _lights)
|
|
{
|
|
_lightsToUpdate.Add(kvp.Key);
|
|
}
|
|
|
|
for (int i = 0; i < _lightsToUpdate.Count; i++)
|
|
{
|
|
var key = _lightsToUpdate[i];
|
|
var lightInfo = _lights[key];
|
|
|
|
lightInfo.Position = key.transform.position;
|
|
_lights[key] = lightInfo;
|
|
}
|
|
|
|
//CleanupUnusedCookies();
|
|
|
|
}
|
|
|
|
public IEnumerable<LightInfo> GetLights(Camera camera)
|
|
{
|
|
if (camera == null)
|
|
yield break;
|
|
|
|
var cameraPosition = camera.transform.position;
|
|
|
|
foreach (var kvp in _lights)
|
|
{
|
|
var light = kvp.Key;
|
|
var lightInfo = kvp.Value;
|
|
|
|
if (!light || !light.enabled)
|
|
continue;
|
|
{
|
|
yield return lightInfo;
|
|
}
|
|
}
|
|
}
|
|
|
|
#if NONE
|
|
public IEnumerable<LightInfo> GetLights(Camera camera)
|
|
{
|
|
foreach (var kvp in _lights)
|
|
{
|
|
var light = kvp.Key;
|
|
var lightInfo = kvp.Value;
|
|
|
|
if (!light || !light.enabled)
|
|
continue;
|
|
|
|
yield return lightInfo;
|
|
}
|
|
}
|
|
#endif
|
|
|
|
|
|
public void Cleanup()
|
|
{
|
|
_lights.Clear();
|
|
}
|
|
}
|
|
}
|