Blender V5.0
node_geo_mesh_primitive_cube.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6#include "BLI_math_euler.hh"
7
8#include "DNA_mesh_types.h"
9
10#include "BKE_material.hh"
11
15#include "GEO_transform.hh"
16
17#include "node_geometry_util.hh"
18
20
22{
23 b.add_input<decl::Vector>("Size")
24 .default_value(float3(1))
25 .min(0.0f)
27 .description("Side length along each axis");
28 b.add_input<decl::Int>("Vertices X")
29 .default_value(2)
30 .min(2)
31 .max(1000)
32 .description("Number of vertices for the X side of the shape");
33 b.add_input<decl::Int>("Vertices Y")
34 .default_value(2)
35 .min(2)
36 .max(1000)
37 .description("Number of vertices for the Y side of the shape");
38 b.add_input<decl::Int>("Vertices Z")
39 .default_value(2)
40 .min(2)
41 .max(1000)
42 .description("Number of vertices for the Z side of the shape");
43 b.add_output<decl::Geometry>("Mesh");
44 b.add_output<decl::Vector>("UV Map").field_on_all();
45}
46
48 const int verts_x,
49 const int verts_y,
50 const int verts_z,
51 const std::optional<StringRef> &uv_map_id)
52{
53 const int dimensions = (verts_x - 1 > 0) + (verts_y - 1 > 0) + (verts_z - 1 > 0);
54 if (dimensions == 0) {
56 }
57 if (dimensions == 1) {
58 float3 start;
59 float3 delta;
60 if (verts_x > 1) {
61 start = {-size.x / 2.0f, 0, 0};
62 delta = {size.x / (verts_x - 1), 0, 0};
63 }
64 else if (verts_y > 1) {
65 start = {0, -size.y / 2.0f, 0};
66 delta = {0, size.y / (verts_y - 1), 0};
67 }
68 else {
69 start = {0, 0, -size.z / 2.0f};
70 delta = {0, 0, size.z / (verts_z - 1)};
71 }
72
73 return geometry::create_line_mesh(start, delta, verts_x * verts_y * verts_z);
74 }
75 if (dimensions == 2) {
76 if (verts_z == 1) { /* XY plane. */
77 return geometry::create_grid_mesh(verts_x, verts_y, size.x, size.y, uv_map_id);
78 }
79 if (verts_y == 1) { /* XZ plane. */
80 Mesh *mesh = geometry::create_grid_mesh(verts_x, verts_z, size.x, size.z, uv_map_id);
82 *mesh, float3(0), math::to_quaternion(math::EulerXYZ(M_PI_2, 0.0f, 0.0f)), float3(1));
83 return mesh;
84 }
85 /* YZ plane. */
86 Mesh *mesh = geometry::create_grid_mesh(verts_z, verts_y, size.z, size.y, uv_map_id);
88 *mesh, float3(0), math::to_quaternion(math::EulerXYZ(0.0f, M_PI_2, 0.0f)), float3(1));
89 return mesh;
90 }
91
92 return geometry::create_cuboid_mesh(size, verts_x, verts_y, verts_z, uv_map_id);
93}
94
96{
97 const float3 size = params.extract_input<float3>("Size");
98 const int verts_x = params.extract_input<int>("Vertices X");
99 const int verts_y = params.extract_input<int>("Vertices Y");
100 const int verts_z = params.extract_input<int>("Vertices Z");
101 if (verts_x < 1 || verts_y < 1 || verts_z < 1) {
102 params.error_message_add(NodeWarningType::Info, TIP_("Vertices must be at least 1"));
103 params.set_default_remaining_outputs();
104 return;
105 }
106
107 std::optional<std::string> uv_map_id = params.get_output_anonymous_attribute_id_if_needed(
108 "UV Map");
109
110 Mesh *mesh = create_cube_mesh(size, verts_x, verts_y, verts_z, uv_map_id);
112
113 params.set_output("Mesh", GeometrySet::from_mesh(mesh));
114}
115
116static void node_register()
117{
118 static blender::bke::bNodeType ntype;
119
120 geo_node_type_base(&ntype, "GeometryNodeMeshCube", GEO_NODE_MESH_PRIMITIVE_CUBE);
121 ntype.ui_name = "Cube";
122 ntype.ui_description = "Generate a cuboid mesh with variable side lengths and subdivisions";
123 ntype.enum_name_legacy = "MESH_PRIMITIVE_CUBE";
125 ntype.declare = node_declare;
128}
130
131} // namespace blender::nodes::node_geo_mesh_primitive_cube_cc
General operations, lookup, etc. for materials.
void BKE_id_material_eval_ensure_default_slot(ID *id)
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:461
#define GEO_NODE_MESH_PRIMITIVE_CUBE
#define M_PI_2
#define TIP_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
@ PROP_TRANSLATION
Definition RNA_types.hh:261
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
Mesh * create_cuboid_mesh(const float3 &size, int verts_x, int verts_y, int verts_z, std::optional< StringRef > uv_id)
Mesh * create_grid_mesh(int verts_x, int verts_y, float size_x, float size_y, std::optional< StringRef > uv_map_id)
void transform_mesh(Mesh &mesh, float3 translation, math::Quaternion rotation, float3 scale)
Mesh * create_line_mesh(float3 start, float3 delta, int count)
QuaternionBase< T > to_quaternion(const AxisAngleBase< T, AngleT > &axis_angle)
EulerXYZBase< float > EulerXYZ
static Mesh * create_cube_mesh(const float3 size, const int verts_x, const int verts_y, const int verts_z, const std::optional< StringRef > &uv_map_id)
VecBase< float, 3 > float3
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
Defines a node type.
Definition BKE_node.hh:238
std::string ui_description
Definition BKE_node.hh:244
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:354
const char * enum_name_legacy
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:362
static GeometrySet from_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)