Skip to content

Commit

Permalink
Fix 478: Transform instances with vertex_spacing
Browse files Browse the repository at this point in the history
  • Loading branch information
TokisanGames committed Sep 23, 2024
1 parent f6acd4b commit a4ce492
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ using namespace godot;
#define FLT_MAX __FLT_MAX__
#endif

#define V2(x) Vector2(x, x)
#define V2_ZERO Vector2(0.f, 0.f)
#define V2I_ZERO Vector2i(0, 0)
#define V2_MAX Vector2(FLT_MAX, FLT_MAX)
#define V2I_MAX Vector2i(INT32_MAX, INT32_MAX)
#define V3(x) Vector3(x, x, x)
#define V3_ZERO Vector3(0.f, 0.f, 0.f)
#define V3_MAX Vector3(FLT_MAX, FLT_MAX, FLT_MAX)
#define V2I_ZERO Vector2i(0, 0)
#define V2I_MAX Vector2i(INT32_MAX, INT32_MAX)

// Set class name for logger.h

Expand Down
6 changes: 4 additions & 2 deletions src/terrain_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -850,13 +850,15 @@ void Terrain3D::set_mesh_size(const int p_size) {
void Terrain3D::set_vertex_spacing(const real_t p_spacing) {
real_t spacing = CLAMP(p_spacing, 0.25f, 100.0f);
if (_vertex_spacing != spacing) {
LOG(INFO, "Setting vertex spacing: ", spacing);
real_t scale = spacing / _vertex_spacing;
_vertex_spacing = spacing;
LOG(INFO, "Setting vertex spacing: ", _vertex_spacing);
_clear_meshes();
_destroy_collision();
_destroy_instancer();
_initialize();
_data->_vertex_spacing = spacing;
_data->_vertex_spacing = _vertex_spacing;
_instancer->scale_positions(scale);
}
if (IS_EDITOR && _plugin != nullptr) {
_plugin->call("update_region_grid");
Expand Down
6 changes: 4 additions & 2 deletions src/terrain_3d_editor.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright © 2024 Cory Petkovsek, Roope Palmroos, and Contributors.

#include <godot_cpp/classes/editor_undo_redo_manager.hpp>
#include <godot_cpp/classes/engine.hpp>
#include <godot_cpp/classes/time.hpp>

#include "logger.h"
Expand Down Expand Up @@ -152,8 +153,9 @@ void Terrain3DEditor::_operate_map(const Vector3 &p_global_position, const real_
rot += p_camera_direction;
}
// Rotate the decal to align with the brush
cast_to<Node>(_terrain->get_plugin()->get("ui"))->call("set_decal_rotation", rot);

if (IS_EDITOR && _terrain->get_plugin() != nullptr) {
cast_to<Node>(_terrain->get_plugin()->get("ui"))->call("set_decal_rotation", rot);
}
AABB edited_area;
edited_area.position = p_global_position - Vector3(brush_size, 0.f, brush_size) * .5f;
edited_area.size = Vector3(brush_size, 0.f, brush_size);
Expand Down
29 changes: 29 additions & 0 deletions src/terrain_3d_instancer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,35 @@ void Terrain3DInstancer::update_transforms(const AABB &p_aabb) {
}
}

void Terrain3DInstancer::scale_positions(const real_t p_scale) {
IS_DATA_INIT_MESG("Instancer isn't initialized.", VOID);
real_t scale = CLAMP(p_scale, 0.01f, 100.0f);
if (p_scale == 1.f) {
return;
}
LOG(INFO, "Scaling all instancer transforms by: ", scale);
TypedArray<Terrain3DRegion> regions = _terrain->get_data()->get_regions_active();
for (int i = 0; i < regions.size(); i++) {
Ref<Terrain3DRegion> region = regions[i];
if (region.is_null()) {
continue;
}
Dictionary mms = region->get_multimeshes();
for (int j = 0; j < mms.keys().size(); j++) {
Ref<MultiMesh> mm = mms.get(mms.keys()[j], nullptr);
if (mm.is_null()) {
continue;
}
for (int k = 0; k < mm->get_instance_count(); k++) {
Transform3D xf = mm->get_instance_transform(k);
xf.origin.x *= scale;
xf.origin.z *= scale;
mm->set_instance_transform(k, xf);
}
}
}
}

// Changes the ID of a mesh, without changing the mesh on the ground
// Called when the mesh asset id has changed. Updates Multimeshes and MMIs dictionary keys
void Terrain3DInstancer::swap_ids(const int p_src_id, const int p_dst_id) {
Expand Down
1 change: 1 addition & 0 deletions src/terrain_3d_instancer.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class Terrain3DInstancer : public Object {
void add_transforms(const int p_mesh_id, const TypedArray<Transform3D> &p_xforms, const TypedArray<Color> &p_colors = TypedArray<Color>());
void append_multimesh(const Vector2i &p_region_loc, const int p_mesh_id, const TypedArray<Transform3D> &p_xforms, const TypedArray<Color> &p_colors, const bool p_clear = false);
void update_transforms(const AABB &p_aabb);
void scale_positions(const real_t p_scale);

void swap_ids(const int p_src_id, const int p_dst_id);
Ref<MultiMesh> get_multimeshp(const Vector3 &p_global_position, const int p_mesh_id) const;
Expand Down

0 comments on commit a4ce492

Please sign in to comment.