2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* mesh.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "mesh.h"
|
|
|
|
#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"
|
2016-07-07 22:32:28 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
static const char *_array_name[] = {
|
2014-02-10 01:10:30 +00:00
|
|
|
"vertex_array",
|
|
|
|
"normal_array",
|
|
|
|
"tangent_array",
|
|
|
|
"color_array",
|
|
|
|
"tex_uv_array",
|
|
|
|
"tex_uv2_array",
|
|
|
|
"bone_array",
|
|
|
|
"weights_array",
|
|
|
|
"index_array",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
static const Mesh::ArrayType _array_types[] = {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Mesh::ARRAY_VERTEX,
|
|
|
|
Mesh::ARRAY_NORMAL,
|
|
|
|
Mesh::ARRAY_TANGENT,
|
|
|
|
Mesh::ARRAY_COLOR,
|
|
|
|
Mesh::ARRAY_TEX_UV,
|
|
|
|
Mesh::ARRAY_TEX_UV2,
|
|
|
|
Mesh::ARRAY_BONES,
|
|
|
|
Mesh::ARRAY_WEIGHTS,
|
|
|
|
Mesh::ARRAY_INDEX
|
|
|
|
};
|
|
|
|
|
|
|
|
/* compatibility */
|
2017-03-18 23:36:26 +00:00
|
|
|
static const int _format_translate[] = {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Mesh::ARRAY_FORMAT_VERTEX,
|
|
|
|
Mesh::ARRAY_FORMAT_NORMAL,
|
|
|
|
Mesh::ARRAY_FORMAT_TANGENT,
|
|
|
|
Mesh::ARRAY_FORMAT_COLOR,
|
|
|
|
Mesh::ARRAY_FORMAT_TEX_UV,
|
|
|
|
Mesh::ARRAY_FORMAT_TEX_UV2,
|
|
|
|
Mesh::ARRAY_FORMAT_BONES,
|
|
|
|
Mesh::ARRAY_FORMAT_WEIGHTS,
|
|
|
|
Mesh::ARRAY_FORMAT_INDEX,
|
|
|
|
};
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool Mesh::_set(const StringName &p_name, const Variant &p_value) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
String sname = p_name;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p_name == "morph_target/names") {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
DVector<String> sk = p_value;
|
2014-02-10 01:10:30 +00:00
|
|
|
int sz = sk.size();
|
|
|
|
DVector<String>::Read r = sk.read();
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < sz; i++)
|
2014-02-10 01:10:30 +00:00
|
|
|
add_morph_target(r[i]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p_name == "morph_target/mode") {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
set_morph_target_mode(MorphTargetMode(int(p_value)));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-14 04:22:15 +00:00
|
|
|
if (sname.begins_with("surface_")) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int sl = sname.find("/");
|
|
|
|
if (sl == -1)
|
2014-05-14 04:22:15 +00:00
|
|
|
return false;
|
2017-03-18 23:36:26 +00:00
|
|
|
int idx = sname.substr(8, sl - 8).to_int() - 1;
|
|
|
|
String what = sname.get_slicec('/', 1);
|
|
|
|
if (what == "material")
|
|
|
|
surface_set_material(idx, p_value);
|
|
|
|
else if (what == "name")
|
|
|
|
surface_set_name(idx, p_value);
|
2014-02-10 01:10:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (sname == "custom_aabb/custom_aabb") {
|
2014-05-05 01:50:23 +00:00
|
|
|
|
|
|
|
set_custom_aabb(p_value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (!sname.begins_with("surfaces"))
|
|
|
|
return false;
|
|
|
|
|
2017-03-18 23:36:26 +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-03-18 23:36:26 +00:00
|
|
|
if (idx == surfaces.size()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (what == "custom") {
|
2014-02-10 01:10:30 +00:00
|
|
|
add_custom_surface(p_value);
|
|
|
|
return true;
|
2016-03-08 23:00:52 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
//create
|
2017-03-18 23:36:26 +00:00
|
|
|
Dictionary d = p_value;
|
|
|
|
ERR_FAIL_COND_V(!d.has("primitive"), false);
|
|
|
|
ERR_FAIL_COND_V(!d.has("arrays"), false);
|
|
|
|
ERR_FAIL_COND_V(!d.has("morph_arrays"), false);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
bool alphasort = d.has("alphasort") && bool(d["alphasort"]);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
add_surface(PrimitiveType(int(d["primitive"])), d["arrays"], d["morph_arrays"], alphasort);
|
2014-02-10 01:10:30 +00:00
|
|
|
if (d.has("material")) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
surface_set_material(idx, d["material"]);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
if (d.has("name")) {
|
2017-03-18 23:36:26 +00:00
|
|
|
surface_set_name(idx, d["name"]);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool Mesh::_get(const StringName &p_name, Variant &r_ret) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
String sname = p_name;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p_name == "morph_target/names") {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
DVector<String> sk;
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < morph_targets.size(); i++)
|
2014-02-10 01:10:30 +00:00
|
|
|
sk.push_back(morph_targets[i]);
|
2017-03-18 23:36:26 +00:00
|
|
|
r_ret = sk;
|
2014-02-10 01:10:30 +00:00
|
|
|
return true;
|
2017-03-18 23:36:26 +00:00
|
|
|
} else if (p_name == "morph_target/mode") {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
r_ret = get_morph_target_mode();
|
|
|
|
return true;
|
2014-05-14 04:22:15 +00:00
|
|
|
} else if (sname.begins_with("surface_")) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int sl = sname.find("/");
|
|
|
|
if (sl == -1)
|
2014-05-14 04:22:15 +00:00
|
|
|
return false;
|
2017-03-18 23:36:26 +00:00
|
|
|
int idx = sname.substr(8, sl - 8).to_int() - 1;
|
|
|
|
String what = sname.get_slicec('/', 1);
|
|
|
|
if (what == "material")
|
|
|
|
r_ret = surface_get_material(idx);
|
|
|
|
else if (what == "name")
|
|
|
|
r_ret = surface_get_name(idx);
|
2014-02-10 01:10:30 +00:00
|
|
|
return true;
|
2017-03-18 23:36:26 +00:00
|
|
|
} else if (sname == "custom_aabb/custom_aabb") {
|
2014-05-05 01:50:23 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
r_ret = custom_aabb;
|
2014-05-05 01:50:23 +00:00
|
|
|
return true;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
} else if (!sname.begins_with("surfaces"))
|
|
|
|
return false;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int idx = sname.get_slicec('/', 1).to_int();
|
|
|
|
ERR_FAIL_INDEX_V(idx, surfaces.size(), false);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Dictionary d;
|
2017-03-18 23:36:26 +00:00
|
|
|
d["primitive"] = surface_get_primitive_type(idx);
|
|
|
|
d["arrays"] = surface_get_arrays(idx);
|
|
|
|
d["morph_arrays"] = surface_get_morph_arrays(idx);
|
|
|
|
d["alphasort"] = surface_is_alpha_sorting_enabled(idx);
|
2014-02-10 01:10:30 +00:00
|
|
|
Ref<Material> m = surface_get_material(idx);
|
|
|
|
if (m.is_valid())
|
2017-03-18 23:36:26 +00:00
|
|
|
d["material"] = m;
|
2014-02-10 01:10:30 +00:00
|
|
|
String n = surface_get_name(idx);
|
2017-03-18 23:36:26 +00:00
|
|
|
if (n != "")
|
|
|
|
d["name"] = n;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
r_ret = d;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void Mesh::_get_property_list(List<PropertyInfo> *p_list) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (morph_targets.size()) {
|
2017-03-18 23:36:26 +00:00
|
|
|
p_list->push_back(PropertyInfo(Variant::STRING_ARRAY, "morph_target/names", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
|
|
|
|
p_list->push_back(PropertyInfo(Variant::INT, "morph_target/mode", PROPERTY_HINT_ENUM, "Normalized,Relative"));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < surfaces.size(); i++) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
p_list->push_back(PropertyInfo(Variant::DICTIONARY, "surfaces/" + itos(i), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
|
|
|
|
p_list->push_back(PropertyInfo(Variant::STRING, "surface_" + itos(i + 1) + "/name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
|
|
|
|
p_list->push_back(PropertyInfo(Variant::OBJECT, "surface_" + itos(i + 1) + "/material", PROPERTY_HINT_RESOURCE_TYPE, "Material", PROPERTY_USAGE_EDITOR));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2014-05-05 01:50:23 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
p_list->push_back(PropertyInfo(Variant::_AABB, "custom_aabb/custom_aabb"));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Mesh::_recompute_aabb() {
|
|
|
|
|
|
|
|
// regenerate AABB
|
2017-03-18 23:36:26 +00:00
|
|
|
aabb = AABB();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < surfaces.size(); i++) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (i == 0)
|
|
|
|
aabb = surfaces[i].aabb;
|
2014-02-10 01:10:30 +00:00
|
|
|
else
|
|
|
|
aabb.merge_with(surfaces[i].aabb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void Mesh::add_surface(PrimitiveType p_primitive, const Array &p_arrays, const Array &p_blend_shapes, bool p_alphasort) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND(p_arrays.size() != ARRAY_MAX);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Surface s;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
VisualServer::get_singleton()->mesh_add_surface(mesh, (VisualServer::PrimitiveType)p_primitive, p_arrays, p_blend_shapes, p_alphasort);
|
2014-02-10 01:10:30 +00:00
|
|
|
surfaces.push_back(s);
|
|
|
|
|
|
|
|
/* make aABB? */ {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
DVector<Vector3> vertices = p_arrays[ARRAY_VERTEX];
|
|
|
|
int len = vertices.size();
|
|
|
|
ERR_FAIL_COND(len == 0);
|
|
|
|
DVector<Vector3>::Read r = vertices.read();
|
|
|
|
const Vector3 *vtx = r.ptr();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
// check AABB
|
|
|
|
AABB aabb;
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < len; i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (i == 0)
|
|
|
|
aabb.pos = vtx[i];
|
2014-02-10 01:10:30 +00:00
|
|
|
else
|
|
|
|
aabb.expand_to(vtx[i]);
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
surfaces[surfaces.size() - 1].aabb = aabb;
|
|
|
|
surfaces[surfaces.size() - 1].alphasort = p_alphasort;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
_recompute_aabb();
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
triangle_mesh = Ref<TriangleMesh>();
|
2014-02-10 01:10:30 +00:00
|
|
|
_change_notify();
|
2016-05-27 17:18:40 +00:00
|
|
|
emit_changed();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Array Mesh::surface_get_arrays(int p_surface) const {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_surface, surfaces.size(), Array());
|
|
|
|
return VisualServer::get_singleton()->mesh_get_surface_arrays(mesh, p_surface);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
Array Mesh::surface_get_morph_arrays(int p_surface) const {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_surface, surfaces.size(), Array());
|
|
|
|
return VisualServer::get_singleton()->mesh_get_surface_morph_arrays(mesh, p_surface);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void Mesh::add_custom_surface(const Variant &p_data) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Surface s;
|
2017-03-18 23:36:26 +00:00
|
|
|
s.aabb = AABB();
|
|
|
|
VisualServer::get_singleton()->mesh_add_custom_surface(mesh, p_data);
|
2014-02-10 01:10:30 +00:00
|
|
|
surfaces.push_back(s);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
triangle_mesh = Ref<TriangleMesh>();
|
2014-02-10 01:10:30 +00:00
|
|
|
_change_notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
int Mesh::get_surface_count() const {
|
|
|
|
|
|
|
|
return surfaces.size();
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void Mesh::add_morph_target(const StringName &p_name) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (surfaces.size()) {
|
|
|
|
ERR_EXPLAIN("Can't add a shape key count if surfaces are already created.");
|
|
|
|
ERR_FAIL_COND(surfaces.size());
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
StringName name = p_name;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (morph_targets.find(name) != -1) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int count = 2;
|
2014-02-10 01:10:30 +00:00
|
|
|
do {
|
|
|
|
|
|
|
|
name = String(p_name) + " " + itos(count);
|
|
|
|
count++;
|
2017-03-18 23:36:26 +00:00
|
|
|
} while (morph_targets.find(name) != -1);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
morph_targets.push_back(name);
|
2017-03-18 23:36:26 +00:00
|
|
|
VS::get_singleton()->mesh_set_morph_target_count(mesh, morph_targets.size());
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Mesh::get_morph_target_count() const {
|
|
|
|
|
|
|
|
return morph_targets.size();
|
|
|
|
}
|
|
|
|
StringName Mesh::get_morph_target_name(int p_index) const {
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_index, morph_targets.size(), StringName());
|
2014-02-10 01:10:30 +00:00
|
|
|
return morph_targets[p_index];
|
|
|
|
}
|
|
|
|
void Mesh::clear_morph_targets() {
|
|
|
|
|
|
|
|
if (surfaces.size()) {
|
|
|
|
ERR_EXPLAIN("Can't set shape key count if surfaces are already created.");
|
|
|
|
ERR_FAIL_COND(surfaces.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
morph_targets.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mesh::set_morph_target_mode(MorphTargetMode p_mode) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
morph_target_mode = p_mode;
|
|
|
|
VS::get_singleton()->mesh_set_morph_target_mode(mesh, (VS::MorphTargetMode)p_mode);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Mesh::MorphTargetMode Mesh::get_morph_target_mode() const {
|
|
|
|
|
|
|
|
return morph_target_mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mesh::surface_remove(int p_idx) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_INDEX(p_idx, surfaces.size());
|
|
|
|
VisualServer::get_singleton()->mesh_remove_surface(mesh, p_idx);
|
2014-02-10 01:10:30 +00:00
|
|
|
surfaces.remove(p_idx);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
triangle_mesh = Ref<TriangleMesh>();
|
2014-02-10 01:10:30 +00:00
|
|
|
_recompute_aabb();
|
|
|
|
_change_notify();
|
2016-05-27 17:18:40 +00:00
|
|
|
emit_changed();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Mesh::surface_get_array_len(int p_idx) const {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
int Mesh::surface_get_array_index_len(int p_idx) const {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Mesh::surface_get_format(int p_idx) const {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
Mesh::PrimitiveType Mesh::surface_get_primitive_type(int p_idx) const {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
bool Mesh::surface_is_alpha_sorting_enabled(int p_idx) const {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_idx, surfaces.size(), 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
return surfaces[p_idx].alphasort;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void Mesh::surface_set_material(int p_idx, const Ref<Material> &p_material) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_INDEX(p_idx, surfaces.size());
|
|
|
|
if (surfaces[p_idx].material == p_material)
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2017-03-18 23:36:26 +00:00
|
|
|
surfaces[p_idx].material = p_material;
|
|
|
|
VisualServer::get_singleton()->mesh_surface_set_material(mesh, p_idx, p_material.is_null() ? RID() : p_material->get_rid());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
_change_notify("material");
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void Mesh::surface_set_name(int p_idx, const String &p_name) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_INDEX(p_idx, surfaces.size());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
surfaces[p_idx].name = p_name;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
String Mesh::surface_get_name(int p_idx) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_idx, surfaces.size(), String());
|
2014-02-10 01:10:30 +00:00
|
|
|
return surfaces[p_idx].name;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void Mesh::surface_set_custom_aabb(int p_idx, const AABB &p_aabb) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_INDEX(p_idx, surfaces.size());
|
|
|
|
surfaces[p_idx].aabb = p_aabb;
|
|
|
|
// set custom aabb too?
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Ref<Material> Mesh::surface_get_material(int p_idx) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_idx, surfaces.size(), Ref<Material>());
|
2014-02-10 01:10:30 +00:00
|
|
|
return surfaces[p_idx].material;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void Mesh::add_surface_from_mesh_data(const Geometry::MeshData &p_mesh_data) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
VisualServer::get_singleton()->mesh_add_surface_from_mesh_data(mesh, p_mesh_data);
|
2014-02-10 01:10:30 +00:00
|
|
|
AABB aabb;
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < p_mesh_data.vertices.size(); i++) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (i == 0)
|
|
|
|
aabb.pos = p_mesh_data.vertices[i];
|
2014-02-10 01:10:30 +00:00
|
|
|
else
|
|
|
|
aabb.expand_to(p_mesh_data.vertices[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Surface s;
|
2017-03-18 23:36:26 +00:00
|
|
|
s.aabb = aabb;
|
|
|
|
if (surfaces.size() == 0)
|
|
|
|
aabb = s.aabb;
|
2014-02-10 01:10:30 +00:00
|
|
|
else
|
|
|
|
aabb.merge_with(s.aabb);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
triangle_mesh = Ref<TriangleMesh>();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
surfaces.push_back(s);
|
|
|
|
_change_notify();
|
2016-05-27 17:18:40 +00:00
|
|
|
|
|
|
|
emit_changed();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RID Mesh::get_rid() const {
|
|
|
|
|
|
|
|
return mesh;
|
|
|
|
}
|
|
|
|
AABB Mesh::get_aabb() const {
|
|
|
|
|
|
|
|
return aabb;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void Mesh::set_custom_aabb(const AABB &p_custom) {
|
2014-05-05 01:50:23 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
custom_aabb = p_custom;
|
|
|
|
VS::get_singleton()->mesh_set_custom_aabb(mesh, custom_aabb);
|
2014-05-05 01:50:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AABB Mesh::get_custom_aabb() const {
|
|
|
|
|
|
|
|
return custom_aabb;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
DVector<Face3> Mesh::get_faces() const {
|
|
|
|
|
|
|
|
Ref<TriangleMesh> tm = generate_triangle_mesh();
|
|
|
|
if (tm.is_valid())
|
|
|
|
return tm->get_faces();
|
|
|
|
return DVector<Face3>();
|
2017-03-18 23:36:26 +00:00
|
|
|
/*
|
2014-02-10 01:10:30 +00:00
|
|
|
for (int i=0;i<surfaces.size();i++) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (VisualServer::get_singleton()->mesh_surface_get_primitive_type( mesh, i ) != VisualServer::PRIMITIVE_TRIANGLES )
|
|
|
|
continue;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
DVector<int> indices;
|
|
|
|
DVector<Vector3> vertices;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
vertices=VisualServer::get_singleton()->mesh_surface_get_array(mesh, i,VisualServer::ARRAY_VERTEX);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
int len=VisualServer::get_singleton()->mesh_surface_get_array_index_len(mesh, i);
|
|
|
|
bool has_indices;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (len>0) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
indices=VisualServer::get_singleton()->mesh_surface_get_array(mesh, i,VisualServer::ARRAY_INDEX);
|
|
|
|
has_indices=true;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
len=vertices.size();
|
|
|
|
has_indices=false;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (len<=0)
|
|
|
|
continue;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
DVector<int>::Read indicesr = indices.read();
|
|
|
|
const int *indicesptr = indicesr.ptr();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
DVector<Vector3>::Read verticesr = vertices.read();
|
|
|
|
const Vector3 *verticesptr = verticesr.ptr();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
int old_faces=faces.size();
|
|
|
|
int new_faces=old_faces+(len/3);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
faces.resize(new_faces);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
DVector<Face3>::Write facesw = faces.write();
|
|
|
|
Face3 *facesptr=facesw.ptr();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
for (int i=0;i<len/3;i++) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
Face3 face;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
for (int j=0;j<3;j++) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
int idx=i*3+j;
|
|
|
|
face.vertex[j] = has_indices ? verticesptr[ indicesptr[ idx ] ] : verticesptr[idx];
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
facesptr[i+old_faces]=face;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Shape> Mesh::create_convex_shape() const {
|
|
|
|
|
|
|
|
DVector<Vector3> vertices;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < get_surface_count(); i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Array a = surface_get_arrays(i);
|
2017-03-18 23:36:26 +00:00
|
|
|
DVector<Vector3> v = a[ARRAY_VERTEX];
|
2014-02-10 01:10:30 +00:00
|
|
|
vertices.append_array(v);
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Ref<ConvexPolygonShape> shape = memnew(ConvexPolygonShape);
|
2014-02-10 01:10:30 +00:00
|
|
|
shape->set_points(vertices);
|
|
|
|
return shape;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Shape> Mesh::create_trimesh_shape() const {
|
|
|
|
|
|
|
|
DVector<Face3> faces = get_faces();
|
2017-03-18 23:36:26 +00:00
|
|
|
if (faces.size() == 0)
|
2014-02-10 01:10:30 +00:00
|
|
|
return Ref<Shape>();
|
|
|
|
|
|
|
|
DVector<Vector3> face_points;
|
2017-03-18 23:36:26 +00:00
|
|
|
face_points.resize(faces.size() * 3);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < face_points.size(); i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Face3 f = faces.get(i / 3);
|
|
|
|
face_points.set(i, f.vertex[i % 3]);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Ref<ConcavePolygonShape> shape = memnew(ConcavePolygonShape);
|
2014-02-10 01:10:30 +00:00
|
|
|
shape->set_faces(face_points);
|
|
|
|
return shape;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mesh::center_geometry() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
/*
|
2014-02-10 01:10:30 +00:00
|
|
|
Vector3 ofs = aabb.pos+aabb.size*0.5;
|
|
|
|
|
|
|
|
for(int i=0;i<get_surface_count();i++) {
|
|
|
|
|
|
|
|
DVector<Vector3> geom = surface_get_array(i,ARRAY_VERTEX);
|
|
|
|
int gc =geom.size();
|
|
|
|
DVector<Vector3>::Write w = geom.write();
|
|
|
|
surfaces[i].aabb.pos-=ofs;
|
|
|
|
|
|
|
|
for(int i=0;i<gc;i++) {
|
|
|
|
|
|
|
|
w[i]-=ofs;
|
|
|
|
}
|
|
|
|
|
|
|
|
w = DVector<Vector3>::Write();
|
|
|
|
|
|
|
|
surface_set_array(i,ARRAY_VERTEX,geom);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
aabb.pos-=ofs;
|
|
|
|
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2014-10-14 04:01:25 +00:00
|
|
|
void Mesh::regen_normalmaps() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Vector<Ref<SurfaceTool> > surfs;
|
|
|
|
for (int i = 0; i < get_surface_count(); i++) {
|
2014-10-14 04:01:25 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Ref<SurfaceTool> st = memnew(SurfaceTool);
|
|
|
|
st->create_from(Ref<Mesh>(this), i);
|
2014-10-14 04:01:25 +00:00
|
|
|
surfs.push_back(st);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (get_surface_count()) {
|
|
|
|
surface_remove(0);
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < surfs.size(); i++) {
|
2014-10-14 04:01:25 +00:00
|
|
|
|
|
|
|
surfs[i]->generate_tangents();
|
|
|
|
surfs[i]->commit(Ref<Mesh>(this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
Ref<TriangleMesh> Mesh::generate_triangle_mesh() const {
|
|
|
|
|
|
|
|
if (triangle_mesh.is_valid())
|
|
|
|
return triangle_mesh;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int facecount = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < get_surface_count(); i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (surface_get_primitive_type(i) != PRIMITIVE_TRIANGLES)
|
2014-02-10 01:10:30 +00:00
|
|
|
continue;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (surface_get_format(i) & ARRAY_FORMAT_INDEX) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
facecount += surface_get_array_index_len(i);
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
facecount += surface_get_array_len(i);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (facecount == 0 || (facecount % 3) != 0)
|
2014-02-10 01:10:30 +00:00
|
|
|
return triangle_mesh;
|
|
|
|
|
|
|
|
DVector<Vector3> faces;
|
|
|
|
faces.resize(facecount);
|
2017-03-18 23:36:26 +00:00
|
|
|
DVector<Vector3>::Write facesw = faces.write();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int widx = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < get_surface_count(); i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (surface_get_primitive_type(i) != PRIMITIVE_TRIANGLES)
|
2014-02-10 01:10:30 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
Array a = surface_get_arrays(i);
|
|
|
|
|
|
|
|
int vc = surface_get_array_len(i);
|
|
|
|
DVector<Vector3> vertices = a[ARRAY_VERTEX];
|
2017-03-18 23:36:26 +00:00
|
|
|
DVector<Vector3>::Read vr = vertices.read();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (surface_get_format(i) & ARRAY_FORMAT_INDEX) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int ic = surface_get_array_index_len(i);
|
2014-02-10 01:10:30 +00:00
|
|
|
DVector<int> indices = a[ARRAY_INDEX];
|
|
|
|
DVector<int>::Read ir = indices.read();
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < ic; i++)
|
|
|
|
facesw[widx++] = vr[ir[i]];
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < vc; i++)
|
|
|
|
facesw[widx++] = vr[i];
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
facesw = DVector<Vector3>::Write();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
triangle_mesh = Ref<TriangleMesh>(memnew(TriangleMesh));
|
2014-02-10 01:10:30 +00:00
|
|
|
triangle_mesh->create(faces);
|
|
|
|
|
|
|
|
return triangle_mesh;
|
|
|
|
}
|
|
|
|
|
2014-11-02 14:31:01 +00:00
|
|
|
Ref<Mesh> Mesh::create_outline(float p_margin) const {
|
|
|
|
|
|
|
|
Array arrays;
|
2017-03-18 23:36:26 +00:00
|
|
|
int index_accum = 0;
|
|
|
|
for (int i = 0; i < get_surface_count(); i++) {
|
2014-11-02 14:31:01 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (surface_get_primitive_type(i) != PRIMITIVE_TRIANGLES)
|
2014-11-02 14:31:01 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
Array a = surface_get_arrays(i);
|
2017-03-18 23:36:26 +00:00
|
|
|
int vcount = 0;
|
2014-11-02 14:31:01 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (i == 0) {
|
|
|
|
arrays = a;
|
|
|
|
DVector<Vector3> v = a[ARRAY_VERTEX];
|
|
|
|
index_accum += v.size();
|
2014-11-02 14:31:01 +00:00
|
|
|
} else {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int j = 0; j < arrays.size(); j++) {
|
2014-11-02 14:31:01 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (arrays[j].get_type() == Variant::NIL || a[j].get_type() == Variant::NIL) {
|
2014-11-02 14:31:01 +00:00
|
|
|
//mismatch, do not use
|
2017-03-18 23:36:26 +00:00
|
|
|
arrays[j] = Variant();
|
2014-11-02 14:31:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
switch (j) {
|
2014-11-02 14:31:01 +00:00
|
|
|
|
|
|
|
case ARRAY_VERTEX:
|
2017-03-18 23:36:26 +00:00
|
|
|
case ARRAY_NORMAL: {
|
2014-11-02 14:31:01 +00:00
|
|
|
|
|
|
|
DVector<Vector3> dst = arrays[j];
|
|
|
|
DVector<Vector3> src = a[j];
|
2017-03-18 23:36:26 +00:00
|
|
|
if (j == ARRAY_VERTEX)
|
|
|
|
vcount = src.size();
|
|
|
|
if (dst.size() == 0 || src.size() == 0) {
|
|
|
|
arrays[j] = Variant();
|
2014-11-02 14:31:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
dst.append_array(src);
|
2017-03-18 23:36:26 +00:00
|
|
|
arrays[j] = dst;
|
2014-11-02 14:31:01 +00:00
|
|
|
} break;
|
|
|
|
case ARRAY_TANGENT:
|
|
|
|
case ARRAY_BONES:
|
|
|
|
case ARRAY_WEIGHTS: {
|
|
|
|
|
|
|
|
DVector<real_t> dst = arrays[j];
|
|
|
|
DVector<real_t> src = a[j];
|
2017-03-18 23:36:26 +00:00
|
|
|
if (dst.size() == 0 || src.size() == 0) {
|
|
|
|
arrays[j] = Variant();
|
2014-11-02 14:31:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
dst.append_array(src);
|
2017-03-18 23:36:26 +00:00
|
|
|
arrays[j] = dst;
|
2014-11-02 14:31:01 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case ARRAY_COLOR: {
|
|
|
|
DVector<Color> dst = arrays[j];
|
|
|
|
DVector<Color> src = a[j];
|
2017-03-18 23:36:26 +00:00
|
|
|
if (dst.size() == 0 || src.size() == 0) {
|
|
|
|
arrays[j] = Variant();
|
2014-11-02 14:31:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
dst.append_array(src);
|
2017-03-18 23:36:26 +00:00
|
|
|
arrays[j] = dst;
|
2014-11-02 14:31:01 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case ARRAY_TEX_UV:
|
|
|
|
case ARRAY_TEX_UV2: {
|
|
|
|
DVector<Vector2> dst = arrays[j];
|
|
|
|
DVector<Vector2> src = a[j];
|
2017-03-18 23:36:26 +00:00
|
|
|
if (dst.size() == 0 || src.size() == 0) {
|
|
|
|
arrays[j] = Variant();
|
2014-11-02 14:31:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
dst.append_array(src);
|
2017-03-18 23:36:26 +00:00
|
|
|
arrays[j] = dst;
|
2014-11-02 14:31:01 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case ARRAY_INDEX: {
|
|
|
|
DVector<int> dst = arrays[j];
|
|
|
|
DVector<int> src = a[j];
|
2017-03-18 23:36:26 +00:00
|
|
|
if (dst.size() == 0 || src.size() == 0) {
|
|
|
|
arrays[j] = Variant();
|
2014-11-02 14:31:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
int ss = src.size();
|
|
|
|
DVector<int>::Write w = src.write();
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int k = 0; k < ss; k++) {
|
|
|
|
w[k] += index_accum;
|
2014-11-02 14:31:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
dst.append_array(src);
|
2017-03-18 23:36:26 +00:00
|
|
|
arrays[j] = dst;
|
|
|
|
index_accum += vcount;
|
2014-11-02 14:31:01 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DVector<int>::Write ir;
|
2017-03-18 23:36:26 +00:00
|
|
|
DVector<int> indices = arrays[ARRAY_INDEX];
|
|
|
|
bool has_indices = false;
|
|
|
|
DVector<Vector3> vertices = arrays[ARRAY_VERTEX];
|
2014-11-02 14:31:01 +00:00
|
|
|
int vc = vertices.size();
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND_V(!vc, Ref<Mesh>());
|
|
|
|
DVector<Vector3>::Write r = vertices.write();
|
2014-11-02 14:31:01 +00:00
|
|
|
|
|
|
|
if (indices.size()) {
|
2017-03-18 23:36:26 +00:00
|
|
|
vc = indices.size();
|
|
|
|
ir = indices.write();
|
|
|
|
has_indices = true;
|
2014-11-02 14:31:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Map<Vector3, Vector3> normal_accum;
|
2014-11-02 14:31:01 +00:00
|
|
|
|
|
|
|
//fill normals with triangle normals
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < vc; i += 3) {
|
2014-11-02 14:31:01 +00:00
|
|
|
|
|
|
|
Vector3 t[3];
|
|
|
|
|
|
|
|
if (has_indices) {
|
2017-03-18 23:36:26 +00:00
|
|
|
t[0] = r[ir[i + 0]];
|
|
|
|
t[1] = r[ir[i + 1]];
|
|
|
|
t[2] = r[ir[i + 2]];
|
2014-11-02 14:31:01 +00:00
|
|
|
} else {
|
2017-03-18 23:36:26 +00:00
|
|
|
t[0] = r[i + 0];
|
|
|
|
t[1] = r[i + 1];
|
|
|
|
t[2] = r[i + 2];
|
2014-11-02 14:31:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Vector3 n = Plane(t[0], t[1], t[2]).normal;
|
2014-11-02 14:31:01 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int j = 0; j < 3; j++) {
|
2014-11-02 14:31:01 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Map<Vector3, Vector3>::Element *E = normal_accum.find(t[j]);
|
2014-11-02 14:31:01 +00:00
|
|
|
if (!E) {
|
2017-03-18 23:36:26 +00:00
|
|
|
normal_accum[t[j]] = n;
|
2014-11-02 14:31:01 +00:00
|
|
|
} else {
|
|
|
|
float d = n.dot(E->get());
|
2017-03-18 23:36:26 +00:00
|
|
|
if (d < 1.0)
|
|
|
|
E->get() += n * (1.0 - d);
|
2014-11-02 14:31:01 +00:00
|
|
|
//E->get()+=n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//normalize
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (Map<Vector3, Vector3>::Element *E = normal_accum.front(); E; E = E->next()) {
|
2014-11-02 14:31:01 +00:00
|
|
|
E->get().normalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
//displace normals
|
|
|
|
int vc2 = vertices.size();
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < vc2; i++) {
|
2014-11-02 14:31:01 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Vector3 t = r[i];
|
2014-11-02 14:31:01 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Map<Vector3, Vector3>::Element *E = normal_accum.find(t);
|
2014-11-02 14:31:01 +00:00
|
|
|
ERR_CONTINUE(!E);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
t += E->get() * p_margin;
|
|
|
|
r[i] = t;
|
2014-11-02 14:31:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r = DVector<Vector3>::Write();
|
2017-03-18 23:36:26 +00:00
|
|
|
arrays[ARRAY_VERTEX] = vertices;
|
2014-11-02 14:31:01 +00:00
|
|
|
|
|
|
|
if (!has_indices) {
|
|
|
|
|
|
|
|
DVector<int> new_indices;
|
|
|
|
new_indices.resize(vertices.size());
|
|
|
|
DVector<int>::Write iw = new_indices.write();
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int j = 0; j < vc2; j += 3) {
|
2014-11-02 14:31:01 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
iw[j] = j;
|
|
|
|
iw[j + 1] = j + 2;
|
|
|
|
iw[j + 2] = j + 1;
|
2014-11-02 14:31:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
iw = DVector<int>::Write();
|
|
|
|
arrays[ARRAY_INDEX] = new_indices;
|
2014-11-02 14:31:01 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int j = 0; j < vc; j += 3) {
|
2014-11-02 14:31:01 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
SWAP(ir[j + 1], ir[j + 2]);
|
2014-11-02 14:31:01 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
ir = DVector<int>::Write();
|
|
|
|
arrays[ARRAY_INDEX] = indices;
|
2014-11-02 14:31:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Ref<Mesh> newmesh = memnew(Mesh);
|
|
|
|
newmesh->add_surface(PRIMITIVE_TRIANGLES, arrays);
|
2014-11-02 14:31:01 +00:00
|
|
|
return newmesh;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void Mesh::_bind_methods() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("add_morph_target", "name"), &Mesh::add_morph_target);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_morph_target_count"), &Mesh::get_morph_target_count);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_morph_target_name", "index"), &Mesh::get_morph_target_name);
|
|
|
|
ObjectTypeDB::bind_method(_MD("clear_morph_targets"), &Mesh::clear_morph_targets);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_morph_target_mode", "mode"), &Mesh::set_morph_target_mode);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_morph_target_mode"), &Mesh::get_morph_target_mode);
|
|
|
|
|
|
|
|
ObjectTypeDB::bind_method(_MD("add_surface", "primitive", "arrays", "morph_arrays", "alphasort"), &Mesh::add_surface, DEFVAL(Array()), DEFVAL(false));
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_surface_count"), &Mesh::get_surface_count);
|
|
|
|
ObjectTypeDB::bind_method(_MD("surface_remove", "surf_idx"), &Mesh::surface_remove);
|
|
|
|
ObjectTypeDB::bind_method(_MD("surface_get_array_len", "surf_idx"), &Mesh::surface_get_array_len);
|
|
|
|
ObjectTypeDB::bind_method(_MD("surface_get_array_index_len", "surf_idx"), &Mesh::surface_get_array_index_len);
|
|
|
|
ObjectTypeDB::bind_method(_MD("surface_get_format", "surf_idx"), &Mesh::surface_get_format);
|
|
|
|
ObjectTypeDB::bind_method(_MD("surface_get_primitive_type", "surf_idx"), &Mesh::surface_get_primitive_type);
|
|
|
|
ObjectTypeDB::bind_method(_MD("surface_set_material", "surf_idx", "material:Material"), &Mesh::surface_set_material);
|
|
|
|
ObjectTypeDB::bind_method(_MD("surface_get_material:Material", "surf_idx"), &Mesh::surface_get_material);
|
|
|
|
ObjectTypeDB::bind_method(_MD("surface_set_name", "surf_idx", "name"), &Mesh::surface_set_name);
|
|
|
|
ObjectTypeDB::bind_method(_MD("surface_get_name", "surf_idx"), &Mesh::surface_get_name);
|
|
|
|
ObjectTypeDB::bind_method(_MD("center_geometry"), &Mesh::center_geometry);
|
|
|
|
ObjectTypeDB::set_method_flags(get_type_static(), _SCS("center_geometry"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
|
|
|
|
ObjectTypeDB::bind_method(_MD("regen_normalmaps"), &Mesh::regen_normalmaps);
|
|
|
|
ObjectTypeDB::set_method_flags(get_type_static(), _SCS("regen_normalmaps"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
|
|
|
|
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_custom_aabb", "aabb"), &Mesh::set_custom_aabb);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_custom_aabb"), &Mesh::get_custom_aabb);
|
|
|
|
|
|
|
|
BIND_CONSTANT(NO_INDEX_ARRAY);
|
|
|
|
BIND_CONSTANT(ARRAY_WEIGHTS_SIZE);
|
|
|
|
|
|
|
|
BIND_CONSTANT(ARRAY_VERTEX);
|
|
|
|
BIND_CONSTANT(ARRAY_NORMAL);
|
|
|
|
BIND_CONSTANT(ARRAY_TANGENT);
|
|
|
|
BIND_CONSTANT(ARRAY_COLOR);
|
|
|
|
BIND_CONSTANT(ARRAY_TEX_UV);
|
|
|
|
BIND_CONSTANT(ARRAY_TEX_UV2);
|
|
|
|
BIND_CONSTANT(ARRAY_BONES);
|
|
|
|
BIND_CONSTANT(ARRAY_WEIGHTS);
|
|
|
|
BIND_CONSTANT(ARRAY_INDEX);
|
|
|
|
|
|
|
|
BIND_CONSTANT(ARRAY_FORMAT_VERTEX);
|
|
|
|
BIND_CONSTANT(ARRAY_FORMAT_NORMAL);
|
|
|
|
BIND_CONSTANT(ARRAY_FORMAT_TANGENT);
|
|
|
|
BIND_CONSTANT(ARRAY_FORMAT_COLOR);
|
|
|
|
BIND_CONSTANT(ARRAY_FORMAT_TEX_UV);
|
|
|
|
BIND_CONSTANT(ARRAY_FORMAT_TEX_UV2);
|
|
|
|
BIND_CONSTANT(ARRAY_FORMAT_BONES);
|
|
|
|
BIND_CONSTANT(ARRAY_FORMAT_WEIGHTS);
|
|
|
|
BIND_CONSTANT(ARRAY_FORMAT_INDEX);
|
|
|
|
|
|
|
|
BIND_CONSTANT(PRIMITIVE_POINTS);
|
|
|
|
BIND_CONSTANT(PRIMITIVE_LINES);
|
|
|
|
BIND_CONSTANT(PRIMITIVE_LINE_STRIP);
|
|
|
|
BIND_CONSTANT(PRIMITIVE_LINE_LOOP);
|
|
|
|
BIND_CONSTANT(PRIMITIVE_TRIANGLES);
|
|
|
|
BIND_CONSTANT(PRIMITIVE_TRIANGLE_STRIP);
|
|
|
|
BIND_CONSTANT(PRIMITIVE_TRIANGLE_FAN);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Mesh::Mesh() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
mesh = VisualServer::get_singleton()->mesh_create();
|
|
|
|
morph_target_mode = MORPH_MODE_RELATIVE;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Mesh::~Mesh() {
|
|
|
|
|
|
|
|
VisualServer::get_singleton()->free(mesh);
|
|
|
|
}
|