Path: blob/master/scene/resources/3d/box_shape_3d.cpp
9898 views
/**************************************************************************/1/* box_shape_3d.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 "box_shape_3d.h"3132#include "scene/resources/3d/primitive_meshes.h"33#include "servers/physics_server_3d.h"3435Vector<Vector3> BoxShape3D::get_debug_mesh_lines() const {36Vector<Vector3> lines;37AABB aabb;38aabb.position = -size / 2;39aabb.size = size;4041for (int i = 0; i < 12; i++) {42Vector3 a, b;43aabb.get_edge(i, a, b);44lines.push_back(a);45lines.push_back(b);46}4748return lines;49}5051Ref<ArrayMesh> BoxShape3D::get_debug_arraymesh_faces(const Color &p_modulate) const {52Array box_array;53box_array.resize(RS::ARRAY_MAX);54BoxMesh::create_mesh_array(box_array, size);5556Vector<Color> colors;57const PackedVector3Array &verts = box_array[RS::ARRAY_VERTEX];58const int32_t verts_size = verts.size();59for (int i = 0; i < verts_size; i++) {60colors.append(p_modulate);61}6263Ref<ArrayMesh> box_mesh = memnew(ArrayMesh);64box_array[RS::ARRAY_COLOR] = colors;65box_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, box_array);66return box_mesh;67}6869real_t BoxShape3D::get_enclosing_radius() const {70return size.length() / 2;71}7273void BoxShape3D::_update_shape() {74PhysicsServer3D::get_singleton()->shape_set_data(get_shape(), size / 2);75Shape3D::_update_shape();76}7778#ifndef DISABLE_DEPRECATED79bool BoxShape3D::_set(const StringName &p_name, const Variant &p_value) {80if (p_name == "extents") { // Compatibility with Godot 3.x.81// Convert to `size`, twice as big.82set_size((Vector3)p_value * 2);83return true;84}85return false;86}8788bool BoxShape3D::_get(const StringName &p_name, Variant &r_property) const {89if (p_name == "extents") { // Compatibility with Godot 3.x.90// Convert to `extents`, half as big.91r_property = size / 2;92return true;93}94return false;95}96#endif // DISABLE_DEPRECATED9798void BoxShape3D::set_size(const Vector3 &p_size) {99ERR_FAIL_COND_MSG(p_size.x < 0 || p_size.y < 0 || p_size.z < 0, "BoxShape3D size cannot be negative.");100size = p_size;101_update_shape();102emit_changed();103}104105Vector3 BoxShape3D::get_size() const {106return size;107}108109void BoxShape3D::_bind_methods() {110ClassDB::bind_method(D_METHOD("set_size", "size"), &BoxShape3D::set_size);111ClassDB::bind_method(D_METHOD("get_size"), &BoxShape3D::get_size);112113ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "size", PROPERTY_HINT_NONE, "suffix:m"), "set_size", "get_size");114}115116BoxShape3D::BoxShape3D() :117Shape3D(PhysicsServer3D::get_singleton()->shape_create(PhysicsServer3D::SHAPE_BOX)) {118set_size(Vector3(1, 1, 1));119}120121122