Geometry Nodes: new Align Rotation to Vector node

This adds a new Align Rotation to Vector node based on the mockup
in T83669.

Reviewers: HooglyBoogly, simonthommes

Differential Revision: https://developer.blender.org/D10081
This commit is contained in:
Jacques Lucke 2021-01-12 12:55:14 +01:00
parent 3a254b93fd
commit 3b77bd48f9
Notes: blender-bot 2023-02-14 06:32:27 +01:00
Referenced by issue #83669, Align Rotation to Vector for tree leaves
10 changed files with 226 additions and 0 deletions

View File

@ -516,6 +516,7 @@ geometry_node_categories = [
NodeItem("GeometryNodePointInstance"),
NodeItem("GeometryNodePointSeparate"),
NodeItem("GeometryNodeRotatePoints"),
NodeItem("GeometryNodeAlignRotationToVector"),
]),
GeometryNodeCategory("GEO_UTILITIES", "Utilities", items=[
NodeItem("ShaderNodeMapRange"),

View File

@ -1358,6 +1358,7 @@ int ntreeTexExecTree(struct bNodeTree *ntree,
#define GEO_NODE_ATTRIBUTE_COMPARE 1015
#define GEO_NODE_ROTATE_POINTS 1016
#define GEO_NODE_ATTRIBUTE_VECTOR_MATH 1017
#define GEO_NODE_ALIGN_ROTATION_TO_VECTOR 1018
/** \} */

View File

@ -4746,6 +4746,7 @@ static void registerGeometryNodes(void)
register_node_type_geo_attribute_mix();
register_node_type_geo_attribute_color_ramp();
register_node_type_geo_rotate_points();
register_node_type_geo_align_rotation_to_vector();
}
static void registerFunctionNodes(void)

View File

@ -3319,6 +3319,16 @@ static void node_geometry_buts_rotate_points(uiLayout *layout,
}
}
static void node_geometry_buts_align_rotation_to_vector(uiLayout *layout,
bContext *UNUSED(C),
PointerRNA *ptr)
{
uiItemR(layout, ptr, "axis", DEFAULT_FLAGS | UI_ITEM_R_EXPAND, NULL, ICON_NONE);
uiLayout *col = uiLayoutColumn(layout, false);
uiItemR(col, ptr, "input_type_factor", DEFAULT_FLAGS, IFACE_("Factor"), ICON_NONE);
uiItemR(col, ptr, "input_type_vector", DEFAULT_FLAGS, IFACE_("Vector"), ICON_NONE);
}
static void node_geometry_set_butfunc(bNodeType *ntype)
{
switch (ntype->type) {
@ -3361,6 +3371,9 @@ static void node_geometry_set_butfunc(bNodeType *ntype)
case GEO_NODE_ROTATE_POINTS:
ntype->draw_buttons = node_geometry_buts_rotate_points;
break;
case GEO_NODE_ALIGN_ROTATION_TO_VECTOR:
ntype->draw_buttons = node_geometry_buts_align_rotation_to_vector;
break;
}
}

View File

@ -1141,6 +1141,17 @@ typedef struct NodeGeometryRotatePoints {
char _pad[3];
} NodeGeometryRotatePoints;
typedef struct NodeGeometryAlignRotationToVector {
/* GeometryNodeAlignRotationToVectorAxis */
uint8_t axis;
/* GeometryNodeAttributeInputMode */
uint8_t input_type_factor;
uint8_t input_type_vector;
char _pad[5];
} NodeGeometryAlignRotationToVector;
/* script node mode */
#define NODE_SCRIPT_INTERNAL 0
#define NODE_SCRIPT_EXTERNAL 1
@ -1576,6 +1587,12 @@ typedef enum GeometryNodeRotatePointsSpace {
GEO_NODE_ROTATE_POINTS_SPACE_POINT = 1,
} GeometryNodeRotatePointsSpace;
typedef enum GeometryNodeAlignRotationToVectorAxis {
GEO_NODE_ALIGN_ROTATION_TO_VECTOR_AXIS_X = 0,
GEO_NODE_ALIGN_ROTATION_TO_VECTOR_AXIS_Y = 1,
GEO_NODE_ALIGN_ROTATION_TO_VECTOR_AXIS_Z = 2,
} GeometryNodeAlignRotationToVectorAxis;
#ifdef __cplusplus
}
#endif

View File

