ManiaMap.Unity
Procedural generation of metroidvania style maps for Unity.
DoorThreshold.cs
1using UnityEngine;
2
4{
8 public class DoorThreshold : MonoBehaviour
9 {
10 [SerializeField] private Vector3 _size = Vector3.one;
14 public Vector3 Size { get => _size; set => _size = Vector3.Max(value, Vector3.zero); }
15
16 private void OnValidate()
17 {
18 Size = Size;
19 }
20
21 private void OnDrawGizmos()
22 {
23 var bounds = GetAABB();
24 var lineColor = Color.yellow;
25 var fillColor = new Color(lineColor.r, lineColor.g, lineColor.b, 0.2f);
26
27 Gizmos.color = fillColor;
28 Gizmos.DrawCube(bounds.center, bounds.size);
29 Gizmos.color = lineColor;
30 Gizmos.DrawWireCube(bounds.center, bounds.size);
31 }
32
36 public Bounds GetAABB()
37 {
38 var size = 0.5f * Size;
39 var min = new Vector3(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity);
40 var max = new Vector3(float.NegativeInfinity, float.NegativeInfinity, float.NegativeInfinity);
41
42 System.Span<Vector3> corners = stackalloc Vector3[]
43 {
44 new Vector3(-1, -1, -1),
45 new Vector3(1, -1, -1),
46 new Vector3(1, 1, -1),
47 new Vector3(-1, 1, -1),
48 new Vector3(-1, -1, 1),
49 new Vector3(1, -1, 1),
50 new Vector3(1, 1, 1),
51 new Vector3(-1, 1, 1),
52 };
53
54 for (int i = 0; i < corners.Length; i++)
55 {
56 var corner = corners[i];
57 corners[i] = new Vector3(corner.x * size.x, corner.y * size.y, corner.z * size.z);
58 }
59
60 transform.TransformVectors(corners);
61
62 foreach (var corner in corners)
63 {
64 min = Vector3.Min(min, corner);
65 max = Vector3.Max(max, corner);
66 }
67
68 return new Bounds(transform.position, max - min);
69 }
70
76 public Vector3 ParameterizePosition(Vector3 position)
77 {
78 var bounds = GetAABB();
79 var size = bounds.size;
80 var topLeft = bounds.center - 0.5f * size;
81 var delta = position - topLeft;
82 var x = size.x > 0 ? Mathf.Clamp(delta.x / size.x, 0, 1) : 0.5f;
83 var y = size.y > 0 ? Mathf.Clamp(delta.y / size.y, 0, 1) : 0.5f;
84 var z = size.z > 0 ? Mathf.Clamp(delta.z / size.z, 0, 1) : 0.5f;
85 return new Vector3(x, y, z);
86 }
87
93 public Vector3 InterpolatePosition(Vector3 parameters)
94 {
95 var bounds = GetAABB();
96 var size = bounds.size;
97 var topLeft = bounds.center - 0.5f * size;
98 var bottomRight = topLeft + size;
99
100 var x = Mathf.Lerp(topLeft.x, bottomRight.x, Mathf.Clamp(parameters.x, 0, 1));
101 var y = Mathf.Lerp(topLeft.y, bottomRight.y, Mathf.Clamp(parameters.y, 0, 1));
102 var z = Mathf.Lerp(topLeft.z, bottomRight.z, Mathf.Clamp(parameters.z, 0, 1));
103
104 return new Vector3(x, y, z);
105 }
106 }
107}
An interpolatable area, useful for locating characters moving between door thresholds.
Definition: DoorThreshold.cs:9
Vector3 InterpolatePosition(Vector3 parameters)
Returns the global position corresponding to the specified interpolation parameters....
Bounds GetAABB()
Returns the axis aligned bounding box for the threshold area.
Vector3 Size
The size of the threshold area.
Vector3 ParameterizePosition(Vector3 position)
Returns the interpolation parameters corresponding to the specified global position....