Skip to content
This repository has been archived by the owner on Sep 24, 2020. It is now read-only.

Commit

Permalink
Consider StaticBody and NavMesh position when converting to a MeshLib…
Browse files Browse the repository at this point in the history
…rary

Would have added mesh transform, but realized that will cause problems when moving the mesh around the scene for visualization purposes.

Closes godotengine#11722
  • Loading branch information
bojidar-bg committed Feb 26, 2019
1 parent 22ee7ba commit 1a397a7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
5 changes: 4 additions & 1 deletion editor/plugins/mesh_library_editor_plugin.cpp
Expand Up @@ -127,7 +127,7 @@ void MeshLibraryEditor::_import_scene(Node *p_scene, Ref<MeshLibrary> p_library,
continue;
MeshLibrary::ShapeData shape_data;
shape_data.shape = collision;
shape_data.local_transform = sb->shape_owner_get_transform(E->get());
shape_data.local_transform = sb->get_transform() * sb->shape_owner_get_transform(E->get());
collisions.push_back(shape_data);
}
}
Expand All @@ -136,17 +136,20 @@ void MeshLibraryEditor::_import_scene(Node *p_scene, Ref<MeshLibrary> p_library,
p_library->set_item_shapes(id, collisions);

Ref<NavigationMesh> navmesh;
Transform navmesh_transform;
for (int j = 0; j < mi->get_child_count(); j++) {
Node *child2 = mi->get_child(j);
if (!Object::cast_to<NavigationMeshInstance>(child2))
continue;
NavigationMeshInstance *sb = Object::cast_to<NavigationMeshInstance>(child2);
navmesh = sb->get_navigation_mesh();
navmesh_transform = sb->get_transform();
if (!navmesh.is_null())
break;
}
if (!navmesh.is_null()) {
p_library->set_item_navmesh(id, navmesh);
p_library->set_item_navmesh_transform(id, navmesh_transform);
}
}

