Blender V4.5
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
5#include "BLI_math_euler.hh"
6
7#include "DNA_mesh_types.h"
8
9#include "BKE_material.hh"
10
14#include "GEO_transform.hh"
15
16#include "node_geometry_util.hh"
17
19
21{
22 b.add_input<decl::Vector>("Size")
23 .default_value(float3(1))
24 .min(0.0f)
26 .description("Side length along each axis");
27 b.add_input<decl::Int>("Vertices X")
28 .default_value(2)
29 .min(2)
30 .max(1000)
31 .description("Number of vertices for the X side of the shape");
32 b.add_input<decl::Int>("Vertices Y")
33 .default_value(2)
34 .min(2)
35 .max(1000)
36 .description("Number of vertices for the Y side of the shape");
37 b.add_input<decl::Int>("Vertices Z")
38 .default_value(2)
39 .min(2)
40 .max(1000)
41 .description("Number of vertices for the Z side of the shape");
42 b.add_output<decl::Geometry>("Mesh");
43 b.add_output<decl::Vector>("UV Map").field_on_all();
44}
45
47 const int verts_x,
48 const int verts_y,
49 const int verts_z,
50 const std::optional<StringRef> &uv_map_id)
51{
52 const int dimensions = (verts_x - 1 > 0) + (verts_y - 1 > 0) + (verts_z - 1 > 0);
53 if (dimensions == 0) {
55 }
56 if (dimensions == 1) {
57 float3 start;
58 float3 delta;
59 if (verts_x > 1) {
60 start = {-size.x / 2.0f, 0, 0};
61 delta = {size.x / (verts_x - 1), 0, 0};
62 }
63 else if (verts_y > 1) {
64 start = {0, -size.y / 2.0f, 0};
65 delta = {0, size.y / (verts_y - 1), 0};
66 }
67 else {
68 start = {0, 0, -size.z / 2.0f};
69 delta = {0, 0, size.z / (verts_z - 1)};
70 }
71
72 return geometry::create_line_mesh(start, delta, verts_x * verts_y * verts_z);
73 }
74 if (dimensions == 2) {
75 if (verts_z == 1) { /* XY plane. */
76 return geometry::create_grid_mesh(verts_x, verts_y, size.x, size.y, uv_map_id);
77 }
78 if (verts_y == 1) { /* XZ plane. */
79 Mesh *mesh = geometry::create_grid_mesh(verts_x, verts_z, size.x, size.z, uv_map_id);
81 *mesh, float3(0), math::to_quaternion(math::EulerXYZ(M_PI_2, 0.0f, 0.0f)), float3(1));
82 return mesh;
83 }
84 /* YZ plane. */
85 Mesh *mesh = geometry::create_grid_mesh(verts_z, verts_y, size.z, size.y, uv_map_id);
87 *mesh, float3(0), math::to_quaternion(math::EulerXYZ(0.0f, M_PI_2, 0.0f)), float3(1));
88 return mesh;
89 }
90
91 return geometry::create_cuboid_mesh(size, verts_x, verts_y, verts_z, uv_map_id);
92}
93
95{
96 const float3 size = params.extract_input<float3>("Size");
97 const int verts_x = params.extract_input<int>("Vertices X");
98 const int verts_y = params.extract_input<int>("Vertices Y");
99 const int verts_z = params.extract_input<int>("Vertices Z");
100 if (verts_x < 1 || verts_y < 1 || verts_z < 1) {
101 params.error_message_add(NodeWarningType::Info, TIP_("Vertices must be at least 1"));
102 params.set_default_remaining_outputs();
103 return;
104 }
105
106 std::optional<std::string> uv_map_id = params.get_output_anonymous_attribute_id_if_needed(
107 "UV Map");
108
109 Mesh *mesh = create_cube_mesh(size, verts_x, verts_y, verts_z, uv_map_id);
111
112 params.set_output("Mesh", GeometrySet::from_mesh(mesh));
113}
114
115static void node_register()
116{
117 static blender::bke::bNodeType ntype;
118
119 geo_node_type_base(&ntype, "GeometryNodeMeshCube", GEO_NODE_MESH_PRIMITIVE_CUBE);
120 ntype.ui_name = "Cube";
121 ntype.ui_description = "Generate a cuboid mesh with variable side lengths and subdivisions";
122 ntype.enum_name_legacy = "MESH_PRIMITIVE_CUBE";
124 ntype.declare = node_declare;
127}
129
130} // 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:447
#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:249
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:2748
Mesh * create_grid_mesh(int verts_x, int verts_y, float size_x, float size_y, const std::optional< StringRef > &uv_map_id)
void transform_mesh(Mesh &mesh, float3 translation, math::Quaternion rotation, float3 scale)
Mesh * create_cuboid_mesh(const float3 &size, int verts_x, int verts_y, int verts_z, const std::optional< StringRef > &uv_id)
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:226
std::string ui_description
Definition BKE_node.hh:232
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:347
const char * enum_name_legacy
Definition BKE_node.hh:235
NodeDeclareFunction declare
Definition BKE_node.hh:355
static GeometrySet from_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)