2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* surface_tool.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "surface_tool.h"
|
2016-04-18 19:33:54 +02:00
|
|
|
#include "method_bind_ext.inc"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
#define _VERTEX_SNAP 0.0001
|
|
|
|
#define EQ_VERTEX_DIST 0.00001
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
bool SurfaceTool::Vertex::operator==(const Vertex &p_b) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (vertex != p_b.vertex)
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (uv != p_b.uv)
|
2014-05-14 06:22:15 +02:00
|
|
|
return false;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (uv2 != p_b.uv2)
|
2014-05-14 06:22:15 +02:00
|
|
|
return false;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (normal != p_b.normal)
|
2014-05-14 06:22:15 +02:00
|
|
|
return false;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (binormal != p_b.binormal)
|
2014-05-14 06:22:15 +02:00
|
|
|
return false;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (color != p_b.color)
|
2014-05-14 06:22:15 +02:00
|
|
|
return false;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (bones.size() != p_b.bones.size())
|
2014-05-14 06:22:15 +02:00
|
|
|
return false;
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < bones.size(); i++) {
|
|
|
|
if (bones[i] != p_b.bones[i])
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < weights.size(); i++) {
|
|
|
|
if (weights[i] != p_b.weights[i])
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-14 06:22:15 +02:00
|
|
|
return true;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2014-05-14 06:22:15 +02:00
|
|
|
uint32_t SurfaceTool::VertexHasher::hash(const Vertex &p_vtx) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
uint32_t h = hash_djb2_buffer((const uint8_t *)&p_vtx.vertex, sizeof(real_t) * 3);
|
|
|
|
h = hash_djb2_buffer((const uint8_t *)&p_vtx.normal, sizeof(real_t) * 3, h);
|
|
|
|
h = hash_djb2_buffer((const uint8_t *)&p_vtx.binormal, sizeof(real_t) * 3, h);
|
|
|
|
h = hash_djb2_buffer((const uint8_t *)&p_vtx.tangent, sizeof(real_t) * 3, h);
|
|
|
|
h = hash_djb2_buffer((const uint8_t *)&p_vtx.uv, sizeof(real_t) * 2, h);
|
|
|
|
h = hash_djb2_buffer((const uint8_t *)&p_vtx.uv2, sizeof(real_t) * 2, h);
|
|
|
|
h = hash_djb2_buffer((const uint8_t *)&p_vtx.color, sizeof(real_t) * 4, h);
|
|
|
|
h = hash_djb2_buffer((const uint8_t *)p_vtx.bones.ptr(), p_vtx.bones.size() * sizeof(int), h);
|
|
|
|
h = hash_djb2_buffer((const uint8_t *)p_vtx.weights.ptr(), p_vtx.weights.size() * sizeof(float), h);
|
2014-05-14 06:22:15 +02:00
|
|
|
return h;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SurfaceTool::begin(Mesh::PrimitiveType p_primitive) {
|
|
|
|
|
|
|
|
clear();
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
primitive = p_primitive;
|
|
|
|
begun = true;
|
|
|
|
first = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void SurfaceTool::add_vertex(const Vector3 &p_vertex) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!begun);
|
|
|
|
|
|
|
|
Vertex vtx;
|
2017-03-19 00:36:26 +01:00
|
|
|
vtx.vertex = p_vertex;
|
|
|
|
vtx.color = last_color;
|
|
|
|
vtx.normal = last_normal;
|
|
|
|
vtx.uv = last_uv;
|
|
|
|
vtx.weights = last_weights;
|
|
|
|
vtx.bones = last_bones;
|
|
|
|
vtx.tangent = last_tangent.normal;
|
|
|
|
vtx.binormal = last_normal.cross(last_tangent.normal).normalized() * last_tangent.d;
|
2014-02-10 02:10:30 +01:00
|
|
|
vertex_array.push_back(vtx);
|
2017-03-19 00:36:26 +01:00
|
|
|
first = false;
|
|
|
|
format |= Mesh::ARRAY_FORMAT_VERTEX;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
void SurfaceTool::add_color(Color p_color) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!begun);
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_COND(!first && !(format & Mesh::ARRAY_FORMAT_COLOR));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
format |= Mesh::ARRAY_FORMAT_COLOR;
|
|
|
|
last_color = p_color;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
void SurfaceTool::add_normal(const Vector3 &p_normal) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!begun);
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_COND(!first && !(format & Mesh::ARRAY_FORMAT_NORMAL));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
format |= Mesh::ARRAY_FORMAT_NORMAL;
|
|
|
|
last_normal = p_normal;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void SurfaceTool::add_tangent(const Plane &p_tangent) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!begun);
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_COND(!first && !(format & Mesh::ARRAY_FORMAT_TANGENT));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
format |= Mesh::ARRAY_FORMAT_TANGENT;
|
|
|
|
last_tangent = p_tangent;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void SurfaceTool::add_uv(const Vector2 &p_uv) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!begun);
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_COND(!first && !(format & Mesh::ARRAY_FORMAT_TEX_UV));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
format |= Mesh::ARRAY_FORMAT_TEX_UV;
|
|
|
|
last_uv = p_uv;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void SurfaceTool::add_uv2(const Vector2 &p_uv2) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!begun);
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_COND(!first && !(format & Mesh::ARRAY_FORMAT_TEX_UV2));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
format |= Mesh::ARRAY_FORMAT_TEX_UV2;
|
|
|
|
last_uv2 = p_uv2;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void SurfaceTool::add_bones(const Vector<int> &p_bones) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!begun);
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_COND(p_bones.size() != 4);
|
|
|
|
ERR_FAIL_COND(!first && !(format & Mesh::ARRAY_FORMAT_BONES));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
format |= Mesh::ARRAY_FORMAT_BONES;
|
|
|
|
last_bones = p_bones;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void SurfaceTool::add_weights(const Vector<float> &p_weights) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!begun);
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_COND(p_weights.size() != 4);
|
|
|
|
ERR_FAIL_COND(!first && !(format & Mesh::ARRAY_FORMAT_WEIGHTS));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
format |= Mesh::ARRAY_FORMAT_WEIGHTS;
|
|
|
|
last_weights = p_weights;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2014-05-14 06:22:15 +02:00
|
|
|
void SurfaceTool::add_smooth_group(bool p_smooth) {
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!begun);
|
|
|
|
if (index_array.size()) {
|
2017-03-19 00:36:26 +01:00
|
|
|
smooth_groups[index_array.size()] = p_smooth;
|
2014-05-14 06:22:15 +02:00
|
|
|
} else {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
smooth_groups[vertex_array.size()] = p_smooth;
|
2014-05-14 06:22:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void SurfaceTool::add_triangle_fan(const Vector<Vector3> &p_vertexes, const Vector<Vector2> &p_uvs, const Vector<Color> &p_colors, const Vector<Vector2> &p_uv2s, const Vector<Vector3> &p_normals, const Vector<Plane> &p_tangents) {
|
2016-04-18 19:33:54 +02:00
|
|
|
ERR_FAIL_COND(!begun);
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_COND(primitive != Mesh::PRIMITIVE_TRIANGLES);
|
|
|
|
ERR_FAIL_COND(p_vertexes.size() < 3);
|
|
|
|
|
|
|
|
#define ADD_POINT(n) \
|
|
|
|
{ \
|
|
|
|
if (p_colors.size() > n) \
|
|
|
|
add_color(p_colors[n]); \
|
|
|
|
if (p_uvs.size() > n) \
|
|
|
|
add_uv(p_uvs[n]); \
|
|
|
|
if (p_uv2s.size() > n) \
|
|
|
|
add_uv2(p_uv2s[n]); \
|
|
|
|
if (p_normals.size() > n) \
|
|
|
|
add_normal(p_normals[n]); \
|
|
|
|
if (p_tangents.size() > n) \
|
|
|
|
add_tangent(p_tangents[n]); \
|
|
|
|
add_vertex(p_vertexes[n]); \
|
2016-04-18 19:33:54 +02:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < p_vertexes.size() - 2; i++) {
|
2016-04-18 19:33:54 +02:00
|
|
|
ADD_POINT(0);
|
2017-03-19 00:36:26 +01:00
|
|
|
ADD_POINT(i + 1);
|
|
|
|
ADD_POINT(i + 2);
|
2016-04-18 19:33:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef ADD_POINT
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void SurfaceTool::add_index(int p_index) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!begun);
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
format |= Mesh::ARRAY_FORMAT_INDEX;
|
2014-02-10 02:10:30 +01:00
|
|
|
index_array.push_back(p_index);
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Ref<Mesh> SurfaceTool::commit(const Ref<Mesh> &p_existing) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Ref<Mesh> mesh;
|
|
|
|
if (p_existing.is_valid())
|
2017-03-19 00:36:26 +01:00
|
|
|
mesh = p_existing;
|
2014-02-10 02:10:30 +01:00
|
|
|
else
|
2017-03-19 00:36:26 +01:00
|
|
|
mesh = Ref<Mesh>(memnew(Mesh));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
int varr_len = vertex_array.size();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (varr_len == 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return mesh;
|
|
|
|
|
|
|
|
int surface = mesh->get_surface_count();
|
|
|
|
|
|
|
|
Array a;
|
|
|
|
a.resize(Mesh::ARRAY_MAX);
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < Mesh::ARRAY_MAX; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
switch (format & (1 << i)) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
case Mesh::ARRAY_FORMAT_VERTEX:
|
|
|
|
case Mesh::ARRAY_FORMAT_NORMAL: {
|
|
|
|
|
|
|
|
DVector<Vector3> array;
|
|
|
|
array.resize(varr_len);
|
|
|
|
DVector<Vector3>::Write w = array.write();
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
int idx = 0;
|
|
|
|
for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next(), idx++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
const Vertex &v = E->get();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
switch (i) {
|
2014-02-10 02:10:30 +01:00
|
|
|
case Mesh::ARRAY_VERTEX: {
|
2017-03-19 00:36:26 +01:00
|
|
|
w[idx] = v.vertex;
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
|
|
|
case Mesh::ARRAY_NORMAL: {
|
2017-03-19 00:36:26 +01:00
|
|
|
w[idx] = v.normal;
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
w = DVector<Vector3>::Write();
|
|
|
|
a[i] = array;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Mesh::ARRAY_FORMAT_TEX_UV:
|
|
|
|
case Mesh::ARRAY_FORMAT_TEX_UV2: {
|
|
|
|
|
|
|
|
DVector<Vector2> array;
|
|
|
|
array.resize(varr_len);
|
|
|
|
DVector<Vector2>::Write w = array.write();
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
int idx = 0;
|
|
|
|
for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next(), idx++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
const Vertex &v = E->get();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
switch (i) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
case Mesh::ARRAY_TEX_UV: {
|
2017-03-19 00:36:26 +01:00
|
|
|
w[idx] = v.uv;
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
|
|
|
case Mesh::ARRAY_TEX_UV2: {
|
2017-03-19 00:36:26 +01:00
|
|
|
w[idx] = v.uv2;
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
w = DVector<Vector2>::Write();
|
|
|
|
a[i] = array;
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
|
|
|
case Mesh::ARRAY_FORMAT_TANGENT: {
|
|
|
|
|
|
|
|
DVector<float> array;
|
2017-03-19 00:36:26 +01:00
|
|
|
array.resize(varr_len * 4);
|
2014-02-10 02:10:30 +01:00
|
|
|
DVector<float>::Write w = array.write();
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
int idx = 0;
|
|
|
|
for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next(), idx += 4) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
const Vertex &v = E->get();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
w[idx + 0] = v.tangent.x;
|
|
|
|
w[idx + 1] = v.tangent.y;
|
|
|
|
w[idx + 2] = v.tangent.z;
|
2014-10-14 06:01:25 +02:00
|
|
|
|
|
|
|
//float d = v.tangent.dot(v.binormal,v.normal);
|
2017-03-19 00:36:26 +01:00
|
|
|
float d = v.binormal.dot(v.normal.cross(v.tangent));
|
|
|
|
w[idx + 3] = d < 0 ? -1 : 1;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
w = DVector<float>::Write();
|
|
|
|
a[i] = array;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
} break;
|
2017-03-19 00:36:26 +01:00
|
|
|
case Mesh::ARRAY_FORMAT_COLOR: {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
DVector<Color> array;
|
|
|
|
array.resize(varr_len);
|
|
|
|
DVector<Color>::Write w = array.write();
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
int idx = 0;
|
|
|
|
for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next(), idx++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
const Vertex &v = E->get();
|
|
|
|
w[idx] = v.color;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
w = DVector<Color>::Write();
|
|
|
|
a[i] = array;
|
2016-03-09 00:00:52 +01:00
|
|
|
} break;
|
2014-02-10 02:10:30 +01:00
|
|
|
case Mesh::ARRAY_FORMAT_BONES:
|
|
|
|
case Mesh::ARRAY_FORMAT_WEIGHTS: {
|
|
|
|
|
|
|
|
DVector<float> array;
|
2017-03-19 00:36:26 +01:00
|
|
|
array.resize(varr_len * 4);
|
2014-02-10 02:10:30 +01:00
|
|
|
DVector<float>::Write w = array.write();
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
int idx = 0;
|
|
|
|
for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next(), idx += 4) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
const Vertex &v = E->get();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int j = 0; j < 4; j++) {
|
|
|
|
switch (i) {
|
2014-02-10 02:10:30 +01:00
|
|
|
case Mesh::ARRAY_WEIGHTS: {
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_CONTINUE(v.weights.size() != 4);
|
|
|
|
w[idx + j] = v.weights[j];
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
|
|
|
case Mesh::ARRAY_BONES: {
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_CONTINUE(v.bones.size() != 4);
|
|
|
|
w[idx + j] = v.bones[j];
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
w = DVector<float>::Write();
|
|
|
|
a[i] = array;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case Mesh::ARRAY_FORMAT_INDEX: {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_CONTINUE(index_array.size() == 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
DVector<int> array;
|
|
|
|
array.resize(index_array.size());
|
|
|
|
DVector<int>::Write w = array.write();
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
int idx = 0;
|
|
|
|
for (List<int>::Element *E = index_array.front(); E; E = E->next(), idx++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
w[idx] = E->get();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
w = DVector<int>::Write();
|
|
|
|
a[i] = array;
|
2014-02-10 02:10:30 +01:00
|
|
|
} break;
|
|
|
|
|
|
|
|
default: {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
mesh->add_surface(primitive, a);
|
2014-02-10 02:10:30 +01:00
|
|
|
if (material.is_valid())
|
2017-03-19 00:36:26 +01:00
|
|
|
mesh->surface_set_material(surface, material);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return mesh;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SurfaceTool::index() {
|
|
|
|
|
2014-05-14 06:22:15 +02:00
|
|
|
if (index_array.size())
|
|
|
|
return; //already indexed
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
HashMap<Vertex, int, VertexHasher> indices;
|
2014-05-14 06:22:15 +02:00
|
|
|
List<Vertex> new_vertices;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
int *idxptr = indices.getptr(E->get());
|
2014-05-14 06:22:15 +02:00
|
|
|
int idx;
|
|
|
|
if (!idxptr) {
|
2017-03-19 00:36:26 +01:00
|
|
|
idx = indices.size();
|
2014-05-14 06:22:15 +02:00
|
|
|
new_vertices.push_back(E->get());
|
2017-03-19 00:36:26 +01:00
|
|
|
indices[E->get()] = idx;
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
2017-03-19 00:36:26 +01:00
|
|
|
idx = *idxptr;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2014-05-14 06:22:15 +02:00
|
|
|
index_array.push_back(idx);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2014-05-14 06:22:15 +02:00
|
|
|
vertex_array.clear();
|
2017-03-19 00:36:26 +01:00
|
|
|
vertex_array = new_vertices;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
format |= Mesh::ARRAY_FORMAT_INDEX;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SurfaceTool::deindex() {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (index_array.size() == 0)
|
2014-05-14 06:22:15 +02:00
|
|
|
return; //nothing to deindex
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector<Vertex> varr;
|
2014-05-14 06:22:15 +02:00
|
|
|
varr.resize(vertex_array.size());
|
2017-03-19 00:36:26 +01:00
|
|
|
int idx = 0;
|
|
|
|
for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
varr[idx++] = E->get();
|
2014-05-14 06:22:15 +02:00
|
|
|
}
|
|
|
|
vertex_array.clear();
|
2017-03-19 00:36:26 +01:00
|
|
|
for (List<int>::Element *E = index_array.front(); E; E = E->next()) {
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_INDEX(E->get(), varr.size());
|
2014-05-14 06:22:15 +02:00
|
|
|
vertex_array.push_back(varr[E->get()]);
|
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
format &= ~Mesh::ARRAY_FORMAT_INDEX;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void SurfaceTool::_create_list(const Ref<Mesh> &p_existing, int p_surface, List<Vertex> *r_vertex, List<int> *r_index, int &lformat) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Array arr = p_existing->surface_get_arrays(p_surface);
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_COND(arr.size() != VS::ARRAY_MAX);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
DVector<Vector3> varr = arr[VS::ARRAY_VERTEX];
|
|
|
|
DVector<Vector3> narr = arr[VS::ARRAY_NORMAL];
|
|
|
|
DVector<float> tarr = arr[VS::ARRAY_TANGENT];
|
|
|
|
DVector<Color> carr = arr[VS::ARRAY_COLOR];
|
|
|
|
DVector<Vector2> uvarr = arr[VS::ARRAY_TEX_UV];
|
|
|
|
DVector<Vector2> uv2arr = arr[VS::ARRAY_TEX_UV2];
|
|
|
|
DVector<int> barr = arr[VS::ARRAY_BONES];
|
|
|
|
DVector<float> warr = arr[VS::ARRAY_WEIGHTS];
|
|
|
|
|
|
|
|
int vc = varr.size();
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (vc == 0)
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2017-03-19 00:36:26 +01:00
|
|
|
lformat = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
DVector<Vector3>::Read rv;
|
|
|
|
if (varr.size()) {
|
2017-03-19 00:36:26 +01:00
|
|
|
lformat |= VS::ARRAY_FORMAT_VERTEX;
|
|
|
|
rv = varr.read();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
DVector<Vector3>::Read rn;
|
|
|
|
if (narr.size()) {
|
2017-03-19 00:36:26 +01:00
|
|
|
lformat |= VS::ARRAY_FORMAT_NORMAL;
|
|
|
|
rn = narr.read();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
DVector<float>::Read rt;
|
|
|
|
if (tarr.size()) {
|
2017-03-19 00:36:26 +01:00
|
|
|
lformat |= VS::ARRAY_FORMAT_TANGENT;
|
|
|
|
rt = tarr.read();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
DVector<Color>::Read rc;
|
|
|
|
if (carr.size()) {
|
2017-03-19 00:36:26 +01:00
|
|
|
lformat |= VS::ARRAY_FORMAT_COLOR;
|
|
|
|
rc = carr.read();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DVector<Vector2>::Read ruv;
|
|
|
|
if (uvarr.size()) {
|
2017-03-19 00:36:26 +01:00
|
|
|
lformat |= VS::ARRAY_FORMAT_TEX_UV;
|
|
|
|
ruv = uvarr.read();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DVector<Vector2>::Read ruv2;
|
|
|
|
if (uv2arr.size()) {
|
2017-03-19 00:36:26 +01:00
|
|
|
lformat |= VS::ARRAY_FORMAT_TEX_UV2;
|
|
|
|
ruv2 = uv2arr.read();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DVector<int>::Read rb;
|
|
|
|
if (barr.size()) {
|
2017-03-19 00:36:26 +01:00
|
|
|
lformat |= VS::ARRAY_FORMAT_BONES;
|
|
|
|
rb = barr.read();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DVector<float>::Read rw;
|
|
|
|
if (warr.size()) {
|
2017-03-19 00:36:26 +01:00
|
|
|
lformat |= VS::ARRAY_FORMAT_WEIGHTS;
|
|
|
|
rw = warr.read();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < vc; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Vertex v;
|
2017-03-19 00:36:26 +01:00
|
|
|
if (lformat & VS::ARRAY_FORMAT_VERTEX)
|
|
|
|
v.vertex = varr[i];
|
|
|
|
if (lformat & VS::ARRAY_FORMAT_NORMAL)
|
|
|
|
v.normal = narr[i];
|
|
|
|
if (lformat & VS::ARRAY_FORMAT_TANGENT) {
|
|
|
|
Plane p(tarr[i * 4 + 0], tarr[i * 4 + 1], tarr[i * 4 + 2], tarr[i * 4 + 3]);
|
|
|
|
v.tangent = p.normal;
|
|
|
|
v.binormal = p.normal.cross(last_normal).normalized() * p.d;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
if (lformat & VS::ARRAY_FORMAT_COLOR)
|
|
|
|
v.color = carr[i];
|
|
|
|
if (lformat & VS::ARRAY_FORMAT_TEX_UV)
|
|
|
|
v.uv = uvarr[i];
|
|
|
|
if (lformat & VS::ARRAY_FORMAT_TEX_UV2)
|
|
|
|
v.uv2 = uv2arr[i];
|
|
|
|
if (lformat & VS::ARRAY_FORMAT_BONES) {
|
2014-02-10 02:10:30 +01:00
|
|
|
Vector<int> b;
|
|
|
|
b.resize(4);
|
2017-03-19 00:36:26 +01:00
|
|
|
b[0] = barr[i * 4 + 0];
|
|
|
|
b[1] = barr[i * 4 + 1];
|
|
|
|
b[2] = barr[i * 4 + 2];
|
|
|
|
b[3] = barr[i * 4 + 3];
|
|
|
|
v.bones = b;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
if (lformat & VS::ARRAY_FORMAT_WEIGHTS) {
|
2014-02-10 02:10:30 +01:00
|
|
|
Vector<float> w;
|
|
|
|
w.resize(4);
|
2017-03-19 00:36:26 +01:00
|
|
|
w[0] = warr[i * 4 + 0];
|
|
|
|
w[1] = warr[i * 4 + 1];
|
|
|
|
w[2] = warr[i * 4 + 2];
|
|
|
|
w[3] = warr[i * 4 + 3];
|
|
|
|
v.weights = w;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
r_vertex->push_back(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
//indices
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
DVector<int> idx = arr[VS::ARRAY_INDEX];
|
2014-02-10 02:10:30 +01:00
|
|
|
int is = idx.size();
|
|
|
|
if (is) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
lformat |= VS::ARRAY_FORMAT_INDEX;
|
|
|
|
DVector<int>::Read iarr = idx.read();
|
|
|
|
for (int i = 0; i < is; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
r_index->push_back(iarr[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void SurfaceTool::create_from(const Ref<Mesh> &p_existing, int p_surface) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
clear();
|
2017-03-19 00:36:26 +01:00
|
|
|
primitive = p_existing->surface_get_primitive_type(p_surface);
|
|
|
|
_create_list(p_existing, p_surface, &vertex_array, &index_array, format);
|
|
|
|
material = p_existing->surface_get_material(p_surface);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void SurfaceTool::append_from(const Ref<Mesh> &p_existing, int p_surface, const Transform &p_xform) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (vertex_array.size() == 0) {
|
|
|
|
primitive = p_existing->surface_get_primitive_type(p_surface);
|
|
|
|
format = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int nformat;
|
|
|
|
List<Vertex> nvertices;
|
|
|
|
List<int> nindices;
|
2017-03-19 00:36:26 +01:00
|
|
|
_create_list(p_existing, p_surface, &nvertices, &nindices, nformat);
|
|
|
|
format |= nformat;
|
2014-02-10 02:10:30 +01:00
|
|
|
int vfrom = vertex_array.size();
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (List<Vertex>::Element *E = nvertices.front(); E; E = E->next()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Vertex v = E->get();
|
|
|
|
v.vertex = p_xform.xform(v.vertex);
|
|
|
|
if (nformat & VS::ARRAY_FORMAT_NORMAL) {
|
|
|
|
v.normal = p_xform.basis.xform(v.normal);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
if (nformat & VS::ARRAY_FORMAT_TANGENT) {
|
|
|
|
v.tangent = p_xform.basis.xform(v.tangent);
|
|
|
|
v.binormal = p_xform.basis.xform(v.binormal);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
vertex_array.push_back(v);
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (List<int>::Element *E = nindices.front(); E; E = E->next()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
int dst_index = E->get() + vfrom;
|
2014-02-10 02:10:30 +01:00
|
|
|
//if (dst_index <0 || dst_index>=vertex_array.size()) {
|
|
|
|
// print_line("invalid index!");
|
|
|
|
//}
|
|
|
|
index_array.push_back(dst_index);
|
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
if (index_array.size() % 3)
|
2014-02-10 02:10:30 +01:00
|
|
|
print_line("IA not div of 3?");
|
|
|
|
}
|
|
|
|
|
2014-10-14 06:01:25 +02:00
|
|
|
//mikktspace callbacks
|
2017-03-19 00:36:26 +01:00
|
|
|
int SurfaceTool::mikktGetNumFaces(const SMikkTSpaceContext *pContext) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector<List<Vertex>::Element *> &varr = *((Vector<List<Vertex>::Element *> *)pContext->m_pUserData);
|
|
|
|
return varr.size() / 3;
|
2014-10-14 06:01:25 +02:00
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
int SurfaceTool::mikktGetNumVerticesOfFace(const SMikkTSpaceContext *pContext, const int iFace) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2014-10-14 06:01:25 +02:00
|
|
|
return 3; //always 3
|
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
void SurfaceTool::mikktGetPosition(const SMikkTSpaceContext *pContext, float fvPosOut[], const int iFace, const int iVert) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector<List<Vertex>::Element *> &varr = *((Vector<List<Vertex>::Element *> *)pContext->m_pUserData);
|
|
|
|
Vector3 v = varr[iFace * 3 + iVert]->get().vertex;
|
|
|
|
fvPosOut[0] = v.x;
|
|
|
|
fvPosOut[1] = v.y;
|
|
|
|
fvPosOut[2] = v.z;
|
2014-10-14 06:01:25 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void SurfaceTool::mikktGetNormal(const SMikkTSpaceContext *pContext, float fvNormOut[], const int iFace, const int iVert) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector<List<Vertex>::Element *> &varr = *((Vector<List<Vertex>::Element *> *)pContext->m_pUserData);
|
|
|
|
Vector3 v = varr[iFace * 3 + iVert]->get().normal;
|
|
|
|
fvNormOut[0] = v.x;
|
|
|
|
fvNormOut[1] = v.y;
|
|
|
|
fvNormOut[2] = v.z;
|
2014-10-14 06:01:25 +02:00
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
void SurfaceTool::mikktGetTexCoord(const SMikkTSpaceContext *pContext, float fvTexcOut[], const int iFace, const int iVert) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector<List<Vertex>::Element *> &varr = *((Vector<List<Vertex>::Element *> *)pContext->m_pUserData);
|
|
|
|
Vector2 v = varr[iFace * 3 + iVert]->get().uv;
|
|
|
|
fvTexcOut[0] = v.x;
|
|
|
|
fvTexcOut[1] = v.y;
|
2015-01-04 15:03:31 +01:00
|
|
|
//fvTexcOut[1]=1.0-v.y;
|
2014-10-14 06:01:25 +02:00
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
void SurfaceTool::mikktSetTSpaceBasic(const SMikkTSpaceContext *pContext, const float fvTangent[], const float fSign, const int iFace, const int iVert) {
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector<List<Vertex>::Element *> &varr = *((Vector<List<Vertex>::Element *> *)pContext->m_pUserData);
|
|
|
|
Vertex &vtx = varr[iFace * 3 + iVert]->get();
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
vtx.tangent = Vector3(fvTangent[0], fvTangent[1], fvTangent[2]);
|
2014-10-14 06:01:25 +02:00
|
|
|
vtx.binormal = vtx.normal.cross(vtx.tangent) * fSign;
|
|
|
|
}
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2014-10-14 06:01:25 +02:00
|
|
|
void SurfaceTool::generate_tangents() {
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_COND(!(format & Mesh::ARRAY_FORMAT_TEX_UV));
|
|
|
|
ERR_FAIL_COND(!(format & Mesh::ARRAY_FORMAT_NORMAL));
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
bool indexed = index_array.size() > 0;
|
2014-10-14 06:01:25 +02:00
|
|
|
if (indexed)
|
|
|
|
deindex();
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2014-10-14 06:01:25 +02:00
|
|
|
SMikkTSpaceInterface mkif;
|
2017-03-19 00:36:26 +01:00
|
|
|
mkif.m_getNormal = mikktGetNormal;
|
|
|
|
mkif.m_getNumFaces = mikktGetNumFaces;
|
|
|
|
mkif.m_getNumVerticesOfFace = mikktGetNumVerticesOfFace;
|
|
|
|
mkif.m_getPosition = mikktGetPosition;
|
|
|
|
mkif.m_getTexCoord = mikktGetTexCoord;
|
|
|
|
mkif.m_setTSpaceBasic = mikktSetTSpaceBasic;
|
|
|
|
mkif.m_setTSpace = NULL;
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2014-10-14 06:01:25 +02:00
|
|
|
SMikkTSpaceContext msc;
|
2017-03-19 00:36:26 +01:00
|
|
|
msc.m_pInterface = &mkif;
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector<List<Vertex>::Element *> vtx;
|
2014-10-14 06:01:25 +02:00
|
|
|
vtx.resize(vertex_array.size());
|
2017-03-19 00:36:26 +01:00
|
|
|
int idx = 0;
|
|
|
|
for (List<Vertex>::Element *E = vertex_array.front(); E; E = E->next()) {
|
|
|
|
vtx[idx++] = E;
|
|
|
|
E->get().binormal = Vector3();
|
|
|
|
E->get().tangent = Vector3();
|
2014-10-14 06:01:25 +02:00
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
msc.m_pUserData = &vtx;
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2014-10-14 06:01:25 +02:00
|
|
|
bool res = genTangSpaceDefault(&msc);
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2014-10-14 06:01:25 +02:00
|
|
|
ERR_FAIL_COND(!res);
|
2017-03-19 00:36:26 +01:00
|
|
|
format |= Mesh::ARRAY_FORMAT_TANGENT;
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2014-10-14 06:01:25 +02:00
|
|
|
if (indexed)
|
|
|
|
index();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2014-05-14 06:22:15 +02:00
|
|
|
void SurfaceTool::generate_normals() {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_COND(primitive != Mesh::PRIMITIVE_TRIANGLES);
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
bool was_indexed = index_array.size();
|
2014-05-14 06:22:15 +02:00
|
|
|
|
|
|
|
deindex();
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
HashMap<Vertex, Vector3, VertexHasher> vertex_hash;
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
int count = 0;
|
|
|
|
bool smooth = false;
|
2014-05-14 06:22:15 +02:00
|
|
|
if (smooth_groups.has(0))
|
2017-03-19 00:36:26 +01:00
|
|
|
smooth = smooth_groups[0];
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
List<Vertex>::Element *B = vertex_array.front();
|
|
|
|
for (List<Vertex>::Element *E = B; E;) {
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
List<Vertex>::Element *v[3];
|
|
|
|
v[0] = E;
|
|
|
|
v[1] = v[0]->next();
|
2014-05-14 06:22:15 +02:00
|
|
|
ERR_FAIL_COND(!v[1]);
|
2017-03-19 00:36:26 +01:00
|
|
|
v[2] = v[1]->next();
|
2014-05-14 06:22:15 +02:00
|
|
|
ERR_FAIL_COND(!v[2]);
|
2017-03-19 00:36:26 +01:00
|
|
|
E = v[2]->next();
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector3 normal = Plane(v[0]->get().vertex, v[1]->get().vertex, v[2]->get().vertex).normal;
|
2014-05-14 06:22:15 +02:00
|
|
|
|
|
|
|
if (smooth) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < 3; i++) {
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector3 *lv = vertex_hash.getptr(v[i]->get());
|
2014-05-14 06:22:15 +02:00
|
|
|
if (!lv) {
|
2017-03-19 00:36:26 +01:00
|
|
|
vertex_hash.set(v[i]->get(), normal);
|
2014-05-14 06:22:15 +02:00
|
|
|
} else {
|
2017-03-19 00:36:26 +01:00
|
|
|
(*lv) += normal;
|
2014-05-14 06:22:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < 3; i++) {
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
v[i]->get().normal = normal;
|
2014-05-14 06:22:15 +02:00
|
|
|
}
|
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
count += 3;
|
2014-05-14 06:22:15 +02:00
|
|
|
|
|
|
|
if (smooth_groups.has(count) || !E) {
|
|
|
|
|
|
|
|
if (vertex_hash.size()) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
while (B != E) {
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector3 *lv = vertex_hash.getptr(B->get());
|
2014-05-14 06:22:15 +02:00
|
|
|
if (lv) {
|
2017-03-19 00:36:26 +01:00
|
|
|
B->get().normal = lv->normalized();
|
2014-05-14 06:22:15 +02:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
B = B->next();
|
2014-05-14 06:22:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2017-03-19 00:36:26 +01:00
|
|
|
B = E;
|
2014-05-14 06:22:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
vertex_hash.clear();
|
|
|
|
if (E) {
|
2017-03-19 00:36:26 +01:00
|
|
|
smooth = smooth_groups[count];
|
|
|
|
print_line("SMOOTH AT " + itos(count) + ": " + itos(smooth));
|
2014-05-14 06:22:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
format |= Mesh::ARRAY_FORMAT_NORMAL;
|
2014-05-14 06:22:15 +02:00
|
|
|
|
|
|
|
if (was_indexed) {
|
|
|
|
index();
|
|
|
|
smooth_groups.clear();
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void SurfaceTool::set_material(const Ref<Material> &p_material) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
material = p_material;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SurfaceTool::clear() {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
begun = false;
|
|
|
|
primitive = Mesh::PRIMITIVE_LINES;
|
|
|
|
format = 0;
|
2017-01-14 18:03:38 +01:00
|
|
|
last_bones.clear();
|
2014-02-10 02:10:30 +01:00
|
|
|
last_weights.clear();
|
|
|
|
index_array.clear();
|
|
|
|
vertex_array.clear();
|
2014-05-14 06:22:15 +02:00
|
|
|
smooth_groups.clear();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SurfaceTool::_bind_methods() {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("begin", "primitive"), &SurfaceTool::begin);
|
|
|
|
ObjectTypeDB::bind_method(_MD("add_vertex", "vertex"), &SurfaceTool::add_vertex);
|
|
|
|
ObjectTypeDB::bind_method(_MD("add_color", "color"), &SurfaceTool::add_color);
|
|
|
|
ObjectTypeDB::bind_method(_MD("add_normal", "normal"), &SurfaceTool::add_normal);
|
|
|
|
ObjectTypeDB::bind_method(_MD("add_tangent", "tangent"), &SurfaceTool::add_tangent);
|
|
|
|
ObjectTypeDB::bind_method(_MD("add_uv", "uv"), &SurfaceTool::add_uv);
|
|
|
|
ObjectTypeDB::bind_method(_MD("add_uv2", "uv2"), &SurfaceTool::add_uv2);
|
|
|
|
ObjectTypeDB::bind_method(_MD("add_bones", "bones"), &SurfaceTool::add_bones);
|
|
|
|
ObjectTypeDB::bind_method(_MD("add_weights", "weights"), &SurfaceTool::add_weights);
|
|
|
|
ObjectTypeDB::bind_method(_MD("add_smooth_group", "smooth"), &SurfaceTool::add_smooth_group);
|
|
|
|
ObjectTypeDB::bind_method(_MD("add_triangle_fan", "vertexes", "uvs", "colors", "uv2s", "normals", "tangents"), &SurfaceTool::add_triangle_fan, DEFVAL(Vector<Vector2>()), DEFVAL(Vector<Color>()), DEFVAL(Vector<Vector2>()), DEFVAL(Vector<Vector3>()), DEFVAL(Vector<Plane>()));
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_material", "material:Material"), &SurfaceTool::set_material);
|
|
|
|
ObjectTypeDB::bind_method(_MD("index"), &SurfaceTool::index);
|
|
|
|
ObjectTypeDB::bind_method(_MD("deindex"), &SurfaceTool::deindex);
|
2014-05-14 06:22:15 +02:00
|
|
|
///ObjectTypeDB::bind_method(_MD("generate_flat_normals"),&SurfaceTool::generate_flat_normals);
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("generate_normals"), &SurfaceTool::generate_normals);
|
2016-07-22 05:51:07 +02:00
|
|
|
ObjectTypeDB::bind_method(_MD("add_index", "index"), &SurfaceTool::add_index);
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("commit:Mesh", "existing:Mesh"), &SurfaceTool::commit, DEFVAL(Variant()));
|
|
|
|
ObjectTypeDB::bind_method(_MD("clear"), &SurfaceTool::clear);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SurfaceTool::SurfaceTool() {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
first = false;
|
|
|
|
begun = false;
|
|
|
|
primitive = Mesh::PRIMITIVE_LINES;
|
|
|
|
format = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|