From fd0370acc2129fbc3a07a776bca024ba4512b654 Mon Sep 17 00:00:00 2001 From: Nikhil Shringarpurey Date: Mon, 5 Jul 2021 11:52:10 -0500 Subject: Geometry Nodes: Add explicit Float to Int conversion node This patch adds a very simple node that explicitly converts a float to an int. While this may seem redundant, it would offer 2 benefits to the current requirement to use implicit float conversions: 1. It makes the node tree's intent more clear and self-documenting (especially if changes in the future require integer inputs). 2. It eliminates undefined behavior in current/future nodes from float inputs by guaranteeing that the input is an integer. The node offers a variety of rounding techniques to make it more flexible. Differential Revision: https://developer.blender.org/D11700 --- source/blender/makesrna/RNA_enum_types.h | 1 + source/blender/makesrna/intern/rna_nodetree.c | 37 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) (limited to 'source/blender/makesrna') diff --git a/source/blender/makesrna/RNA_enum_types.h b/source/blender/makesrna/RNA_enum_types.h index 71af69f77a1..c8010a0e1ae 100644 --- a/source/blender/makesrna/RNA_enum_types.h +++ b/source/blender/makesrna/RNA_enum_types.h @@ -204,6 +204,7 @@ extern const EnumPropertyItem rna_enum_node_vec_math_items[]; extern const EnumPropertyItem rna_enum_node_boolean_math_items[]; extern const EnumPropertyItem rna_enum_node_float_compare_items[]; extern const EnumPropertyItem rna_enum_node_filter_items[]; +extern const EnumPropertyItem rna_enum_node_float_to_int_items[]; extern const EnumPropertyItem rna_enum_node_map_range_items[]; extern const EnumPropertyItem rna_enum_node_clamp_items[]; diff --git a/source/blender/makesrna/intern/rna_nodetree.c b/source/blender/makesrna/intern/rna_nodetree.c index d9e199e5eb2..93d5197f931 100644 --- a/source/blender/makesrna/intern/rna_nodetree.c +++ b/source/blender/makesrna/intern/rna_nodetree.c @@ -337,6 +337,31 @@ const EnumPropertyItem rna_enum_node_float_compare_items[] = { {0, NULL, 0, NULL, NULL}, }; +const EnumPropertyItem rna_enum_node_float_to_int_items[] = { + {FN_NODE_FLOAT_TO_INT_ROUND, + "ROUND", + 0, + "Round", + "Round the float up or down to the nearest integer"}, + {FN_NODE_FLOAT_TO_INT_FLOOR, + "FLOOR", + 0, + "Floor", + "Round the float down to the next smallest integer"}, + {FN_NODE_FLOAT_TO_INT_CEIL, + "CEILING", + 0, + "Ceiling", + "Round the float up to the next largest integer"}, + {FN_NODE_FLOAT_TO_INT_TRUNCATE, + "TRUNCATE", + 0, + "Truncate", + "Round the float to the closest integer in the direction of zero (floor if positive; ceiling " + "if negative)"}, + {0, NULL, 0, NULL, NULL}, +}; + const EnumPropertyItem rna_enum_node_map_range_items[] = { {NODE_MAP_RANGE_LINEAR, "LINEAR", @@ -4794,6 +4819,18 @@ static void def_float_compare(StructRNA *srna) RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_socket_update"); } +static void def_float_to_int(StructRNA *srna) +{ + PropertyRNA *prop; + + prop = RNA_def_property(srna, "rounding_mode", PROP_ENUM, PROP_NONE); + RNA_def_property_enum_sdna(prop, NULL, "custom1"); + RNA_def_property_enum_items(prop, rna_enum_node_float_to_int_items); + RNA_def_property_ui_text( + prop, "Rounding Mode", "Method used to convert the float to an integer"); + RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_update"); +} + static void def_vector_math(StructRNA *srna) { PropertyRNA *prop; -- cgit v1.2.3