Path: blob/master/scene/resources/3d/mesh_library.cpp
21147 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#include "scene/resources/texture.h"3334#ifndef PHYSICS_3D_DISABLED35#include "box_shape_3d.h"36#endif // PHYSICS_3D_DISABLED3738bool MeshLibrary::_set(const StringName &p_name, const Variant &p_value) {39String prop_name = p_name;40if (prop_name.begins_with("item/")) {41int idx = prop_name.get_slicec('/', 1).to_int();42String what = prop_name.get_slicec('/', 2);43if (!item_map.has(idx)) {44create_item(idx);45}4647if (what == "name") {48set_item_name(idx, p_value);49} else if (what == "mesh") {50set_item_mesh(idx, p_value);51} else if (what == "mesh_transform") {52set_item_mesh_transform(idx, p_value);53} else if (what == "mesh_cast_shadow") {54switch ((int)p_value) {55case 0: {56set_item_mesh_cast_shadow(idx, RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_OFF);57} break;58case 1: {59set_item_mesh_cast_shadow(idx, RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_ON);60} break;61case 2: {62set_item_mesh_cast_shadow(idx, RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_DOUBLE_SIDED);63} break;64case 3: {65set_item_mesh_cast_shadow(idx, RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_SHADOWS_ONLY);66} break;67default: {68set_item_mesh_cast_shadow(idx, RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_ON);69} break;70}71#ifndef PHYSICS_3D_DISABLED72} else if (what == "shape") {73Vector<ShapeData> shapes;74ShapeData sd;75sd.shape = p_value;76shapes.push_back(sd);77set_item_shapes(idx, shapes);78} else if (what == "shapes") {79_set_item_shapes(idx, p_value);80#endif // PHYSICS_3D_DISABLED81} else if (what == "preview") {82set_item_preview(idx, p_value);83} else if (what == "navigation_mesh") {84set_item_navigation_mesh(idx, p_value);85} else if (what == "navigation_mesh_transform") {86set_item_navigation_mesh_transform(idx, p_value);87#ifndef DISABLE_DEPRECATED88} else if (what == "navmesh") { // Renamed in 4.0 beta 9.89set_item_navigation_mesh(idx, p_value);90} else if (what == "navmesh_transform") { // Renamed in 4.0 beta 9.91set_item_navigation_mesh_transform(idx, p_value);92#endif // DISABLE_DEPRECATED93} else if (what == "navigation_layers") {94set_item_navigation_layers(idx, p_value);95} else {96return false;97}9899return true;100}101102return false;103}104105bool MeshLibrary::_get(const StringName &p_name, Variant &r_ret) const {106String prop_name = p_name;107int idx = prop_name.get_slicec('/', 1).to_int();108ERR_FAIL_COND_V(!item_map.has(idx), false);109String what = prop_name.get_slicec('/', 2);110111if (what == "name") {112r_ret = get_item_name(idx);113} else if (what == "mesh") {114r_ret = get_item_mesh(idx);115} else if (what == "mesh_transform") {116r_ret = get_item_mesh_transform(idx);117} else if (what == "mesh_cast_shadow") {118r_ret = (int)get_item_mesh_cast_shadow(idx);119#ifndef PHYSICS_3D_DISABLED120} else if (what == "shapes") {121r_ret = _get_item_shapes(idx);122#endif // PHYSICS_3D_DISABLED123} else if (what == "navigation_mesh") {124r_ret = get_item_navigation_mesh(idx);125} else if (what == "navigation_mesh_transform") {126r_ret = get_item_navigation_mesh_transform(idx);127#ifndef DISABLE_DEPRECATED128} else if (what == "navmesh") { // Renamed in 4.0 beta 9.129r_ret = get_item_navigation_mesh(idx);130} else if (what == "navmesh_transform") { // Renamed in 4.0 beta 9.131r_ret = get_item_navigation_mesh_transform(idx);132#endif // DISABLE_DEPRECATED133} else if (what == "navigation_layers") {134r_ret = get_item_navigation_layers(idx);135} else if (what == "preview") {136r_ret = get_item_preview(idx);137} else {138return false;139}140141return true;142}143144void MeshLibrary::_get_property_list(List<PropertyInfo> *p_list) const {145for (const KeyValue<int, Item> &E : item_map) {146String prop_name = vformat("%s/%d/", PNAME("item"), E.key);147p_list->push_back(PropertyInfo(Variant::STRING, prop_name + PNAME("name")));148p_list->push_back(PropertyInfo(Variant::OBJECT, prop_name + PNAME("mesh"), PROPERTY_HINT_RESOURCE_TYPE, Mesh::get_class_static()));149p_list->push_back(PropertyInfo(Variant::TRANSFORM3D, prop_name + PNAME("mesh_transform"), PROPERTY_HINT_NONE, "suffix:m"));150p_list->push_back(PropertyInfo(Variant::INT, prop_name + PNAME("mesh_cast_shadow"), PROPERTY_HINT_ENUM, "Off,On,Double-Sided,Shadows Only"));151p_list->push_back(PropertyInfo(Variant::ARRAY, prop_name + PNAME("shapes")));152p_list->push_back(PropertyInfo(Variant::OBJECT, prop_name + PNAME("navigation_mesh"), PROPERTY_HINT_RESOURCE_TYPE, NavigationMesh::get_class_static()));153p_list->push_back(PropertyInfo(Variant::TRANSFORM3D, prop_name + PNAME("navigation_mesh_transform"), PROPERTY_HINT_NONE, "suffix:m"));154p_list->push_back(PropertyInfo(Variant::INT, prop_name + PNAME("navigation_layers"), PROPERTY_HINT_LAYERS_3D_NAVIGATION));155p_list->push_back(PropertyInfo(Variant::OBJECT, prop_name + PNAME("preview"), PROPERTY_HINT_RESOURCE_TYPE, Texture2D::get_class_static(), PROPERTY_USAGE_DEFAULT));156}157}158159void MeshLibrary::create_item(int p_item) {160ERR_FAIL_COND(p_item < 0);161ERR_FAIL_COND(item_map.has(p_item));162item_map[p_item] = Item();163emit_changed();164notify_property_list_changed();165}166167void MeshLibrary::set_item_name(int p_item, const String &p_name) {168ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");169item_map[p_item].name = p_name;170emit_changed();171}172173void MeshLibrary::set_item_mesh(int p_item, const Ref<Mesh> &p_mesh) {174ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");175item_map[p_item].mesh = p_mesh;176emit_changed();177}178179void MeshLibrary::set_item_mesh_transform(int p_item, const Transform3D &p_transform) {180ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");181item_map[p_item].mesh_transform = p_transform;182emit_changed();183}184185void MeshLibrary::set_item_mesh_cast_shadow(int p_item, RS::ShadowCastingSetting p_shadow_casting_setting) {186ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");187item_map[p_item].mesh_cast_shadow = p_shadow_casting_setting;188emit_changed();189}190191#ifndef PHYSICS_3D_DISABLED192void MeshLibrary::set_item_shapes(int p_item, const Vector<ShapeData> &p_shapes) {193ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");194item_map[p_item].shapes = p_shapes;195emit_changed();196notify_property_list_changed();197}198#endif // PHYSICS_3D_DISABLED199200void MeshLibrary::set_item_navigation_mesh(int p_item, const Ref<NavigationMesh> &p_navigation_mesh) {201ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");202item_map[p_item].navigation_mesh = p_navigation_mesh;203emit_changed();204}205206void MeshLibrary::set_item_navigation_mesh_transform(int p_item, const Transform3D &p_transform) {207ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");208item_map[p_item].navigation_mesh_transform = p_transform;209emit_changed();210}211212void MeshLibrary::set_item_navigation_layers(int p_item, uint32_t p_navigation_layers) {213ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");214item_map[p_item].navigation_layers = p_navigation_layers;215emit_changed();216}217218void MeshLibrary::set_item_preview(int p_item, const Ref<Texture2D> &p_preview) {219ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");220item_map[p_item].preview = p_preview;221emit_changed();222}223224String MeshLibrary::get_item_name(int p_item) const {225ERR_FAIL_COND_V_MSG(!item_map.has(p_item), "", "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");226return item_map[p_item].name;227}228229Ref<Mesh> MeshLibrary::get_item_mesh(int p_item) const {230ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Ref<Mesh>(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");231return item_map[p_item].mesh;232}233234Transform3D MeshLibrary::get_item_mesh_transform(int p_item) const {235ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Transform3D(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");236return item_map[p_item].mesh_transform;237}238239RS::ShadowCastingSetting MeshLibrary::get_item_mesh_cast_shadow(int p_item) const {240ERR_FAIL_COND_V_MSG(!item_map.has(p_item), RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_ON, "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");241return item_map[p_item].mesh_cast_shadow;242}243244#ifndef PHYSICS_3D_DISABLED245Vector<MeshLibrary::ShapeData> MeshLibrary::get_item_shapes(int p_item) const {246ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Vector<ShapeData>(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");247return item_map[p_item].shapes;248}249#endif // PHYSICS_3D_DISABLED250251Ref<NavigationMesh> MeshLibrary::get_item_navigation_mesh(int p_item) const {252ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Ref<NavigationMesh>(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");253return item_map[p_item].navigation_mesh;254}255256Transform3D MeshLibrary::get_item_navigation_mesh_transform(int p_item) const {257ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Transform3D(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");258return item_map[p_item].navigation_mesh_transform;259}260261uint32_t MeshLibrary::get_item_navigation_layers(int p_item) const {262ERR_FAIL_COND_V_MSG(!item_map.has(p_item), 0, "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");263return item_map[p_item].navigation_layers;264}265266Ref<Texture2D> MeshLibrary::get_item_preview(int p_item) const {267ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Ref<Texture2D>(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");268return item_map[p_item].preview;269}270271bool MeshLibrary::has_item(int p_item) const {272return item_map.has(p_item);273}274275void MeshLibrary::remove_item(int p_item) {276ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");277item_map.erase(p_item);278notify_property_list_changed();279emit_changed();280}281282void MeshLibrary::clear() {283item_map.clear();284notify_property_list_changed();285emit_changed();286}287288Vector<int> MeshLibrary::get_item_list() const {289Vector<int> ret;290ret.resize(item_map.size());291int idx = 0;292for (const KeyValue<int, Item> &E : item_map) {293ret.write[idx++] = E.key;294}295296return ret;297}298299int MeshLibrary::find_item_by_name(const String &p_name) const {300for (const KeyValue<int, Item> &E : item_map) {301if (E.value.name == p_name) {302return E.key;303}304}305return -1;306}307308int MeshLibrary::get_last_unused_item_id() const {309if (!item_map.size()) {310return 0;311} else {312return item_map.back()->key() + 1;313}314}315316#ifndef PHYSICS_3D_DISABLED317void MeshLibrary::_set_item_shapes(int p_item, const Array &p_shapes) {318Array arr_shapes = p_shapes;319int size = p_shapes.size();320if (size & 1) {321ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");322int prev_size = item_map[p_item].shapes.size() * 2;323324if (prev_size < size) {325// Check if last element is a shape.326Ref<Shape3D> shape = arr_shapes[size - 1];327if (shape.is_null()) {328Ref<BoxShape3D> box_shape;329box_shape.instantiate();330arr_shapes[size - 1] = box_shape;331}332333// Make sure the added element is a Transform3D.334arr_shapes.push_back(Transform3D());335size++;336} else {337size--;338arr_shapes.resize(size);339}340}341342Vector<ShapeData> shapes;343for (int i = 0; i < size; i += 2) {344ShapeData sd;345sd.shape = arr_shapes[i + 0];346sd.local_transform = arr_shapes[i + 1];347348if (sd.shape.is_valid()) {349shapes.push_back(sd);350}351}352353set_item_shapes(p_item, shapes);354}355356Array MeshLibrary::_get_item_shapes(int p_item) const {357Vector<ShapeData> shapes = get_item_shapes(p_item);358Array ret;359for (int i = 0; i < shapes.size(); i++) {360ret.push_back(shapes[i].shape);361ret.push_back(shapes[i].local_transform);362}363364return ret;365}366#endif // PHYSICS_3D_DISABLED367368void MeshLibrary::reset_state() {369clear();370}371void MeshLibrary::_bind_methods() {372ClassDB::bind_method(D_METHOD("create_item", "id"), &MeshLibrary::create_item);373ClassDB::bind_method(D_METHOD("set_item_name", "id", "name"), &MeshLibrary::set_item_name);374ClassDB::bind_method(D_METHOD("set_item_mesh", "id", "mesh"), &MeshLibrary::set_item_mesh);375ClassDB::bind_method(D_METHOD("set_item_mesh_transform", "id", "mesh_transform"), &MeshLibrary::set_item_mesh_transform);376ClassDB::bind_method(D_METHOD("set_item_mesh_cast_shadow", "id", "shadow_casting_setting"), &MeshLibrary::set_item_mesh_cast_shadow);377ClassDB::bind_method(D_METHOD("set_item_navigation_mesh", "id", "navigation_mesh"), &MeshLibrary::set_item_navigation_mesh);378ClassDB::bind_method(D_METHOD("set_item_navigation_mesh_transform", "id", "navigation_mesh"), &MeshLibrary::set_item_navigation_mesh_transform);379ClassDB::bind_method(D_METHOD("set_item_navigation_layers", "id", "navigation_layers"), &MeshLibrary::set_item_navigation_layers);380#ifndef PHYSICS_3D_DISABLED381ClassDB::bind_method(D_METHOD("set_item_shapes", "id", "shapes"), &MeshLibrary::_set_item_shapes);382#endif // PHYSICS_3D_DISABLED383ClassDB::bind_method(D_METHOD("set_item_preview", "id", "texture"), &MeshLibrary::set_item_preview);384ClassDB::bind_method(D_METHOD("get_item_name", "id"), &MeshLibrary::get_item_name);385ClassDB::bind_method(D_METHOD("get_item_mesh", "id"), &MeshLibrary::get_item_mesh);386ClassDB::bind_method(D_METHOD("get_item_mesh_transform", "id"), &MeshLibrary::get_item_mesh_transform);387ClassDB::bind_method(D_METHOD("get_item_mesh_cast_shadow", "id"), &MeshLibrary::get_item_mesh_cast_shadow);388ClassDB::bind_method(D_METHOD("get_item_navigation_mesh", "id"), &MeshLibrary::get_item_navigation_mesh);389ClassDB::bind_method(D_METHOD("get_item_navigation_mesh_transform", "id"), &MeshLibrary::get_item_navigation_mesh_transform);390ClassDB::bind_method(D_METHOD("get_item_navigation_layers", "id"), &MeshLibrary::get_item_navigation_layers);391#ifndef PHYSICS_3D_DISABLED392ClassDB::bind_method(D_METHOD("get_item_shapes", "id"), &MeshLibrary::_get_item_shapes);393#endif // PHYSICS_3D_DISABLED394ClassDB::bind_method(D_METHOD("get_item_preview", "id"), &MeshLibrary::get_item_preview);395ClassDB::bind_method(D_METHOD("remove_item", "id"), &MeshLibrary::remove_item);396ClassDB::bind_method(D_METHOD("find_item_by_name", "name"), &MeshLibrary::find_item_by_name);397398ClassDB::bind_method(D_METHOD("clear"), &MeshLibrary::clear);399ClassDB::bind_method(D_METHOD("get_item_list"), &MeshLibrary::get_item_list);400ClassDB::bind_method(D_METHOD("get_last_unused_item_id"), &MeshLibrary::get_last_unused_item_id);401}402403MeshLibrary::MeshLibrary() {404}405406MeshLibrary::~MeshLibrary() {407}408409410