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 "register_types.h"
#include "core/object/class_db.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_mesh_generator.h"
#include "terrain_editor/terrain_primitive.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<PointPrimitive>();
ClassDB::register_class<NoisePrimitive>(); ClassDB::register_class<NoisePrimitive>();
ClassDB::register_class<ExpressionPrimitive>(); ClassDB::register_class<ExpressionPrimitive>();
ClassDB::register_class<PointPrimitiveNode>();
ClassDB::register_class<TerrainMeshEditor>();
} }
void uninitialize_terrain_editor_module(ModuleInitializationLevel p_level) { 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 "terrain_mesh_generator.h"
#include "core/io/resource_saver.h"
#include "core/math/math_funcs.h" #include "core/math/math_funcs.h"
#include "core/math/rect2.h" #include "core/math/rect2.h"
#include "core/object/class_db.h" #include "core/object/class_db.h"
@ -10,6 +9,7 @@
#include <limits> #include <limits>
String const TerrainMeshGenerator::sig_primitives_changed{ "primitives_changed" }; String const TerrainMeshGenerator::sig_primitives_changed{ "primitives_changed" };
String const TerrainMeshGenerator::sig_primitive_list_changed{ "primitive_list_changed" };
void TerrainMeshGenerator::_bind_methods() { void TerrainMeshGenerator::_bind_methods() {
BIND_HPROPERTY(Variant::ARRAY, primitives, PROPERTY_HINT_ARRAY_TYPE, vformat("%s/%s:TerrainPrimitive", Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE)); 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_start_height);
BIND_PROPERTY(Variant::FLOAT, color_gradient_end_height); BIND_PROPERTY(Variant::FLOAT, color_gradient_end_height);
ADD_SIGNAL(MethodInfo(sig_primitives_changed)); 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); 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); primitive->connect_changed(this->generation_changed);
} }
} }
emit_signal(sig_primitive_list_changed, get_primitives());
on_configuration_changed(); on_configuration_changed();
} }

View file

@ -39,4 +39,5 @@ private:
public: public:
static String const sig_primitives_changed; 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; return over + smooth_center_distance;
} else { } else {
return (this->blend_mode == Peak return (this->blend_mode == Peak
? (under >= over ? under : over) + smooth_center_distance ? (under > over ? under : over) + smooth_center_distance
: (under >= over ? over : under) - 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 { void NoisePrimitive::evaluate(Vector2 at, float &io_height) const {
if (this->noise.is_valid()) { if (this->noise.is_valid()) {
float noise_sample{ this->noise->get_noise_2dv(at / this->noise_scale) }; 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); io_height = blend(io_height, io_height + noise_sample);
} }
} }

View file

@ -0,0 +1,8 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://dgym4g3uxbvl3"]
[resource]
render_priority = 10
no_depth_test = true
shading_mode = 0
albedo_color = Color(0.36, 1, 0.594667, 1)
point_size = 11.2

Binary file not shown.

View file

@ -0,0 +1,61 @@
[remap]
importer="scene"
importer_version=1
type="PackedScene"
uid="uid://bwrqsnetcn8yr"
path="res://.godot/imported/point_handle.blend-262ed5ea08d47e6ba78fb31cb5cbab65.scn"
[deps]
source_file="res://assets/models/point_handle.blend"
dest_files=["res://.godot/imported/point_handle.blend-262ed5ea08d47e6ba78fb31cb5cbab65.scn"]
[params]
nodes/root_type=""
nodes/root_name=""
nodes/apply_root_scale=true
nodes/root_scale=1.0
nodes/import_as_skeleton_bones=false
nodes/use_node_type_suffixes=true
meshes/ensure_tangents=true
meshes/generate_lods=false
meshes/create_shadow_meshes=true
meshes/light_baking=1
meshes/lightmap_texel_size=0.2
meshes/force_disable_compression=false
skins/use_named_skins=true
animation/import=true
animation/fps=30
animation/trimming=false
animation/remove_immutable_tracks=true
animation/import_rest_as_RESET=false
import_script/path=""
_subresources={
"materials": {
"Material": {
"use_external/enabled": true,
"use_external/fallback_path": "res://assets/materials/peak_handle.tres",
"use_external/path": "uid://dgym4g3uxbvl3"
}
}
}
blender/nodes/visible=0
blender/nodes/active_collection_only=false
blender/nodes/punctual_lights=true
blender/nodes/cameras=true
blender/nodes/custom_properties=true
blender/nodes/modifiers=1
blender/meshes/colors=false
blender/meshes/uvs=true
blender/meshes/normals=true
blender/meshes/export_geometry_nodes_instances=false
blender/meshes/tangents=true
blender/meshes/skins=2
blender/meshes/export_bones_deforming_mesh_only=false
blender/materials/unpack_enabled=true
blender/materials/export_materials=1
blender/animation/limit_playback=true
blender/animation/always_sample=true
blender/animation/group_tracks=true

Binary file not shown.

View file

@ -0,0 +1,48 @@
[gd_scene load_steps=4 format=3 uid="uid://cnux2fqne284i"]
[ext_resource type="PackedScene" uid="uid://bwrqsnetcn8yr" path="res://assets/models/point_handle.blend" id="1_njtj3"]
[sub_resource type="GDScript" id="GDScript_njtj3"]
script/source = "extends Area3D
var dragged : bool = false
func _input(event: InputEvent) -> void:
if not dragged:
return
if event is InputEventMouseButton and not (event as InputEventMouseButton).is_pressed():
dragged = false
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
$\"..\".push_transform_changes()
elif event is InputEventMouseMotion:
var motion := event as InputEventMouseMotion
get_parent_node_3d().global_position.y -= motion.screen_relative.y
get_viewport().set_input_as_handled()
func _input_event(_camera: Camera3D, event: InputEvent, _event_position: Vector3, _normal: Vector3, _shape_idx: int) -> void:
if not dragged and event is InputEventMouseButton and (event as InputEventMouseButton).is_pressed():
dragged = true
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
get_viewport().set_input_as_handled()
"
[sub_resource type="CylinderShape3D" id="CylinderShape3D_mx0s0"]
height = 9.59302
radius = 4.85693
[node name="PointPrimitiveNode" type="PointPrimitiveNode"]
[node name="point_handle" parent="." instance=ExtResource("1_njtj3")]
transform = Transform3D(10, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0)
[node name="Area3D" type="Area3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 27.8381, 0)
script = SubResource("GDScript_njtj3")
[node name="CollisionShape3D" type="CollisionShape3D" parent="Area3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.80421, 0)
shape = SubResource("CylinderShape3D_mx0s0")
[node name="CollisionShape3D2" type="CollisionShape3D" parent="Area3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -57.945, 0)
shape = SubResource("CylinderShape3D_mx0s0")

View file

@ -11,5 +11,12 @@ config_version=5
[application] [application]
config/name="terrain_editor" config/name="terrain_editor"
config/features=PackedStringArray("4.4", "Forward Plus") run/main_scene="uid://xm383pc5pcnn"
config/features=PackedStringArray("4.5", "Forward Plus")
config/icon="res://icon.svg" config/icon="res://icon.svg"
[rendering]
anti_aliasing/quality/msaa_2d=1
anti_aliasing/quality/msaa_3d=2
anti_aliasing/quality/use_debanding=true

File diff suppressed because one or more lines are too long