feat: implemented point primitive gizmo

This commit is contained in:
Sara Gerretsen 2025-11-18 00:04:00 +01:00
parent 60865b74c7
commit dfbe37a2e7
15 changed files with 330 additions and 60 deletions

View file

@ -0,0 +1,54 @@
#include "point_primitive_node.h"
#include "scene/3d/node_3d.h"
#include "terrain_editor/macros.h"
void PointPrimitiveNode::_bind_methods() {
BIND_GET_SET(primitive);
ClassDB::bind_method(D_METHOD("push_transform_changes"), &self_type::push_transform_changes);
}
void PointPrimitiveNode::on_underlying_changed() {
if (!this->pushing_change && this->is_inside_tree()) {
this->pushing_change = true;
Vector2 const center{ this->primitive->get_center() };
set_global_position({ center.x, this->primitive->get_height(), center.y });
this->pushing_change = false;
}
}
void PointPrimitiveNode::_notification(int what) {
switch (what) {
default:
return;
case NOTIFICATION_ENTER_TREE:
if (this->primitive.is_valid()) {
on_underlying_changed();
}
return;
}
}
void PointPrimitiveNode::push_transform_changes() {
if (this->primitive.is_valid()) {
this->pushing_change = true;
Vector3 const position{ get_global_position() };
this->primitive->set_center({ position.x, position.z });
this->primitive->set_height(position.y);
this->pushing_change = false;
}
}
void PointPrimitiveNode::set_primitive(Ref<PointPrimitive> primitive) {
if (this->primitive.is_valid()) {
this->primitive->disconnect_changed(underlying_changed_callable);
}
this->primitive = primitive;
if (this->primitive.is_valid()) {
primitive->connect_changed(underlying_changed_callable);
on_underlying_changed();
}
}
Ref<PointPrimitive> PointPrimitiveNode::get_primitive() const {
return this->primitive;
}

View file

@ -0,0 +1,23 @@
#pragma once
#include "scene/3d/node_3d.h"
#include "terrain_editor/terrain_primitive.h"
class PointPrimitiveNode : public Node3D {
GDCLASS(PointPrimitiveNode, Node3D);
static void _bind_methods();
void on_underlying_changed();
protected:
void _notification(int what);
public:
void push_transform_changes();
void set_primitive(Ref<PointPrimitive> primitive);
Ref<PointPrimitive> get_primitive() const;
private:
bool pushing_change{ false };
Ref<PointPrimitive> primitive{};
Callable underlying_changed_callable{ callable_mp(this, &self_type::on_underlying_changed) };
};

View file

