Blender V5.3
node_shader_color_ramp.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2005 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "DNA_texture_types.h"
10
11#include "BKE_colorband.hh"
12
13#include "BLI_color_types.hh"
14
15#include "NOD_multi_function.hh"
16
17#include "node_shader_util.hh"
18#include "node_util.hh"
19
20namespace blender {
21
23
25{
26 b.is_function_node();
27 b.add_input<decl::Float>("Factor"_ustr, "Fac"_ustr)
28 .default_value(0.5f)
29 .min(0.0f)
30 .max(1.0f)
32 .description(
33 "The value used to map onto the color gradient. 0.0 results in the leftmost color, "
34 "while 1.0 results in the rightmost");
35 b.add_output<decl::Color>("Color"_ustr);
36 b.add_output<decl::Float>("Alpha"_ustr);
37}
38
40{
41 node->storage = BKE_colorband_add(true);
42}
43
45 bNode *node,
46 bNodeExecData * /*execdata*/,
49{
50 ColorBand *coba = static_cast<ColorBand *>(node->storage);
51 float *array, layer;
52 int size;
53
54 /* Common / easy case optimization. */
55 if (coba->tot == 1) {
56 return GPU_link(mat, "set_rgba", GPU_uniform(&coba->data[0].r), &out[0].link);
57 }
58 if ((coba->tot == 2) && (coba->color_mode == COLBAND_BLEND_RGB)) {
59 float mul_bias[2];
60 switch (coba->ipotype) {
62 mul_bias[0] = 1.0f / (coba->data[1].pos - coba->data[0].pos);
63 mul_bias[1] = -mul_bias[0] * coba->data[0].pos;
64 return GPU_stack_link(mat,
65 node,
66 "valtorgb_opti_linear",
67 in,
68 out,
69 GPU_uniform(mul_bias),
70 GPU_uniform(&coba->data[0].r),
71 GPU_uniform(&coba->data[1].r));
73 mul_bias[1] = max_ff(coba->data[0].pos, coba->data[1].pos);
74 return GPU_stack_link(mat,
75 node,
76 "valtorgb_opti_constant",
77 in,
78 out,
79 GPU_uniform(&mul_bias[1]),
80 GPU_uniform(&coba->data[0].r),
81 GPU_uniform(&coba->data[1].r));
83 mul_bias[0] = 1.0f / (coba->data[1].pos - coba->data[0].pos);
84 mul_bias[1] = -mul_bias[0] * coba->data[0].pos;
85 return GPU_stack_link(mat,
86 node,
87 "valtorgb_opti_ease",
88 in,
89 out,
90 GPU_uniform(mul_bias),
91 GPU_uniform(&coba->data[0].r),
92 GPU_uniform(&coba->data[1].r));
93 default:
94 break;
95 }
96 }
97
99 GPUNodeLink *tex = GPU_color_band(mat, size, array, &layer);
100
101 if (coba->ipotype == COLBAND_INTERP_CONSTANT) {
102 return GPU_stack_link(mat, node, "valtorgb_nearest", in, out, tex, GPU_constant(&layer));
103 }
104
105 return GPU_stack_link(mat, node, "valtorgb", in, out, tex, GPU_constant(&layer));
106}
107
108class ColorBandFunction : public mf::MultiFunction {
109 private:
111 std::shared_ptr<const bNodeTree> tree_;
112 const ColorBand &color_band_;
113
114 public:
115 ColorBandFunction(const ColorBand &color_band, std::shared_ptr<const bNodeTree> tree)
116 : tree_(tree), color_band_(color_band)
117 {
118 static const mf::Signature signature = []() {
119 mf::Signature signature;
120 mf::SignatureBuilder builder{"Color Band", signature};
121 builder.single_input<float>("Value");
122 builder.single_output<ColorGeometry4f>("Color");
123 builder.single_output<float>("Alpha");
124 return signature;
125 }();
126 this->set_signature(&signature);
127 }
128
129 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const override
130 {
131 const VArray<float> &values = params.readonly_single_input<float>(0, "Value");
132 MutableSpan<ColorGeometry4f> colors = params.uninitialized_single_output<ColorGeometry4f>(
133 1, "Color");
134 MutableSpan<float> alphas = params.uninitialized_single_output<float>(2, "Alpha");
135
136 mask.foreach_index([&](const int64_t i) {
138 BKE_colorband_evaluate(&color_band_, values[i], color);
139 colors[i] = color;
140 alphas[i] = color.a;
141 });
142 }
143
144 void hash_unique(UniqueHashBytes &hash) const override
145 {
146 static constexpr int8_t id = 0;
147 hash.add(&id);
148 hash.add(&color_band_);
149 }
150};
151
153{
154 const bNode &bnode = builder.node();
155 const ColorBand *color_band = static_cast<const ColorBand *>(bnode.storage);
156 builder.construct_and_set_matching_fn<ColorBandFunction>(*color_band, builder.shared_tree());
157}
158
160#ifdef WITH_MATERIALX
161{
162 /* TODO: Implement */
163 NodeItem res = empty();
164 return res;
165}
166#endif
168
169} // namespace nodes::node_shader_color_ramp_cc
170
172{
173 namespace file_ns = nodes::node_shader_color_ramp_cc;
174
175 static bke::bNodeType ntype;
176
177 common_node_type_base(&ntype, "ShaderNodeValToRGB"_ustr, SH_NODE_VALTORGB);
178 ntype.ui_name = "Color Ramp";
179 ntype.ui_description = "Map values to colors with the use of a gradient";
180 ntype.enum_name_legacy = "VALTORGB";
182 ntype.declare = file_ns::sh_node_valtorgb_declare;
183 ntype.initfunc = file_ns::node_shader_init_valtorgb;
187 ntype.gpu_fn = file_ns::gpu_shader_valtorgb;
188 ntype.build_multi_function = file_ns::sh_node_valtorgb_build_multi_function;
189 ntype.materialx_fn = file_ns::node_shader_materialx;
190
192}
193
194} // namespace blender
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define SH_NODE_VALTORGB
void set_signature(const Signature *signature)
const std::shared_ptr< const bNodeTree > & shared_tree()
ColorBandFunction(const ColorBand &color_band, std::shared_ptr< const bNodeTree > tree)
void call(const IndexMask &mask, mf::Params params, mf::Context) const override
#define layer
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:6166
static void node_shader_init_valtorgb(bNodeTree *, bNode *node)
static void sh_node_valtorgb_build_multi_function(nodes::NodeMultiFunctionBuilder &builder)
static void sh_node_valtorgb_declare(NodeDeclarationBuilder &b)
static int gpu_shader_valtorgb(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
BMesh Iterators.
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
__int64 int64_t
Definition BKE_text.h:90
ColorBand * BKE_colorband_add(bool rangetype)
Definition colorband.cc:301
bool BKE_colorband_evaluate(const ColorBand *coba, float in, float out[4])
Definition colorband.cc:399
GPUNodeLink * GPU_color_band(GPUMaterial *mat, int size, float *pixels, float *r_row)
GPUNodeLink * GPU_uniform(const float *num)
void BKE_colorband_evaluate_table_rgba(const ColorBand *coba, float **array, int *size)
Definition colorband.cc:562
GPUNodeLink * GPU_constant(const float *num)
bool GPU_link(GPUMaterial *mat, const char *name,...)
void register_node_type_sh_valtorgb()
MINLINE float max_ff(float a, float b)
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
@ PROP_FACTOR
Definition RNA_types.hh:308
signed char int8_t
Definition BKE_text.h:76
void common_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
#define NODE_SHADER_MATERIALX_BEGIN
#define NODE_SHADER_MATERIALX_END
#define hash
Definition noise_c.cc:156
eColorBand_ColorMode color_mode
eColorBand_Interp ipotype
static constexpr int _240
Definition BKE_node.hh:250
Defines a node type.
Definition BKE_node.hh:265
NodeMaterialXFunction materialx_fn
Definition BKE_node.hh:375
std::string ui_description
Definition BKE_node.hh:271
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:373
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:382
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393