Expand Down
2 changes: 1 addition & 1 deletion modules/gridmap/grid_map.cpp
Expand Up @@ -517,7 +517,7 @@ bool GridMap::_octant_update(const OctantKey &p_key) {
Ref<NavigationMesh> navmesh = mesh_library->get_item_navmesh(c.item);
if (navmesh.is_valid()) {
Octant::NavMesh nm;
nm.xform = xform;
nm.xform = xform * mesh_library->get_item_navmesh_transform(c.item);

if (navigation) {
nm.id = navigation->navmesh_add(navmesh, xform, this);
Expand Down
26 changes: 26 additions & 0 deletions scene/resources/mesh_library.cpp
Expand Up @@ -56,6 +56,8 @@ bool MeshLibrary::_set(const StringName &p_name, const Variant &p_value) {
set_item_preview(idx, p_value);
else if (what == "navmesh")
set_item_navmesh(idx, p_value);
else if (what == "navmesh_transform")
set_item_navmesh_transform(idx, p_value);
else
return false;

Expand All @@ -80,6 +82,8 @@ bool MeshLibrary::_get(const StringName &p_name, Variant &r_ret) const {
r_ret = _get_item_shapes(idx);
else if (what == "navmesh")
r_ret = get_item_navmesh(idx);
else if (what == "navmesh_transform")
r_ret = get_item_navmesh_transform(idx);
else if (what == "preview")
r_ret = get_item_preview(idx);
else
Expand All @@ -95,8 +99,10 @@ void MeshLibrary::_get_property_list(List<PropertyInfo> *p_list) const {
String name = "item/" + itos(E->key()) + "/";
p_list->push_back(PropertyInfo(Variant::STRING, name + "name"));
p_list->push_back(PropertyInfo(Variant::OBJECT, name + "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"));
p_list->push_back(PropertyInfo(Variant::TRANSFORM, name + "mesh_transform"));
p_list->push_back(PropertyInfo(Variant::ARRAY, name + "shapes"));
p_list->push_back(PropertyInfo(Variant::OBJECT, name + "navmesh", PROPERTY_HINT_RESOURCE_TYPE, "NavigationMesh"));
p_list->push_back(PropertyInfo(Variant::TRANSFORM, name + "navmesh_transform"));
p_list->push_back(PropertyInfo(Variant::OBJECT, name + "preview", PROPERTY_HINT_RESOURCE_TYPE, "Texture", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_HELPER));
}
}
Expand All @@ -116,6 +122,7 @@ void MeshLibrary::set_item_name(int p_item, const String &p_name) {
emit_changed();
_change_notify();
}

void MeshLibrary::set_item_mesh(int p_item, const Ref<Mesh> &p_mesh) {

ERR_FAIL_COND(!item_map.has(p_item));
Expand Down Expand Up @@ -145,18 +152,29 @@ void MeshLibrary::set_item_navmesh(int p_item, const Ref<NavigationMesh> &p_navm
_change_notify();
}

void MeshLibrary::set_item_navmesh_transform(int p_item, const Transform &p_transform) {

ERR_FAIL_COND(!item_map.has(p_item));
item_map[p_item].navmesh_transform = p_transform;
notify_change_to_owners();
emit_changed();
_change_notify();
}

void MeshLibrary::set_item_preview(int p_item, const Ref<Texture> &p_preview) {

ERR_FAIL_COND(!item_map.has(p_item));
item_map[p_item].preview = p_preview;
emit_changed();
_change_notify();
}

String MeshLibrary::get_item_name(int p_item) const {

ERR_FAIL_COND_V(!item_map.has(p_item), "");
return item_map[p_item].name;
}

Ref<Mesh> MeshLibrary::get_item_mesh(int p_item) const {

ERR_FAIL_COND_V(!item_map.has(p_item), Ref<Mesh>());
Expand All @@ -175,6 +193,12 @@ Ref<NavigationMesh> MeshLibrary::get_item_navmesh(int p_item) const {
return item_map[p_item].navmesh;
}

Transform MeshLibrary::get_item_navmesh_transform(int p_item) const {

ERR_FAIL_COND_V(!item_map.has(p_item), Transform());
return item_map[p_item].navmesh_transform;
}

Ref<Texture> MeshLibrary::get_item_preview(int p_item) const {

ERR_FAIL_COND_V(!item_map.has(p_item), Ref<Texture>());
Expand Down Expand Up @@ -268,11 +292,13 @@ void MeshLibrary::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_item_name", "id", "name"), &MeshLibrary::set_item_name);
ClassDB::bind_method(D_METHOD("set_item_mesh", "id", "mesh"), &MeshLibrary::set_item_mesh);
ClassDB::bind_method(D_METHOD("set_item_navmesh", "id", "navmesh"), &MeshLibrary::set_item_navmesh);
ClassDB::bind_method(D_METHOD("set_item_navmesh_transform", "id", "navmesh"), &MeshLibrary::set_item_navmesh_transform);
ClassDB::bind_method(D_METHOD("set_item_shapes", "id", "shapes"), &MeshLibrary::_set_item_shapes);
ClassDB::bind_method(D_METHOD("set_item_preview", "id", "texture"), &MeshLibrary::set_item_preview);
ClassDB::bind_method(D_METHOD("get_item_name", "id"), &MeshLibrary::get_item_name);
ClassDB::bind_method(D_METHOD("get_item_mesh", "id"), &MeshLibrary::get_item_mesh);
ClassDB::bind_method(D_METHOD("get_item_navmesh", "id"), &MeshLibrary::get_item_navmesh);
ClassDB::bind_method(D_METHOD("get_item_navmesh_transform", "id"), &MeshLibrary::get_item_navmesh_transform);
ClassDB::bind_method(D_METHOD("get_item_shapes", "id"), &MeshLibrary::_get_item_shapes);
ClassDB::bind_method(D_METHOD("get_item_preview", "id"), &MeshLibrary::get_item_preview);
ClassDB::bind_method(D_METHOD("remove_item", "id"), &MeshLibrary::remove_item);
Expand Down
3 changes: 3 additions & 0 deletions scene/resources/mesh_library.h
Expand Up @@ -52,6 +52,7 @@ class MeshLibrary : public Resource {
Ref<Mesh> mesh;
Vector<ShapeData> shapes;
Ref<Texture> preview;
Transform navmesh_transform;
Ref<NavigationMesh> navmesh;
};

Expand All @@ -72,11 +73,13 @@ class MeshLibrary : public Resource {
void set_item_name(int p_item, const String &p_name);
void set_item_mesh(int p_item, const Ref<Mesh> &p_mesh);
void set_item_navmesh(int p_item, const Ref<NavigationMesh> &p_navmesh);
void set_item_navmesh_transform(int p_item, const Transform &p_transform);
void set_item_shapes(int p_item, const Vector<ShapeData> &p_shapes);
void set_item_preview(int p_item, const Ref<Texture> &p_preview);
String get_item_name(int p_item) const;
Ref<Mesh> get_item_mesh(int p_item) const;
Ref<NavigationMesh> get_item_navmesh(int p_item) const;
Transform get_item_navmesh_transform(int p_item) const;
Vector<ShapeData> get_item_shapes(int p_item) const;
Ref<Texture> get_item_preview(int p_item) const;

Expand Down

0 comments on commit 1a397a7

Please sign in to comment.