2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* mesh.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 01:10:30 +00: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. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "mesh.h"
|
2017-12-09 18:36:32 +00:00
|
|
|
|
2020-12-30 14:36:39 +00:00
|
|
|
#include "core/crypto/crypto_core.h"
|
2020-12-19 17:17:22 +00:00
|
|
|
#include "core/local_vector.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/pair.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "scene/resources/concave_polygon_shape.h"
|
|
|
|
#include "scene/resources/convex_polygon_shape.h"
|
2014-10-14 04:01:25 +00:00
|
|
|
#include "surface_tool.h"
|
2017-12-09 18:36:32 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2021-05-04 14:00:45 +00:00
|
|
|
Mesh::ConvexDecompositionFunc Mesh::convex_composition_function = nullptr;
|
2019-04-10 20:46:04 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
Ref<TriangleMesh> Mesh::generate_triangle_mesh() const {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (triangle_mesh.is_valid()) {
|
2017-06-07 21:18:55 +00:00
|
|
|
return triangle_mesh;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
int facecount = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
for (int i = 0; i < get_surface_count(); i++) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (surface_get_primitive_type(i) != PRIMITIVE_TRIANGLES) {
|
2017-06-07 21:18:55 +00:00
|
|
|
continue;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
if (surface_get_format(i) & ARRAY_FORMAT_INDEX) {
|
|
|
|
facecount += surface_get_array_index_len(i);
|
|
|
|
} else {
|
|
|
|
facecount += surface_get_array_len(i);
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (facecount == 0 || (facecount % 3) != 0) {
|
2017-06-07 21:18:55 +00:00
|
|
|
return triangle_mesh;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-05-14 04:22:15 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
PoolVector<Vector3> faces;
|
|
|
|
faces.resize(facecount);
|
|
|
|
PoolVector<Vector3>::Write facesw = faces.write();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
int widx = 0;
|
2014-05-05 01:50:23 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
for (int i = 0; i < get_surface_count(); i++) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (surface_get_primitive_type(i) != PRIMITIVE_TRIANGLES) {
|
2017-06-07 21:18:55 +00:00
|
|
|
continue;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
Array a = surface_get_arrays(i);
|
2019-12-11 00:57:57 +00:00
|
|
|
ERR_FAIL_COND_V(a.empty(), Ref<TriangleMesh>());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
int vc = surface_get_array_len(i);
|
|
|
|
PoolVector<Vector3> vertices = a[ARRAY_VERTEX];
|
|
|
|
PoolVector<Vector3>::Read vr = vertices.read();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
if (surface_get_format(i) & ARRAY_FORMAT_INDEX) {
|
|
|
|
int ic = surface_get_array_index_len(i);
|
|
|
|
PoolVector<int> indices = a[ARRAY_INDEX];
|
|
|
|
PoolVector<int>::Read ir = indices.read();
|
2016-11-10 02:55:06 +00:00
|
|
|
|
2019-02-12 20:10:08 +00:00
|
|
|
for (int j = 0; j < ic; j++) {
|
|
|
|
int index = ir[j];
|
2017-06-07 21:18:55 +00:00
|
|
|
facesw[widx++] = vr[index];
|
|
|
|
}
|
2016-11-10 02:55:06 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
} else {
|
2021-05-05 10:44:11 +00:00
|
|
|
for (int j = 0; j < vc; j++) {
|
2019-02-12 20:10:08 +00:00
|
|
|
facesw[widx++] = vr[j];
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-06-07 21:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-10 02:55:06 +00:00
|
|
|
|
2019-07-05 17:08:43 +00:00
|
|
|
facesw.release();
|
2016-11-10 02:55:06 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
triangle_mesh = Ref<TriangleMesh>(memnew(TriangleMesh));
|
|
|
|
triangle_mesh->create(faces);
|
2016-11-10 02:55:06 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
return triangle_mesh;
|
|
|
|
}
|
2016-11-10 02:55:06 +00:00
|
|
|
|
2017-11-21 00:36:32 +00:00
|
|
|
void Mesh::generate_debug_mesh_lines(Vector<Vector3> &r_lines) {
|
2018-09-09 17:39:34 +00:00
|
|
|
if (debug_lines.size() > 0) {
|
|
|
|
r_lines = debug_lines;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-21 00:36:32 +00:00
|
|
|
Ref<TriangleMesh> tm = generate_triangle_mesh();
|
2021-05-05 10:44:11 +00:00
|
|
|
if (tm.is_null()) {
|
2017-11-21 00:36:32 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-11-21 00:36:32 +00:00
|
|
|
|
|
|
|
PoolVector<int> triangle_indices;
|
|
|
|
tm->get_indices(&triangle_indices);
|
|
|
|
const int triangles_num = tm->get_triangles().size();
|
|
|
|
PoolVector<Vector3> vertices = tm->get_vertices();
|
|
|
|
|
2018-09-09 17:39:34 +00:00
|
|
|
debug_lines.resize(tm->get_triangles().size() * 6); // 3 lines x 2 points each line
|
2017-11-21 00:36:32 +00:00
|
|
|
|
|
|
|
PoolVector<int>::Read ind_r = triangle_indices.read();
|
|
|
|
PoolVector<Vector3>::Read ver_r = vertices.read();
|
|
|
|
for (int j = 0, x = 0, i = 0; i < triangles_num; j += 6, x += 3, ++i) {
|
|
|
|
// Triangle line 1
|
2018-09-09 17:39:34 +00:00
|
|
|
debug_lines.write[j + 0] = ver_r[ind_r[x + 0]];
|
|
|
|
debug_lines.write[j + 1] = ver_r[ind_r[x + 1]];
|
2017-11-21 00:36:32 +00:00
|
|
|
|
|
|
|
// Triangle line 2
|
2018-09-09 17:39:34 +00:00
|
|
|
debug_lines.write[j + 2] = ver_r[ind_r[x + 1]];
|
|
|
|
debug_lines.write[j + 3] = ver_r[ind_r[x + 2]];
|
2017-11-21 00:36:32 +00:00
|
|
|
|
|
|
|
// Triangle line 3
|
2018-09-09 17:39:34 +00:00
|
|
|
debug_lines.write[j + 4] = ver_r[ind_r[x + 2]];
|
|
|
|
debug_lines.write[j + 5] = ver_r[ind_r[x + 0]];
|
2017-11-21 00:36:32 +00:00
|
|
|
}
|
2018-09-09 17:39:34 +00:00
|
|
|
|
|
|
|
r_lines = debug_lines;
|
2017-11-21 00:36:32 +00:00
|
|
|
}
|
|
|
|
void Mesh::generate_debug_mesh_indices(Vector<Vector3> &r_points) {
|
|
|
|
Ref<TriangleMesh> tm = generate_triangle_mesh();
|
2021-05-05 10:44:11 +00:00
|
|
|
if (tm.is_null()) {
|
2017-11-21 00:36:32 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-11-21 00:36:32 +00:00
|
|
|
|
|
|
|
PoolVector<Vector3> vertices = tm->get_vertices();
|
|
|
|
|
|
|
|
int vertices_size = vertices.size();
|
|
|
|
r_points.resize(vertices_size);
|
|
|
|
for (int i = 0; i < vertices_size; ++i) {
|
2018-07-25 01:11:03 +00:00
|
|
|
r_points.write[i] = vertices[i];
|
2017-11-21 00:36:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Mesh::surface_is_softbody_friendly(int p_idx) const {
|
|
|
|
const uint32_t surface_format = surface_get_format(p_idx);
|
|
|
|
return (surface_format & Mesh::ARRAY_FLAG_USE_DYNAMIC_UPDATE && (!(surface_format & Mesh::ARRAY_COMPRESS_VERTEX)) && (!(surface_format & Mesh::ARRAY_COMPRESS_NORMAL)));
|
|
|
|
}
|
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
PoolVector<Face3> Mesh::get_faces() const {
|
|
|
|
Ref<TriangleMesh> tm = generate_triangle_mesh();
|
2021-05-05 10:44:11 +00:00
|
|
|
if (tm.is_valid()) {
|
2017-06-07 21:18:55 +00:00
|
|
|
return tm->get_faces();
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-06-07 21:18:55 +00:00
|
|
|
return PoolVector<Face3>();
|
|
|
|
/*
|
|
|
|
for (int i=0;i<surfaces.size();i++) {
|
2016-11-10 02:55:06 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
if (VisualServer::get_singleton()->mesh_surface_get_primitive_type( mesh, i ) != VisualServer::PRIMITIVE_TRIANGLES )
|
|
|
|
continue;
|
2016-11-10 02:55:06 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
PoolVector<int> indices;
|
|
|
|
PoolVector<Vector3> vertices;
|
2016-11-10 02:55:06 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
vertices=VisualServer::get_singleton()->mesh_surface_get_array(mesh, i,VisualServer::ARRAY_VERTEX);
|
2016-11-10 02:55:06 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
int len=VisualServer::get_singleton()->mesh_surface_get_array_index_len(mesh, i);
|
|
|
|
bool has_indices;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
if (len>0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
indices=VisualServer::get_singleton()->mesh_surface_get_array(mesh, i,VisualServer::ARRAY_INDEX);
|
|
|
|
has_indices=true;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
} else {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
len=vertices.size();
|
|
|
|
has_indices=false;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
if (len<=0)
|
|
|
|
continue;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
PoolVector<int>::Read indicesr = indices.read();
|
|
|
|
const int *indicesptr = indicesr.ptr();
|
2017-04-07 02:36:37 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
PoolVector<Vector3>::Read verticesr = vertices.read();
|
|
|
|
const Vector3 *verticesptr = verticesr.ptr();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
int old_faces=faces.size();
|
|
|
|
int new_faces=old_faces+(len/3);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
faces.resize(new_faces);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
PoolVector<Face3>::Write facesw = faces.write();
|
|
|
|
Face3 *facesptr=facesw.ptr();
|
2014-05-14 04:22:15 +00:00
|
|
|
|
2014-05-05 01:50:23 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
for (int i=0;i<len/3;i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
Face3 face;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
for (int j=0;j<3;j++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
int idx=i*3+j;
|
|
|
|
face.vertex[j] = has_indices ? verticesptr[ indicesptr[ idx ] ] : verticesptr[idx];
|
|
|
|
}
|
2016-11-10 02:55:06 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
facesptr[i+old_faces]=face;
|
|
|
|
}
|
2016-11-10 02:55:06 +00:00
|
|
|
|
|
|
|
}
|
2017-06-07 21:18:55 +00:00
|
|
|
*/
|
|
|
|
}
|
2016-11-10 02:55:06 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
Ref<Shape> Mesh::create_convex_shape() const {
|
|
|
|
PoolVector<Vector3> vertices;
|
2016-11-10 02:55:06 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
for (int i = 0; i < get_surface_count(); i++) {
|
|
|
|
Array a = surface_get_arrays(i);
|
2019-12-11 00:57:57 +00:00
|
|
|
ERR_FAIL_COND_V(a.empty(), Ref<ConvexPolygonShape>());
|
2017-06-07 21:18:55 +00:00
|
|
|
PoolVector<Vector3> v = a[ARRAY_VERTEX];
|
|
|
|
vertices.append_array(v);
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
Ref<ConvexPolygonShape> shape = memnew(ConvexPolygonShape);
|
|
|
|
shape->set_points(vertices);
|
|
|
|
return shape;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
Ref<Shape> Mesh::create_trimesh_shape() const {
|
|
|
|
PoolVector<Face3> faces = get_faces();
|
2021-05-05 10:44:11 +00:00
|
|
|
if (faces.size() == 0) {
|
2017-06-07 21:18:55 +00:00
|
|
|
return Ref<Shape>();
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-04-07 02:36:37 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
PoolVector<Vector3> face_points;
|
|
|
|
face_points.resize(faces.size() * 3);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-03-10 23:33:39 +00:00
|
|
|
for (int i = 0; i < face_points.size(); i += 3) {
|
2017-06-07 21:18:55 +00:00
|
|
|
Face3 f = faces.get(i / 3);
|
2020-03-10 23:33:39 +00:00
|
|
|
face_points.set(i, f.vertex[0]);
|
|
|
|
face_points.set(i + 1, f.vertex[1]);
|
|
|
|
face_points.set(i + 2, f.vertex[2]);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2014-05-05 01:50:23 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
Ref<ConcavePolygonShape> shape = memnew(ConcavePolygonShape);
|
|
|
|
shape->set_faces(face_points);
|
|
|
|
return shape;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
Ref<Mesh> Mesh::create_outline(float p_margin) const {
|
|
|
|
Array arrays;
|
|
|
|
int index_accum = 0;
|
|
|
|
for (int i = 0; i < get_surface_count(); i++) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (surface_get_primitive_type(i) != PRIMITIVE_TRIANGLES) {
|
2017-06-07 21:18:55 +00:00
|
|
|
continue;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
Array a = surface_get_arrays(i);
|
2019-12-11 00:57:57 +00:00
|
|
|
ERR_FAIL_COND_V(a.empty(), Ref<ArrayMesh>());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
if (i == 0) {
|
|
|
|
arrays = a;
|
|
|
|
PoolVector<Vector3> v = a[ARRAY_VERTEX];
|
|
|
|
index_accum += v.size();
|
|
|
|
} else {
|
2018-05-14 02:26:45 +00:00
|
|
|
int vcount = 0;
|
2017-06-07 21:18:55 +00:00
|
|
|
for (int j = 0; j < arrays.size(); j++) {
|
|
|
|
if (arrays[j].get_type() == Variant::NIL || a[j].get_type() == Variant::NIL) {
|
|
|
|
//mismatch, do not use
|
|
|
|
arrays[j] = Variant();
|
|
|
|
continue;
|
|
|
|
}
|
2016-11-10 02:55:06 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
switch (j) {
|
|
|
|
case ARRAY_VERTEX:
|
|
|
|
case ARRAY_NORMAL: {
|
|
|
|
PoolVector<Vector3> dst = arrays[j];
|
|
|
|
PoolVector<Vector3> src = a[j];
|
2021-05-05 10:44:11 +00:00
|
|
|
if (j == ARRAY_VERTEX) {
|
2017-06-07 21:18:55 +00:00
|
|
|
vcount = src.size();
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-06-07 21:18:55 +00:00
|
|
|
if (dst.size() == 0 || src.size() == 0) {
|
|
|
|
arrays[j] = Variant();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
dst.append_array(src);
|
|
|
|
arrays[j] = dst;
|
|
|
|
} break;
|
|
|
|
case ARRAY_TANGENT:
|
|
|
|
case ARRAY_BONES:
|
|
|
|
case ARRAY_WEIGHTS: {
|
|
|
|
PoolVector<real_t> dst = arrays[j];
|
|
|
|
PoolVector<real_t> src = a[j];
|
|
|
|
if (dst.size() == 0 || src.size() == 0) {
|
|
|
|
arrays[j] = Variant();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
dst.append_array(src);
|
|
|
|
arrays[j] = dst;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
} break;
|
|
|
|
case ARRAY_COLOR: {
|
|
|
|
PoolVector<Color> dst = arrays[j];
|
|
|
|
PoolVector<Color> src = a[j];
|
|
|
|
if (dst.size() == 0 || src.size() == 0) {
|
|
|
|
arrays[j] = Variant();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
dst.append_array(src);
|
|
|
|
arrays[j] = dst;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
} break;
|
|
|
|
case ARRAY_TEX_UV:
|
|
|
|
case ARRAY_TEX_UV2: {
|
|
|
|
PoolVector<Vector2> dst = arrays[j];
|
|
|
|
PoolVector<Vector2> src = a[j];
|
|
|
|
if (dst.size() == 0 || src.size() == 0) {
|
|
|
|
arrays[j] = Variant();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
dst.append_array(src);
|
|
|
|
arrays[j] = dst;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
} break;
|
|
|
|
case ARRAY_INDEX: {
|
|
|
|
PoolVector<int> dst = arrays[j];
|
|
|
|
PoolVector<int> src = a[j];
|
|
|
|
if (dst.size() == 0 || src.size() == 0) {
|
|
|
|
arrays[j] = Variant();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
int ss = src.size();
|
|
|
|
PoolVector<int>::Write w = src.write();
|
|
|
|
for (int k = 0; k < ss; k++) {
|
|
|
|
w[k] += index_accum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dst.append_array(src);
|
|
|
|
arrays[j] = dst;
|
|
|
|
index_accum += vcount;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-06-07 21:18:55 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-04-01 15:06:47 +00:00
|
|
|
ERR_FAIL_COND_V(arrays.size() != ARRAY_MAX, Ref<ArrayMesh>());
|
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
{
|
|
|
|
PoolVector<int>::Write ir;
|
|
|
|
PoolVector<int> indices = arrays[ARRAY_INDEX];
|
|
|
|
bool has_indices = false;
|
|
|
|
PoolVector<Vector3> vertices = arrays[ARRAY_VERTEX];
|
|
|
|
int vc = vertices.size();
|
|
|
|
ERR_FAIL_COND_V(!vc, Ref<ArrayMesh>());
|
|
|
|
PoolVector<Vector3>::Write r = vertices.write();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
if (indices.size()) {
|
2019-12-11 00:57:57 +00:00
|
|
|
ERR_FAIL_COND_V(indices.size() % 3 != 0, Ref<ArrayMesh>());
|
2017-06-07 21:18:55 +00:00
|
|
|
vc = indices.size();
|
|
|
|
ir = indices.write();
|
|
|
|
has_indices = true;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
Map<Vector3, Vector3> normal_accum;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
//fill normals with triangle normals
|
|
|
|
for (int i = 0; i < vc; i += 3) {
|
|
|
|
Vector3 t[3];
|
|
|
|
|
|
|
|
if (has_indices) {
|
|
|
|
t[0] = r[ir[i + 0]];
|
|
|
|
t[1] = r[ir[i + 1]];
|
|
|
|
t[2] = r[ir[i + 2]];
|
|
|
|
} else {
|
|
|
|
t[0] = r[i + 0];
|
|
|
|
t[1] = r[i + 1];
|
|
|
|
t[2] = r[i + 2];
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector3 n = Plane(t[0], t[1], t[2]).normal;
|
|
|
|
|
|
|
|
for (int j = 0; j < 3; j++) {
|
|
|
|
Map<Vector3, Vector3>::Element *E = normal_accum.find(t[j]);
|
|
|
|
if (!E) {
|
|
|
|
normal_accum[t[j]] = n;
|
|
|
|
} else {
|
|
|
|
float d = n.dot(E->get());
|
2021-05-05 10:44:11 +00:00
|
|
|
if (d < 1.0) {
|
2017-06-07 21:18:55 +00:00
|
|
|
E->get() += n * (1.0 - d);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-06-07 21:18:55 +00:00
|
|
|
//E->get()+=n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//normalize
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
for (Map<Vector3, Vector3>::Element *E = normal_accum.front(); E; E = E->next()) {
|
|
|
|
E->get().normalize();
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
//displace normals
|
|
|
|
int vc2 = vertices.size();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
for (int i = 0; i < vc2; i++) {
|
|
|
|
Vector3 t = r[i];
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
Map<Vector3, Vector3>::Element *E = normal_accum.find(t);
|
|
|
|
ERR_CONTINUE(!E);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
t += E->get() * p_margin;
|
|
|
|
r[i] = t;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-07-05 17:08:43 +00:00
|
|
|
r.release();
|
2017-06-07 21:18:55 +00:00
|
|
|
arrays[ARRAY_VERTEX] = vertices;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
if (!has_indices) {
|
|
|
|
PoolVector<int> new_indices;
|
|
|
|
new_indices.resize(vertices.size());
|
|
|
|
PoolVector<int>::Write iw = new_indices.write();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
for (int j = 0; j < vc2; j += 3) {
|
|
|
|
iw[j] = j;
|
|
|
|
iw[j + 1] = j + 2;
|
|
|
|
iw[j + 2] = j + 1;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-07-05 17:08:43 +00:00
|
|
|
iw.release();
|
2017-06-07 21:18:55 +00:00
|
|
|
arrays[ARRAY_INDEX] = new_indices;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
} else {
|
|
|
|
for (int j = 0; j < vc; j += 3) {
|
|
|
|
SWAP(ir[j + 1], ir[j + 2]);
|
|
|
|
}
|
2019-07-05 17:08:43 +00:00
|
|
|
ir.release();
|
2017-06-07 21:18:55 +00:00
|
|
|
arrays[ARRAY_INDEX] = indices;
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
Ref<ArrayMesh> newmesh = memnew(ArrayMesh);
|
|
|
|
newmesh->add_surface_from_arrays(PRIMITIVE_TRIANGLES, arrays);
|
|
|
|
return newmesh;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-12-09 17:11:26 +00:00
|
|
|
void Mesh::set_lightmap_size_hint(const Vector2 &p_size) {
|
|
|
|
lightmap_size_hint = p_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
Size2 Mesh::get_lightmap_size_hint() const {
|
|
|
|
return lightmap_size_hint;
|
|
|
|
}
|
|
|
|
|
2017-07-13 22:15:55 +00:00
|
|
|
void Mesh::_bind_methods() {
|
2017-12-09 17:11:26 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_lightmap_size_hint", "size"), &Mesh::set_lightmap_size_hint);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_lightmap_size_hint"), &Mesh::get_lightmap_size_hint);
|
2019-12-08 02:23:05 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_aabb"), &Mesh::get_aabb);
|
2017-12-09 17:11:26 +00:00
|
|
|
|
2018-11-08 14:30:02 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "lightmap_size_hint"), "set_lightmap_size_hint", "get_lightmap_size_hint");
|
2017-12-09 17:11:26 +00:00
|
|
|
|
2018-08-27 11:33:32 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_surface_count"), &Mesh::get_surface_count);
|
|
|
|
ClassDB::bind_method(D_METHOD("surface_get_arrays", "surf_idx"), &Mesh::surface_get_arrays);
|
|
|
|
ClassDB::bind_method(D_METHOD("surface_get_blend_shape_arrays", "surf_idx"), &Mesh::surface_get_blend_shape_arrays);
|
2019-01-31 17:04:36 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("surface_set_material", "surf_idx", "material"), &Mesh::surface_set_material);
|
2018-08-27 11:33:32 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("surface_get_material", "surf_idx"), &Mesh::surface_get_material);
|
|
|
|
|
2017-08-20 15:45:01 +00:00
|
|
|
BIND_ENUM_CONSTANT(PRIMITIVE_POINTS);
|
|
|
|
BIND_ENUM_CONSTANT(PRIMITIVE_LINES);
|
|
|
|
BIND_ENUM_CONSTANT(PRIMITIVE_LINE_STRIP);
|
|
|
|
BIND_ENUM_CONSTANT(PRIMITIVE_LINE_LOOP);
|
|
|
|
BIND_ENUM_CONSTANT(PRIMITIVE_TRIANGLES);
|
|
|
|
BIND_ENUM_CONSTANT(PRIMITIVE_TRIANGLE_STRIP);
|
|
|
|
BIND_ENUM_CONSTANT(PRIMITIVE_TRIANGLE_FAN);
|
2017-09-12 19:09:06 +00:00
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(BLEND_SHAPE_MODE_NORMALIZED);
|
|
|
|
BIND_ENUM_CONSTANT(BLEND_SHAPE_MODE_RELATIVE);
|
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_FORMAT_VERTEX);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_FORMAT_NORMAL);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_FORMAT_TANGENT);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_FORMAT_COLOR);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_FORMAT_TEX_UV);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_FORMAT_TEX_UV2);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_FORMAT_BONES);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_FORMAT_WEIGHTS);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_FORMAT_INDEX);
|
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_COMPRESS_BASE);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_COMPRESS_VERTEX);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_COMPRESS_NORMAL);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_COMPRESS_TANGENT);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_COMPRESS_COLOR);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_COMPRESS_TEX_UV);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_COMPRESS_TEX_UV2);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_COMPRESS_BONES);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_COMPRESS_WEIGHTS);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_COMPRESS_INDEX);
|
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_FLAG_USE_2D_VERTICES);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_FLAG_USE_16_BIT_BONES);
|
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_COMPRESS_DEFAULT);
|
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_VERTEX);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_NORMAL);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_TANGENT);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_COLOR);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_TEX_UV);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_TEX_UV2);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_BONES);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_WEIGHTS);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_INDEX);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_MAX);
|
2017-07-13 22:15:55 +00:00
|
|
|
}
|
|
|
|
|
2018-09-09 17:39:34 +00:00
|
|
|
void Mesh::clear_cache() const {
|
|
|
|
triangle_mesh.unref();
|
|
|
|
debug_lines.clear();
|
2017-11-21 00:36:32 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 12:20:36 +00:00
|
|
|
Vector<Ref<Shape>> Mesh::convex_decompose() const {
|
|
|
|
ERR_FAIL_COND_V(!convex_composition_function, Vector<Ref<Shape>>());
|
2019-04-10 20:46:04 +00:00
|
|
|
|
|
|
|
PoolVector<Face3> faces = get_faces();
|
|
|
|
Vector<Face3> f3;
|
|
|
|
f3.resize(faces.size());
|
|
|
|
PoolVector<Face3>::Read f = faces.read();
|
|
|
|
for (int i = 0; i < f3.size(); i++) {
|
|
|
|
f3.write[i] = f[i];
|
|
|
|
}
|
|
|
|
|
2021-05-04 12:20:36 +00:00
|
|
|
Vector<Vector<Face3>> decomposed = convex_composition_function(f3);
|
2019-04-10 20:46:04 +00:00
|
|
|
|
2021-05-04 12:20:36 +00:00
|
|
|
Vector<Ref<Shape>> ret;
|
2019-04-10 20:46:04 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < decomposed.size(); i++) {
|
|
|
|
Set<Vector3> points;
|
|
|
|
for (int j = 0; j < decomposed[i].size(); j++) {
|
|
|
|
points.insert(decomposed[i][j].vertex[0]);
|
|
|
|
points.insert(decomposed[i][j].vertex[1]);
|
|
|
|
points.insert(decomposed[i][j].vertex[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
PoolVector<Vector3> convex_points;
|
|
|
|
convex_points.resize(points.size());
|
|
|
|
{
|
|
|
|
PoolVector<Vector3>::Write w = convex_points.write();
|
|
|
|
int idx = 0;
|
|
|
|
for (Set<Vector3>::Element *E = points.front(); E; E = E->next()) {
|
|
|
|
w[idx++] = E->get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<ConvexPolygonShape> shape;
|
|
|
|
shape.instance();
|
|
|
|
shape->set_points(convex_points);
|
|
|
|
ret.push_back(shape);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
Mesh::Mesh() {
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
bool ArrayMesh::_set(const StringName &p_name, const Variant &p_value) {
|
|
|
|
String sname = p_name;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
if (p_name == "blend_shape/names") {
|
|
|
|
PoolVector<String> sk = p_value;
|
|
|
|
int sz = sk.size();
|
|
|
|
PoolVector<String>::Read r = sk.read();
|
2021-05-05 10:44:11 +00:00
|
|
|
for (int i = 0; i < sz; i++) {
|
2017-06-07 21:18:55 +00:00
|
|
|
add_blend_shape(r[i]);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-06-07 21:18:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
if (p_name == "blend_shape/mode") {
|
|
|
|
set_blend_shape_mode(BlendShapeMode(int(p_value)));
|
|
|
|
return true;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
if (sname.begins_with("surface_")) {
|
|
|
|
int sl = sname.find("/");
|
2021-05-05 10:44:11 +00:00
|
|
|
if (sl == -1) {
|
2017-06-07 21:18:55 +00:00
|
|
|
return false;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-06-07 21:18:55 +00:00
|
|
|
int idx = sname.substr(8, sl - 8).to_int() - 1;
|
|
|
|
String what = sname.get_slicec('/', 1);
|
2021-05-05 10:44:11 +00:00
|
|
|
if (what == "material") {
|
2017-06-07 21:18:55 +00:00
|
|
|
surface_set_material(idx, p_value);
|
2021-05-05 10:44:11 +00:00
|
|
|
} else if (what == "name") {
|
2017-06-07 21:18:55 +00:00
|
|
|
surface_set_name(idx, p_value);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-06-07 21:18:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!sname.begins_with("surfaces")) {
|
2017-06-07 21:18:55 +00:00
|
|
|
return false;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
int idx = sname.get_slicec('/', 1).to_int();
|
|
|
|
String what = sname.get_slicec('/', 2);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
if (idx == surfaces.size()) {
|
|
|
|
//create
|
|
|
|
Dictionary d = p_value;
|
|
|
|
ERR_FAIL_COND_V(!d.has("primitive"), false);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
if (d.has("arrays")) {
|
|
|
|
//old format
|
|
|
|
ERR_FAIL_COND_V(!d.has("morph_arrays"), false);
|
|
|
|
add_surface_from_arrays(PrimitiveType(int(d["primitive"])), d["arrays"], d["morph_arrays"]);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
} else if (d.has("array_data")) {
|
|
|
|
PoolVector<uint8_t> array_data = d["array_data"];
|
|
|
|
PoolVector<uint8_t> array_index_data;
|
2021-05-05 10:44:11 +00:00
|
|
|
if (d.has("array_index_data")) {
|
2017-06-07 21:18:55 +00:00
|
|
|
array_index_data = d["array_index_data"];
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
ERR_FAIL_COND_V(!d.has("format"), false);
|
|
|
|
uint32_t format = d["format"];
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
uint32_t primitive = d["primitive"];
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
ERR_FAIL_COND_V(!d.has("vertex_count"), false);
|
|
|
|
int vertex_count = d["vertex_count"];
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
int index_count = 0;
|
2021-05-05 10:44:11 +00:00
|
|
|
if (d.has("index_count")) {
|
2017-06-07 21:18:55 +00:00
|
|
|
index_count = d["index_count"];
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2021-05-04 12:20:36 +00:00
|
|
|
Vector<PoolVector<uint8_t>> blend_shapes;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
if (d.has("blend_shape_data")) {
|
|
|
|
Array blend_shape_data = d["blend_shape_data"];
|
|
|
|
for (int i = 0; i < blend_shape_data.size(); i++) {
|
|
|
|
PoolVector<uint8_t> shape = blend_shape_data[i];
|
|
|
|
blend_shapes.push_back(shape);
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
ERR_FAIL_COND_V(!d.has("aabb"), false);
|
2017-11-17 02:09:00 +00:00
|
|
|
AABB aabb = d["aabb"];
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
Vector<AABB> bone_aabb;
|
2017-11-12 03:48:00 +00:00
|
|
|
if (d.has("skeleton_aabb")) {
|
|
|
|
Array baabb = d["skeleton_aabb"];
|
2017-06-07 21:18:55 +00:00
|
|
|
bone_aabb.resize(baabb.size());
|
2016-05-27 17:18:40 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
for (int i = 0; i < baabb.size(); i++) {
|
2018-07-25 01:11:03 +00:00
|
|
|
bone_aabb.write[i] = baabb[i];
|
2017-06-07 21:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
add_surface(format, PrimitiveType(primitive), array_data, vertex_count, array_index_data, index_count, aabb, blend_shapes, bone_aabb);
|
|
|
|
} else {
|
|
|
|
ERR_FAIL_V(false);
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
if (d.has("material")) {
|
|
|
|
surface_set_material(idx, d["material"]);
|
|
|
|
}
|
|
|
|
if (d.has("name")) {
|
|
|
|
surface_set_name(idx, d["name"]);
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
2014-05-05 01:50:23 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
return false;
|
2014-05-05 01:50:23 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
bool ArrayMesh::_get(const StringName &p_name, Variant &r_ret) const {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (_is_generated()) {
|
2017-06-07 21:18:55 +00:00
|
|
|
return false;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
String sname = p_name;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
if (p_name == "blend_shape/names") {
|
|
|
|
PoolVector<String> sk;
|
2021-05-05 10:44:11 +00:00
|
|
|
for (int i = 0; i < blend_shapes.size(); i++) {
|
2017-06-07 21:18:55 +00:00
|
|
|
sk.push_back(blend_shapes[i]);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-06-07 21:18:55 +00:00
|
|
|
r_ret = sk;
|
|
|
|
return true;
|
|
|
|
} else if (p_name == "blend_shape/mode") {
|
|
|
|
r_ret = get_blend_shape_mode();
|
|
|
|
return true;
|
|
|
|
} else if (sname.begins_with("surface_")) {
|
|
|
|
int sl = sname.find("/");
|
2021-05-05 10:44:11 +00:00
|
|
|
if (sl == -1) {
|
2017-06-07 21:18:55 +00:00
|
|
|
return false;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-06-07 21:18:55 +00:00
|
|
|
int idx = sname.substr(8, sl - 8).to_int() - 1;
|
|
|
|
String what = sname.get_slicec('/', 1);
|
2021-05-05 10:44:11 +00:00
|
|
|
if (what == "material") {
|
2017-06-07 21:18:55 +00:00
|
|
|
r_ret = surface_get_material(idx);
|
2021-05-05 10:44:11 +00:00
|
|
|
} else if (what == "name") {
|
2017-06-07 21:18:55 +00:00
|
|
|
r_ret = surface_get_name(idx);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-06-07 21:18:55 +00:00
|
|
|
return true;
|
2021-05-05 10:44:11 +00:00
|
|
|
} else if (!sname.begins_with("surfaces")) {
|
2017-06-07 21:18:55 +00:00
|
|
|
return false;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
int idx = sname.get_slicec('/', 1).to_int();
|
|
|
|
ERR_FAIL_INDEX_V(idx, surfaces.size(), false);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
Dictionary d;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
d["array_data"] = VS::get_singleton()->mesh_surface_get_array(mesh, idx);
|
|
|
|
d["vertex_count"] = VS::get_singleton()->mesh_surface_get_array_len(mesh, idx);
|
|
|
|
d["array_index_data"] = VS::get_singleton()->mesh_surface_get_index_array(mesh, idx);
|
|
|
|
d["index_count"] = VS::get_singleton()->mesh_surface_get_array_index_len(mesh, idx);
|
|
|
|
d["primitive"] = VS::get_singleton()->mesh_surface_get_primitive_type(mesh, idx);
|
|
|
|
d["format"] = VS::get_singleton()->mesh_surface_get_format(mesh, idx);
|
|
|
|
d["aabb"] = VS::get_singleton()->mesh_surface_get_aabb(mesh, idx);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
Vector<AABB> skel_aabb = VS::get_singleton()->mesh_surface_get_skeleton_aabb(mesh, idx);
|
2017-06-07 21:18:55 +00:00
|
|
|
Array arr;
|
2018-11-19 13:42:27 +00:00
|
|
|
arr.resize(skel_aabb.size());
|
2017-06-07 21:18:55 +00:00
|
|
|
for (int i = 0; i < skel_aabb.size(); i++) {
|
|
|
|
arr[i] = skel_aabb[i];
|
|
|
|
}
|
|
|
|
d["skeleton_aabb"] = arr;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2021-05-04 12:20:36 +00:00
|
|
|
Vector<PoolVector<uint8_t>> blend_shape_data = VS::get_singleton()->mesh_surface_get_blend_shapes(mesh, idx);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
Array md;
|
|
|
|
for (int i = 0; i < blend_shape_data.size(); i++) {
|
|
|
|
md.push_back(blend_shape_data[i]);
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
d["blend_shape_data"] = md;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
Ref<Material> m = surface_get_material(idx);
|
2021-05-05 10:44:11 +00:00
|
|
|
if (m.is_valid()) {
|
2017-06-07 21:18:55 +00:00
|
|
|
d["material"] = m;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-06-07 21:18:55 +00:00
|
|
|
String n = surface_get_name(idx);
|
2021-05-05 10:44:11 +00:00
|
|
|
if (n != "") {
|
2017-06-07 21:18:55 +00:00
|
|
|
d["name"] = n;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
r_ret = d;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
void ArrayMesh::_get_property_list(List<PropertyInfo> *p_list) const {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (_is_generated()) {
|
2017-06-07 21:18:55 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
if (blend_shapes.size()) {
|
2018-01-11 22:35:12 +00:00
|
|
|
p_list->push_back(PropertyInfo(Variant::POOL_STRING_ARRAY, "blend_shape/names", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
|
2017-06-07 21:18:55 +00:00
|
|
|
p_list->push_back(PropertyInfo(Variant::INT, "blend_shape/mode", PROPERTY_HINT_ENUM, "Normalized,Relative"));
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
for (int i = 0; i < surfaces.size(); i++) {
|
2018-01-11 22:35:12 +00:00
|
|
|
p_list->push_back(PropertyInfo(Variant::DICTIONARY, "surfaces/" + itos(i), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
|
2017-06-07 21:18:55 +00:00
|
|
|
p_list->push_back(PropertyInfo(Variant::STRING, "surface_" + itos(i + 1) + "/name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
|
2017-06-22 12:17:06 +00:00
|
|
|
if (surfaces[i].is_2d) {
|
|
|
|
p_list->push_back(PropertyInfo(Variant::OBJECT, "surface_" + itos(i + 1) + "/material", PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial,CanvasItemMaterial", PROPERTY_USAGE_EDITOR));
|
|
|
|
} else {
|
|
|
|
p_list->push_back(PropertyInfo(Variant::OBJECT, "surface_" + itos(i + 1) + "/material", PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial,SpatialMaterial", PROPERTY_USAGE_EDITOR));
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
void ArrayMesh::_recompute_aabb() {
|
|
|
|
// regenerate AABB
|
2017-11-17 02:09:00 +00:00
|
|
|
aabb = AABB();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
for (int i = 0; i < surfaces.size(); i++) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (i == 0) {
|
2017-06-07 21:18:55 +00:00
|
|
|
aabb = surfaces[i].aabb;
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2017-06-07 21:18:55 +00:00
|
|
|
aabb.merge_with(surfaces[i].aabb);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-04 12:20:36 +00:00
|
|
|
void ArrayMesh::add_surface(uint32_t p_format, PrimitiveType p_primitive, const PoolVector<uint8_t> &p_array, int p_vertex_count, const PoolVector<uint8_t> &p_index_array, int p_index_count, const AABB &p_aabb, const Vector<PoolVector<uint8_t>> &p_blend_shapes, const Vector<AABB> &p_bone_aabbs) {
|
2017-06-07 21:18:55 +00:00
|
|
|
Surface s;
|
|
|
|
s.aabb = p_aabb;
|
2017-06-22 12:17:06 +00:00
|
|
|
s.is_2d = p_format & ARRAY_FLAG_USE_2D_VERTICES;
|
2017-06-07 21:18:55 +00:00
|
|
|
surfaces.push_back(s);
|
2017-06-09 03:23:50 +00:00
|
|
|
_recompute_aabb();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
VisualServer::get_singleton()->mesh_add_surface(mesh, p_format, (VS::PrimitiveType)p_primitive, p_array, p_vertex_count, p_index_array, p_index_count, p_aabb, p_blend_shapes, p_bone_aabbs);
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
void ArrayMesh::add_surface_from_arrays(PrimitiveType p_primitive, const Array &p_arrays, const Array &p_blend_shapes, uint32_t p_flags) {
|
|
|
|
ERR_FAIL_COND(p_arrays.size() != ARRAY_MAX);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
Surface s;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
VisualServer::get_singleton()->mesh_add_surface_from_arrays(mesh, (VisualServer::PrimitiveType)p_primitive, p_arrays, p_blend_shapes, p_flags);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
/* make aABB? */ {
|
2017-06-22 12:17:06 +00:00
|
|
|
Variant arr = p_arrays[ARRAY_VERTEX];
|
|
|
|
PoolVector<Vector3> vertices = arr;
|
2017-06-07 21:18:55 +00:00
|
|
|
int len = vertices.size();
|
|
|
|
ERR_FAIL_COND(len == 0);
|
|
|
|
PoolVector<Vector3>::Read r = vertices.read();
|
|
|
|
const Vector3 *vtx = r.ptr();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
// check AABB
|
2017-11-17 02:09:00 +00:00
|
|
|
AABB aabb;
|
2017-06-07 21:18:55 +00:00
|
|
|
for (int i = 0; i < len; i++) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (i == 0) {
|
2017-06-06 18:33:51 +00:00
|
|
|
aabb.position = vtx[i];
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2017-06-07 21:18:55 +00:00
|
|
|
aabb.expand_to(vtx[i]);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 19:47:35 +00:00
|
|
|
s.aabb = aabb;
|
|
|
|
s.is_2d = arr.get_type() == Variant::POOL_VECTOR2_ARRAY;
|
|
|
|
surfaces.push_back(s);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
_recompute_aabb();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2018-09-09 17:39:34 +00:00
|
|
|
clear_cache();
|
2017-06-07 21:18:55 +00:00
|
|
|
_change_notify();
|
|
|
|
emit_changed();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
Array ArrayMesh::surface_get_arrays(int p_surface) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_surface, surfaces.size(), Array());
|
|
|
|
return VisualServer::get_singleton()->mesh_surface_get_arrays(mesh, p_surface);
|
|
|
|
}
|
|
|
|
Array ArrayMesh::surface_get_blend_shape_arrays(int p_surface) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_surface, surfaces.size(), Array());
|
2017-09-10 10:10:28 +00:00
|
|
|
return VisualServer::get_singleton()->mesh_surface_get_blend_shape_arrays(mesh, p_surface);
|
2017-06-07 21:18:55 +00:00
|
|
|
}
|
2014-10-14 04:01:25 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
int ArrayMesh::get_surface_count() const {
|
|
|
|
return surfaces.size();
|
|
|
|
}
|
2014-10-14 04:01:25 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
void ArrayMesh::add_blend_shape(const StringName &p_name) {
|
2019-08-08 20:11:48 +00:00
|
|
|
ERR_FAIL_COND_MSG(surfaces.size(), "Can't add a shape key count if surfaces are already created.");
|
2014-10-14 04:01:25 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
StringName name = p_name;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
if (blend_shapes.find(name) != -1) {
|
|
|
|
int count = 2;
|
|
|
|
do {
|
|
|
|
name = String(p_name) + " " + itos(count);
|
|
|
|
count++;
|
|
|
|
} while (blend_shapes.find(name) != -1);
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
blend_shapes.push_back(name);
|
|
|
|
VS::get_singleton()->mesh_set_blend_shape_count(mesh, blend_shapes.size());
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
int ArrayMesh::get_blend_shape_count() const {
|
|
|
|
return blend_shapes.size();
|
|
|
|
}
|
|
|
|
StringName ArrayMesh::get_blend_shape_name(int p_index) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_index, blend_shapes.size(), StringName());
|
|
|
|
return blend_shapes[p_index];
|
|
|
|
}
|
2020-10-15 14:53:18 +00:00
|
|
|
|
|
|
|
void ArrayMesh::set_blend_shape_name(int p_index, const StringName &p_name) {
|
|
|
|
ERR_FAIL_INDEX(p_index, blend_shapes.size());
|
|
|
|
|
|
|
|
StringName name = p_name;
|
|
|
|
int found = blend_shapes.find(name);
|
|
|
|
if (found != -1 && found != p_index) {
|
|
|
|
int count = 2;
|
|
|
|
do {
|
|
|
|
name = String(p_name) + " " + itos(count);
|
|
|
|
count++;
|
|
|
|
} while (blend_shapes.find(name) != -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
blend_shapes.write[p_index] = name;
|
|
|
|
}
|
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
void ArrayMesh::clear_blend_shapes() {
|
2019-08-08 20:11:48 +00:00
|
|
|
ERR_FAIL_COND_MSG(surfaces.size(), "Can't set shape key count if surfaces are already created.");
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
blend_shapes.clear();
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
void ArrayMesh::set_blend_shape_mode(BlendShapeMode p_mode) {
|
|
|
|
blend_shape_mode = p_mode;
|
|
|
|
VS::get_singleton()->mesh_set_blend_shape_mode(mesh, (VS::BlendShapeMode)p_mode);
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
ArrayMesh::BlendShapeMode ArrayMesh::get_blend_shape_mode() const {
|
|
|
|
return blend_shape_mode;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
void ArrayMesh::surface_remove(int p_idx) {
|
|
|
|
ERR_FAIL_INDEX(p_idx, surfaces.size());
|
|
|
|
VisualServer::get_singleton()->mesh_remove_surface(mesh, p_idx);
|
|
|
|
surfaces.remove(p_idx);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-09-09 17:39:34 +00:00
|
|
|
clear_cache();
|
2017-06-07 21:18:55 +00:00
|
|
|
_recompute_aabb();
|
|
|
|
_change_notify();
|
|
|
|
emit_changed();
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
int ArrayMesh::surface_get_array_len(int p_idx) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_idx, surfaces.size(), -1);
|
|
|
|
return VisualServer::get_singleton()->mesh_surface_get_array_len(mesh, p_idx);
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
int ArrayMesh::surface_get_array_index_len(int p_idx) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_idx, surfaces.size(), -1);
|
|
|
|
return VisualServer::get_singleton()->mesh_surface_get_array_index_len(mesh, p_idx);
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
uint32_t ArrayMesh::surface_get_format(int p_idx) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_idx, surfaces.size(), 0);
|
|
|
|
return VisualServer::get_singleton()->mesh_surface_get_format(mesh, p_idx);
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
ArrayMesh::PrimitiveType ArrayMesh::surface_get_primitive_type(int p_idx) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_idx, surfaces.size(), PRIMITIVE_LINES);
|
|
|
|
return (PrimitiveType)VisualServer::get_singleton()->mesh_surface_get_primitive_type(mesh, p_idx);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
void ArrayMesh::surface_set_material(int p_idx, const Ref<Material> &p_material) {
|
|
|
|
ERR_FAIL_INDEX(p_idx, surfaces.size());
|
2021-05-05 10:44:11 +00:00
|
|
|
if (surfaces[p_idx].material == p_material) {
|
2017-06-07 21:18:55 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2018-07-25 01:11:03 +00:00
|
|
|
surfaces.write[p_idx].material = p_material;
|
2017-06-07 21:18:55 +00:00
|
|
|
VisualServer::get_singleton()->mesh_surface_set_material(mesh, p_idx, p_material.is_null() ? RID() : p_material->get_rid());
|
2014-11-02 14:31:01 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
_change_notify("material");
|
2018-04-28 00:52:15 +00:00
|
|
|
emit_changed();
|
2017-06-07 21:18:55 +00:00
|
|
|
}
|
2014-11-02 14:31:01 +00:00
|
|
|
|
2018-07-28 00:34:31 +00:00
|
|
|
int ArrayMesh::surface_find_by_name(const String &p_name) const {
|
|
|
|
for (int i = 0; i < surfaces.size(); i++) {
|
|
|
|
if (surfaces[i].name == p_name) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
void ArrayMesh::surface_set_name(int p_idx, const String &p_name) {
|
|
|
|
ERR_FAIL_INDEX(p_idx, surfaces.size());
|
2014-11-02 14:31:01 +00:00
|
|
|
|
2018-07-25 01:11:03 +00:00
|
|
|
surfaces.write[p_idx].name = p_name;
|
2018-04-28 00:52:15 +00:00
|
|
|
emit_changed();
|
2017-06-07 21:18:55 +00:00
|
|
|
}
|
2014-11-02 14:31:01 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
String ArrayMesh::surface_get_name(int p_idx) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_idx, surfaces.size(), String());
|
|
|
|
return surfaces[p_idx].name;
|
|
|
|
}
|
2014-11-02 14:31:01 +00:00
|
|
|
|
2017-11-14 20:25:42 +00:00
|
|
|
void ArrayMesh::surface_update_region(int p_surface, int p_offset, const PoolVector<uint8_t> &p_data) {
|
|
|
|
ERR_FAIL_INDEX(p_surface, surfaces.size());
|
|
|
|
VS::get_singleton()->mesh_surface_update_region(mesh, p_surface, p_offset, p_data);
|
2018-04-28 00:52:15 +00:00
|
|
|
emit_changed();
|
2017-11-14 20:25:42 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
void ArrayMesh::surface_set_custom_aabb(int p_idx, const AABB &p_aabb) {
|
2017-06-07 21:18:55 +00:00
|
|
|
ERR_FAIL_INDEX(p_idx, surfaces.size());
|
2018-07-25 01:11:03 +00:00
|
|
|
surfaces.write[p_idx].aabb = p_aabb;
|
2017-06-07 21:18:55 +00:00
|
|
|
// set custom aabb too?
|
2018-04-28 00:52:15 +00:00
|
|
|
emit_changed();
|
2017-06-07 21:18:55 +00:00
|
|
|
}
|
2014-11-02 14:31:01 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
Ref<Material> ArrayMesh::surface_get_material(int p_idx) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_idx, surfaces.size(), Ref<Material>());
|
|
|
|
return surfaces[p_idx].material;
|
|
|
|
}
|
2014-11-02 14:31:01 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
void ArrayMesh::add_surface_from_mesh_data(const Geometry::MeshData &p_mesh_data) {
|
|
|
|
VisualServer::get_singleton()->mesh_add_surface_from_mesh_data(mesh, p_mesh_data);
|
2017-11-17 02:09:00 +00:00
|
|
|
AABB aabb;
|
2017-06-07 21:18:55 +00:00
|
|
|
for (int i = 0; i < p_mesh_data.vertices.size(); i++) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (i == 0) {
|
2017-06-06 18:33:51 +00:00
|
|
|
aabb.position = p_mesh_data.vertices[i];
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2017-06-07 21:18:55 +00:00
|
|
|
aabb.expand_to(p_mesh_data.vertices[i]);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-11-02 14:31:01 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
Surface s;
|
|
|
|
s.aabb = aabb;
|
2021-05-05 10:44:11 +00:00
|
|
|
if (surfaces.size() == 0) {
|
2017-06-07 21:18:55 +00:00
|
|
|
aabb = s.aabb;
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2017-06-07 21:18:55 +00:00
|
|
|
aabb.merge_with(s.aabb);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-11-02 14:31:01 +00:00
|
|
|
|
2018-09-09 17:39:34 +00:00
|
|
|
clear_cache();
|
2014-11-02 14:31:01 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
surfaces.push_back(s);
|
|
|
|
_change_notify();
|
2014-11-02 14:31:01 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
emit_changed();
|
|
|
|
}
|
2014-11-02 14:31:01 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
RID ArrayMesh::get_rid() const {
|
|
|
|
return mesh;
|
|
|
|
}
|
2017-11-17 02:09:00 +00:00
|
|
|
AABB ArrayMesh::get_aabb() const {
|
2017-06-07 21:18:55 +00:00
|
|
|
return aabb;
|
|
|
|
}
|
2014-11-02 14:31:01 +00:00
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
void ArrayMesh::set_custom_aabb(const AABB &p_custom) {
|
2017-06-07 21:18:55 +00:00
|
|
|
custom_aabb = p_custom;
|
|
|
|
VS::get_singleton()->mesh_set_custom_aabb(mesh, custom_aabb);
|
2018-04-28 00:52:15 +00:00
|
|
|
emit_changed();
|
2017-06-07 21:18:55 +00:00
|
|
|
}
|
2014-11-02 14:31:01 +00:00
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
AABB ArrayMesh::get_custom_aabb() const {
|
2017-06-07 21:18:55 +00:00
|
|
|
return custom_aabb;
|
|
|
|
}
|
2014-11-02 14:31:01 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
void ArrayMesh::regen_normalmaps() {
|
2021-05-04 12:20:36 +00:00
|
|
|
Vector<Ref<SurfaceTool>> surfs;
|
2017-06-07 21:18:55 +00:00
|
|
|
for (int i = 0; i < get_surface_count(); i++) {
|
|
|
|
Ref<SurfaceTool> st = memnew(SurfaceTool);
|
|
|
|
st->create_from(Ref<ArrayMesh>(this), i);
|
|
|
|
surfs.push_back(st);
|
|
|
|
}
|
2014-11-02 14:31:01 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
while (get_surface_count()) {
|
|
|
|
surface_remove(0);
|
2014-11-02 14:31:01 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
for (int i = 0; i < surfs.size(); i++) {
|
2018-07-25 01:11:03 +00:00
|
|
|
surfs.write[i]->generate_tangents();
|
|
|
|
surfs.write[i]->commit(Ref<ArrayMesh>(this));
|
2017-06-07 21:18:55 +00:00
|
|
|
}
|
2014-11-02 14:31:01 +00:00
|
|
|
}
|
|
|
|
|
2017-12-09 17:11:26 +00:00
|
|
|
//dirty hack
|
2021-05-04 14:00:45 +00:00
|
|
|
bool (*array_mesh_lightmap_unwrap_callback)(float p_texel_size, const float *p_vertices, const float *p_normals, int p_vertex_count, const int *p_indices, const int *p_face_materials, int p_index_count, float **r_uv, int **r_vertex, int *r_vertex_count, int **r_index, int *r_index_count, int *r_size_hint_x, int *r_size_hint_y) = nullptr;
|
2017-12-09 17:11:26 +00:00
|
|
|
|
|
|
|
struct ArrayMeshLightmapSurface {
|
|
|
|
Ref<Material> material;
|
|
|
|
Vector<SurfaceTool::Vertex> vertices;
|
|
|
|
Mesh::PrimitiveType primitive;
|
|
|
|
uint32_t format;
|
|
|
|
};
|
|
|
|
|
|
|
|
Error ArrayMesh::lightmap_unwrap(const Transform &p_base_transform, float p_texel_size) {
|
2020-12-19 17:17:22 +00:00
|
|
|
int *cache_data = nullptr;
|
|
|
|
unsigned int cache_size = 0;
|
|
|
|
bool use_cache = false; // Don't use cache
|
|
|
|
return lightmap_unwrap_cached(cache_data, cache_size, use_cache, p_base_transform, p_texel_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
Error ArrayMesh::lightmap_unwrap_cached(int *&r_cache_data, unsigned int &r_cache_size, bool &r_used_cache, const Transform &p_base_transform, float p_texel_size) {
|
2017-12-09 17:11:26 +00:00
|
|
|
ERR_FAIL_COND_V(!array_mesh_lightmap_unwrap_callback, ERR_UNCONFIGURED);
|
2019-08-08 20:11:48 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(blend_shapes.size() != 0, ERR_UNAVAILABLE, "Can't unwrap mesh with blend shapes.");
|
2017-12-09 17:11:26 +00:00
|
|
|
|
2020-12-19 17:17:22 +00:00
|
|
|
LocalVector<float> vertices;
|
|
|
|
LocalVector<float> normals;
|
|
|
|
LocalVector<int> indices;
|
|
|
|
LocalVector<int> face_materials;
|
|
|
|
LocalVector<float> uv;
|
2021-05-04 12:20:36 +00:00
|
|
|
LocalVector<Pair<int, int>> uv_indices;
|
2020-12-19 17:17:22 +00:00
|
|
|
|
|
|
|
Vector<ArrayMeshLightmapSurface> lightmap_surfaces;
|
|
|
|
|
|
|
|
// Keep only the scale
|
|
|
|
Basis basis = p_base_transform.get_basis();
|
|
|
|
Vector3 scale = Vector3(basis.get_axis(0).length(), basis.get_axis(1).length(), basis.get_axis(2).length());
|
|
|
|
|
|
|
|
Transform transform;
|
|
|
|
transform.scale(scale);
|
|
|
|
|
|
|
|
Basis normal_basis = transform.basis.inverse().transposed();
|
2017-12-09 17:11:26 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < get_surface_count(); i++) {
|
|
|
|
ArrayMeshLightmapSurface s;
|
|
|
|
s.primitive = surface_get_primitive_type(i);
|
|
|
|
|
2019-08-08 20:11:48 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(s.primitive != Mesh::PRIMITIVE_TRIANGLES, ERR_UNAVAILABLE, "Only triangles are supported for lightmap unwrap.");
|
2017-12-09 17:11:26 +00:00
|
|
|
s.format = surface_get_format(i);
|
2019-08-08 20:11:48 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(!(s.format & ARRAY_FORMAT_NORMAL), ERR_UNAVAILABLE, "Normals are required for lightmap unwrap.");
|
2017-12-09 17:11:26 +00:00
|
|
|
|
|
|
|
Array arrays = surface_get_arrays(i);
|
|
|
|
s.material = surface_get_material(i);
|
|
|
|
s.vertices = SurfaceTool::create_vertex_array_from_triangle_arrays(arrays);
|
|
|
|
|
|
|
|
PoolVector<Vector3> rvertices = arrays[Mesh::ARRAY_VERTEX];
|
|
|
|
int vc = rvertices.size();
|
|
|
|
PoolVector<Vector3>::Read r = rvertices.read();
|
|
|
|
|
|
|
|
PoolVector<Vector3> rnormals = arrays[Mesh::ARRAY_NORMAL];
|
|
|
|
PoolVector<Vector3>::Read rn = rnormals.read();
|
|
|
|
|
|
|
|
int vertex_ofs = vertices.size() / 3;
|
|
|
|
|
|
|
|
vertices.resize((vertex_ofs + vc) * 3);
|
|
|
|
normals.resize((vertex_ofs + vc) * 3);
|
2020-12-19 17:17:22 +00:00
|
|
|
uv_indices.resize(vertex_ofs + vc);
|
2017-12-09 17:11:26 +00:00
|
|
|
|
|
|
|
for (int j = 0; j < vc; j++) {
|
2020-12-19 17:17:22 +00:00
|
|
|
Vector3 v = transform.xform(r[j]);
|
|
|
|
Vector3 n = normal_basis.xform(rn[j]).normalized();
|
2017-12-09 17:11:26 +00:00
|
|
|
|
2020-12-19 17:17:22 +00:00
|
|
|
vertices[(j + vertex_ofs) * 3 + 0] = v.x;
|
|
|
|
vertices[(j + vertex_ofs) * 3 + 1] = v.y;
|
|
|
|
vertices[(j + vertex_ofs) * 3 + 2] = v.z;
|
|
|
|
normals[(j + vertex_ofs) * 3 + 0] = n.x;
|
|
|
|
normals[(j + vertex_ofs) * 3 + 1] = n.y;
|
|
|
|
normals[(j + vertex_ofs) * 3 + 2] = n.z;
|
|
|
|
uv_indices[j + vertex_ofs] = Pair<int, int>(i, j);
|
2017-12-09 17:11:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PoolVector<int> rindices = arrays[Mesh::ARRAY_INDEX];
|
|
|
|
int ic = rindices.size();
|
|
|
|
|
2020-12-19 17:17:22 +00:00
|
|
|
float eps = 1.19209290e-7F; // Taken from xatlas.h
|
2017-12-09 17:11:26 +00:00
|
|
|
if (ic == 0) {
|
|
|
|
for (int j = 0; j < vc / 3; j++) {
|
2020-12-19 17:17:22 +00:00
|
|
|
Vector3 p0 = transform.xform(r[j * 3 + 0]);
|
|
|
|
Vector3 p1 = transform.xform(r[j * 3 + 1]);
|
|
|
|
Vector3 p2 = transform.xform(r[j * 3 + 2]);
|
|
|
|
|
|
|
|
if ((p0 - p1).length_squared() < eps || (p1 - p2).length_squared() < eps || (p2 - p0).length_squared() < eps) {
|
2017-12-14 11:59:46 +00:00
|
|
|
continue;
|
2020-12-19 17:17:22 +00:00
|
|
|
}
|
2017-12-14 11:59:46 +00:00
|
|
|
|
|
|
|
indices.push_back(vertex_ofs + j * 3 + 0);
|
|
|
|
indices.push_back(vertex_ofs + j * 3 + 1);
|
|
|
|
indices.push_back(vertex_ofs + j * 3 + 2);
|
|
|
|
face_materials.push_back(i);
|
2017-12-09 17:11:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
PoolVector<int>::Read ri = rindices.read();
|
2017-12-14 11:59:46 +00:00
|
|
|
|
2017-12-09 17:11:26 +00:00
|
|
|
for (int j = 0; j < ic / 3; j++) {
|
2020-12-19 17:17:22 +00:00
|
|
|
Vector3 p0 = transform.xform(r[ri[j * 3 + 0]]);
|
|
|
|
Vector3 p1 = transform.xform(r[ri[j * 3 + 1]]);
|
|
|
|
Vector3 p2 = transform.xform(r[ri[j * 3 + 2]]);
|
|
|
|
|
|
|
|
if ((p0 - p1).length_squared() < eps || (p1 - p2).length_squared() < eps || (p2 - p0).length_squared() < eps) {
|
2017-12-14 11:59:46 +00:00
|
|
|
continue;
|
2020-12-19 17:17:22 +00:00
|
|
|
}
|
|
|
|
|
2017-12-14 11:59:46 +00:00
|
|
|
indices.push_back(vertex_ofs + ri[j * 3 + 0]);
|
|
|
|
indices.push_back(vertex_ofs + ri[j * 3 + 1]);
|
|
|
|
indices.push_back(vertex_ofs + ri[j * 3 + 2]);
|
|
|
|
face_materials.push_back(i);
|
2017-12-09 17:11:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-19 17:17:22 +00:00
|
|
|
lightmap_surfaces.push_back(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
CryptoCore::MD5Context ctx;
|
|
|
|
ctx.start();
|
|
|
|
|
|
|
|
ctx.update((unsigned char *)&p_texel_size, sizeof(float));
|
|
|
|
ctx.update((unsigned char *)indices.ptr(), sizeof(int) * indices.size());
|
|
|
|
ctx.update((unsigned char *)face_materials.ptr(), sizeof(int) * face_materials.size());
|
|
|
|
ctx.update((unsigned char *)vertices.ptr(), sizeof(float) * vertices.size());
|
|
|
|
ctx.update((unsigned char *)normals.ptr(), sizeof(float) * normals.size());
|
|
|
|
|
|
|
|
unsigned char hash[16];
|
|
|
|
ctx.finish(hash);
|
|
|
|
|
|
|
|
bool cached = false;
|
|
|
|
unsigned int cache_idx = 0;
|
|
|
|
|
|
|
|
if (r_used_cache && r_cache_data) {
|
|
|
|
//Check if hash is in cache data
|
|
|
|
|
|
|
|
int *cache_data = r_cache_data;
|
|
|
|
int n_entries = cache_data[0];
|
|
|
|
unsigned int r_idx = 1;
|
|
|
|
for (int i = 0; i < n_entries; ++i) {
|
|
|
|
if (memcmp(&cache_data[r_idx], hash, 16) == 0) {
|
|
|
|
cached = true;
|
|
|
|
cache_idx = r_idx;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
r_idx += 4; // hash
|
|
|
|
r_idx += 2; // size hint
|
|
|
|
|
|
|
|
int vertex_count = cache_data[r_idx];
|
|
|
|
r_idx += 1; // vertex count
|
|
|
|
r_idx += vertex_count; // vertex
|
|
|
|
r_idx += vertex_count * 2; // uvs
|
|
|
|
|
|
|
|
int index_count = cache_data[r_idx];
|
|
|
|
r_idx += 1; // index count
|
|
|
|
r_idx += index_count; // indices
|
|
|
|
}
|
2017-12-09 17:11:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//unwrap
|
|
|
|
|
|
|
|
float *gen_uvs;
|
|
|
|
int *gen_vertices;
|
|
|
|
int *gen_indices;
|
|
|
|
int gen_vertex_count;
|
|
|
|
int gen_index_count;
|
|
|
|
int size_x;
|
|
|
|
int size_y;
|
|
|
|
|
2020-12-19 17:17:22 +00:00
|
|
|
if (r_used_cache && cached) {
|
|
|
|
int *cache_data = r_cache_data;
|
|
|
|
|
|
|
|
// Return cache data pointer to the caller
|
|
|
|
r_cache_data = &cache_data[cache_idx];
|
|
|
|
|
|
|
|
cache_idx += 4;
|
2017-12-09 17:11:26 +00:00
|
|
|
|
2020-12-19 17:17:22 +00:00
|
|
|
// Load size
|
|
|
|
size_x = ((int *)cache_data)[cache_idx];
|
|
|
|
size_y = ((int *)cache_data)[cache_idx + 1];
|
|
|
|
cache_idx += 2;
|
|
|
|
|
|
|
|
// Load vertices
|
|
|
|
gen_vertex_count = cache_data[cache_idx];
|
|
|
|
cache_idx++;
|
|
|
|
gen_vertices = &cache_data[cache_idx];
|
|
|
|
cache_idx += gen_vertex_count;
|
|
|
|
|
|
|
|
// Load UVs
|
|
|
|
gen_uvs = (float *)&cache_data[cache_idx];
|
|
|
|
cache_idx += gen_vertex_count * 2;
|
|
|
|
|
|
|
|
// Load indices
|
|
|
|
gen_index_count = cache_data[cache_idx];
|
|
|
|
cache_idx++;
|
|
|
|
gen_indices = &cache_data[cache_idx];
|
|
|
|
|
|
|
|
// Return cache data size to the caller
|
|
|
|
r_cache_size = sizeof(int) * (4 + 2 + 1 + gen_vertex_count + (gen_vertex_count * 2) + 1 + gen_index_count); // hash + size hint + vertex_count + vertices + uvs + index_count + indices
|
|
|
|
r_used_cache = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cached) {
|
|
|
|
bool ok = array_mesh_lightmap_unwrap_callback(p_texel_size, vertices.ptr(), normals.ptr(), vertices.size() / 3, indices.ptr(), face_materials.ptr(), indices.size(), &gen_uvs, &gen_vertices, &gen_vertex_count, &gen_indices, &gen_index_count, &size_x, &size_y);
|
|
|
|
|
|
|
|
if (!ok) {
|
|
|
|
return ERR_CANT_CREATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r_used_cache) {
|
|
|
|
unsigned int new_cache_size = 4 + 2 + 1 + gen_vertex_count + (gen_vertex_count * 2) + 1 + gen_index_count; // hash + size hint + vertex_count + vertices + uvs + index_count + indices
|
|
|
|
new_cache_size *= sizeof(int);
|
|
|
|
int *new_cache_data = (int *)memalloc(new_cache_size);
|
|
|
|
unsigned int new_cache_idx = 0;
|
|
|
|
|
|
|
|
// hash
|
|
|
|
memcpy(&new_cache_data[new_cache_idx], hash, 16);
|
|
|
|
new_cache_idx += 4;
|
|
|
|
|
|
|
|
// size hint
|
|
|
|
new_cache_data[new_cache_idx] = size_x;
|
|
|
|
new_cache_data[new_cache_idx + 1] = size_y;
|
|
|
|
new_cache_idx += 2;
|
|
|
|
|
|
|
|
// vertex count
|
|
|
|
new_cache_data[new_cache_idx] = gen_vertex_count;
|
|
|
|
new_cache_idx++;
|
|
|
|
|
|
|
|
// vertices
|
|
|
|
memcpy(&new_cache_data[new_cache_idx], gen_vertices, sizeof(int) * gen_vertex_count);
|
|
|
|
new_cache_idx += gen_vertex_count;
|
|
|
|
|
|
|
|
// uvs
|
|
|
|
memcpy(&new_cache_data[new_cache_idx], gen_uvs, sizeof(float) * gen_vertex_count * 2);
|
|
|
|
new_cache_idx += gen_vertex_count * 2;
|
|
|
|
|
|
|
|
// index count
|
|
|
|
new_cache_data[new_cache_idx] = gen_index_count;
|
|
|
|
new_cache_idx++;
|
|
|
|
|
|
|
|
// indices
|
|
|
|
memcpy(&new_cache_data[new_cache_idx], gen_indices, sizeof(int) * gen_index_count);
|
|
|
|
new_cache_idx += gen_index_count;
|
|
|
|
|
|
|
|
// Return cache data to the caller
|
|
|
|
r_cache_data = new_cache_data;
|
|
|
|
r_cache_size = new_cache_size;
|
|
|
|
r_used_cache = false;
|
|
|
|
}
|
2017-12-09 17:11:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//remove surfaces
|
|
|
|
while (get_surface_count()) {
|
|
|
|
surface_remove(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
//create surfacetools for each surface..
|
2021-05-04 12:20:36 +00:00
|
|
|
LocalVector<Ref<SurfaceTool>> surfaces_tools;
|
2017-12-09 17:11:26 +00:00
|
|
|
|
2020-12-19 17:17:22 +00:00
|
|
|
for (int i = 0; i < lightmap_surfaces.size(); i++) {
|
2017-12-09 17:11:26 +00:00
|
|
|
Ref<SurfaceTool> st;
|
|
|
|
st.instance();
|
|
|
|
st->begin(Mesh::PRIMITIVE_TRIANGLES);
|
2020-12-19 17:17:22 +00:00
|
|
|
st->set_material(lightmap_surfaces[i].material);
|
2017-12-09 17:11:26 +00:00
|
|
|
surfaces_tools.push_back(st); //stay there
|
|
|
|
}
|
|
|
|
|
2018-08-24 07:35:07 +00:00
|
|
|
print_verbose("Mesh: Gen indices: " + itos(gen_index_count));
|
2017-12-09 17:11:26 +00:00
|
|
|
//go through all indices
|
|
|
|
for (int i = 0; i < gen_index_count; i += 3) {
|
2020-12-19 17:17:22 +00:00
|
|
|
ERR_FAIL_INDEX_V(gen_vertices[gen_indices[i + 0]], (int)uv_indices.size(), ERR_BUG);
|
|
|
|
ERR_FAIL_INDEX_V(gen_vertices[gen_indices[i + 1]], (int)uv_indices.size(), ERR_BUG);
|
|
|
|
ERR_FAIL_INDEX_V(gen_vertices[gen_indices[i + 2]], (int)uv_indices.size(), ERR_BUG);
|
2017-12-09 17:11:26 +00:00
|
|
|
|
2020-12-19 17:17:22 +00:00
|
|
|
ERR_FAIL_COND_V(uv_indices[gen_vertices[gen_indices[i + 0]]].first != uv_indices[gen_vertices[gen_indices[i + 1]]].first || uv_indices[gen_vertices[gen_indices[i + 0]]].first != uv_indices[gen_vertices[gen_indices[i + 2]]].first, ERR_BUG);
|
2017-12-09 17:11:26 +00:00
|
|
|
|
2020-12-19 17:17:22 +00:00
|
|
|
int surface = uv_indices[gen_vertices[gen_indices[i + 0]]].first;
|
2017-12-09 17:11:26 +00:00
|
|
|
|
|
|
|
for (int j = 0; j < 3; j++) {
|
2020-12-19 17:17:22 +00:00
|
|
|
SurfaceTool::Vertex v = lightmap_surfaces[surface].vertices[uv_indices[gen_vertices[gen_indices[i + j]]].second];
|
2017-12-09 17:11:26 +00:00
|
|
|
|
2020-12-19 17:17:22 +00:00
|
|
|
if (lightmap_surfaces[surface].format & ARRAY_FORMAT_COLOR) {
|
|
|
|
surfaces_tools[surface]->add_color(v.color);
|
2017-12-09 17:11:26 +00:00
|
|
|
}
|
2020-12-19 17:17:22 +00:00
|
|
|
if (lightmap_surfaces[surface].format & ARRAY_FORMAT_TEX_UV) {
|
|
|
|
surfaces_tools[surface]->add_uv(v.uv);
|
2017-12-09 17:11:26 +00:00
|
|
|
}
|
2020-12-19 17:17:22 +00:00
|
|
|
if (lightmap_surfaces[surface].format & ARRAY_FORMAT_NORMAL) {
|
|
|
|
surfaces_tools[surface]->add_normal(v.normal);
|
2017-12-09 17:11:26 +00:00
|
|
|
}
|
2020-12-19 17:17:22 +00:00
|
|
|
if (lightmap_surfaces[surface].format & ARRAY_FORMAT_TANGENT) {
|
2017-12-09 17:11:26 +00:00
|
|
|
Plane t;
|
|
|
|
t.normal = v.tangent;
|
|
|
|
t.d = v.binormal.dot(v.normal.cross(v.tangent)) < 0 ? -1 : 1;
|
2020-12-19 17:17:22 +00:00
|
|
|
surfaces_tools[surface]->add_tangent(t);
|
2017-12-09 17:11:26 +00:00
|
|
|
}
|
2020-12-19 17:17:22 +00:00
|
|
|
if (lightmap_surfaces[surface].format & ARRAY_FORMAT_BONES) {
|
|
|
|
surfaces_tools[surface]->add_bones(v.bones);
|
2017-12-09 17:11:26 +00:00
|
|
|
}
|
2020-12-19 17:17:22 +00:00
|
|
|
if (lightmap_surfaces[surface].format & ARRAY_FORMAT_WEIGHTS) {
|
|
|
|
surfaces_tools[surface]->add_weights(v.weights);
|
2017-12-09 17:11:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector2 uv2(gen_uvs[gen_indices[i + j] * 2 + 0], gen_uvs[gen_indices[i + j] * 2 + 1]);
|
2020-12-19 17:17:22 +00:00
|
|
|
surfaces_tools[surface]->add_uv2(uv2);
|
2017-12-09 17:11:26 +00:00
|
|
|
|
2020-12-19 17:17:22 +00:00
|
|
|
surfaces_tools[surface]->add_vertex(v.vertex);
|
2017-12-09 17:11:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//generate surfaces
|
2020-12-19 17:17:22 +00:00
|
|
|
for (unsigned int i = 0; i < surfaces_tools.size(); i++) {
|
|
|
|
surfaces_tools[i]->index();
|
|
|
|
surfaces_tools[i]->commit(Ref<ArrayMesh>((ArrayMesh *)this), lightmap_surfaces[i].format);
|
2017-12-09 17:11:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
set_lightmap_size_hint(Size2(size_x, size_y));
|
|
|
|
|
2020-12-19 17:17:22 +00:00
|
|
|
if (!cached) {
|
|
|
|
//free stuff
|
|
|
|
::free(gen_vertices);
|
|
|
|
::free(gen_indices);
|
|
|
|
::free(gen_uvs);
|
|
|
|
}
|
|
|
|
|
2017-12-09 17:11:26 +00:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
void ArrayMesh::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("add_blend_shape", "name"), &ArrayMesh::add_blend_shape);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_blend_shape_count"), &ArrayMesh::get_blend_shape_count);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_blend_shape_name", "index"), &ArrayMesh::get_blend_shape_name);
|
2020-10-15 14:53:18 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_blend_shape_name", "index", "name"), &ArrayMesh::set_blend_shape_name);
|
2017-06-07 21:18:55 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("clear_blend_shapes"), &ArrayMesh::clear_blend_shapes);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_blend_shape_mode", "mode"), &ArrayMesh::set_blend_shape_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_blend_shape_mode"), &ArrayMesh::get_blend_shape_mode);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("add_surface_from_arrays", "primitive", "arrays", "blend_shapes", "compress_flags"), &ArrayMesh::add_surface_from_arrays, DEFVAL(Array()), DEFVAL(ARRAY_COMPRESS_DEFAULT));
|
|
|
|
ClassDB::bind_method(D_METHOD("surface_remove", "surf_idx"), &ArrayMesh::surface_remove);
|
2017-11-14 20:25:42 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("surface_update_region", "surf_idx", "offset", "data"), &ArrayMesh::surface_update_region);
|
2017-06-07 21:18:55 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("surface_get_array_len", "surf_idx"), &ArrayMesh::surface_get_array_len);
|
|
|
|
ClassDB::bind_method(D_METHOD("surface_get_array_index_len", "surf_idx"), &ArrayMesh::surface_get_array_index_len);
|
|
|
|
ClassDB::bind_method(D_METHOD("surface_get_format", "surf_idx"), &ArrayMesh::surface_get_format);
|
|
|
|
ClassDB::bind_method(D_METHOD("surface_get_primitive_type", "surf_idx"), &ArrayMesh::surface_get_primitive_type);
|
2018-07-28 00:34:31 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("surface_find_by_name", "name"), &ArrayMesh::surface_find_by_name);
|
2017-06-07 21:18:55 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("surface_set_name", "surf_idx", "name"), &ArrayMesh::surface_set_name);
|
|
|
|
ClassDB::bind_method(D_METHOD("surface_get_name", "surf_idx"), &ArrayMesh::surface_get_name);
|
2017-08-09 11:19:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("create_trimesh_shape"), &ArrayMesh::create_trimesh_shape);
|
|
|
|
ClassDB::bind_method(D_METHOD("create_convex_shape"), &ArrayMesh::create_convex_shape);
|
|
|
|
ClassDB::bind_method(D_METHOD("create_outline", "margin"), &ArrayMesh::create_outline);
|
2017-06-07 21:18:55 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("regen_normalmaps"), &ArrayMesh::regen_normalmaps);
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::set_method_flags(get_class_static(), _scs_create("regen_normalmaps"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
|
2018-08-20 13:58:53 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("lightmap_unwrap", "transform", "texel_size"), &ArrayMesh::lightmap_unwrap);
|
2018-01-17 03:39:06 +00:00
|
|
|
ClassDB::set_method_flags(get_class_static(), _scs_create("lightmap_unwrap"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
|
2017-06-07 21:18:55 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_faces"), &ArrayMesh::get_faces);
|
2017-08-09 11:19:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("generate_triangle_mesh"), &ArrayMesh::generate_triangle_mesh);
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_custom_aabb", "aabb"), &ArrayMesh::set_custom_aabb);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_custom_aabb"), &ArrayMesh::get_custom_aabb);
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2018-01-11 22:35:12 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "blend_shape_mode", PROPERTY_HINT_ENUM, "Normalized,Relative", PROPERTY_USAGE_NOEDITOR), "set_blend_shape_mode", "get_blend_shape_mode");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::AABB, "custom_aabb", PROPERTY_HINT_NONE, ""), "set_custom_aabb", "get_custom_aabb");
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
BIND_CONSTANT(NO_INDEX_ARRAY);
|
|
|
|
BIND_CONSTANT(ARRAY_WEIGHTS_SIZE);
|
|
|
|
|
2017-08-20 15:45:01 +00:00
|
|
|
BIND_ENUM_CONSTANT(ARRAY_VERTEX);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_NORMAL);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_TANGENT);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_COLOR);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_TEX_UV);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_TEX_UV2);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_BONES);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_WEIGHTS);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_INDEX);
|
2017-08-27 02:18:02 +00:00
|
|
|
BIND_ENUM_CONSTANT(ARRAY_MAX);
|
2017-08-20 15:45:01 +00:00
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_FORMAT_VERTEX);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_FORMAT_NORMAL);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_FORMAT_TANGENT);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_FORMAT_COLOR);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_FORMAT_TEX_UV);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_FORMAT_TEX_UV2);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_FORMAT_BONES);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_FORMAT_WEIGHTS);
|
|
|
|
BIND_ENUM_CONSTANT(ARRAY_FORMAT_INDEX);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-11-12 03:48:00 +00:00
|
|
|
void ArrayMesh::reload_from_file() {
|
2017-11-17 22:47:11 +00:00
|
|
|
VisualServer::get_singleton()->mesh_clear(mesh);
|
|
|
|
surfaces.clear();
|
|
|
|
clear_blend_shapes();
|
2018-09-09 18:52:43 +00:00
|
|
|
clear_cache();
|
2017-11-17 22:47:11 +00:00
|
|
|
|
2017-11-12 03:48:00 +00:00
|
|
|
Resource::reload_from_file();
|
2017-11-17 22:47:11 +00:00
|
|
|
|
|
|
|
_change_notify();
|
2017-11-12 03:48:00 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
ArrayMesh::ArrayMesh() {
|
2017-03-05 15:44:50 +00:00
|
|
|
mesh = VisualServer::get_singleton()->mesh_create();
|
|
|
|
blend_shape_mode = BLEND_SHAPE_MODE_RELATIVE;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
ArrayMesh::~ArrayMesh() {
|
2014-02-10 01:10:30 +00:00
|
|
|
VisualServer::get_singleton()->free(mesh);
|
|
|
|
}
|