Commit: 22ba85b510210d623e06174d0b73996585bf36fc Author: Hans Goudey Date: Wed Apr 7 13:15:43 2021 -0500 Branches: master https://developer.blender.org/rB22ba85b510210d623e06174d0b73996585bf36fc
Geometry Nodes: Greatly improve speed of some mesh primitives Some of the BMesh primitive operators have a lot of overhead due to the fact that they use other operators like extrusion, removing doubles, and rotation, to build each shape rather than filling arrays directly. Implementing the primitives directly with the Mesh data structure is much more efficient, since it's simple to fill the result data based on the known inputs. It also allows also skip the conversion from BMesh to Mesh after the calculations. Speed matters more for procedural operations than for the existing operators accessible from the add menu, since they will be executed every evaluation rather than once before a destructive workflow. | Shape | Before (ms) | After (ms) | Speedup (x times faster) | | -------- | -------------| ------------| -------------------------| | Cylinder | 1.1676 | 0.31327 | 3.72 | | Cone | 4.5890 | 0.17762 | 25.8 | | Sphere | 64213.3 | 13.595 | 4720 | The downside of this change is that there will be two implementations for these three primitives, in these nodes and in `bmo_primitive.c`. One option would be re-implementing the BMesh primitives in terms of this code, but that would require `BMesh` to depend on `Mesh`, which is currently avoided. On the other hand, it will be easier to add new features to these nodes like different fill types for the top and the bottom of a cylinder, rings for a cylinder, and tagging the output with boolean attributes. Another factor to consider is that the add mesh object operator could be implemented with these nodes, just providing more benefit for a faster implementation. As a future cleanup, there is room to share code that generates the "rings" topology between different nodes that generate meshes. Differential Revision: https://developer.blender.org/D10730 =================================================================== M source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_cone.cc M source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_uv_sphere.cc =================================================================== diff --git a/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_cone.cc b/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_cone.cc index e9228a2942b..761d5d6c388 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_cone.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_cone.cc @@ -17,14 +17,11 @@ #include "DNA_mesh_types.h" #include "DNA_meshdata_types.h" -#include "BKE_lib_id.h" #include "BKE_mesh.h" #include "UI_interface.h" #include "UI_resources.h" -#include "bmesh.h" - #include "node_geometry_util.hh" static bNodeSocketTemplate geo_node_mesh_primitive_cone_in[] = { @@ -189,45 +186,357 @@ static int face_total(const GeometryNodeMeshCircleFillType fill_type, return face_total; } +static void calculate_uvs(Mesh *mesh, + const bool top_is_point, + const bool bottom_is_point, + const int verts_num, + const GeometryNodeMeshCircleFillType fill_type) +{ + MeshComponent mesh_component; + mesh_component.replace(mesh, GeometryOwnershipType::Editable); + OutputAttributePtr uv_attribute = mesh_component.attribute_try_get_for_output( + "uv_map", ATTR_DOMAIN_CORNER, CD_PROP_FLOAT2, nullptr); + MutableSpan<float2> uvs = uv_attribute->get_span_for_write_only<float2>(); + + Array<float2> circle(verts_num); + float angle = 0.0f; + const float angle_delta = 2.0f * M_PI / static_cast<float>(verts_num); + for (const int i : IndexRange(verts_num)) { + circle[i].x = std::cos(angle) * 0.225f + 0.25f; + circle[i].y = std::sin(angle) * 0.225f + 0.25f; + angle += angle_delta; + } + + int loop_index = 0; + if (!top_is_point) { + if (fill_type == GEO_NODE_MESH_CIRCLE_FILL_NGON) { + for (const int i : IndexRange(verts_num)) { + uvs[loop_index++] = circle[i]; + } + } + else if (fill_type == GEO_NODE_MESH_CIRCLE_FILL_TRIANGLE_FAN) { + for (const int i : IndexRange(verts_num)) { + uvs[loop_index++] = circle[i]; + uvs[loop_index++] = circle[(i + 1) % verts_num]; + uvs[loop_index++] = float2(0.25f, 0.25f); + } + } + } + + /* Create side corners and faces. */ + if (!top_is_point && !bottom_is_point) { + const float bottom = (fill_type == GEO_NODE_MESH_CIRCLE_FILL_NONE) ? 0.0f : 0.5f; + /* Quads connect the top and bottom. */ + for (const int i : IndexRange(verts_num)) { + const float vert = static_cast<float>(i); + uvs[loop_index++] = float2(vert / verts_num, bottom); + uvs[loop_index++] = float2(vert / verts_num, 1.0f); + uvs[loop_index++] = float2((vert + 1.0f) / verts_num, 1.0f); + uvs[loop_index++] = float2((vert + 1.0f) / verts_num, bottom); + } + } + else { + /* Triangles connect the top and bottom section. */ + if (!top_is_point) { + for (const int i : IndexRange(verts_num)) { + uvs[loop_index++] = circle[i] + float2(0.5f, 0.0f); + uvs[loop_index++] = float2(0.75f, 0.25f); + uvs[loop_index++] = circle[(i + 1) % verts_num] + float2(0.5f, 0.0f); + } + } + else { + BLI_assert(!bottom_is_point); + for (const int i : IndexRange(verts_num)) { + uvs[loop_index++] = circle[i]; + uvs[loop_index++] = circle[(i + 1) % verts_num]; + uvs[loop_index++] = float2(0.25f, 0.25f); + } + } + } + + /* Create bottom corners and faces. */ + if (!bottom_is_point) { + if (fill_type == GEO_NODE_MESH_CIRCLE_FILL_NGON) { + for (const int i : IndexRange(verts_num)) { + /* Go backwards because of reversed face normal. */ + uvs[loop_index++] = circle[verts_num - 1 - i] + float2(0.5f, 0.0f); + } + } + else if (fill_type == GEO_NODE_MESH_CIRCLE_FILL_TRIANGLE_FAN) { + for (const int i : IndexRange(verts_num)) { + uvs[loop_index++] = circle[i] + float2(0.5f, 0.0f); + uvs[loop_index++] = float2(0.75f, 0.25f); + uvs[loop_index++] = circle[(i + 1) % verts_num] + float2(0.5f, 0.0f); + } + } + } + + uv_attribute.apply_span_and_save(); +} + Mesh *create_cylinder_or_cone_mesh(const float radius_top, const float radius_bottom, const float depth, const int verts_num, const GeometryNodeMeshCircleFillType fill_type) { - const float4x4 transform = float4x4::identity(); - const bool top_is_point = radius_top == 0.0f; const bool bottom_is_point = radius_bottom == 0.0f; + const float height = depth * 0.5f; + /* Handle the case of a line / single point before everything else to avoid + * the need to check for it later. */ + if (top_is_point && bottom_is_point) { + const bool single_vertex = height == 0.0f; + Mesh *mesh = BKE_mesh_new_nomain(single_vertex ? 1 : 2, single_vertex ? 0 : 1, 0, 0, 0); + copy_v3_v3(mesh->mvert[0].co, float3(0.0f, 0.0f, height)); + if (single_vertex) { + const short up[3] = {0, 0, SHRT_MAX}; + copy_v3_v3_short(mesh->mvert[0].no, up); + return mesh; + } + copy_v3_v3(mesh->mvert[1].co, float3(0.0f, 0.0f, -height)); + mesh->medge[0].v1 = 0; + mesh->medge[0].v2 = 1; + mesh->medge[0].flag |= ME_LOOSEEDGE; + BKE_mesh_calc_normals(mesh); + return mesh; + } - const BMeshCreateParams bmcp = {true}; - const BMAllocTemplate allocsize = { + Mesh *mesh = BKE_mesh_new_nomain( vert_total(fill_type, verts_num, top_is_point, bottom_is_point), edge_total(fill_type, verts_num, top_is_point, bottom_is_point), + 0, corner_total(fill_type, verts_num, top_is_point, bottom_is_point), - face_total(fill_type, verts_num, top_is_point, bottom_is_point)}; - BMesh *bm = BM_mesh_create(&allocsize, &bmcp); - - const bool cap_end = (fill_type != GEO_NODE_MESH_CIRCLE_FILL_NONE); - const bool cap_tri = (fill_type == GEO_NODE_MESH_CIRCLE_FILL_TRIANGLE_FAN); - BMO_op_callf(bm, - BMO_FLAG_DEFAULTS, - "create_cone segments=%i diameter1=%f diameter2=%f cap_ends=%b " - "cap_tris=%b depth=%f matrix=%m4 calc_uvs=%b", - verts_num, - radius_bottom, - radius_top, - cap_end, - cap_tri, - depth, - transform.values, - true); - - BMeshToMeshParams params{}; - params.calc_object_remap = false; - Mesh *mesh = (Mesh *)BKE_id_new_nomain(ID_ME, nullptr); - BM_mesh_bm_to_me(nullptr, bm, mesh, ¶ms); - BM_mesh_free(bm); + face_total(fill_type, verts_num, top_is_point, bottom_is_point)); + MutableSpan<MVert> verts{mesh->mvert, mesh->totvert}; + MutableSpan<MLoop> loops{mesh->mloop, mesh->totloop}; + MutableSpan<MEdge> edges{mesh->medge, mesh->totedge}; + MutableSpan<MPoly> polys{mesh->mpoly, mesh->totpoly}; + + /* Calculate vertex positions. */ + const int top_verts_start = 0; + const int bottom_verts_start = top_verts_start + (!top_is_point ? verts_num : 1); + float angle = 0.0f; + const float angle_delta = 2.0f * M_PI / static_cast<float>(verts_num); + for (const int i : IndexRange(verts_num)) { + const float x = std::cos(angle); + const float y = std::sin(angle); + if (!top_is_point) { + copy_v3_v3(verts[top_verts_start + i].co, float3(x * radius_top, y * radius_top, height)); + } + if (!bottom_is_point) { + copy_v3_v3(verts[bottom_verts_start + i].co, + float3(x * radius_bottom, y * radius_bottom, -height)); + } + angle += angle_delta; + } + if (top_is_point) { + copy_v3_v3(verts[top_verts_start].co, float3(0.0f, 0.0f, height)); + } + if (bottom_is_point) { + copy_v3_v3(verts[bottom_verts_start].co, float3(0.0f, 0.0f, -height)); + } + + /* Add center vertices for the triangle fans at the end. */ + const int top_center_vert_index = bottom_verts_start + (bottom_is_point ? 1 : verts_num); + const int bottom_center_vert_index = top_center_vert_index + (top_is_point ? 0 : 1); + if (fill_type == GEO_NODE_MESH_CIRCLE_FILL_TRIANGLE_FAN) { + if (!top_is_point) { + copy_v3_v3(verts[top_center_vert_index].co, float3(0.0f, 0.0f, height)); + } + if (!bottom_is_point) { + copy_v3_v3(verts[bottom_center_vert_index].co, float3(0.0f, 0.0f, -height)); + } + } + + /* Create top edges. */ + const int top_edges_start = 0; + const int top_fan_edges_start = (!top_is_point && + fill_type == GEO_NODE_MESH_CIRCLE_FILL_TRIANGLE_FAN) ? + top_edges_start + verts_num : + top_edges_start; + if (!top_is_point) { + for (const int i : IndexRange(verts_num)) { + MEdge &edge = edges[top_edges_start + i]; + edge.v1 = top_verts_start + i; + edge.v2 = top_verts_start + (i + 1) % verts_num; + edge.flag = ME_EDGEDRAW | ME_EDGERENDER; + } + if (fill_type == GEO_NODE_MESH_CIRCLE_FILL_TRIANGLE_FAN) { + for (const int i : IndexRange(verts_num)) { + MEdge &edge = edges[top_fan_edges_start + i]; + edge.v1 = top_center_vert_index; + edge.v2 = top_verts_start + i; + edge.flag = ME_EDGEDRAW | ME_EDGERENDER; + } + } + } + + /* Create connecting edges. */ + const int connecting_edges_start = top_fan_edges_start + (!top_is_point ? verts_num : 0); + for (const int i : IndexRange(verts_num)) { + MEdge &edge = edges[connecting_edges_start + i]; + edge.v1 = top_verts_start + (!top_is_point ? i : 0); + edge.v2 = bottom_verts_start + (!bottom_is_point ? i : 0); + edge.flag = ME_EDGEDRAW | ME_EDGERENDER; + } + + /* Create bottom edges. */ + const int bottom_edges_start = connecting_edges_start + verts_num; + const int bottom_fan_edges_start = (!bottom_is_point && + fill_type == GEO_NODE_MESH_CIRCLE_FILL_TRIANGLE_FAN) ? + bottom_edges_start + verts_num : + bottom_edges_start; + if (!bottom_is_point) { + for (const int i : IndexRange(verts_num)) { + MEdge &edge = edges[bottom_edges_start + i]; + edge.v1 = bottom_verts_start + i; @@ Diff output truncated at 10240 characters. @@ _______________________________________________ Bf-blender-cvs mailing list [email protected] https://lists.blender.org/mailman/listinfo/bf-blender-cvs