@ -8774,6 +8774,47 @@ static void def_geo_rotate_points(StructRNA *srna)
RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_socket_update");
}
static void def_geo_align_rotation_to_vector(StructRNA *srna)
{
static const EnumPropertyItem axis_items[] = {
{GEO_NODE_ALIGN_ROTATION_TO_VECTOR_AXIS_X,
"X",
ICON_NONE,
"X",
"Align the X axis with the vector"},
{GEO_NODE_ALIGN_ROTATION_TO_VECTOR_AXIS_Y,
"Y",
ICON_NONE,
"Y",
"Align the Y axis with the vector"},
{GEO_NODE_ALIGN_ROTATION_TO_VECTOR_AXIS_Z,
"Z",
ICON_NONE,
"Z",
"Align the Z axis with the vector"},
{0, NULL, 0, NULL, NULL},
};
PropertyRNA *prop;
RNA_def_struct_sdna_from(srna, "NodeGeometryAlignRotationToVector", "storage");
prop = RNA_def_property(srna, "axis", PROP_ENUM, PROP_NONE);
RNA_def_property_enum_items(prop, axis_items);
RNA_def_property_ui_text(prop, "Axis", "Axis to align to the vector");
RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_update");
prop = RNA_def_property(srna, "input_type_factor", PROP_ENUM, PROP_NONE);
RNA_def_property_enum_items(prop, rna_node_geometry_attribute_input_type_items_float);
RNA_def_property_ui_text(prop, "Input Type Factor", "");
RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_socket_update");
prop = RNA_def_property(srna, "input_type_vector", PROP_ENUM, PROP_NONE);
RNA_def_property_enum_items(prop, rna_node_geometry_attribute_input_type_items_vector);
RNA_def_property_ui_text(prop, "Input Type Vector", "");
RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_socket_update");
}
/* -------------------------------------------------------------------------- */
static void rna_def_shader_node(BlenderRNA *brna)

View File

@ -140,6 +140,7 @@ set(SRC
function/nodes/node_fn_switch.cc
function/node_function_util.cc
geometry/nodes/node_geo_align_rotation_to_vector.cc
geometry/nodes/node_geo_attribute_color_ramp.cc
geometry/nodes/node_geo_attribute_compare.cc
geometry/nodes/node_geo_attribute_fill.cc

View File

@ -44,6 +44,7 @@ void register_node_type_geo_attribute_compare(void);
void register_node_type_geo_attribute_mix(void);
void register_node_type_geo_attribute_color_ramp(void);
void register_node_type_geo_rotate_points(void);
void register_node_type_geo_align_rotation_to_vector(void);
#ifdef __cplusplus
}

View File

