2021-08-12 22:34:00 +03:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace Terkoiz.MapZoneHighlighter.BoundedBoxes
|
|
|
|
{
|
|
|
|
public class ColliderBoundsController : MonoBehaviour
|
|
|
|
{
|
|
|
|
public bool IsActive;
|
|
|
|
|
|
|
|
public static readonly Color InactiveLineColor = new Color(1f, 0f, 0.8f, 0.8f);
|
|
|
|
public static readonly Color ActiveLineColor = new Color(0f, 1f, 0f, 0.8f);
|
|
|
|
|
|
|
|
private Bounds _bound;
|
|
|
|
private Vector3[] _corners;
|
|
|
|
private Vector3[,] _lines;
|
|
|
|
private Quaternion _quat;
|
|
|
|
|
|
|
|
private Camera _mainCamera;
|
|
|
|
private ColliderBoundsRenderer _cameraLines;
|
|
|
|
|
|
|
|
private Vector3 _topFrontLeft;
|
|
|
|
private Vector3 _topFrontRight;
|
|
|
|
private Vector3 _topBackLeft;
|
|
|
|
private Vector3 _topBackRight;
|
|
|
|
private Vector3 _bottomFrontLeft;
|
|
|
|
private Vector3 _bottomFrontRight;
|
|
|
|
private Vector3 _bottomBackLeft;
|
|
|
|
private Vector3 _bottomBackRight;
|
|
|
|
|
|
|
|
public Vector3 ScreenPosition;
|
|
|
|
public bool IsOnScreen;
|
|
|
|
public float DistanceToCamera;
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
{
|
|
|
|
_mainCamera = Camera.main;
|
|
|
|
_cameraLines = _mainCamera.GetComponent<ColliderBoundsRenderer>();
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Init()
|
|
|
|
{
|
|
|
|
CalculateBounds();
|
|
|
|
SetPoints();
|
|
|
|
SetLines();
|
|
|
|
_cameraLines.SetOutlines(_lines, InactiveLineColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void LateUpdate()
|
|
|
|
{
|
|
|
|
_cameraLines.SetOutlines(_lines, IsActive ? ActiveLineColor : InactiveLineColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnGUI()
|
|
|
|
{
|
2021-09-14 20:59:43 +03:00
|
|
|
if (Camera.main == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-12 22:34:00 +03:00
|
|
|
ScreenPosition = UIDrawingUtil.WorldPointToScreenPoint(transform.position);
|
|
|
|
IsOnScreen = UIDrawingUtil.IsScreenPointVisible(ScreenPosition);
|
|
|
|
DistanceToCamera = Vector3.Distance(Camera.main.transform.position, transform.position);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void CalculateBounds()
|
|
|
|
{
|
|
|
|
BoxCollider coll = GetComponent<BoxCollider>();
|
|
|
|
if (coll)
|
|
|
|
{
|
|
|
|
_bound = coll.bounds;
|
|
|
|
_quat = Quaternion.Euler(0f, coll.transform.rotation.y, 0f);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Debug.LogError($"[{nameof(ColliderBoundsController)}] No collider attached to '{name}'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SetPoints()
|
|
|
|
{
|
|
|
|
Vector3 bc = transform.position + _quat * (_bound.center - transform.position);
|
|
|
|
|
|
|
|
_topFrontRight = bc + _quat * Vector3.Scale(_bound.extents, new Vector3(1, 1, 1));
|
|
|
|
_topFrontLeft = bc + _quat * Vector3.Scale(_bound.extents, new Vector3(-1, 1, 1));
|
|
|
|
_topBackLeft = bc + _quat * Vector3.Scale(_bound.extents, new Vector3(-1, 1, -1));
|
|
|
|
_topBackRight = bc + _quat * Vector3.Scale(_bound.extents, new Vector3(1, 1, -1));
|
|
|
|
_bottomFrontRight = bc + _quat * Vector3.Scale(_bound.extents, new Vector3(1, -1, 1));
|
|
|
|
_bottomFrontLeft = bc + _quat * Vector3.Scale(_bound.extents, new Vector3(-1, -1, 1));
|
|
|
|
_bottomBackLeft = bc + _quat * Vector3.Scale(_bound.extents, new Vector3(-1, -1, -1));
|
|
|
|
_bottomBackRight = bc + _quat * Vector3.Scale(_bound.extents, new Vector3(1, -1, -1));
|
|
|
|
_corners = new Vector3[]
|
|
|
|
{
|
|
|
|
_topFrontRight, _topFrontLeft, _topBackLeft, _topBackRight, _bottomFrontRight, _bottomFrontLeft, _bottomBackLeft,
|
|
|
|
_bottomBackRight
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SetLines()
|
|
|
|
{
|
|
|
|
const int linesCount = 12;
|
|
|
|
|
|
|
|
_lines = new Vector3[linesCount, 2];
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
var i1 = (i + 1) % 4;
|
|
|
|
_lines[i, 0] = _corners[i];
|
|
|
|
_lines[i, 1] = _corners[i1];
|
|
|
|
i1 = i + 4; //vertical lines
|
|
|
|
_lines[i + 4, 0] = _corners[i];
|
|
|
|
_lines[i + 4, 1] = _corners[i1];
|
|
|
|
//bottom rectangle
|
|
|
|
_lines[i + 8, 0] = _corners[i1];
|
|
|
|
i1 = 4 + (i + 1) % 4;
|
|
|
|
_lines[i + 8, 1] = _corners[i1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|