ThomagicRenderer/Runtime/Scripts/InstanceRenderSettings.cs

61 lines
2.5 KiB
C#

using System;
using UnityEngine;
namespace Assets.ThoMagic.Renderer
{
public interface IInstanceRenderSettings
{
InstanceRenderSettings Settings { get; }
}
public struct InstanceRenderSettings
{
int hashCodeCache;
public bool Supported;
public bool Render;
public float RenderDistance;
public float ShadowDistance;
public bool Shadows;
public float DensityInDistance;
public Vector2 DensityInDistanceFalloff;
public static InstanceRenderSettings Default(Camera camera) => new InstanceRenderSettings()
{
Supported = true,
Render = true,
Shadows = true,
RenderDistance = camera.farClipPlane,
ShadowDistance = camera.farClipPlane,
DensityInDistance = 1f,
DensityInDistanceFalloff = Vector2.zero,
};
public void Merge(InstanceRenderSettings other)
{
Supported = Supported && other.Supported;
Render = Render && other.Render;
Shadows = Shadows && other.Shadows;
if (RenderDistance > 0.0f && other.RenderDistance > 0.0f)
RenderDistance = Mathf.Min(RenderDistance, other.RenderDistance);
else if (other.RenderDistance > 0.0f)
RenderDistance = other.RenderDistance;
if (ShadowDistance > 0.0f && other.ShadowDistance > 0.0f)
ShadowDistance = Mathf.Min(ShadowDistance, other.ShadowDistance);
else if (other.ShadowDistance > 0.0f)
ShadowDistance = other.ShadowDistance;
DensityInDistance = Mathf.Min(DensityInDistance, other.DensityInDistance);
DensityInDistanceFalloff.x = Mathf.Max(DensityInDistanceFalloff.x, other.DensityInDistanceFalloff.x);
DensityInDistanceFalloff.y = Mathf.Max(DensityInDistanceFalloff.y, other.DensityInDistanceFalloff.y);
hashCodeCache = HashCode.Combine(Render, Shadows, RenderDistance, ShadowDistance, DensityInDistance, DensityInDistanceFalloff.x, DensityInDistanceFalloff.y);
}
public override int GetHashCode()
{
if(hashCodeCache == 0 || (Application.isEditor && !Application.isPlaying))
hashCodeCache = HashCode.Combine(Render, Shadows, RenderDistance, ShadowDistance, DensityInDistance, DensityInDistanceFalloff.x, DensityInDistanceFalloff.y);
return hashCodeCache;
}
}
}