summaryrefslogtreecommitdiff
path: root/scene/resources/surface_tool.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'scene/resources/surface_tool.cpp')
-rw-r--r--scene/resources/surface_tool.cpp75
1 files changed, 50 insertions, 25 deletions
diff --git a/scene/resources/surface_tool.cpp b/scene/resources/surface_tool.cpp
index 8478432a04..3932917ebe 100644
--- a/scene/resources/surface_tool.cpp
+++ b/scene/resources/surface_tool.cpp
@@ -224,30 +224,22 @@ void SurfaceTool::add_index(int p_index) {
index_array.push_back(p_index);
}
-Ref<ArrayMesh> SurfaceTool::commit(const Ref<ArrayMesh> &p_existing) {
-
- Ref<ArrayMesh> mesh;
- if (p_existing.is_valid())
- mesh = p_existing;
- else
- mesh.instance();
+Array SurfaceTool::commit_to_arrays() {
int varr_len = vertex_array.size();
- if (varr_len == 0)
- return mesh;
-
- int surface = mesh->get_surface_count();
-
Array a;
a.resize(Mesh::ARRAY_MAX);
for (int i = 0; i < Mesh::ARRAY_MAX; i++) {
- switch (format & (1 << i)) {
+ if (!(format & (1 << i)))
+ continue; //not in format
+
+ switch (i) {
- case Mesh::ARRAY_FORMAT_VERTEX:
- case Mesh::ARRAY_FORMAT_NORMAL: {
+ case Mesh::ARRAY_VERTEX:
+ case Mesh::ARRAY_NORMAL: {
PoolVector<Vector3> array;
array.resize(varr_len);
@@ -273,8 +265,8 @@ Ref<ArrayMesh> SurfaceTool::commit(const Ref<ArrayMesh> &p_existing) {
} break;
- case Mesh::ARRAY_FORMAT_TEX_UV:
- case Mesh::ARRAY_FORMAT_TEX_UV2: {
+ case Mesh::ARRAY_TEX_UV:
+ case Mesh::ARRAY_TEX_UV2: {
PoolVector<Vector2> array;
array.resize(varr_len);
@@ -299,7 +291,7 @@ Ref<ArrayMesh> SurfaceTool::commit(const Ref<ArrayMesh> &p_existing) {
w = PoolVector<Vector2>::Write();
a[i] = array;
} break;
- case Mesh::ARRAY_FORMAT_TANGENT: {
+ case Mesh::ARRAY_TANGENT: {
PoolVector<float> array;
array.resize(varr_len * 4);
@@ -323,7 +315,7 @@ Ref<ArrayMesh> SurfaceTool::commit(const Ref<ArrayMesh> &p_existing) {
a[i] = array;
} break;
- case Mesh::ARRAY_FORMAT_COLOR: {
+ case Mesh::ARRAY_COLOR: {
PoolVector<Color> array;
array.resize(varr_len);
@@ -339,7 +331,7 @@ Ref<ArrayMesh> SurfaceTool::commit(const Ref<ArrayMesh> &p_existing) {
w = PoolVector<Color>::Write();
a[i] = array;
} break;
- case Mesh::ARRAY_FORMAT_BONES: {
+ case Mesh::ARRAY_BONES: {
PoolVector<int> array;
array.resize(varr_len * 4);
@@ -361,7 +353,7 @@ Ref<ArrayMesh> SurfaceTool::commit(const Ref<ArrayMesh> &p_existing) {
a[i] = array;
} break;
- case Mesh::ARRAY_FORMAT_WEIGHTS: {
+ case Mesh::ARRAY_WEIGHTS: {
PoolVector<float> array;
array.resize(varr_len * 4);
@@ -383,7 +375,7 @@ Ref<ArrayMesh> SurfaceTool::commit(const Ref<ArrayMesh> &p_existing) {
a[i] = array;
} break;
- case Mesh::ARRAY_FORMAT_INDEX: {
+ case Mesh::ARRAY_INDEX: {
ERR_CONTINUE(index_array.size() == 0);
@@ -398,6 +390,7 @@ Ref<ArrayMesh> SurfaceTool::commit(const Ref<ArrayMesh> &p_existing) {
}
w = PoolVector<int>::Write();
+
a[i] = array;
} break;
@@ -405,6 +398,26 @@ Ref<ArrayMesh> SurfaceTool::commit(const Ref<ArrayMesh> &p_existing) {
}
}
+ return a;
+}
+
+Ref<ArrayMesh> SurfaceTool::commit(const Ref<ArrayMesh> &p_existing) {
+
+ Ref<ArrayMesh> mesh;
+ if (p_existing.is_valid())
+ mesh = p_existing;
+ else
+ mesh.instance();
+
+ int varr_len = vertex_array.size();
+
+ if (varr_len == 0)
+ return mesh;
+
+ int surface = mesh->get_surface_count();
+
+ Array a = commit_to_arrays();
+
mesh->add_surface_from_arrays(primitive, a);
if (material.is_valid())
mesh->surface_set_material(surface, material);
@@ -459,12 +472,17 @@ void SurfaceTool::deindex() {
vertex_array.push_back(varr[E->get()]);
}
format &= ~Mesh::ARRAY_FORMAT_INDEX;
+ index_array.clear();
}
void SurfaceTool::_create_list(const Ref<Mesh> &p_existing, int p_surface, List<Vertex> *r_vertex, List<int> *r_index, int &lformat) {
Array arr = p_existing->surface_get_arrays(p_surface);
ERR_FAIL_COND(arr.size() != VS::ARRAY_MAX);
+ _create_list_from_arrays(arr, r_vertex, r_index, lformat);
+}
+
+void SurfaceTool::_create_list_from_arrays(Array arr, List<Vertex> *r_vertex, List<int> *r_index, int &lformat) {
PoolVector<Vector3> varr = arr[VS::ARRAY_VERTEX];
PoolVector<Vector3> narr = arr[VS::ARRAY_NORMAL];
@@ -536,7 +554,7 @@ void SurfaceTool::_create_list(const Ref<Mesh> &p_existing, int p_surface, List<
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;
+ v.binormal = p.normal.cross(v.tangent).normalized() * p.d;
}
if (lformat & VS::ARRAY_FORMAT_COLOR)
v.color = carr[i];
@@ -580,6 +598,13 @@ void SurfaceTool::_create_list(const Ref<Mesh> &p_existing, int p_surface, List<
}
}
+void SurfaceTool::create_from_triangle_arrays(const Array &p_arrays) {
+
+ clear();
+ primitive = Mesh::PRIMITIVE_TRIANGLES;
+ _create_list_from_arrays(p_arrays, &vertex_array, &index_array, format);
+}
+
void SurfaceTool::create_from(const Ref<Mesh> &p_existing, int p_surface) {
clear();
@@ -711,8 +736,9 @@ void SurfaceTool::generate_tangents() {
ERR_FAIL_COND(!res);
format |= Mesh::ARRAY_FORMAT_TANGENT;
- if (indexed)
+ if (indexed) {
index();
+ }
}
void SurfaceTool::generate_normals() {
@@ -784,7 +810,6 @@ void SurfaceTool::generate_normals() {
vertex_hash.clear();
if (E) {
smooth = smooth_groups[count];
- print_line("SMOOTH AT " + itos(count) + ": " + itos(smooth));
}
}
}