GodotComponentTest/utils/DebugGeometry.cs

92 lines
3.7 KiB
C#
Raw Permalink Normal View History

using System.Collections.Generic;
using Godot;
namespace GodotComponentTest.utils;
2023-11-18 22:32:57 +01:00
public class DebugGeometry : Spatial {
private ImmediateGeometry _immediateGeometry;
private List<Transform> _transformStack;
2023-11-18 22:32:57 +01:00
private Transform _currentTransform = Transform.Identity;
2023-11-18 22:32:57 +01:00
public override void _Ready() {
base._Ready();
2023-11-18 22:32:57 +01:00
_immediateGeometry = (ImmediateGeometry)FindNode("ImmediateGeometry");
2023-11-18 22:32:57 +01:00
Clear();
}
2023-11-18 22:32:57 +01:00
public void Clear() {
_immediateGeometry.Clear();
_transformStack = new List<Transform>();
_transformStack.Add(Transform.Identity);
}
2023-11-18 22:32:57 +01:00
public void PushTransform(Transform transform) {
_transformStack.Add(transform);
_currentTransform = transform;
}
2023-11-18 22:32:57 +01:00
public void PushTranslated(Vector3 offset) {
PushTransform(_currentTransform.Translated(offset));
}
2023-11-18 22:32:57 +01:00
public void PopTransform() {
_transformStack.RemoveAt(_transformStack.Count - 1);
_currentTransform = PeekTransform();
}
2023-11-18 22:32:57 +01:00
public Transform PeekTransform() {
return _transformStack[_transformStack.Count - 1];
}
2023-11-18 22:32:57 +01:00
public void Begin(Mesh.PrimitiveType primitive, Texture texture = null) {
_immediateGeometry.Begin(primitive, texture);
}
2023-11-18 22:32:57 +01:00
public void End() {
_immediateGeometry.End();
}
2023-11-18 22:32:57 +01:00
public void AddVertex(Vector3 vertex) {
_immediateGeometry.AddVertex(_currentTransform.Xform(vertex));
}
2023-11-18 22:32:57 +01:00
public void SetColor(Color color) {
_immediateGeometry.SetColor(color);
}
2023-11-18 22:32:57 +01:00
public void AddBox(Vector3 extents) {
Transform currentTransform = PeekTransform();
// bottom square
AddVertex(new Vector3(extents.x * -0.5f, extents.y * -0.5f, extents.z * -0.5f));
AddVertex(new Vector3(extents.x * -0.5f, extents.y * -0.5f, extents.z * 0.5f));
AddVertex(new Vector3(extents.x * -0.5f, extents.y * -0.5f, extents.z * 0.5f));
AddVertex(new Vector3(extents.x * 0.5f, extents.y * -0.5f, extents.z * 0.5f));
AddVertex(new Vector3(extents.x * 0.5f, extents.y * -0.5f, extents.z * 0.5f));
AddVertex(new Vector3(extents.x * 0.5f, extents.y * -0.5f, extents.z * -0.5f));
AddVertex(new Vector3(extents.x * 0.5f, extents.y * -0.5f, extents.z * -0.5f));
AddVertex(new Vector3(extents.x * -0.5f, extents.y * -0.5f, extents.z * -0.5f));
2023-11-18 22:32:57 +01:00
// top square
AddVertex(new Vector3(extents.x * -0.5f, extents.y * 0.5f, extents.z * -0.5f));
AddVertex(new Vector3(extents.x * -0.5f, extents.y * 0.5f, extents.z * 0.5f));
AddVertex(new Vector3(extents.x * -0.5f, extents.y * 0.5f, extents.z * 0.5f));
AddVertex(new Vector3(extents.x * 0.5f, extents.y * 0.5f, extents.z * 0.5f));
AddVertex(new Vector3(extents.x * 0.5f, extents.y * 0.5f, extents.z * 0.5f));
AddVertex(new Vector3(extents.x * 0.5f, extents.y * 0.5f, extents.z * -0.5f));
AddVertex(new Vector3(extents.x * 0.5f, extents.y * 0.5f, extents.z * -0.5f));
AddVertex(new Vector3(extents.x * -0.5f, extents.y * 0.5f, extents.z * -0.5f));
2023-11-18 22:32:57 +01:00
// side
AddVertex(new Vector3(extents.x * -0.5f, extents.y * 0.5f, extents.z * -0.5f));
AddVertex(new Vector3(extents.x * -0.5f, extents.y * -0.5f, extents.z * -0.5f));
AddVertex(new Vector3(extents.x * -0.5f, extents.y * 0.5f, extents.z * 0.5f));
2023-11-18 22:32:57 +01:00
AddVertex(new Vector3(extents.x * -0.5f, extents.y * -0.5f, extents.z * 0.5f));
AddVertex(new Vector3(extents.x * 0.5f, extents.y * 0.5f, extents.z * 0.5f));
AddVertex(new Vector3(extents.x * 0.5f, extents.y * -0.5f, extents.z * 0.5f));
AddVertex(new Vector3(extents.x * 0.5f, extents.y * 0.5f, extents.z * -0.5f));
2023-11-18 22:32:57 +01:00
AddVertex(new Vector3(extents.x * 0.5f, extents.y * -0.5f, extents.z * -0.5f));
}
}