Blender V4.3
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 "BKE_material.h"
8#include "BKE_mesh.hh"
9
13#include "GEO_transform.hh"
14
15#include "node_geometry_util.hh"
16
18
20{
21 b.add_input<decl::Vector>("Size")
22 .default_value(float3(1))
23 .min(0.0f)
25 .description("Side length along each axis");
26 b.add_input<decl::Int>("Vertices X")
27 .default_value(2)
28 .min(2)
29 .max(1000)
30 .description("Number of vertices for the X side of the shape");
31 b.add_input<decl::Int>("Vertices Y")
32 .default_value(2)
33 .min(2)
34 .max(1000)
35 .description("Number of vertices for the Y side of the shape");
36 b.add_input<decl::Int>("Vertices Z")
37 .default_value(2)
38 .min(2)
39 .max(1000)
40 .description("Number of vertices for the Z side of the shape");
41 b.add_output<decl::Geometry>("Mesh");
42 b.add_output<decl::Vector>("UV Map").field_on_all();
43}
44
45static Mesh *create_cube_mesh(const float3 size,
46 const int verts_x,
47 const int verts_y,
48 const int verts_z,
49 const std::optional<StringRef> &uv_map_id)
50{
51 const int dimensions = (verts_x - 1 > 0) + (verts_y - 1 > 0) + (verts_z - 1 > 0);
52 if (dimensions == 0) {
54 }
55 if (dimensions == 1) {
56 float3 start;
57 float3 delta;
58 if (verts_x > 1) {
59 start = {-size.x / 2.0f, 0, 0};
60 delta = {size.x / (verts_x - 1), 0, 0};
61 }
62 else if (verts_y > 1) {
63 start = {0, -size.y / 2.0f, 0};
64 delta = {0, size.y / (verts_y - 1), 0};
65 }
66 else {
67 start = {0, 0, -size.z / 2.0f};
68 delta = {0, 0, size.z / (verts_z - 1)};
69 }
70
71 return geometry::create_line_mesh(start, delta, verts_x * verts_y * verts_z);
72 }
73 if (dimensions == 2) {
74 if (verts_z == 1) { /* XY plane. */
75 return geometry::create_grid_mesh(verts_x, verts_y, size.x, size.y, uv_map_id);
76 }
77 if (verts_y == 1) { /* XZ plane. */
78 Mesh *mesh = geometry::create_grid_mesh(verts_x, verts_z, size.x, size.z, uv_map_id);
80 *mesh, float3(0), math::to_quaternion(math::EulerXYZ(M_PI_2, 0.0f, 0.0f)), float3(1));
81 return mesh;
82 }
83 /* YZ plane. */
84 Mesh *mesh = geometry::create_grid_mesh(verts_z, verts_y, size.z, size.y, uv_map_id);
86 *mesh, float3(0), math::to_quaternion(math::EulerXYZ(0.0f, M_PI_2, 0.0f)), float3(1));
87 return mesh;
88 }
89
90 return geometry::create_cuboid_mesh(size, verts_x, verts_y, verts_z, uv_map_id);
91}
92
94{
95 const float3 size = params.extract_input<float3>("Size");
96 const int verts_x = params.extract_input<int>("Vertices X");
97 const int verts_y = params.extract_input<int>("Vertices Y");
98 const int verts_z = params.extract_input<int>("Vertices Z");
99 if (verts_x < 1 || verts_y < 1 || verts_z < 1) {
100 params.error_message_add(NodeWarningType::Info, TIP_("Vertices must be at least 1"));
101 params.set_default_remaining_outputs();
102 return;
103 }
104
105 std::optional<std::string> uv_map_id = params.get_output_anonymous_attribute_id_if_needed(
106 "UV Map");
107
108 Mesh *mesh = create_cube_mesh(size, verts_x, verts_y, verts_z, uv_map_id);
110
111 params.set_output("Mesh", GeometrySet::from_mesh(mesh));
112}
113
114static void node_register()
115{
116 static blender::bke::bNodeType ntype;
117
118 geo_node_type_base(&ntype, GEO_NODE_MESH_PRIMITIVE_CUBE, "Cube", NODE_CLASS_GEOMETRY);
119 ntype.declare = node_declare;
122}
124
125} // namespace blender::nodes::node_geo_mesh_primitive_cube_cc
General operations, lookup, etc. for materials.
void BKE_id_material_eval_ensure_default_slot(struct ID *id)
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:418
#define M_PI_2
#define TIP_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
@ PROP_TRANSLATION
Definition RNA_types.hh:164
local_group_size(16, 16) .push_constant(Type b
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_register_type(bNodeType *ntype)
Definition node.cc:1708
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)
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, int type, const char *name, short nclass)
#define min(a, b)
Definition sort.c:32
static GeometrySet from_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
Defines a node type.
Definition BKE_node.hh:218
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:339
NodeDeclareFunction declare
Definition BKE_node.hh:347