diff options
author | reduz <reduzio@gmail.com> | 2021-05-10 13:12:44 -0300 |
---|---|---|
committer | reduz <reduzio@gmail.com> | 2021-05-11 11:21:36 -0300 |
commit | 479391ef5405226fa110ac3f5823ecbc7d987b0c (patch) | |
tree | 427b8179758b35cfc859d1e6fb853a4d60e086d5 /scene/2d/polygon_2d.cpp | |
parent | a1cc6b45dca33982003e5646903df1453e2ab3ae (diff) |
Fixes missng 2D engine bits
-Mesh2D now works
-MultiMesh2D now works
-Polygon2D now works
-Added hooks for processing 2D particles
-Skeleton2D now works
2D particles still not working, but stuff needed for it is now implemented.
Diffstat (limited to 'scene/2d/polygon_2d.cpp')
-rw-r--r-- | scene/2d/polygon_2d.cpp | 48 |
1 files changed, 37 insertions, 11 deletions
diff --git a/scene/2d/polygon_2d.cpp b/scene/2d/polygon_2d.cpp index 1a7038bb80..21083e6a4b 100644 --- a/scene/2d/polygon_2d.cpp +++ b/scene/2d/polygon_2d.cpp @@ -302,17 +302,18 @@ void Polygon2D::_notification(int p_what) { colors.write[i] = color_r[i]; } } else { - colors.push_back(color); + colors.resize(len); + for (int i = 0; i < len; i++) { + colors.write[i] = color; + } } + Vector<int> index_array; + if (invert || polygons.size() == 0) { - Vector<int> indices = Geometry2D::triangulate_polygon(points); - if (indices.size()) { - RS::get_singleton()->canvas_item_add_triangle_array(get_canvas_item(), indices, points, colors, uvs, bones, weights, texture.is_valid() ? texture->get_rid() : RID(), -1); - } + index_array = Geometry2D::triangulate_polygon(points); } else { //draw individual polygons - Vector<int> total_indices; for (int i = 0; i < polygons.size(); i++) { Vector<int> src_indices = polygons[i]; int ic = src_indices.size(); @@ -333,18 +334,38 @@ void Polygon2D::_notification(int p_what) { int ic2 = indices.size(); const int *r2 = indices.ptr(); - int bic = total_indices.size(); - total_indices.resize(bic + ic2); - int *w2 = total_indices.ptrw(); + int bic = index_array.size(); + index_array.resize(bic + ic2); + int *w2 = index_array.ptrw(); for (int j = 0; j < ic2; j++) { w2[j + bic] = r[r2[j]]; } } + } + + RS::get_singleton()->mesh_clear(mesh); + + if (index_array.size()) { + Array arr; + arr.resize(RS::ARRAY_MAX); + arr[RS::ARRAY_VERTEX] = points; + if (uvs.size() == points.size()) { + arr[RS::ARRAY_TEX_UV] = uvs; + } + if (colors.size() == points.size()) { + arr[RS::ARRAY_COLOR] = colors; + } - if (total_indices.size()) { - RS::get_singleton()->canvas_item_add_triangle_array(get_canvas_item(), total_indices, points, colors, uvs, bones, weights, texture.is_valid() ? texture->get_rid() : RID()); + if (bones.size() == points.size() * 4) { + arr[RS::ARRAY_BONES] = bones; + arr[RS::ARRAY_WEIGHTS] = weights; } + + arr[RS::ARRAY_INDEX] = index_array; + + RS::get_singleton()->mesh_add_surface_from_arrays(mesh, RS::PRIMITIVE_TRIANGLES, arr, Array(), Dictionary(), RS::ARRAY_FLAG_USE_2D_VERTICES); + RS::get_singleton()->canvas_item_add_mesh(get_canvas_item(), mesh, Transform2D(), Color(), texture.is_valid() ? texture->get_rid() : RID()); } } break; @@ -655,4 +676,9 @@ void Polygon2D::_bind_methods() { } Polygon2D::Polygon2D() { + mesh = RS::get_singleton()->mesh_create(); +} + +Polygon2D::~Polygon2D() { + RS::get_singleton()->free(mesh); } |