@ -1,6 +1,8 @@
#include "register_types.h"
#include "core/object/class_db.h"
#include "terrain_editor/point_primitive_node.h"
#include "terrain_editor/terrain_mesh_editor.h"
#include "terrain_editor/terrain_mesh_generator.h"
#include "terrain_editor/terrain_primitive.h"
@ -14,6 +16,8 @@ void initialize_terrain_editor_module(ModuleInitializationLevel p_level) {
ClassDB::register_class<PointPrimitive>();
ClassDB::register_class<NoisePrimitive>();
ClassDB::register_class<ExpressionPrimitive>();
ClassDB::register_class<PointPrimitiveNode>();
ClassDB::register_class<TerrainMeshEditor>();
}
void uninitialize_terrain_editor_module(ModuleInitializationLevel p_level) {

View file

@ -0,0 +1,53 @@
#include "terrain_mesh_editor.h"
#include "scene/3d/node_3d.h"
#include "scene/resources/packed_scene.h"
#include "terrain_editor/macros.h"
#include "terrain_editor/point_primitive_node.h"
#include "terrain_editor/terrain_primitive.h"
void TerrainMeshEditor::_bind_methods() {
BIND_HPROPERTY(Variant::OBJECT, point_primitive_object, PROPERTY_HINT_RESOURCE_TYPE, "PackedScene");
}
void TerrainMeshEditor::ready() {
connect(sig_primitive_list_changed, callable_mp(this, &self_type::on_primitive_list_changed));
on_primitive_list_changed(get_primitives());
}
void TerrainMeshEditor::on_primitive_list_changed(Array primitives) {
for (Node3D *existing : this->primitive_nodes) {
existing->queue_free();
}
this->primitive_nodes.clear();
for (Variant var : primitives) {
if (this->point_primitive_object.is_valid() && this->point_primitive_object->get_state()->get_node_type(0) == PointPrimitiveNode::get_class_static()) {
Ref<PointPrimitive> point{ var };
if (point.is_valid()) {
PointPrimitiveNode *primitive_node{ cast_to<PointPrimitiveNode>(this->point_primitive_object->instantiate()) };
primitive_node->set_primitive(point);
this->add_child(primitive_node);
}
}
}
}
void TerrainMeshEditor::_notification(int what) {
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
switch (what) {
default:
return;
case NOTIFICATION_READY:
ready();
return;
}
}
void TerrainMeshEditor::set_point_primitive_object(Ref<PackedScene> scene) {
this->point_primitive_object = scene;
}
Ref<PackedScene> TerrainMeshEditor::get_point_primitive_object() const {
return this->point_primitive_object;
}

View file

@ -0,0 +1,20 @@
#pragma once
#include "terrain_editor/terrain_mesh_generator.h"
class TerrainMeshEditor : public TerrainMeshGenerator {
GDCLASS(TerrainMeshEditor, TerrainMeshGenerator);
static void _bind_methods();
void ready();
void on_primitive_list_changed(Array primitives);
void on_primitive_node_removed();
protected:
void _notification(int what);
void set_point_primitive_object(Ref<PackedScene> scene);
Ref<PackedScene> get_point_primitive_object() const;
private:
Vector<Node3D *> primitive_nodes{};
Ref<PackedScene> point_primitive_object{};
};

View file

@ -1,5 +1,4 @@
#include "terrain_mesh_generator.h"
#include "core/io/resource_saver.h"
#include "core/math/math_funcs.h"
#include "core/math/rect2.h"
#include "core/object/class_db.h"
@ -10,6 +9,7 @@
#include <limits>
String const TerrainMeshGenerator::sig_primitives_changed{ "primitives_changed" };
String const TerrainMeshGenerator::sig_primitive_list_changed{ "primitive_list_changed" };
void TerrainMeshGenerator::_bind_methods() {
BIND_HPROPERTY(Variant::ARRAY, primitives, PROPERTY_HINT_ARRAY_TYPE, vformat("%s/%s:TerrainPrimitive", Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE));
@ -17,6 +17,7 @@ void TerrainMeshGenerator::_bind_methods() {
BIND_PROPERTY(Variant::FLOAT, color_gradient_start_height);
BIND_PROPERTY(Variant::FLOAT, color_gradient_end_height);
ADD_SIGNAL(MethodInfo(sig_primitives_changed));
ADD_SIGNAL(MethodInfo(sig_primitive_list_changed, PropertyInfo(Variant::ARRAY, "array", PROPERTY_HINT_ARRAY_TYPE, vformat("%s/%s:TerrainPrimitive", Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE))));
ClassDB::bind_method(D_METHOD("generate_grid", "area", "out_mesh", "side_points"), &self_type::generate_grid);
}
@ -105,6 +106,7 @@ void TerrainMeshGenerator::set_primitives(Array primitives) {
primitive->connect_changed(this->generation_changed);
}
}
emit_signal(sig_primitive_list_changed, get_primitives());
on_configuration_changed();
}

View file

@ -39,4 +39,5 @@ private:
public:
static String const sig_primitives_changed;
static String const sig_primitive_list_changed;
};

View file

@ -31,8 +31,8 @@ float TerrainPrimitive::blend(float under, float over) const {
return over + smooth_center_distance;
} else {
return (this->blend_mode == Peak
? (under >= over ? under : over) + smooth_center_distance
: (under >= over ? over : under) - smooth_center_distance);
? (under > over ? under : over) + smooth_center_distance
: (under > over ? over : under) - smooth_center_distance);
}
}
@ -118,16 +118,7 @@ void NoisePrimitive::_bind_methods() {
void NoisePrimitive::evaluate(Vector2 at, float &io_height) const {
if (this->noise.is_valid()) {
float noise_sample{ this->noise->get_noise_2dv(at / this->noise_scale) };
switch (this->get_blend_mode()) {
case Peak:
noise_sample = Math::remap(noise_sample, -1.f, 1.f, 0.f, this->noise_amplitude);
break;
case Valley:
noise_sample = Math::remap(noise_sample, -1.f, 1.f, -this->noise_amplitude, 0.f);
break;
case Both:
noise_sample *= this->noise_amplitude;
}
noise_sample *= this->noise_amplitude;
io_height = blend(io_height, io_height + noise_sample);
}
}