Added a DebugDraw3D class, which currently draws an outline of a mesh's AABB.
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* ©2026 Batty Bovine Productions, LLC. All Rights Reserved.
|
||||
*
|
||||
* If this source code makes it to the public internet, this software can be
|
||||
* considered to be protected by the MIT licence. Have fun with it.
|
||||
*/
|
||||
|
||||
#include "debug_draw3d.h"
|
||||
|
||||
#include "godot_cpp/variant/aabb.hpp"
|
||||
#include "godot_cpp/classes/immediate_mesh.hpp"
|
||||
#include "godot_cpp/classes/mesh_instance3d.hpp"
|
||||
#include "godot_cpp/classes/standard_material3d.hpp"
|
||||
using namespace godot;
|
||||
|
||||
|
||||
void DebugDraw3D::draw_aabb(Node3D *parent_node, const AABB &aabb, const Color &colour)
|
||||
{
|
||||
Ref<StandardMaterial3D> material;
|
||||
material.instantiate();
|
||||
material->set_flag(BaseMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
|
||||
material->set_shading_mode(BaseMaterial3D::SHADING_MODE_UNSHADED);
|
||||
|
||||
ImmediateMesh *im = memnew(ImmediateMesh);
|
||||
const Vector3 &aabb_position = aabb.get_position();
|
||||
const Vector3 &aabb_size = aabb.get_size();
|
||||
im->surface_begin(Mesh::PRIMITIVE_LINES, material);
|
||||
im->surface_set_color(colour);
|
||||
{
|
||||
im->surface_add_vertex(aabb_position);
|
||||
im->surface_add_vertex(aabb_position + Vector3(aabb_size.x, 0.0f, 0.0f));
|
||||
|
||||
im->surface_add_vertex(aabb_position);
|
||||
im->surface_add_vertex(aabb_position + Vector3(0.0f, aabb_size.y, 0.0f));
|
||||
|
||||
im->surface_add_vertex(aabb_position + Vector3(aabb_size.x, 0.0f, 0.0f));
|
||||
im->surface_add_vertex(aabb_position + Vector3(aabb_size.x, aabb_size.y, 0.0f));
|
||||
|
||||
im->surface_add_vertex(aabb_position + Vector3(aabb_size.x, aabb_size.y, 0.0f));
|
||||
im->surface_add_vertex(aabb_position + Vector3(0.0f, aabb_size.y, 0.0f));
|
||||
|
||||
im->surface_add_vertex(aabb_position + Vector3(0.0f, 0.0f, aabb_size.z));
|
||||
im->surface_add_vertex(aabb_position + Vector3(aabb_size.x, 0.0f, aabb_size.z));
|
||||
|
||||
im->surface_add_vertex(aabb_position + Vector3(0.0f, 0.0f, aabb_size.z));
|
||||
im->surface_add_vertex(aabb_position + Vector3(0.0f, aabb_size.y, aabb_size.z));
|
||||
|
||||
im->surface_add_vertex(aabb_position + Vector3(aabb_size.x, 0.0f, aabb_size.z));
|
||||
im->surface_add_vertex(aabb_position + Vector3(aabb_size.x, aabb_size.y, aabb_size.z));
|
||||
|
||||
im->surface_add_vertex(aabb_position + Vector3(aabb_size.x, aabb_size.y, aabb_size.z));
|
||||
im->surface_add_vertex(aabb_position + Vector3(0.0f, aabb_size.y, aabb_size.z));
|
||||
|
||||
im->surface_add_vertex(aabb_position);
|
||||
im->surface_add_vertex(aabb_position + Vector3(0.0f, 0.0f, aabb_size.z));
|
||||
|
||||
im->surface_add_vertex(aabb_position + Vector3(aabb_size.x, 0.0f, 0.0f));
|
||||
im->surface_add_vertex(aabb_position + Vector3(aabb_size.x, 0.0f, aabb_size.z));
|
||||
|
||||
im->surface_add_vertex(aabb_position + Vector3(0.0f, aabb_size.y, 0.0f));
|
||||
im->surface_add_vertex(aabb_position + Vector3(0.0f, aabb_size.y, aabb_size.z));
|
||||
|
||||
im->surface_add_vertex(aabb_position + Vector3(aabb_size.x, aabb_size.y, 0.0f));
|
||||
im->surface_add_vertex(aabb_position + Vector3(aabb_size.x, aabb_size.y, aabb_size.z));
|
||||
}
|
||||
im->surface_end();
|
||||
|
||||
MeshInstance3D *mi = memnew(MeshInstance3D);
|
||||
parent_node->add_child(mi);
|
||||
mi->set_transform(Transform3D());
|
||||
mi->set_mesh(im);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* ©2026 Batty Bovine Productions, LLC. All Rights Reserved.
|
||||
*
|
||||
* If this source code makes it to the public internet, this software can be
|
||||
* considered to be protected by the MIT licence. Have fun with it.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <godot_cpp/classes/node3d.hpp>
|
||||
using namespace godot;
|
||||
|
||||
#define DEBUG_DRAW3D_TAG "DebugDraw3D"
|
||||
|
||||
|
||||
class DebugDraw3D : public Node
|
||||
{
|
||||
GDCLASS(DebugDraw3D, Node);
|
||||
|
||||
public:
|
||||
DebugDraw3D(){}
|
||||
~DebugDraw3D(){}
|
||||
|
||||
static void draw_aabb(Node3D *parent_node, const AABB &aabb, const Color &colour);
|
||||
|
||||
protected:
|
||||
|
||||
private:
|
||||
|
||||
// Godot boilerplate below
|
||||
protected:
|
||||
static void _bind_methods()
|
||||
{
|
||||
ClassDB::bind_static_method("DebugDraw3D", D_METHOD("draw_aabb", "parent_node", "aabb", "colour"), &DebugDraw3D::draw_aabb);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user