diff options
author | Hans Goudey <h.goudey@me.com> | 2021-03-17 00:35:12 +0300 |
---|---|---|
committer | Hans Goudey <h.goudey@me.com> | 2021-03-17 00:35:36 +0300 |
commit | 9a56a3865c06b472ac54c0351e270dcf738add07 (patch) | |
tree | 375bfcdabd2bb184be581ebb95f249938952ad19 /source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_uv_sphere.cc | |
parent | 3e87d8a4315d794efff659e40f0bb9e34e2aec8a (diff) |
Geometry Nodes: Add initial version of mesh primitives
This commit includes nodes to build the following primitives:
- Cone
- Cylinder
- Circle
- Cube
- UV Sphere
- Ico Sphere
- Line
- Plane/Grid
In general the inputs are the same as the corresponding operators
in the 3D view.
**Line Primitive**
The line primitive has two modes-- adding vertices between two end
points, or adding vertices each at an offset from the start point.
For the former mode, there is a choice between a vertex count
and a distance between each point.
**Plane Primitive**
This commit includes the "Plane" and "Grid" primitives as one node.
Generally primitives are named after the simpler form of the shape they
create (i.e. "Cone" can make some more complex shapes). Also, generally
you want to tweak the number of subdivisions anyway, so defaulting to
plane is not an inconvenience. And generally having fewer redundant
base primitives is better.
**Future Improvements**
A following patch proposes to improve the speed of the cylinder, cone,
and sphere primitives: D10730. Additional possible future improvements
would be adding subdivisions to the cube node and rings to the cone
and cylinder nodes.
Differential Revision: https://developer.blender.org/D10715
Diffstat (limited to 'source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_uv_sphere.cc')
-rw-r--r-- | source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_uv_sphere.cc | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_uv_sphere.cc b/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_uv_sphere.cc new file mode 100644 index 00000000000..e164ad247a3 --- /dev/null +++ b/source/blender/nodes/geometry/nodes/node_geo_mesh_primitive_uv_sphere.cc @@ -0,0 +1,131 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#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_uv_sphere_in[] = { + {SOCK_INT, N_("Segments"), 32, 0.0f, 0.0f, 0.0f, 3, 1024}, + {SOCK_INT, N_("Rings"), 16, 0.0f, 0.0f, 0.0f, 3, 1024}, + {SOCK_FLOAT, N_("Radius"), 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, FLT_MAX, PROP_DISTANCE}, + {SOCK_VECTOR, N_("Location"), 0.0f, 0.0f, 0.0f, 0.0f, -FLT_MAX, FLT_MAX, PROP_TRANSLATION}, + {SOCK_VECTOR, N_("Rotation"), 0.0f, 0.0f, 0.0f, 0.0f, -FLT_MAX, FLT_MAX, PROP_EULER}, + {-1, ""}, +}; + +static bNodeSocketTemplate geo_node_mesh_primitive_uv_sphere_out[] = { + {SOCK_GEOMETRY, N_("Geometry")}, + {-1, ""}, +}; + +namespace blender::nodes { + +static int sphere_vert_total(const int segments, const int rings) +{ + return segments * (rings - 1) + 2; +} + +static int sphere_edge_total(const int segments, const int rings) +{ + return segments * (rings * 2 - 1); +} + +static int sphere_corner_total(const int segments, const int rings) +{ + const int quad_corners = 4 * segments * (rings - 2); + const int tri_corners = 3 * segments * 2; + return quad_corners + tri_corners; +} + +static int sphere_face_total(const int segments, const int rings) +{ + const int quads = segments * (rings - 2); + const int triangles = segments * 2; + return quads + triangles; +} + +static Mesh *create_uv_sphere_mesh_bmesh(const float3 location, + const float3 rotation, + const float radius, + const int segments, + const int rings) +{ + float4x4 transform; + loc_eul_size_to_mat4(transform.values, location, rotation, float3(radius)); + + const BMeshCreateParams bmcp = {true}; + const BMAllocTemplate allocsize = {sphere_vert_total(segments, rings), + sphere_edge_total(segments, rings), + sphere_corner_total(segments, rings), + sphere_face_total(segments, rings)}; + BMesh *bm = BM_mesh_create(&allocsize, &bmcp); + + BMO_op_callf(bm, + BMO_FLAG_DEFAULTS, + "create_uvsphere u_segments=%i v_segments=%i diameter=%f matrix=%m4 calc_uvs=%b", + segments, + rings, + radius, + transform.values, + true); + + Mesh *mesh = (Mesh *)BKE_id_new_nomain(ID_ME, nullptr); + BM_mesh_bm_to_me_for_eval(bm, mesh, nullptr); + BM_mesh_free(bm); + + return mesh; +} + +static void geo_node_mesh_primitive_uv_sphere_exec(GeoNodeExecParams params) +{ + const int segments_num = params.extract_input<int>("Segments"); + const int rings_num = params.extract_input<int>("Rings"); + if (segments_num < 3 || rings_num < 3) { + params.set_output("Geometry", GeometrySet()); + return; + } + + const float radius = params.extract_input<float>("Radius"); + const float3 location = params.extract_input<float3>("Location"); + const float3 rotation = params.extract_input<float3>("Rotation"); + + Mesh *mesh = create_uv_sphere_mesh_bmesh(location, rotation, radius, segments_num, rings_num); + params.set_output("Geometry", GeometrySet::create_with_mesh(mesh)); +} + +} // namespace blender::nodes + +void register_node_type_geo_mesh_primitive_uv_sphere() +{ + static bNodeType ntype; + + geo_node_type_base( + &ntype, GEO_NODE_MESH_PRIMITIVE_UV_SPHERE, "UV Sphere", NODE_CLASS_GEOMETRY, 0); + node_type_socket_templates( + &ntype, geo_node_mesh_primitive_uv_sphere_in, geo_node_mesh_primitive_uv_sphere_out); + ntype.geometry_node_execute = blender::nodes::geo_node_mesh_primitive_uv_sphere_exec; + nodeRegisterType(&ntype); +} |