Now mesh merging for elements works

This commit is contained in:
2024-10-10 17:20:35 +03:00
parent f4f8bbce0b
commit bbb608611c
5 changed files with 377 additions and 7 deletions

View File

@@ -1692,6 +1692,331 @@ grid_layouts={
"index": 80,
"rotation": 0
} ],
"floor_-6": [ {
"element": "empty",
"index": 0,
"rotation": 0
}, {
"element": "empty",
"index": 1,
"rotation": 0
}, {
"element": "empty",
"index": 2,
"rotation": 0
}, {
"element": "empty",
"index": 3,
"rotation": 0
}, {
"element": "empty",
"index": 4,
"rotation": 0
}, {
"element": "empty",
"index": 5,
"rotation": 0
}, {
"element": "empty",
"index": 6,
"rotation": 0
}, {
"element": "empty",
"index": 7,
"rotation": 0
}, {
"element": "empty",
"index": 8,
"rotation": 0
}, {
"element": "empty",
"index": 9,
"rotation": 0
}, {
"element": "empty",
"index": 10,
"rotation": 0
}, {
"element": "empty",
"index": 11,
"rotation": 0
}, {
"element": "empty",
"index": 12,
"rotation": 0
}, {
"element": "empty",
"index": 13,
"rotation": 0
}, {
"element": "empty",
"index": 14,
"rotation": 0
}, {
"element": "empty",
"index": 15,
"rotation": 0
}, {
"element": "empty",
"index": 16,
"rotation": 0
}, {
"element": "empty",
"index": 17,
"rotation": 0
}, {
"element": "empty",
"index": 18,
"rotation": 0
}, {
"element": "empty",
"index": 19,
"rotation": 0
}, {
"element": "empty",
"index": 20,
"rotation": 0
}, {
"element": "empty",
"index": 21,
"rotation": 0
}, {
"element": "empty",
"index": 22,
"rotation": 0
}, {
"element": "empty",
"index": 23,
"rotation": 0
}, {
"element": "empty",
"index": 24,
"rotation": 0
}, {
"element": "empty",
"index": 25,
"rotation": 0
}, {
"element": "empty",
"index": 26,
"rotation": 0
}, {
"element": "empty",
"index": 27,
"rotation": 0
}, {
"element": "empty",
"index": 28,
"rotation": 0
}, {
"element": "empty",
"index": 29,
"rotation": 0
}, {
"element": "empty",
"index": 30,
"rotation": 0
}, {
"element": "empty",
"index": 31,
"rotation": 0
}, {
"element": "empty",
"index": 32,
"rotation": 0
}, {
"element": "empty",
"index": 33,
"rotation": 0
}, {
"element": "empty",
"index": 34,
"rotation": 0
}, {
"element": "empty",
"index": 35,
"rotation": 0
}, {
"element": "empty",
"index": 36,
"rotation": 0
}, {
"element": "empty",
"index": 37,
"rotation": 0
}, {
"element": "empty",
"index": 38,
"rotation": 0
}, {
"element": "empty",
"index": 39,
"rotation": 0
}, {
"element": "empty",
"index": 40,
"rotation": 0
}, {
"element": "empty",
"index": 41,
"rotation": 0
}, {
"element": "empty",
"index": 42,
"rotation": 0
}, {
"element": "empty",
"index": 43,
"rotation": 0
}, {
"element": "empty",
"index": 44,
"rotation": 0
}, {
"element": "empty",
"index": 45,
"rotation": 0
}, {
"element": "empty",
"index": 46,
"rotation": 0
}, {
"element": "empty",
"index": 47,
"rotation": 0
}, {
"element": "empty",
"index": 48,
"rotation": 0
}, {
"element": "empty",
"index": 49,
"rotation": 0
}, {
"element": "empty",
"index": 50,
"rotation": 0
}, {
"element": "empty",
"index": 51,
"rotation": 0
}, {
"element": "empty",
"index": 52,
"rotation": 0
}, {
"element": "empty",
"index": 53,
"rotation": 0
}, {
"element": "empty",
"index": 54,
"rotation": 0
}, {
"element": "empty",
"index": 55,
"rotation": 0
}, {
"element": "empty",
"index": 56,
"rotation": 0
}, {
"element": "empty",
"index": 57,
"rotation": 0
}, {
"element": "empty",
"index": 58,
"rotation": 0
}, {
"element": "empty",
"index": 59,
"rotation": 0
}, {
"element": "empty",
"index": 60,
"rotation": 0
}, {
"element": "empty",
"index": 61,
"rotation": 0
}, {
"element": "empty",
"index": 62,
"rotation": 0
}, {
"element": "empty",
"index": 63,
"rotation": 0
}, {
"element": "empty",
"index": 64,
"rotation": 0
}, {
"element": "empty",
"index": 65,
"rotation": 0
}, {
"element": "empty",
"index": 66,
"rotation": 0
}, {
"element": "empty",
"index": 67,
"rotation": 0
}, {
"element": "empty",
"index": 68,
"rotation": 0
}, {
"element": "empty",
"index": 69,
"rotation": 0
}, {
"element": "empty",
"index": 70,
"rotation": 0
}, {
"element": "empty",
"index": 71,
"rotation": 0
}, {
"element": "empty",
"index": 72,
"rotation": 0
}, {
"element": "empty",
"index": 73,
"rotation": 0
}, {
"element": "empty",
"index": 74,
"rotation": 0
}, {
"element": "empty",
"index": 75,
"rotation": 0
}, {
"element": "empty",
"index": 76,
"rotation": 0
}, {
"element": "empty",
"index": 77,
"rotation": 0
}, {
"element": "empty",
"index": 78,
"rotation": 0
}, {
"element": "empty",
"index": 79,
"rotation": 0
}, {
"element": "empty",
"index": 80,
"rotation": 0
} ],
"floor_0": [ {
"element": "empty",
"index": 0,

View File

@@ -60,6 +60,7 @@ public:
}
void activate()
{
editor->update_element_meshes();
get_as_node<Control>("%layout_editor")->show();
get_as_node<Spatial>("%bg_floor")->show();
{
@@ -177,6 +178,12 @@ public:
->select(rotation);
}
}
} else if (event == "elements_update") {
print_line(event);
editor->update_element_meshes();
} else if (event == "elements_update_all") {
print_line(event);
editor->update_element_meshes();
}
}
void set_current_floor(float value)
@@ -1040,6 +1047,14 @@ void BuildingLayoutEditor::visualize_element_at(const String &element,
ElementData::get_singleton()->get_element_type(element);
for (i = 0; i < node->get_child_count(); i++)
node->get_child(i)->queue_delete();
Ref<Mesh> mesh =
ElementData::get_singleton()->get_element_mesh(element);
assert(mesh.is_valid());
MeshInstance *mi = memnew(MeshInstance);
mi->set_mesh(mesh);
mi->set_transform(Transform());
node->call_deferred("add_child", mi);
#if 0
for (i = 0; i < ELEMENT_SOCKETS; i++) {
Transform xform =
ElementData::get_singleton()->get_element_type_socket(
@@ -1056,14 +1071,18 @@ void BuildingLayoutEditor::visualize_element_at(const String &element,
node->call_deferred("add_child", mi);
print_line("displaying: " + mesh_name);
}
#endif
}
Ref<Mesh> BuildingLayoutEditor::get_element_mesh(const String &element) const
{
Ref<ArrayMesh> ret;
ret.instance();
ret->set_storage_mode(Mesh::STORAGE_MODE_CPU);
int i;
SurfaceTool sf;
Ref<Material> mat;
for (i = 0; i < ELEMENT_SOCKETS; i++) {
SurfaceTool sf;
const String &mesh_name =
ElementData::get_singleton()->get_element_mesh_name(
element, i);
@@ -1072,18 +1091,35 @@ Ref<Mesh> BuildingLayoutEditor::get_element_mesh(const String &element) const
const Transform &xform =
ElementData::get_singleton()->get_element_type_socket(
element_type, i);
if (mesh_name.length() == 0)
continue;
print_line("merge meshes: " + mesh_name);
assert(meshes.has(mesh_name));
Ref<ArrayMesh> mesh = meshes[mesh_name].mesh;
assert(mesh.is_valid());
if (!mat.is_valid())
mat = mesh->surface_get_material(0);
sf.append_from(mesh, 0, xform);
}
Array arr = sf.commit_to_arrays();
ret->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arr, Array(),
Mesh::ARRAY_COMPRESS_DEFAULT);
ret->surface_set_material(0, mat);
ret->set_storage_mode(Mesh::STORAGE_MODE_CPU_AND_GPU);
assert(ret.is_valid());
return ret;
}
void BuildingLayoutEditor::update_element_meshes()
{
List<String> keys;
List<String>::Element *e;
print_line("update_element_meshes");
ElementData::get_singleton()->get_element_key_list(&keys);
e = keys.front();
while (e) {
print_line("update_element_meshes: " + e->get());
Ref<Mesh> mesh = get_element_mesh(e->get());
assert(mesh.is_valid());
ElementData::get_singleton()->set_element_mesh(e->get(), mesh);
e = e->next();
}

View File

@@ -28,7 +28,6 @@ public:
void visualize_element_type(const String &key,
const String &test_element);
void visualize_element_at(const String &element, Spatial *node);
void visualize_below_level();
Ref<Mesh> get_element_mesh(const String &element) const;
void update_element_meshes();

View File

@@ -1,6 +1,12 @@
#undef NDEBUG
#include <cassert>
#include "element_data.h"
ElementData *ElementData::singleton = nullptr;
const String &ElementData::get_element_type(const String &key) const
{
assert(elements.has(key));
return elements[key].type;
}
List<int> ElementData::get_grow_cells_side(const String &key, bool exterior,
int fl, int cell) const
{
@@ -475,6 +481,8 @@ void ElementData::load_data()
e = e->next();
}
unserialize_layouts(conf_grid_layouts);
EditorEvent::get_singleton()->event.emit("elements_update_all",
varray());
}
void ElementData::save_data()
{

View File

@@ -1,5 +1,7 @@
#ifndef ELEMENT_DATA_H
#define ELEMENT_DATA_H
#undef NDEBUG
#include <cassert>
#include <vector>
#include <core/ustring.h>
#include <scene/main/node.h>
@@ -309,11 +311,15 @@ public:
assert(element_type.has(key));
assert(socket >= 0 && socket < ELEMENT_SOCKETS);
element_type[key].sockets[socket] = xform;
EditorEvent::get_singleton()->event.emit("elements_update_all",
varray());
}
void set_element_type_name(const String &key, const String &name)
{
assert(element_type.has(key));
element_type[key].name = name;
EditorEvent::get_singleton()->event.emit("elements_update",
varray(key));
}
bool has_element_type(const String &key)
{
@@ -348,11 +354,7 @@ public:
EditorEvent::get_singleton()->event.emit("elements_update",
varray(key));
}
const String &get_element_type(const String &key) const
{
assert(elements.has(key));
return elements[key].type;
}
const String &get_element_type(const String &key) const;
void set_element_mesh_name(const String &key, int socket,
const String &mesh_name)
{