2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* mesh_data_tool.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_data_tool.h"
|
|
|
|
|
|
|
|
void MeshDataTool::clear() {
|
|
|
|
vertices.clear();
|
|
|
|
edges.clear();
|
|
|
|
faces.clear();
|
|
|
|
material = Ref<Material>();
|
2017-03-05 15:44:50 +00:00
|
|
|
format = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
Error MeshDataTool::create_from_surface(const Ref<ArrayMesh> &p_mesh, int p_surface) {
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(p_mesh.is_null(), ERR_INVALID_PARAMETER);
|
|
|
|
ERR_FAIL_COND_V(p_mesh->surface_get_primitive_type(p_surface) != Mesh::PRIMITIVE_TRIANGLES, ERR_INVALID_PARAMETER);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Array arrays = p_mesh->surface_get_arrays(p_surface);
|
2020-12-15 12:04:21 +00:00
|
|
|
ERR_FAIL_COND_V(arrays.is_empty(), ERR_INVALID_PARAMETER);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector<Vector3> varray = arrays[Mesh::ARRAY_VERTEX];
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
int vcount = varray.size();
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(vcount == 0, ERR_INVALID_PARAMETER);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-02-23 20:37:32 +00:00
|
|
|
Vector<int> indices;
|
|
|
|
|
|
|
|
if (arrays[Mesh::ARRAY_INDEX].get_type() != Variant::NIL) {
|
|
|
|
indices = arrays[Mesh::ARRAY_INDEX];
|
|
|
|
} else {
|
|
|
|
//make code simpler
|
|
|
|
indices.resize(vcount);
|
|
|
|
int *iw = indices.ptrw();
|
|
|
|
for (int i = 0; i < vcount; i++) {
|
|
|
|
iw[i] = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int icount = indices.size();
|
|
|
|
const int *r = indices.ptr();
|
|
|
|
|
|
|
|
ERR_FAIL_COND_V(icount == 0, ERR_INVALID_PARAMETER);
|
|
|
|
ERR_FAIL_COND_V(icount % 3, ERR_INVALID_PARAMETER);
|
|
|
|
for (int i = 0; i < icount; i++) {
|
|
|
|
ERR_FAIL_INDEX_V(r[i], vcount, ERR_INVALID_PARAMETER);
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
clear();
|
|
|
|
format = p_mesh->surface_get_format(p_surface);
|
2017-03-05 15:44:50 +00:00
|
|
|
material = p_mesh->surface_get_material(p_surface);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
const Vector3 *vr = varray.ptr();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
const Vector3 *nr = nullptr;
|
2020-05-14 14:41:43 +00:00
|
|
|
if (arrays[Mesh::ARRAY_NORMAL].get_type() != Variant::NIL) {
|
2020-02-17 21:06:54 +00:00
|
|
|
nr = arrays[Mesh::ARRAY_NORMAL].operator Vector<Vector3>().ptr();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
const real_t *ta = nullptr;
|
2020-05-14 14:41:43 +00:00
|
|
|
if (arrays[Mesh::ARRAY_TANGENT].get_type() != Variant::NIL) {
|
2020-02-17 21:06:54 +00:00
|
|
|
ta = arrays[Mesh::ARRAY_TANGENT].operator Vector<real_t>().ptr();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
const Vector2 *uv = nullptr;
|
2020-05-14 14:41:43 +00:00
|
|
|
if (arrays[Mesh::ARRAY_TEX_UV].get_type() != Variant::NIL) {
|
2020-02-17 21:06:54 +00:00
|
|
|
uv = arrays[Mesh::ARRAY_TEX_UV].operator Vector<Vector2>().ptr();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2020-04-01 23:20:12 +00:00
|
|
|
const Vector2 *uv2 = nullptr;
|
2020-05-14 14:41:43 +00:00
|
|
|
if (arrays[Mesh::ARRAY_TEX_UV2].get_type() != Variant::NIL) {
|
2020-02-17 21:06:54 +00:00
|
|
|
uv2 = arrays[Mesh::ARRAY_TEX_UV2].operator Vector<Vector2>().ptr();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
const Color *col = nullptr;
|
2020-05-14 14:41:43 +00:00
|
|
|
if (arrays[Mesh::ARRAY_COLOR].get_type() != Variant::NIL) {
|
2020-02-17 21:06:54 +00:00
|
|
|
col = arrays[Mesh::ARRAY_COLOR].operator Vector<Color>().ptr();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
const int *bo = nullptr;
|
2020-05-14 14:41:43 +00:00
|
|
|
if (arrays[Mesh::ARRAY_BONES].get_type() != Variant::NIL) {
|
2020-02-17 21:06:54 +00:00
|
|
|
bo = arrays[Mesh::ARRAY_BONES].operator Vector<int>().ptr();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
const real_t *we = nullptr;
|
2020-05-14 14:41:43 +00:00
|
|
|
if (arrays[Mesh::ARRAY_WEIGHTS].get_type() != Variant::NIL) {
|
2020-02-17 21:06:54 +00:00
|
|
|
we = arrays[Mesh::ARRAY_WEIGHTS].operator Vector<real_t>().ptr();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
vertices.resize(vcount);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < vcount; i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
Vertex v;
|
2017-03-05 15:44:50 +00:00
|
|
|
v.vertex = vr[i];
|
2020-05-14 14:41:43 +00:00
|
|
|
if (nr) {
|
2017-03-05 15:44:50 +00:00
|
|
|
v.normal = nr[i];
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (ta) {
|
2017-03-05 15:44:50 +00:00
|
|
|
v.tangent = Plane(ta[i * 4 + 0], ta[i * 4 + 1], ta[i * 4 + 2], ta[i * 4 + 3]);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (uv) {
|
2017-03-05 15:44:50 +00:00
|
|
|
v.uv = uv[i];
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (uv2) {
|
2017-03-05 15:44:50 +00:00
|
|
|
v.uv2 = uv2[i];
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (col) {
|
2017-03-05 15:44:50 +00:00
|
|
|
v.color = col[i];
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
if (we) {
|
2017-03-05 15:44:50 +00:00
|
|
|
v.weights.push_back(we[i * 4 + 0]);
|
|
|
|
v.weights.push_back(we[i * 4 + 1]);
|
|
|
|
v.weights.push_back(we[i * 4 + 2]);
|
|
|
|
v.weights.push_back(we[i * 4 + 3]);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
if (bo) {
|
2017-03-05 15:44:50 +00:00
|
|
|
v.bones.push_back(bo[i * 4 + 0]);
|
|
|
|
v.bones.push_back(bo[i * 4 + 1]);
|
|
|
|
v.bones.push_back(bo[i * 4 + 2]);
|
|
|
|
v.bones.push_back(bo[i * 4 + 3]);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2018-07-25 01:11:03 +00:00
|
|
|
vertices.write[i] = v;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Map<Point2i, int> edge_indices;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < icount; i += 3) {
|
2018-07-25 01:11:03 +00:00
|
|
|
Vertex *v[3] = { &vertices.write[r[i + 0]], &vertices.write[r[i + 1]], &vertices.write[r[i + 2]] };
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int fidx = faces.size();
|
2014-02-10 01:10:30 +00:00
|
|
|
Face face;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int j = 0; j < 3; j++) {
|
|
|
|
face.v[j] = r[i + j];
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Point2i edge(r[i + j], r[i + (j + 1) % 3]);
|
2014-02-10 01:10:30 +00:00
|
|
|
if (edge.x > edge.y) {
|
2017-03-05 15:44:50 +00:00
|
|
|
SWAP(edge.x, edge.y);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (edge_indices.has(edge)) {
|
2017-03-05 15:44:50 +00:00
|
|
|
face.edges[j] = edge_indices[edge];
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
face.edges[j] = edge_indices.size();
|
|
|
|
edge_indices[edge] = face.edges[j];
|
2014-02-10 01:10:30 +00:00
|
|
|
Edge e;
|
2017-03-05 15:44:50 +00:00
|
|
|
e.vertex[0] = edge.x;
|
|
|
|
e.vertex[1] = edge.y;
|
2014-02-10 01:10:30 +00:00
|
|
|
edges.push_back(e);
|
2019-08-24 02:27:22 +00:00
|
|
|
v[j]->edges.push_back(face.edges[j]);
|
|
|
|
v[(j + 1) % 3]->edges.push_back(face.edges[j]);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2018-07-25 01:11:03 +00:00
|
|
|
edges.write[face.edges[j]].faces.push_back(fidx);
|
2014-02-10 01:10:30 +00:00
|
|
|
v[j]->faces.push_back(fidx);
|
|
|
|
}
|
|
|
|
|
|
|
|
faces.push_back(face);
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
Error MeshDataTool::commit_to_surface(const Ref<ArrayMesh> &p_mesh) {
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(p_mesh.is_null(), ERR_INVALID_PARAMETER);
|
2014-02-10 01:10:30 +00:00
|
|
|
Array arr;
|
|
|
|
arr.resize(Mesh::ARRAY_MAX);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int vcount = vertices.size();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector<Vector3> v;
|
|
|
|
Vector<Vector3> n;
|
|
|
|
Vector<real_t> t;
|
|
|
|
Vector<Vector2> u;
|
|
|
|
Vector<Vector2> u2;
|
|
|
|
Vector<Color> c;
|
|
|
|
Vector<int> b;
|
|
|
|
Vector<real_t> w;
|
|
|
|
Vector<int> in;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
v.resize(vcount);
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector3 *vr = v.ptrw();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
Vector3 *nr = nullptr;
|
2017-03-05 15:44:50 +00:00
|
|
|
if (format & Mesh::ARRAY_FORMAT_NORMAL) {
|
2014-02-10 01:10:30 +00:00
|
|
|
n.resize(vcount);
|
2020-02-17 21:06:54 +00:00
|
|
|
nr = n.ptrw();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
real_t *ta = nullptr;
|
2017-03-05 15:44:50 +00:00
|
|
|
if (format & Mesh::ARRAY_FORMAT_TANGENT) {
|
|
|
|
t.resize(vcount * 4);
|
2020-02-17 21:06:54 +00:00
|
|
|
ta = t.ptrw();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
Vector2 *uv = nullptr;
|
2017-03-05 15:44:50 +00:00
|
|
|
if (format & Mesh::ARRAY_FORMAT_TEX_UV) {
|
2014-02-10 01:10:30 +00:00
|
|
|
u.resize(vcount);
|
2020-02-17 21:06:54 +00:00
|
|
|
uv = u.ptrw();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
Vector2 *uv2 = nullptr;
|
2017-03-05 15:44:50 +00:00
|
|
|
if (format & Mesh::ARRAY_FORMAT_TEX_UV2) {
|
2014-02-10 01:10:30 +00:00
|
|
|
u2.resize(vcount);
|
2020-02-17 21:06:54 +00:00
|
|
|
uv2 = u2.ptrw();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
Color *col = nullptr;
|
2017-03-05 15:44:50 +00:00
|
|
|
if (format & Mesh::ARRAY_FORMAT_COLOR) {
|
2014-02-10 01:10:30 +00:00
|
|
|
c.resize(vcount);
|
2020-02-17 21:06:54 +00:00
|
|
|
col = c.ptrw();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
int *bo = nullptr;
|
2017-03-05 15:44:50 +00:00
|
|
|
if (format & Mesh::ARRAY_FORMAT_BONES) {
|
|
|
|
b.resize(vcount * 4);
|
2020-02-17 21:06:54 +00:00
|
|
|
bo = b.ptrw();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
real_t *we = nullptr;
|
2017-03-05 15:44:50 +00:00
|
|
|
if (format & Mesh::ARRAY_FORMAT_WEIGHTS) {
|
|
|
|
w.resize(vcount * 4);
|
2020-02-17 21:06:54 +00:00
|
|
|
we = w.ptrw();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < vcount; i++) {
|
2018-07-25 01:11:03 +00:00
|
|
|
const Vertex &vtx = vertices[i];
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
vr[i] = vtx.vertex;
|
2020-05-14 14:41:43 +00:00
|
|
|
if (nr) {
|
2017-03-05 15:44:50 +00:00
|
|
|
nr[i] = vtx.normal;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2020-02-17 21:06:54 +00:00
|
|
|
if (ta) {
|
2017-03-05 15:44:50 +00:00
|
|
|
ta[i * 4 + 0] = vtx.tangent.normal.x;
|
|
|
|
ta[i * 4 + 1] = vtx.tangent.normal.y;
|
|
|
|
ta[i * 4 + 2] = vtx.tangent.normal.z;
|
2020-05-10 14:47:11 +00:00
|
|
|
ta[i * 4 + 3] = vtx.tangent.d;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2020-05-14 14:41:43 +00:00
|
|
|
if (uv) {
|
2017-03-05 15:44:50 +00:00
|
|
|
uv[i] = vtx.uv;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (uv2) {
|
2017-03-05 15:44:50 +00:00
|
|
|
uv2[i] = vtx.uv2;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (col) {
|
2017-03-05 15:44:50 +00:00
|
|
|
col[i] = vtx.color;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
if (we) {
|
2017-03-05 15:44:50 +00:00
|
|
|
we[i * 4 + 0] = vtx.weights[0];
|
|
|
|
we[i * 4 + 1] = vtx.weights[1];
|
|
|
|
we[i * 4 + 2] = vtx.weights[2];
|
|
|
|
we[i * 4 + 3] = vtx.weights[3];
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
if (bo) {
|
2017-03-05 15:44:50 +00:00
|
|
|
bo[i * 4 + 0] = vtx.bones[0];
|
|
|
|
bo[i * 4 + 1] = vtx.bones[1];
|
|
|
|
bo[i * 4 + 2] = vtx.bones[2];
|
|
|
|
bo[i * 4 + 3] = vtx.bones[3];
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int fc = faces.size();
|
2017-03-05 15:44:50 +00:00
|
|
|
in.resize(fc * 3);
|
2020-02-17 21:06:54 +00:00
|
|
|
int *iw = in.ptrw();
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < fc; i++) {
|
|
|
|
iw[i * 3 + 0] = faces[i].v[0];
|
|
|
|
iw[i * 3 + 1] = faces[i].v[1];
|
|
|
|
iw[i * 3 + 2] = faces[i].v[2];
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
arr[Mesh::ARRAY_VERTEX] = v;
|
|
|
|
arr[Mesh::ARRAY_INDEX] = in;
|
2020-05-14 14:41:43 +00:00
|
|
|
if (n.size()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
arr[Mesh::ARRAY_NORMAL] = n;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (c.size()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
arr[Mesh::ARRAY_COLOR] = c;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (u.size()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
arr[Mesh::ARRAY_TEX_UV] = u;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (u2.size()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
arr[Mesh::ARRAY_TEX_UV2] = u2;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (t.size()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
arr[Mesh::ARRAY_TANGENT] = t;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (b.size()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
arr[Mesh::ARRAY_BONES] = b;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (w.size()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
arr[Mesh::ARRAY_WEIGHTS] = w;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-07 21:18:55 +00:00
|
|
|
Ref<ArrayMesh> ncmesh = p_mesh;
|
2014-02-10 01:10:30 +00:00
|
|
|
int sc = ncmesh->get_surface_count();
|
2017-03-05 15:44:50 +00:00
|
|
|
ncmesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arr);
|
|
|
|
ncmesh->surface_set_material(sc, material);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int MeshDataTool::get_format() const {
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
|
|
|
int MeshDataTool::get_vertex_count() const {
|
|
|
|
return vertices.size();
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int MeshDataTool::get_edge_count() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
return edges.size();
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int MeshDataTool::get_face_count() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
return faces.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector3 MeshDataTool::get_vertex(int p_idx) const {
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_idx, vertices.size(), Vector3());
|
2014-02-10 01:10:30 +00:00
|
|
|
return vertices[p_idx].vertex;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void MeshDataTool::set_vertex(int p_idx, const Vector3 &p_vertex) {
|
|
|
|
ERR_FAIL_INDEX(p_idx, vertices.size());
|
2018-07-25 01:11:03 +00:00
|
|
|
vertices.write[p_idx].vertex = p_vertex;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector3 MeshDataTool::get_vertex_normal(int p_idx) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_idx, vertices.size(), Vector3());
|
2014-02-10 01:10:30 +00:00
|
|
|
return vertices[p_idx].normal;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void MeshDataTool::set_vertex_normal(int p_idx, const Vector3 &p_normal) {
|
|
|
|
ERR_FAIL_INDEX(p_idx, vertices.size());
|
2018-07-25 01:11:03 +00:00
|
|
|
vertices.write[p_idx].normal = p_normal;
|
2017-03-05 15:44:50 +00:00
|
|
|
format |= Mesh::ARRAY_FORMAT_NORMAL;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Plane MeshDataTool::get_vertex_tangent(int p_idx) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_idx, vertices.size(), Plane());
|
2014-02-10 01:10:30 +00:00
|
|
|
return vertices[p_idx].tangent;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void MeshDataTool::set_vertex_tangent(int p_idx, const Plane &p_tangent) {
|
|
|
|
ERR_FAIL_INDEX(p_idx, vertices.size());
|
2018-07-25 01:11:03 +00:00
|
|
|
vertices.write[p_idx].tangent = p_tangent;
|
2017-03-05 15:44:50 +00:00
|
|
|
format |= Mesh::ARRAY_FORMAT_TANGENT;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector2 MeshDataTool::get_vertex_uv(int p_idx) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_idx, vertices.size(), Vector2());
|
2014-02-10 01:10:30 +00:00
|
|
|
return vertices[p_idx].uv;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void MeshDataTool::set_vertex_uv(int p_idx, const Vector2 &p_uv) {
|
|
|
|
ERR_FAIL_INDEX(p_idx, vertices.size());
|
2018-07-25 01:11:03 +00:00
|
|
|
vertices.write[p_idx].uv = p_uv;
|
2017-03-05 15:44:50 +00:00
|
|
|
format |= Mesh::ARRAY_FORMAT_TEX_UV;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector2 MeshDataTool::get_vertex_uv2(int p_idx) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_idx, vertices.size(), Vector2());
|
2014-02-10 01:10:30 +00:00
|
|
|
return vertices[p_idx].uv2;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void MeshDataTool::set_vertex_uv2(int p_idx, const Vector2 &p_uv2) {
|
|
|
|
ERR_FAIL_INDEX(p_idx, vertices.size());
|
2018-07-25 01:11:03 +00:00
|
|
|
vertices.write[p_idx].uv2 = p_uv2;
|
2017-03-05 15:44:50 +00:00
|
|
|
format |= Mesh::ARRAY_FORMAT_TEX_UV2;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Color MeshDataTool::get_vertex_color(int p_idx) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_idx, vertices.size(), Color());
|
2014-02-10 01:10:30 +00:00
|
|
|
return vertices[p_idx].color;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void MeshDataTool::set_vertex_color(int p_idx, const Color &p_color) {
|
|
|
|
ERR_FAIL_INDEX(p_idx, vertices.size());
|
2018-07-25 01:11:03 +00:00
|
|
|
vertices.write[p_idx].color = p_color;
|
2017-03-05 15:44:50 +00:00
|
|
|
format |= Mesh::ARRAY_FORMAT_COLOR;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector<int> MeshDataTool::get_vertex_bones(int p_idx) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_idx, vertices.size(), Vector<int>());
|
2014-02-10 01:10:30 +00:00
|
|
|
return vertices[p_idx].bones;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void MeshDataTool::set_vertex_bones(int p_idx, const Vector<int> &p_bones) {
|
|
|
|
ERR_FAIL_INDEX(p_idx, vertices.size());
|
2018-07-25 01:11:03 +00:00
|
|
|
vertices.write[p_idx].bones = p_bones;
|
2017-03-05 15:44:50 +00:00
|
|
|
format |= Mesh::ARRAY_FORMAT_BONES;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector<float> MeshDataTool::get_vertex_weights(int p_idx) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_idx, vertices.size(), Vector<float>());
|
2014-02-10 01:10:30 +00:00
|
|
|
return vertices[p_idx].weights;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void MeshDataTool::set_vertex_weights(int p_idx, const Vector<float> &p_weights) {
|
|
|
|
ERR_FAIL_INDEX(p_idx, vertices.size());
|
2018-07-25 01:11:03 +00:00
|
|
|
vertices.write[p_idx].weights = p_weights;
|
2017-03-05 15:44:50 +00:00
|
|
|
format |= Mesh::ARRAY_FORMAT_WEIGHTS;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Variant MeshDataTool::get_vertex_meta(int p_idx) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_idx, vertices.size(), Variant());
|
2014-02-10 01:10:30 +00:00
|
|
|
return vertices[p_idx].meta;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void MeshDataTool::set_vertex_meta(int p_idx, const Variant &p_meta) {
|
|
|
|
ERR_FAIL_INDEX(p_idx, vertices.size());
|
2018-07-25 01:11:03 +00:00
|
|
|
vertices.write[p_idx].meta = p_meta;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector<int> MeshDataTool::get_vertex_edges(int p_idx) const {
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_idx, vertices.size(), Vector<int>());
|
2014-02-10 01:10:30 +00:00
|
|
|
return vertices[p_idx].edges;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector<int> MeshDataTool::get_vertex_faces(int p_idx) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_idx, vertices.size(), Vector<int>());
|
2014-02-10 01:10:30 +00:00
|
|
|
return vertices[p_idx].faces;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int MeshDataTool::get_edge_vertex(int p_edge, int p_vertex) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_edge, edges.size(), -1);
|
|
|
|
ERR_FAIL_INDEX_V(p_vertex, 2, -1);
|
2014-02-10 01:10:30 +00:00
|
|
|
return edges[p_edge].vertex[p_vertex];
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector<int> MeshDataTool::get_edge_faces(int p_edge) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_edge, edges.size(), Vector<int>());
|
2014-02-10 01:10:30 +00:00
|
|
|
return edges[p_edge].faces;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Variant MeshDataTool::get_edge_meta(int p_idx) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_idx, edges.size(), Variant());
|
2014-02-10 01:10:30 +00:00
|
|
|
return edges[p_idx].meta;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void MeshDataTool::set_edge_meta(int p_idx, const Variant &p_meta) {
|
|
|
|
ERR_FAIL_INDEX(p_idx, edges.size());
|
2018-07-25 01:11:03 +00:00
|
|
|
edges.write[p_idx].meta = p_meta;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int MeshDataTool::get_face_vertex(int p_face, int p_vertex) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_face, faces.size(), -1);
|
|
|
|
ERR_FAIL_INDEX_V(p_vertex, 3, -1);
|
2014-02-10 01:10:30 +00:00
|
|
|
return faces[p_face].v[p_vertex];
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int MeshDataTool::get_face_edge(int p_face, int p_vertex) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_face, faces.size(), -1);
|
|
|
|
ERR_FAIL_INDEX_V(p_vertex, 3, -1);
|
2014-02-10 01:10:30 +00:00
|
|
|
return faces[p_face].edges[p_vertex];
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Variant MeshDataTool::get_face_meta(int p_face) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_face, faces.size(), Variant());
|
2014-02-10 01:10:30 +00:00
|
|
|
return faces[p_face].meta;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void MeshDataTool::set_face_meta(int p_face, const Variant &p_meta) {
|
|
|
|
ERR_FAIL_INDEX(p_face, faces.size());
|
2018-07-25 01:11:03 +00:00
|
|
|
faces.write[p_face].meta = p_meta;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector3 MeshDataTool::get_face_normal(int p_face) const {
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_face, faces.size(), Vector3());
|
2014-02-10 01:10:30 +00:00
|
|
|
Vector3 v0 = vertices[faces[p_face].v[0]].vertex;
|
|
|
|
Vector3 v1 = vertices[faces[p_face].v[1]].vertex;
|
|
|
|
Vector3 v2 = vertices[faces[p_face].v[2]].vertex;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return Plane(v0, v1, v2).normal;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Material> MeshDataTool::get_material() const {
|
|
|
|
return material;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MeshDataTool::set_material(const Ref<Material> &p_material) {
|
2017-03-05 15:44:50 +00:00
|
|
|
material = p_material;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MeshDataTool::_bind_methods() {
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("clear"), &MeshDataTool::clear);
|
2017-08-09 11:19:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("create_from_surface", "mesh", "surface"), &MeshDataTool::create_from_surface);
|
|
|
|
ClassDB::bind_method(D_METHOD("commit_to_surface", "mesh"), &MeshDataTool::commit_to_surface);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_format"), &MeshDataTool::get_format);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_vertex_count"), &MeshDataTool::get_vertex_count);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_edge_count"), &MeshDataTool::get_edge_count);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_face_count"), &MeshDataTool::get_face_count);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_vertex", "idx", "vertex"), &MeshDataTool::set_vertex);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_vertex", "idx"), &MeshDataTool::get_vertex);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_vertex_normal", "idx", "normal"), &MeshDataTool::set_vertex_normal);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_vertex_normal", "idx"), &MeshDataTool::get_vertex_normal);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_vertex_tangent", "idx", "tangent"), &MeshDataTool::set_vertex_tangent);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_vertex_tangent", "idx"), &MeshDataTool::get_vertex_tangent);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_vertex_uv", "idx", "uv"), &MeshDataTool::set_vertex_uv);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_vertex_uv", "idx"), &MeshDataTool::get_vertex_uv);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_vertex_uv2", "idx", "uv2"), &MeshDataTool::set_vertex_uv2);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_vertex_uv2", "idx"), &MeshDataTool::get_vertex_uv2);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_vertex_color", "idx", "color"), &MeshDataTool::set_vertex_color);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_vertex_color", "idx"), &MeshDataTool::get_vertex_color);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_vertex_bones", "idx", "bones"), &MeshDataTool::set_vertex_bones);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_vertex_bones", "idx"), &MeshDataTool::get_vertex_bones);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_vertex_weights", "idx", "weights"), &MeshDataTool::set_vertex_weights);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_vertex_weights", "idx"), &MeshDataTool::get_vertex_weights);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_vertex_meta", "idx", "meta"), &MeshDataTool::set_vertex_meta);
|
2017-08-09 11:19:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_vertex_meta", "idx"), &MeshDataTool::get_vertex_meta);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_vertex_edges", "idx"), &MeshDataTool::get_vertex_edges);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_vertex_faces", "idx"), &MeshDataTool::get_vertex_faces);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_edge_vertex", "idx", "vertex"), &MeshDataTool::get_edge_vertex);
|
2017-08-09 11:54:55 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_edge_faces", "idx"), &MeshDataTool::get_edge_faces);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_edge_meta", "idx", "meta"), &MeshDataTool::set_edge_meta);
|
2017-08-09 11:19:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_edge_meta", "idx"), &MeshDataTool::get_edge_meta);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_face_vertex", "idx", "vertex"), &MeshDataTool::get_face_vertex);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_face_edge", "idx", "edge"), &MeshDataTool::get_face_edge);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_face_meta", "idx", "meta"), &MeshDataTool::set_face_meta);
|
2017-08-09 11:19:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_face_meta", "idx"), &MeshDataTool::get_face_meta);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_face_normal", "idx"), &MeshDataTool::get_face_normal);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-09 11:19:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_material", "material"), &MeshDataTool::set_material);
|
2017-08-09 11:54:55 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_material"), &MeshDataTool::get_material);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
MeshDataTool::MeshDataTool() {
|
2014-02-10 01:10:30 +00:00
|
|
|
clear();
|
|
|
|
}
|