@ -286,6 +286,7 @@ DefNode(GeometryNode, GEO_NODE_POINT_SEPARATE, 0, "POINT_SEPARATE", PointSeparat
DefNode(GeometryNode, GEO_NODE_ATTRIBUTE_COMPARE, def_geo_attribute_attribute_compare, "ATTRIBUTE_COMPARE", AttributeCompare, "Attribute Compare", "")
DefNode(GeometryNode, GEO_NODE_ROTATE_POINTS, def_geo_rotate_points, "ROTATE_POINTS", RotatePoints, "Rotate Points", "")
DefNode(GeometryNode, GEO_NODE_ATTRIBUTE_VECTOR_MATH, def_geo_attribute_vector_math, "ATTRIBUTE_VECTOR_MATH", AttributeVectorMath, "Attribute Vector Math", "")
DefNode(GeometryNode, GEO_NODE_ALIGN_ROTATION_TO_VECTOR, def_geo_align_rotation_to_vector, "ALIGN_ROTATION_TO_VECTOR", AlignRotationToVector, "Align Rotation to Vector", "")
/* undefine macros */
#undef DefNode

View File

@ -0,0 +1,149 @@
/*
* 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 "node_geometry_util.hh"
#include "BLI_math_rotation.h"
static bNodeSocketTemplate geo_node_align_rotation_to_vector_in[] = {
{SOCK_GEOMETRY, N_("Geometry")},
{SOCK_STRING, N_("Factor")},
{SOCK_FLOAT, N_("Factor"), 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, PROP_FACTOR},
{SOCK_STRING, N_("Vector")},
{SOCK_VECTOR, N_("Vector"), 0.0, 0.0, 1.0, 0.0, -FLT_MAX, FLT_MAX, PROP_ANGLE},
{-1, ""},
};
static bNodeSocketTemplate geo_node_align_rotation_to_vector_out[] = {
{SOCK_GEOMETRY, N_("Geometry")},
{-1, ""},
};
namespace blender::nodes {
static void align_rotations_on_component(GeometryComponent &component,
const GeoNodeExecParams &params)
{
const bNode &node = params.node();
const NodeGeometryAlignRotationToVector &storage = *(const NodeGeometryAlignRotationToVector *)
node.storage;
WriteAttributePtr rotation_attribute = component.attribute_try_ensure_for_write(
"rotation", ATTR_DOMAIN_POINT, CD_PROP_FLOAT3);
if (!rotation_attribute) {
return;
}
MutableSpan<float3> rotations = rotation_attribute->get_span().typed<float3>();
FloatReadAttribute factors = params.get_input_attribute<float>(
"Factor", component, ATTR_DOMAIN_POINT, 1.0f);
Float3ReadAttribute vectors = params.get_input_attribute<float3>(
"Vector", component, ATTR_DOMAIN_POINT, {0, 0, 1});
float3 main_axis{0, 0, 0};
main_axis[storage.axis] = 1;
const int domain_size = component.attribute_domain_size(ATTR_DOMAIN_POINT);
for (const int i : IndexRange(domain_size)) {
const float3 vector = vectors[i];
if (is_zero_v3(vector)) {
continue;
}
float old_rotation[3][3];
eul_to_mat3(old_rotation, rotations[i]);
float3 old_axis;
mul_v3_m3v3(old_axis, old_rotation, main_axis);
const float3 new_axis = vector.normalized();
const float3 rotation_axis = float3::cross_high_precision(old_axis, new_axis);
const float full_angle = angle_normalized_v3v3(old_axis, new_axis);
const float angle = factors[i] * full_angle;
float rotation[3][3];
axis_angle_to_mat3(rotation, rotation_axis, angle);
float new_rotation_matrix[3][3];
mul_m3_m3m3(new_rotation_matrix, rotation, old_rotation);
float3 new_rotation;
mat3_to_eul(new_rotation, new_rotation_matrix);
rotations[i] = new_rotation;
}
rotation_attribute->apply_span();
}
static void geo_node_align_rotation_to_vector_exec(GeoNodeExecParams params)
{
GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");
if (geometry_set.has<MeshComponent>()) {
align_rotations_on_component(geometry_set.get_component_for_write<MeshComponent>(), params);
}
if (geometry_set.has<PointCloudComponent>()) {
align_rotations_on_component(geometry_set.get_component_for_write<PointCloudComponent>(),
params);
}
params.set_output("Geometry", geometry_set);
}
static void geo_node_align_rotation_to_vector_init(bNodeTree *UNUSED(ntree), bNode *node)
{
NodeGeometryAlignRotationToVector *node_storage = (NodeGeometryAlignRotationToVector *)
MEM_callocN(sizeof(NodeGeometryAlignRotationToVector), __func__);
node_storage->axis = GEO_NODE_ALIGN_ROTATION_TO_VECTOR_AXIS_X;
node_storage->input_type_factor = GEO_NODE_ATTRIBUTE_INPUT_FLOAT;
node_storage->input_type_vector = GEO_NODE_ATTRIBUTE_INPUT_VECTOR;
node->storage = node_storage;
}
static void geo_node_align_rotation_to_vector_update(bNodeTree *UNUSED(ntree), bNode *node)
{
NodeGeometryAlignRotationToVector *node_storage = (NodeGeometryAlignRotationToVector *)
node->storage;
update_attribute_input_socket_availabilities(
*node, "Factor", (GeometryNodeAttributeInputMode)node_storage->input_type_factor);
update_attribute_input_socket_availabilities(
*node, "Vector", (GeometryNodeAttributeInputMode)node_storage->input_type_vector);
}
} // namespace blender::nodes
void register_node_type_geo_align_rotation_to_vector()
{
static bNodeType ntype;
geo_node_type_base(&ntype,
GEO_NODE_ALIGN_ROTATION_TO_VECTOR,
"Align Rotation to Vector",
NODE_CLASS_GEOMETRY,
0);
node_type_socket_templates(
&ntype, geo_node_align_rotation_to_vector_in, geo_node_align_rotation_to_vector_out);
node_type_init(&ntype, blender::nodes::geo_node_align_rotation_to_vector_init);
node_type_update(&ntype, blender::nodes::geo_node_align_rotation_to_vector_update);
node_type_storage(&ntype,
"NodeGeometryAlignRotationToVector",
node_free_standard_storage,
node_copy_standard_storage);
ntype.geometry_node_execute = blender::nodes::geo_node_align_rotation_to_vector_exec;
nodeRegisterType(&ntype);
}