summaryrefslogtreecommitdiff
path: root/DeepSky.Haze/DS_HazeZone.cs
blob: b4ffabbb920a1f8fad6f5ce9e5bf9161eb20514c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using UnityEngine;

namespace DeepSky.Haze;

[ExecuteInEditMode]
[AddComponentMenu("DeepSky Haze/Zone", 52)]
public class DS_HazeZone : MonoBehaviour
{
	[SerializeField]
	private DS_HazeContext m_Context = new DS_HazeContext();

	[SerializeField]
	[Range(0f, 250f)]
	private int m_Priority;

	[SerializeField]
	[Range(0.001f, 1f)]
	private float m_BlendRange = 0.1f;

	private Bounds m_AABB;

	private float m_BlendRangeInverse;

	public DS_HazeContext Context => m_Context;

	public int Priority
	{
		get
		{
			return m_Priority;
		}
		set
		{
			m_Priority = ((value > 0) ? value : 0);
		}
	}

	public float BlendRange
	{
		get
		{
			return m_BlendRange;
		}
		set
		{
			m_BlendRange = Mathf.Clamp01(value);
		}
	}

	private void Setup()
	{
		m_AABB = new Bounds(Vector3.zero, base.transform.localScale);
		m_BlendRangeInverse = 1f / Mathf.Max(Mathf.Min(m_AABB.extents.x, m_AABB.extents.y, m_AABB.extents.z) * m_BlendRange, Mathf.Epsilon);
	}

	private void Start()
	{
		Setup();
	}

	private void OnValidate()
	{
		Setup();
	}

	public bool Contains(Vector3 position)
	{
		if (base.transform.hasChanged)
		{
			Setup();
		}
		Vector3 point = base.transform.InverseTransformPoint(position);
		point.Scale(base.transform.localScale);
		return m_AABB.Contains(point);
	}

	public float GetBlendWeight(Vector3 position)
	{
		Vector3 vector = base.transform.InverseTransformPoint(position);
		vector.Scale(base.transform.localScale);
		float num = Mathf.Abs(m_AABB.extents.x - Mathf.Abs(vector.x));
		float num2 = Mathf.Abs(m_AABB.extents.y - Mathf.Abs(vector.y));
		float num3 = Mathf.Abs(m_AABB.extents.z - Mathf.Abs(vector.z));
		float num4 = Mathf.Min(num, num2, num3);
		return Mathf.Clamp01(num4 * m_BlendRangeInverse);
	}

	public static bool operator >(DS_HazeZone c1, DS_HazeZone c2)
	{
		if (c1.m_Priority == c2.m_Priority)
		{
			return (Vector3.Dot(c1.m_AABB.extents, c1.m_AABB.extents) > Vector3.Dot(c2.m_AABB.extents, c2.m_AABB.extents)) ? true : false;
		}
		return (c1.m_Priority > c2.m_Priority) ? true : false;
	}

	public static bool operator <(DS_HazeZone c1, DS_HazeZone c2)
	{
		if (c1.m_Priority == c2.m_Priority)
		{
			return (Vector3.Dot(c1.m_AABB.extents, c1.m_AABB.extents) < Vector3.Dot(c2.m_AABB.extents, c2.m_AABB.extents)) ? true : false;
		}
		return (c1.m_Priority < c2.m_Priority) ? true : false;
	}
}