Blender  V2.93
node_geo_attribute_color_ramp.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #include "BKE_colorband.h"
18 
19 #include "UI_interface.h"
20 #include "UI_resources.h"
21 
22 #include "node_geometry_util.hh"
23 
25  {SOCK_GEOMETRY, N_("Geometry")},
26  {SOCK_STRING, N_("Attribute")},
27  {SOCK_STRING, N_("Result")},
28  {-1, ""},
29 };
30 
32  {SOCK_GEOMETRY, N_("Geometry")},
33  {-1, ""},
34 };
35 
37  bContext *UNUSED(C),
38  PointerRNA *ptr)
39 {
40  uiTemplateColorRamp(layout, ptr, "color_ramp", false);
41 }
42 
43 namespace blender::nodes {
44 
46  StringRef input_name,
47  StringRef result_name)
48 {
49  /* Use the domain of the result attribute if it already exists. */
50  ReadAttributePtr result_attribute = component.attribute_try_get_for_read(result_name);
51  if (result_attribute) {
52  return result_attribute->domain();
53  }
54 
55  /* Otherwise use the input attribute's domain if it exists. */
56  ReadAttributePtr input_attribute = component.attribute_try_get_for_read(input_name);
57  if (input_attribute) {
58  return input_attribute->domain();
59  }
60 
61  return ATTR_DOMAIN_POINT;
62 }
63 
65 {
66  const bNode &bnode = params.node();
67  NodeAttributeColorRamp *node_storage = (NodeAttributeColorRamp *)bnode.storage;
68  const std::string result_name = params.get_input<std::string>("Result");
69  const std::string input_name = params.get_input<std::string>("Attribute");
70 
71  /* Always output a color attribute for now. We might want to allow users to customize.
72  * Using the type of an existing attribute could work, but does not have a real benefit
73  * currently. */
74  const CustomDataType result_type = CD_PROP_COLOR;
75  const AttributeDomain result_domain = get_result_domain(component, input_name, result_name);
76 
77  OutputAttributePtr attribute_result = component.attribute_try_get_for_output(
78  result_name, result_domain, result_type);
79  if (!attribute_result) {
80  return;
81  }
82 
83  FloatReadAttribute attribute_in = component.attribute_get_for_read<float>(
84  input_name, result_domain, 0.0f);
85 
86  Span<float> data_in = attribute_in.get_span();
87  MutableSpan<Color4f> data_out = attribute_result->get_span_for_write_only<Color4f>();
88 
89  ColorBand *color_ramp = &node_storage->color_ramp;
90  for (const int i : data_in.index_range()) {
91  BKE_colorband_evaluate(color_ramp, data_in[i], data_out[i]);
92  }
93 
94  attribute_result.apply_span_and_save();
95 }
96 
98 {
99  GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");
100 
101  geometry_set = geometry_set_realize_instances(geometry_set);
102 
103  if (geometry_set.has<MeshComponent>()) {
105  }
106  if (geometry_set.has<PointCloudComponent>()) {
108  }
109 
110  params.set_output("Geometry", std::move(geometry_set));
111 }
112 
114 {
116  sizeof(NodeAttributeColorRamp), __func__);
117  BKE_colorband_init(&node_storage->color_ramp, true);
118  node->storage = node_storage;
119 }
120 
121 } // namespace blender::nodes
122 
124 {
125  static bNodeType ntype;
126 
128  &ntype, GEO_NODE_ATTRIBUTE_COLOR_RAMP, "Attribute Color Ramp", NODE_CLASS_ATTRIBUTE, 0);
132  &ntype, "NodeAttributeColorRamp", node_free_standard_storage, node_copy_standard_storage);
137  nodeRegisterType(&ntype);
138 }
AttributeDomain
Definition: BKE_attribute.h:41
@ ATTR_DOMAIN_POINT
Definition: BKE_attribute.h:43
void BKE_colorband_init(struct ColorBand *coba, bool rangetype)
Definition: colorband.c:38
bool BKE_colorband_evaluate(const struct ColorBand *coba, float in, float out[4])
void node_type_socket_templates(struct bNodeType *ntype, struct bNodeSocketTemplate *inputs, struct bNodeSocketTemplate *outputs)
Definition: node.cc:4527
void node_type_init(struct bNodeType *ntype, void(*initfunc)(struct bNodeTree *ntree, struct bNode *node))
Definition: node.cc:4559
void node_type_size_preset(struct bNodeType *ntype, eNodeSizePreset size)
Definition: node.cc:4577
void node_type_storage(struct bNodeType *ntype, const char *storagename, void(*freefunc)(struct bNode *node), void(*copyfunc)(struct bNodeTree *dest_ntree, struct bNode *dest_node, const struct bNode *src_node))
Definition: node.cc:4599
#define NODE_CLASS_ATTRIBUTE
Definition: BKE_node.h:360
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1298
@ NODE_SIZE_LARGE
Definition: BKE_node.h:373
#define UNUSED(x)
#define N_(msgid)
static uint8 component(Color32 c, uint i)
Definition: ColorBlock.cpp:126
CustomDataType
@ CD_PROP_COLOR
@ SOCK_GEOMETRY
@ SOCK_STRING
Group RGB to Bright Vector Camera Vector Combine Material Light Line Style Layer Add Ambient Diffuse Glossy Refraction Transparent Toon Principled Hair Volume Principled Light Particle Volume Image Sky Noise Wave Voronoi Brick Texture Vector Combine Vertex Separate Vector White RGB Map Separate Set Z Dilate Combine Combine Color Channel Split ID Combine Luminance Directional Alpha Distance Hue Movie Ellipse Bokeh View Corner Anti Mix RGB Hue Separate TEX_NODE_PROC TEX_NODE_PROC TEX_NODE_PROC TEX_NODE_PROC TEX_NODE_PROC Boolean Random Edge Subdivision Point Object Attribute Attribute GEO_NODE_ATTRIBUTE_COLOR_RAMP
#define C
Definition: RandGen.cpp:39
void uiTemplateColorRamp(uiLayout *layout, struct PointerRNA *ptr, const char *propname, bool expand)
fn::GMutableSpan get_span_for_write_only()
OperationNode * node
bNodeTree * ntree
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
TypedReadAttribute< float > FloatReadAttribute
GeometrySet geometry_set_realize_instances(const GeometrySet &geometry_set)
std::unique_ptr< ReadAttribute > ReadAttributePtr
static void execute_on_component(const GeoNodeExecParams &params, GeometryComponent &component)
static void geo_node_attribute_color_ramp_init(bNodeTree *UNUSED(ntree), bNode *node)
static AttributeDomain get_result_domain(const GeometryComponent &component, StringRef source_name, StringRef result_name)
static void geo_node_attribute_color_ramp_exec(GeoNodeExecParams params)
static bNodeSocketTemplate geo_node_attribute_color_ramp_in[]
static void geo_node_attribute_color_ramp_layout(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
void register_node_type_geo_attribute_color_ramp()
static bNodeSocketTemplate geo_node_attribute_color_ramp_out[]
void geo_node_type_base(bNodeType *ntype, int type, const char *name, short nclass, short flag)
void node_copy_standard_storage(bNodeTree *UNUSED(dest_ntree), bNode *dest_node, const bNode *src_node)
Definition: node_util.c:67
void node_free_standard_storage(bNode *node)
Definition: node_util.c:55
GeometryComponent & get_component_for_write(GeometryComponentType component_type)
bool has(const GeometryComponentType component_type) const
Compact definition of a node socket.
Definition: BKE_node.h:95
Defines a node type.
Definition: BKE_node.h:221
NodeGeometryExecFunction geometry_node_execute
Definition: BKE_node.h:327
void(* draw_buttons)(struct uiLayout *, struct bContext *C, struct PointerRNA *ptr)
Definition: BKE_node.h:253
void * storage
PointerRNA * ptr
Definition: wm_files.c:3157