Path: blob/master/scene/resources/3d/mesh_library.cpp
9898 views
/**************************************************************************/1/* mesh_library.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "mesh_library.h"3132#ifndef PHYSICS_3D_DISABLED33#include "box_shape_3d.h"34#endif // PHYSICS_3D_DISABLED3536bool MeshLibrary::_set(const StringName &p_name, const Variant &p_value) {37String prop_name = p_name;38if (prop_name.begins_with("item/")) {39int idx = prop_name.get_slicec('/', 1).to_int();40String what = prop_name.get_slicec('/', 2);41if (!item_map.has(idx)) {42create_item(idx);43}4445if (what == "name") {46set_item_name(idx, p_value);47} else if (what == "mesh") {48set_item_mesh(idx, p_value);49} else if (what == "mesh_transform") {50set_item_mesh_transform(idx, p_value);51} else if (what == "mesh_cast_shadow") {52switch ((int)p_value) {53case 0: {54set_item_mesh_cast_shadow(idx, RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_OFF);55} break;56case 1: {57set_item_mesh_cast_shadow(idx, RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_ON);58} break;59case 2: {60set_item_mesh_cast_shadow(idx, RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_DOUBLE_SIDED);61} break;62case 3: {63set_item_mesh_cast_shadow(idx, RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_SHADOWS_ONLY);64} break;65default: {66set_item_mesh_cast_shadow(idx, RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_ON);67} break;68}69#ifndef PHYSICS_3D_DISABLED70} else if (what == "shape") {71Vector<ShapeData> shapes;72ShapeData sd;73sd.shape = p_value;74shapes.push_back(sd);75set_item_shapes(idx, shapes);76} else if (what == "shapes") {77_set_item_shapes(idx, p_value);78#endif // PHYSICS_3D_DISABLED79} else if (what == "preview") {80set_item_preview(idx, p_value);81} else if (what == "navigation_mesh") {82set_item_navigation_mesh(idx, p_value);83} else if (what == "navigation_mesh_transform") {84set_item_navigation_mesh_transform(idx, p_value);85#ifndef DISABLE_DEPRECATED86} else if (what == "navmesh") { // Renamed in 4.0 beta 9.87set_item_navigation_mesh(idx, p_value);88} else if (what == "navmesh_transform") { // Renamed in 4.0 beta 9.89set_item_navigation_mesh_transform(idx, p_value);90#endif // DISABLE_DEPRECATED91} else if (what == "navigation_layers") {92set_item_navigation_layers(idx, p_value);93} else {94return false;95}9697return true;98}99100return false;101}102103bool MeshLibrary::_get(const StringName &p_name, Variant &r_ret) const {104String prop_name = p_name;105int idx = prop_name.get_slicec('/', 1).to_int();106ERR_FAIL_COND_V(!item_map.has(idx), false);107String what = prop_name.get_slicec('/', 2);108109if (what == "name") {110r_ret = get_item_name(idx);111} else if (what == "mesh") {112r_ret = get_item_mesh(idx);113} else if (what == "mesh_transform") {114r_ret = get_item_mesh_transform(idx);115} else if (what == "mesh_cast_shadow") {116r_ret = (int)get_item_mesh_cast_shadow(idx);117#ifndef PHYSICS_3D_DISABLED118} else if (what == "shapes") {119r_ret = _get_item_shapes(idx);120#endif // PHYSICS_3D_DISABLED121} else if (what == "navigation_mesh") {122r_ret = get_item_navigation_mesh(idx);123} else if (what == "navigation_mesh_transform") {124r_ret = get_item_navigation_mesh_transform(idx);125#ifndef DISABLE_DEPRECATED126} else if (what == "navmesh") { // Renamed in 4.0 beta 9.127r_ret = get_item_navigation_mesh(idx);128} else if (what == "navmesh_transform") { // Renamed in 4.0 beta 9.129r_ret = get_item_navigation_mesh_transform(idx);130#endif // DISABLE_DEPRECATED131} else if (what == "navigation_layers") {132r_ret = get_item_navigation_layers(idx);133} else if (what == "preview") {134r_ret = get_item_preview(idx);135} else {136return false;137}138139return true;140}141142void MeshLibrary::_get_property_list(List<PropertyInfo> *p_list) const {143for (const KeyValue<int, Item> &E : item_map) {144String prop_name = vformat("%s/%d/", PNAME("item"), E.key);145p_list->push_back(PropertyInfo(Variant::STRING, prop_name + PNAME("name")));146p_list->push_back(PropertyInfo(Variant::OBJECT, prop_name + PNAME("mesh"), PROPERTY_HINT_RESOURCE_TYPE, "Mesh"));147p_list->push_back(PropertyInfo(Variant::TRANSFORM3D, prop_name + PNAME("mesh_transform"), PROPERTY_HINT_NONE, "suffix:m"));148p_list->push_back(PropertyInfo(Variant::INT, prop_name + PNAME("mesh_cast_shadow"), PROPERTY_HINT_ENUM, "Off,On,Double-Sided,Shadows Only"));149p_list->push_back(PropertyInfo(Variant::ARRAY, prop_name + PNAME("shapes")));150p_list->push_back(PropertyInfo(Variant::OBJECT, prop_name + PNAME("navigation_mesh"), PROPERTY_HINT_RESOURCE_TYPE, "NavigationMesh"));151p_list->push_back(PropertyInfo(Variant::TRANSFORM3D, prop_name + PNAME("navigation_mesh_transform"), PROPERTY_HINT_NONE, "suffix:m"));152p_list->push_back(PropertyInfo(Variant::INT, prop_name + PNAME("navigation_layers"), PROPERTY_HINT_LAYERS_3D_NAVIGATION));153p_list->push_back(PropertyInfo(Variant::OBJECT, prop_name + PNAME("preview"), PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", PROPERTY_USAGE_DEFAULT));154}155}156157void MeshLibrary::create_item(int p_item) {158ERR_FAIL_COND(p_item < 0);159ERR_FAIL_COND(item_map.has(p_item));160item_map[p_item] = Item();161emit_changed();162notify_property_list_changed();163}164165void MeshLibrary::set_item_name(int p_item, const String &p_name) {166ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");167item_map[p_item].name = p_name;168emit_changed();169}170171void MeshLibrary::set_item_mesh(int p_item, const Ref<Mesh> &p_mesh) {172ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");173item_map[p_item].mesh = p_mesh;174emit_changed();175}176177void MeshLibrary::set_item_mesh_transform(int p_item, const Transform3D &p_transform) {178ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");179item_map[p_item].mesh_transform = p_transform;180emit_changed();181}182183void MeshLibrary::set_item_mesh_cast_shadow(int p_item, RS::ShadowCastingSetting p_shadow_casting_setting) {184ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");185item_map[p_item].mesh_cast_shadow = p_shadow_casting_setting;186emit_changed();187}188189#ifndef PHYSICS_3D_DISABLED190void MeshLibrary::set_item_shapes(int p_item, const Vector<ShapeData> &p_shapes) {191ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");192item_map[p_item].shapes = p_shapes;193emit_changed();194notify_property_list_changed();195}196#endif // PHYSICS_3D_DISABLED197198void MeshLibrary::set_item_navigation_mesh(int p_item, const Ref<NavigationMesh> &p_navigation_mesh) {199ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");200item_map[p_item].navigation_mesh = p_navigation_mesh;201emit_changed();202}203204void MeshLibrary::set_item_navigation_mesh_transform(int p_item, const Transform3D &p_transform) {205ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");206item_map[p_item].navigation_mesh_transform = p_transform;207emit_changed();208}209210void MeshLibrary::set_item_navigation_layers(int p_item, uint32_t p_navigation_layers) {211ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");212item_map[p_item].navigation_layers = p_navigation_layers;213emit_changed();214}215216void MeshLibrary::set_item_preview(int p_item, const Ref<Texture2D> &p_preview) {217ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");218item_map[p_item].preview = p_preview;219emit_changed();220}221222String MeshLibrary::get_item_name(int p_item) const {223ERR_FAIL_COND_V_MSG(!item_map.has(p_item), "", "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");224return item_map[p_item].name;225}226227Ref<Mesh> MeshLibrary::get_item_mesh(int p_item) const {228ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Ref<Mesh>(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");229return item_map[p_item].mesh;230}231232Transform3D MeshLibrary::get_item_mesh_transform(int p_item) const {233ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Transform3D(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");234return item_map[p_item].mesh_transform;235}236237RS::ShadowCastingSetting MeshLibrary::get_item_mesh_cast_shadow(int p_item) const {238ERR_FAIL_COND_V_MSG(!item_map.has(p_item), RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_ON, "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");239return item_map[p_item].mesh_cast_shadow;240}241242#ifndef PHYSICS_3D_DISABLED243Vector<MeshLibrary::ShapeData> MeshLibrary::get_item_shapes(int p_item) const {244ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Vector<ShapeData>(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");245return item_map[p_item].shapes;246}247#endif // PHYSICS_3D_DISABLED248249Ref<NavigationMesh> MeshLibrary::get_item_navigation_mesh(int p_item) const {250ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Ref<NavigationMesh>(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");251return item_map[p_item].navigation_mesh;252}253254Transform3D MeshLibrary::get_item_navigation_mesh_transform(int p_item) const {255ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Transform3D(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");256return item_map[p_item].navigation_mesh_transform;257}258259uint32_t MeshLibrary::get_item_navigation_layers(int p_item) const {260ERR_FAIL_COND_V_MSG(!item_map.has(p_item), 0, "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");261return item_map[p_item].navigation_layers;262}263264Ref<Texture2D> MeshLibrary::get_item_preview(int p_item) const {265ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Ref<Texture2D>(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");266return item_map[p_item].preview;267}268269bool MeshLibrary::has_item(int p_item) const {270return item_map.has(p_item);271}272273void MeshLibrary::remove_item(int p_item) {274ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");275item_map.erase(p_item);276notify_property_list_changed();277emit_changed();278}279280void MeshLibrary::clear() {281item_map.clear();282notify_property_list_changed();283emit_changed();284}285286Vector<int> MeshLibrary::get_item_list() const {287Vector<int> ret;288ret.resize(item_map.size());289int idx = 0;290for (const KeyValue<int, Item> &E : item_map) {291ret.write[idx++] = E.key;292}293294return ret;295}296297int MeshLibrary::find_item_by_name(const String &p_name) const {298for (const KeyValue<int, Item> &E : item_map) {299if (E.value.name == p_name) {300return E.key;301}302}303return -1;304}305306int MeshLibrary::get_last_unused_item_id() const {307if (!item_map.size()) {308return 0;309} else {310return item_map.back()->key() + 1;311}312}313314#ifndef PHYSICS_3D_DISABLED315void MeshLibrary::_set_item_shapes(int p_item, const Array &p_shapes) {316Array arr_shapes = p_shapes;317int size = p_shapes.size();318if (size & 1) {319ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");320int prev_size = item_map[p_item].shapes.size() * 2;321322if (prev_size < size) {323// Check if last element is a shape.324Ref<Shape3D> shape = arr_shapes[size - 1];325if (shape.is_null()) {326Ref<BoxShape3D> box_shape;327box_shape.instantiate();328arr_shapes[size - 1] = box_shape;329}330331// Make sure the added element is a Transform3D.332arr_shapes.push_back(Transform3D());333size++;334} else {335size--;336arr_shapes.resize(size);337}338}339340Vector<ShapeData> shapes;341for (int i = 0; i < size; i += 2) {342ShapeData sd;343sd.shape = arr_shapes[i + 0];344sd.local_transform = arr_shapes[i + 1];345346if (sd.shape.is_valid()) {347shapes.push_back(sd);348}349}350351set_item_shapes(p_item, shapes);352}353354Array MeshLibrary::_get_item_shapes(int p_item) const {355Vector<ShapeData> shapes = get_item_shapes(p_item);356Array ret;357for (int i = 0; i < shapes.size(); i++) {358ret.push_back(shapes[i].shape);359ret.push_back(shapes[i].local_transform);360}361362return ret;363}364#endif // PHYSICS_3D_DISABLED365366void MeshLibrary::reset_state() {367clear();368}369void MeshLibrary::_bind_methods() {370ClassDB::bind_method(D_METHOD("create_item", "id"), &MeshLibrary::create_item);371ClassDB::bind_method(D_METHOD("set_item_name", "id", "name"), &MeshLibrary::set_item_name);372ClassDB::bind_method(D_METHOD("set_item_mesh", "id", "mesh"), &MeshLibrary::set_item_mesh);373ClassDB::bind_method(D_METHOD("set_item_mesh_transform", "id", "mesh_transform"), &MeshLibrary::set_item_mesh_transform);374ClassDB::bind_method(D_METHOD("set_item_mesh_cast_shadow", "id", "shadow_casting_setting"), &MeshLibrary::set_item_mesh_cast_shadow);375ClassDB::bind_method(D_METHOD("set_item_navigation_mesh", "id", "navigation_mesh"), &MeshLibrary::set_item_navigation_mesh);376ClassDB::bind_method(D_METHOD("set_item_navigation_mesh_transform", "id", "navigation_mesh"), &MeshLibrary::set_item_navigation_mesh_transform);377ClassDB::bind_method(D_METHOD("set_item_navigation_layers", "id", "navigation_layers"), &MeshLibrary::set_item_navigation_layers);378#ifndef PHYSICS_3D_DISABLED379ClassDB::bind_method(D_METHOD("set_item_shapes", "id", "shapes"), &MeshLibrary::_set_item_shapes);380#endif // PHYSICS_3D_DISABLED381ClassDB::bind_method(D_METHOD("set_item_preview", "id", "texture"), &MeshLibrary::set_item_preview);382ClassDB::bind_method(D_METHOD("get_item_name", "id"), &MeshLibrary::get_item_name);383ClassDB::bind_method(D_METHOD("get_item_mesh", "id"), &MeshLibrary::get_item_mesh);384ClassDB::bind_method(D_METHOD("get_item_mesh_transform", "id"), &MeshLibrary::get_item_mesh_transform);385ClassDB::bind_method(D_METHOD("get_item_mesh_cast_shadow", "id"), &MeshLibrary::get_item_mesh_cast_shadow);386ClassDB::bind_method(D_METHOD("get_item_navigation_mesh", "id"), &MeshLibrary::get_item_navigation_mesh);387ClassDB::bind_method(D_METHOD("get_item_navigation_mesh_transform", "id"), &MeshLibrary::get_item_navigation_mesh_transform);388ClassDB::bind_method(D_METHOD("get_item_navigation_layers", "id"), &MeshLibrary::get_item_navigation_layers);389#ifndef PHYSICS_3D_DISABLED390ClassDB::bind_method(D_METHOD("get_item_shapes", "id"), &MeshLibrary::_get_item_shapes);391#endif // PHYSICS_3D_DISABLED392ClassDB::bind_method(D_METHOD("get_item_preview", "id"), &MeshLibrary::get_item_preview);393ClassDB::bind_method(D_METHOD("remove_item", "id"), &MeshLibrary::remove_item);394ClassDB::bind_method(D_METHOD("find_item_by_name", "name"), &MeshLibrary::find_item_by_name);395396ClassDB::bind_method(D_METHOD("clear"), &MeshLibrary::clear);397ClassDB::bind_method(D_METHOD("get_item_list"), &MeshLibrary::get_item_list);398ClassDB::bind_method(D_METHOD("get_last_unused_item_id"), &MeshLibrary::get_last_unused_item_id);399}400401MeshLibrary::MeshLibrary() {402}403404MeshLibrary::~MeshLibrary() {405}406407408