2020-01-31 19:38:20 +01:00
|
|
|
/*
|
2021-04-19 10:11:02 +02:00
|
|
|
Copyright (c) 2019-2021 Péter Magyar
|
2020-01-31 19:38:20 +01:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2019-12-20 16:14:37 +01:00
|
|
|
#include "mesh_data_resource.h"
|
|
|
|
|
2020-06-21 16:29:28 +02:00
|
|
|
#include "core/version.h"
|
|
|
|
|
2020-10-06 23:17:15 +02:00
|
|
|
#if VERSION_MAJOR >= 4
|
|
|
|
#define PoolVector Vector
|
|
|
|
#endif
|
|
|
|
|
2020-06-30 12:49:29 +02:00
|
|
|
const String MeshDataResource::BINDING_STRING_COLLIDER_TYPE = "None,Trimesh Collision Shape,Single Convex Collision Shape,Multiple Convex Collision Shapes,Approximated Box,Approximated Capsule,Approximated Cylinder,Approximated Sphere";
|
2020-06-21 16:29:28 +02:00
|
|
|
|
2019-12-20 16:14:37 +01:00
|
|
|
Array MeshDataResource::get_array() {
|
|
|
|
return _arrays;
|
|
|
|
}
|
|
|
|
void MeshDataResource::set_array(const Array &p_arrays) {
|
2021-03-23 10:36:00 +01:00
|
|
|
_arrays = p_arrays;
|
2020-08-16 11:33:55 +02:00
|
|
|
|
|
|
|
recompute_aabb();
|
|
|
|
}
|
|
|
|
|
2020-08-16 11:54:54 +02:00
|
|
|
AABB MeshDataResource::get_aabb() const {
|
2020-08-16 11:33:55 +02:00
|
|
|
return _aabb;
|
|
|
|
}
|
|
|
|
void MeshDataResource::set_aabb(const AABB &aabb) {
|
|
|
|
_aabb = aabb;
|
2019-12-20 16:14:37 +01:00
|
|
|
}
|
|
|
|
|
2020-06-30 15:23:27 +02:00
|
|
|
void MeshDataResource::add_collision_shape(const Transform &transform, const Ref<Shape> &shape) {
|
2020-06-30 14:42:12 +02:00
|
|
|
MDRData d;
|
2020-06-21 16:29:28 +02:00
|
|
|
|
2020-06-30 15:23:27 +02:00
|
|
|
d.transform = transform;
|
2020-06-30 14:42:12 +02:00
|
|
|
d.shape = shape;
|
|
|
|
|
|
|
|
_collision_shapes.push_back(d);
|
2020-06-21 16:29:28 +02:00
|
|
|
}
|
|
|
|
Ref<Shape> MeshDataResource::get_collision_shape(const int index) {
|
|
|
|
ERR_FAIL_INDEX_V(index, _collision_shapes.size(), Ref<Shape>());
|
|
|
|
|
2020-06-30 14:42:12 +02:00
|
|
|
return _collision_shapes[index].shape;
|
|
|
|
}
|
2020-06-30 15:23:27 +02:00
|
|
|
Transform MeshDataResource::get_collision_shape_offset(const int index) {
|
|
|
|
ERR_FAIL_INDEX_V(index, _collision_shapes.size(), Transform());
|
2020-06-30 14:42:12 +02:00
|
|
|
|
2020-06-30 15:23:27 +02:00
|
|
|
return _collision_shapes[index].transform;
|
2020-06-21 16:29:28 +02:00
|
|
|
}
|
|
|
|
int MeshDataResource::get_collision_shape_count() const {
|
|
|
|
return _collision_shapes.size();
|
|
|
|
}
|
|
|
|
|
2020-06-30 14:42:12 +02:00
|
|
|
Vector<Variant> MeshDataResource::get_collision_shapes() {
|
2020-06-21 16:29:28 +02:00
|
|
|
Vector<Variant> r;
|
|
|
|
for (int i = 0; i < _collision_shapes.size(); i++) {
|
|
|
|
#if VERSION_MAJOR < 4
|
2020-06-30 15:23:27 +02:00
|
|
|
r.push_back(_collision_shapes[i].transform);
|
2020-06-30 14:42:12 +02:00
|
|
|
r.push_back(_collision_shapes[i].shape.get_ref_ptr());
|
2020-06-21 16:29:28 +02:00
|
|
|
#else
|
2020-07-29 00:02:05 +02:00
|
|
|
r.push_back(_collision_shapes[i].transform);
|
2020-06-30 14:42:12 +02:00
|
|
|
r.push_back(_collision_shapes[i].shape);
|
2020-06-21 16:29:28 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
2020-06-30 14:42:12 +02:00
|
|
|
void MeshDataResource::set_collision_shapes(const Vector<Variant> &p_arrays) {
|
|
|
|
ERR_FAIL_COND(p_arrays.size() % 2 == 1);
|
|
|
|
|
2020-06-21 16:29:28 +02:00
|
|
|
_collision_shapes.clear();
|
2020-06-30 14:42:12 +02:00
|
|
|
for (int i = 0; i < p_arrays.size(); i += 2) {
|
|
|
|
MDRData d;
|
|
|
|
|
2020-06-30 15:23:27 +02:00
|
|
|
d.transform = p_arrays[i];
|
2020-06-30 14:42:12 +02:00
|
|
|
d.shape = Ref<Shape>(p_arrays[i + 1]);
|
|
|
|
|
|
|
|
_collision_shapes.push_back(d);
|
2020-06-21 16:29:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-16 11:33:55 +02:00
|
|
|
void MeshDataResource::recompute_aabb() {
|
2021-03-23 10:35:03 +01:00
|
|
|
if (_arrays.size() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-16 11:33:55 +02:00
|
|
|
Variant arr = _arrays[Mesh::ARRAY_VERTEX];
|
|
|
|
PoolVector<Vector3> vertices = arr;
|
|
|
|
int len = vertices.size();
|
2021-03-23 10:35:03 +01:00
|
|
|
|
|
|
|
if (len == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2020-10-06 23:17:15 +02:00
|
|
|
|
|
|
|
#if VERSION_MAJOR < 4
|
2020-08-16 11:33:55 +02:00
|
|
|
PoolVector<Vector3>::Read r = vertices.read();
|
|
|
|
const Vector3 *vtx = r.ptr();
|
2020-10-06 23:17:15 +02:00
|
|
|
#else
|
|
|
|
const Vector3 *vtx = vertices.ptr();
|
|
|
|
#endif
|
2020-08-16 11:33:55 +02:00
|
|
|
|
|
|
|
AABB aabb;
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
if (i == 0)
|
|
|
|
aabb.position = vtx[i];
|
|
|
|
else
|
|
|
|
aabb.expand_to(vtx[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
_aabb = aabb;
|
|
|
|
}
|
|
|
|
|
2019-12-20 16:14:37 +01:00
|
|
|
MeshDataResource::MeshDataResource() {
|
|
|
|
}
|
|
|
|
|
2020-06-21 16:29:28 +02:00
|
|
|
MeshDataResource::~MeshDataResource() {
|
|
|
|
_arrays.clear();
|
|
|
|
_collision_shapes.clear();
|
|
|
|
}
|
|
|
|
|
2019-12-20 16:14:37 +01:00
|
|
|
void MeshDataResource::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("get_array"), &MeshDataResource::get_array);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_array", "array"), &MeshDataResource::set_array);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "array"), "set_array", "get_array");
|
2020-06-21 16:29:28 +02:00
|
|
|
|
2020-08-16 11:33:55 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_aabb"), &MeshDataResource::get_aabb);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_aabb", "array"), &MeshDataResource::set_aabb);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::AABB, "aabb"), "set_aabb", "get_aabb");
|
|
|
|
|
2020-06-30 14:42:12 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_collision_shapes"), &MeshDataResource::get_collision_shapes);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_collision_shapes", "array"), &MeshDataResource::set_collision_shapes);
|
2020-06-21 16:29:28 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "collision_shapes"), "set_collision_shapes", "get_collision_shapes");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("add_collision_shape", "shape"), &MeshDataResource::add_collision_shape);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_collision_shape", "index"), &MeshDataResource::get_collision_shape);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_collision_shape_count"), &MeshDataResource::get_collision_shape_count);
|
2020-08-16 11:33:55 +02:00
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("recompute_aabb"), &MeshDataResource::recompute_aabb);
|
2019-12-20 16:14:37 +01:00
|
|
|
}
|