Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instancer storage rework #523

Draft
wants to merge 36 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
96ad094
store transforms in local space
Xtarsia Oct 10, 2024
3cff37a
vertex spacing embeded in transforms
Xtarsia Oct 12, 2024
0fccf1e
Auxilary update: add str2loc, rename _mmis, force_update_mmis in Data…
TokisanGames Oct 12, 2024
5c3744d
Store MMIs under region containers
TokisanGames Oct 12, 2024
3f9cc74
Add region data dict; Setup initial upgrade infrastructure
TokisanGames Oct 12, 2024
e436c87
Auxilary: replace round_multiple w/ snapped, rename get_instance_count
TokisanGames Oct 12, 2024
5fe7af7
Replaced TypedArray<Color> with PackedColorArray
TokisanGames Oct 12, 2024
11b2c84
general
TokisanGames Oct 12, 2024
8fade62
Change data structure (partially working, lots not)
TokisanGames Oct 12, 2024
fb20483
correct transforms add cell modified flag
Xtarsia Oct 12, 2024
5b0556c
update vertex spacing per cell
Xtarsia Oct 12, 2024
e20732c
update transforms by cell
Xtarsia Oct 12, 2024
2ebf407
WIP remove instances
Xtarsia Oct 12, 2024
daea12d
Fix crashes when tuple doesn't exist. Rename tuple -> triple. Reduce …
TokisanGames Oct 13, 2024
eb269da
improve remove instances
Xtarsia Oct 13, 2024
583d889
remove instances - erase cell data when empty
Xtarsia Oct 13, 2024
ed06faf
remove empty mmi
Xtarsia Oct 13, 2024
fd9d574
fix crash after freeing cell mmi
Xtarsia Oct 13, 2024
81b3667
refactor remove instances
Xtarsia Oct 14, 2024
d7f4220
remove instances small fix
Xtarsia Oct 14, 2024
206c929
fix slope - some edge cases remain
Xtarsia Oct 14, 2024
690763e
refactor update_transforms modify maps_edited signal
Xtarsia Oct 14, 2024
5273f92
fix un-writable cells on first load
Xtarsia Oct 14, 2024
d42fda4
correctly update color arrays alongside xforms
Xtarsia Oct 14, 2024
8a6bd30
Renames. Reduce logging. Improve cleanup
TokisanGames Oct 15, 2024
a698484
modify _backup_region() calls
Xtarsia Oct 15, 2024
de988c5
initial region size change implementation
Xtarsia Oct 15, 2024
8a2a904
swap return to continue inside a loop..
Xtarsia Oct 15, 2024
e014210
fix set-get_pixel error condition on deleted regions
Xtarsia Oct 16, 2024
64117ca
region size change fixes
Xtarsia Oct 16, 2024
39f7f24
remove update xforms vertex spacing correction
Xtarsia Oct 16, 2024
3c9ea71
add instancer grid debug view
Xtarsia Oct 16, 2024
e6e80cb
improve remove instances
Xtarsia Oct 16, 2024
bdae922
Rename mesh_dict -> mesh_inst_dict, etc
TokisanGames Oct 17, 2024
9a9abe9
Consolidate all MMI deletion. Switch MMI storage to unordered_map.
TokisanGames Oct 17, 2024
3a14952
cell specific remove instance quotas
Xtarsia Oct 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions project/demo/src/DemoScene.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
@tool
extends Node

@export var dump_tree: bool = false :
set(value):
print_tree()


func _ready():
if not Engine.is_editor_hint() and has_node("UI"):
Expand Down
6 changes: 3 additions & 3 deletions src/register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ void initialize_terrain_3d(ModuleInitializationLevel p_level) {
ClassDB::register_class<Terrain3DInstancer>();
ClassDB::register_class<Terrain3DMaterial>();
ClassDB::register_class<Terrain3DMeshAsset>();
ClassDB::register_class<Terrain3DStorage>(); // Deprecated 0.9.3 - Remove 0.9.4+
ClassDB::register_class<Terrain3DStorage>(); // Deprecated 0.9.3 - Remove 1.0
ClassDB::register_class<Terrain3DRegion>();
ClassDB::register_class<Terrain3DTextureAsset>();
ClassDB::register_class<Terrain3DUtil>();
ClassDB::register_class<Terrain3DTexture>(); // Deprecated 0.9.2 - Remove 0.9.3+
ClassDB::register_class<Terrain3DTextureList>(); // Deprecated 0.9.2 - Remove 0.9.3+
ClassDB::register_class<Terrain3DTexture>(); // Deprecated 0.9.2 - Remove 1.0
ClassDB::register_class<Terrain3DTextureList>(); // Deprecated 0.9.2 - Remove 1.0
}

