Path: blob/master/scene/resources/3d/world_boundary_shape_3d.cpp
9898 views
/**************************************************************************/1/* world_boundary_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 "world_boundary_shape_3d.h"3132#include "scene/resources/mesh.h"33#include "servers/physics_server_3d.h"3435Vector<Vector3> WorldBoundaryShape3D::get_debug_mesh_lines() const {36Plane p = get_plane();3738Vector3 n1 = p.get_any_perpendicular_normal();39Vector3 n2 = p.normal.cross(n1).normalized();4041Vector3 pface[4] = {42p.normal * p.d + n1 * 10.0 + n2 * 10.0,43p.normal * p.d + n1 * 10.0 + n2 * -10.0,44p.normal * p.d + n1 * -10.0 + n2 * -10.0,45p.normal * p.d + n1 * -10.0 + n2 * 10.0,46};4748Vector<Vector3> points = {49pface[0],50pface[1],51pface[1],52pface[2],53pface[2],54pface[3],55pface[3],56pface[0],57p.normal * p.d,58p.normal * p.d + p.normal * 359};6061return points;62}6364Ref<ArrayMesh> WorldBoundaryShape3D::get_debug_arraymesh_faces(const Color &p_modulate) const {65Plane p = get_plane();6667Vector3 n1 = p.get_any_perpendicular_normal();68Vector3 n2 = p.normal.cross(n1).normalized();6970Vector3 pface[4] = {71p.normal * p.d + n1 * 10.0 + n2 * 10.0,72p.normal * p.d + n1 * 10.0 + n2 * -10.0,73p.normal * p.d + n1 * -10.0 + n2 * -10.0,74p.normal * p.d + n1 * -10.0 + n2 * 10.0,75};7677Vector<Vector3> points = {78pface[0],79pface[1],80pface[2],81pface[3],82};8384Vector<Color> colors = {85p_modulate,86p_modulate,87p_modulate,88p_modulate,89};9091Vector<int> indices = {920,931,942,950,962,973,98};99100Ref<ArrayMesh> mesh = memnew(ArrayMesh);101Array a;102a.resize(Mesh::ARRAY_MAX);103a[RS::ARRAY_VERTEX] = points;104a[RS::ARRAY_COLOR] = colors;105a[RS::ARRAY_INDEX] = indices;106mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, a);107108return mesh;109}110111void WorldBoundaryShape3D::_update_shape() {112PhysicsServer3D::get_singleton()->shape_set_data(get_shape(), plane);113Shape3D::_update_shape();114}115116void WorldBoundaryShape3D::set_plane(const Plane &p_plane) {117plane = p_plane;118_update_shape();119emit_changed();120}121122const Plane &WorldBoundaryShape3D::get_plane() const {123return plane;124}125126void WorldBoundaryShape3D::_bind_methods() {127ClassDB::bind_method(D_METHOD("set_plane", "plane"), &WorldBoundaryShape3D::set_plane);128ClassDB::bind_method(D_METHOD("get_plane"), &WorldBoundaryShape3D::get_plane);129130ADD_PROPERTY(PropertyInfo(Variant::PLANE, "plane", PROPERTY_HINT_NONE, "suffix:m"), "set_plane", "get_plane");131}132133WorldBoundaryShape3D::WorldBoundaryShape3D() :134Shape3D(PhysicsServer3D::get_singleton()->shape_create(PhysicsServer3D::SHAPE_WORLD_BOUNDARY)) {135set_plane(Plane(0, 1, 0, 0));136}137138139