void uninitialize_terrain_3d(ModuleInitializationLevel p_level) {
Expand Down
24 changes: 8 additions & 16 deletions src/terrain_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void Terrain3D::_build_containers() {
_label_nodes->set_name("Labels");
add_child(_label_nodes, true);
_mmi_nodes = memnew(Node);
_mmi_nodes->set_name("MMIs");
_mmi_nodes->set_name("MMI");
add_child(_mmi_nodes, true);
}

Expand Down Expand Up @@ -223,7 +223,7 @@ void Terrain3D::_update_collision() {
int time = Time::get_singleton()->get_ticks_msec();
int shape_size = _region_size + 1;
float hole_const = NAN;
// DEPRECATED - Jolt v0.12 supports NAN. Remove check when it's old.
// DEPRECATED - Jolt v0.12 supports NAN. Remove 1.0. Jolt 0.13 supports 4.3.
if (ProjectSettings::get_singleton()->get_setting("physics/3d/physics_engine") == "JoltPhysics3D") {
hole_const = FLT_MAX;
}
Expand Down Expand Up @@ -984,23 +984,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) {
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 = _vertex_spacing;
Dictionary mmis = _instancer->get_mmis();
Array keys = mmis.keys();
for (int i = 0; i < keys.size(); i++) {
MultiMeshInstance3D *mmi = cast_to<MultiMeshInstance3D>(mmis[keys[i]]);
if (mmi != nullptr) {
mmi->set_scale(Vector3(_vertex_spacing, 1.f, _vertex_spacing));
}
}
update_region_labels();
_instancer->_update_vertex_spacing(_vertex_spacing);
}
if (IS_EDITOR && _plugin != nullptr) {
_plugin->call("update_region_grid");
Expand Down Expand Up @@ -1573,12 +1565,12 @@ void Terrain3D::_bind_methods() {
ADD_SIGNAL(MethodInfo("material_changed"));
ADD_SIGNAL(MethodInfo("assets_changed"));

// DEPRECATED 0.9.2 - Remove 0.9.3+
// DEPRECATED 0.9.2 - Remove 1.0
ClassDB::bind_method(D_METHOD("set_texture_list", "texture_list"), &Terrain3D::set_texture_list);
ClassDB::bind_method(D_METHOD("get_texture_list"), &Terrain3D::get_texture_list);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_list", PROPERTY_HINT_RESOURCE_TYPE, "Terrain3DTextureList", PROPERTY_USAGE_NONE), "set_texture_list", "get_texture_list");

// DEPRECATED 0.9.3 - Remove 0.9.4+
// DEPRECATED 0.9.3 - Remove 1.0
ClassDB::bind_method(D_METHOD("set_storage", "storage"), &Terrain3D::set_storage);
ClassDB::bind_method(D_METHOD("get_storage"), &Terrain3D::get_storage);
ClassDB::bind_method(D_METHOD("split_storage"), &Terrain3D::split_storage);
Expand All @@ -1589,7 +1581,7 @@ void Terrain3D::_bind_methods() {
// DEPRECATED Functions
///////////////////////////

// DEPRECATED 0.9.2 - Remove 0.9.3+
// DEPRECATED 0.9.2 - Remove 1.0
void Terrain3D::set_texture_list(const Ref<Terrain3DTextureList> &p_texture_list) {
if (p_texture_list.is_null()) {
LOG(ERROR, "Attempted to upgrade Terrain3DTextureList, but received null (perhaps already a Terrain3DAssets). Reconnect manually and save.");
Expand All @@ -1603,7 +1595,7 @@ void Terrain3D::set_texture_list(const Ref<Terrain3DTextureList> &p_texture_list
set_assets(assets);
}

// DEPRECATED 0.9.3 - Remove 0.9.4+
// DEPRECATED 0.9.3 - Remove 1.0
void Terrain3D::set_storage(const Ref<Terrain3DStorage> &p_storage) {
_storage = p_storage;
if (p_storage.is_valid()) {
Expand Down Expand Up @@ -1640,7 +1632,7 @@ void Terrain3D::split_storage() {
region->set_height_map(hmaps[i]);
region->set_control_map(ctlmaps[i]);
region->set_color_map(clrmaps[i]);
region->set_multimeshes(mms[locations[i]]);
region->set_multimeshes(mms[locations[i]]); // TODO BROKEN
_data->add_region(region, false);
LOG(INFO, "Splicing region ", locations[i]);
}
Expand Down
4 changes: 2 additions & 2 deletions src/terrain_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,11 @@ class Terrain3D : public Node3D {
static void _bind_methods();

public:
// DEPRECATED 0.9.2 - Remove 0.9.3+
// DEPRECATED 0.9.2 - Remove 1.0
void set_texture_list(const Ref<Terrain3DTextureList> &p_texture_list);
Ref<Terrain3DTextureList> get_texture_list() const { return Ref<Terrain3DTextureList>(); }

// DEPRECATED 0.9.3 - Remove 0.9.4+
// DEPRECATED 0.9.3 - Remove 1.0
Ref<Terrain3DStorage> _storage;
void set_storage(const Ref<Terrain3DStorage> &p_storage);
Ref<Terrain3DStorage> get_storage() const { return _storage; }
Expand Down
2 changes: 1 addition & 1 deletion src/terrain_3d_assets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ void Terrain3DAssets::_bind_methods() {
ADD_SIGNAL(MethodInfo("textures_changed"));
}

// Deprecated 0.9.2 - Remove 0.9.3+
// Deprecated 0.9.2 - Remove 1.0
void Terrain3DTextureList::set_textures(const TypedArray<Terrain3DTexture> &p_textures) {
LOG(WARN, "Terrain3DTextureList: Converting Terrain3DTextures to Terrain3DTextureAssets. Save to complete.");
for (int i = 0; i < p_textures.size(); i++) {
Expand Down
2 changes: 1 addition & 1 deletion src/terrain_3d_assets.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class Terrain3DAssets : public Resource {

VARIANT_ENUM_CAST(Terrain3DAssets::AssetType);

// Deprecated 0.9.2 - Remove 0.9.3+
// Deprecated 0.9.2 - Remove 1.0

class Terrain3DTexture : public Terrain3DTextureAsset {
GDCLASS(Terrain3DTexture, Terrain3DTextureAsset);
Expand Down
7 changes: 5 additions & 2 deletions src/terrain_3d_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ Ref<Terrain3DRegion> Terrain3DData::add_region_blank(const Vector2i &p_region_lo
region.instantiate();
region->set_location(p_region_loc);
region->set_region_size(_region_size);
region->set_vertex_spacing(_vertex_spacing);
if (add_region(region, p_update) == OK) {
region->set_modified(true);
return region;
Expand Down Expand Up @@ -257,6 +258,7 @@ Error Terrain3DData::add_region(const Ref<Terrain3DRegion> &p_region, const bool
LOG(DEBUG, "Storing region ", region_loc, " version ", vformat("%.3f", p_region->get_version()), " id: ", _region_locations.size());
if (p_update) {
force_update_maps();
_terrain->get_instancer()->force_update_mmis();
}
return OK;
}
Expand Down Expand Up @@ -293,6 +295,7 @@ void Terrain3DData::remove_region(const Ref<Terrain3DRegion> &p_region, const bo
if (p_update) {
LOG(DEBUG, "Updating generated maps");
force_update_maps();
_terrain->get_instancer()->force_update_mmis();
}
}

Expand Down Expand Up @@ -625,7 +628,7 @@ real_t Terrain3DData::get_height(const Vector3 &p_global_position) const {
const real_t &step = _vertex_spacing;
pos.y = 0.f;
// Round to nearest vertex
Vector3 pos_round = Vector3(round_multiple(pos.x, step), 0.f, round_multiple(pos.z, step));
Vector3 pos_round = pos.snapped(Vector3(step, 0.f, step));
// If requested position is close to a vertex, return its height
if ((pos - pos_round).length() < 0.01f) {
return get_pixel(TYPE_HEIGHT, pos).r;
Expand Down Expand Up @@ -672,7 +675,7 @@ bool Terrain3DData::is_in_slope(const Vector3 &p_global_position, const Vector2
auto get_height = [&](Vector3 pos) -> real_t {
real_t step = _terrain->get_vertex_spacing();
// Round to nearest vertex
Vector3 pos_round = Vector3(round_multiple(pos.x, step), 0.f, round_multiple(pos.z, step));
Vector3 pos_round = pos.snapped(Vector3(step, 0.f, step));
real_t height = get_pixel(TYPE_HEIGHT, pos_round).r;
return std::isnan(height) ? 0.f : height;
};
Expand Down
3 changes: 1 addition & 2 deletions src/terrain_3d_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ Ref<Terrain3DRegion> Terrain3DEditor::_operate_region(const Vector2i &p_region_l
_original_regions.push_back(region);
height_range = region->get_height_range();
_terrain->get_data()->remove_region(region);
_terrain->get_instancer()->force_update_mmis();
changed = true;
}

Expand Down Expand Up @@ -716,7 +715,7 @@ void Terrain3DEditor::start_operation(const Vector3 &p_global_position) {
_edited_regions = TypedArray<Terrain3DRegion>();
_added_removed_locations = TypedArray<Vector2i>();
// Reset counter at start to ensure first click places an instance
_terrain->get_instancer()->reset_instance_counter();
_terrain->get_instancer()->reset_density_counter();
_terrain->get_data()->clear_edited_area();
_operation_position = p_global_position;
_operation_movement = Vector3();
Expand Down
Loading