Blender V5.0
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.hh"
14
15#include "NOD_multi_function.hh"
16
17#include "node_shader_util.hh"
18#include "node_util.hh"
19
21
23{
24 b.is_function_node();
25 b.add_input<decl::Float>("Factor", "Fac")
26 .default_value(0.5f)
27 .min(0.0f)
28 .max(1.0f)
30 .description(
31 "The value used to map onto the color gradient. 0.0 results in the leftmost color, "
32 "while 1.0 results in the rightmost");
33 b.add_output<decl::Color>("Color");
34 b.add_output<decl::Float>("Alpha");
35}
36
37static void node_shader_init_valtorgb(bNodeTree * /*ntree*/, bNode *node)
38{
39 node->storage = BKE_colorband_add(true);
40}
41
43 bNode *node,
44 bNodeExecData * /*execdata*/,
47{
48 ColorBand *coba = (ColorBand *)node->storage;
49 float *array, layer;
50 int size;
51
52 /* Common / easy case optimization. */
53 if (coba->tot == 1) {
54 return GPU_link(mat, "set_rgba", GPU_uniform(&coba->data[0].r), &out[0].link);
55 }
56 if ((coba->tot == 2) && (coba->color_mode == COLBAND_BLEND_RGB)) {
57 float mul_bias[2];
58 switch (coba->ipotype) {
60 mul_bias[0] = 1.0f / (coba->data[1].pos - coba->data[0].pos);
61 mul_bias[1] = -mul_bias[0] * coba->data[0].pos;
62 return GPU_stack_link(mat,
63 node,
64 "valtorgb_opti_linear",
65 in,
66 out,
67 GPU_uniform(mul_bias),
68 GPU_uniform(&coba->data[0].r),
69 GPU_uniform(&coba->data[1].r));
71 mul_bias[1] = max_ff(coba->data[0].pos, coba->data[1].pos);
72 return GPU_stack_link(mat,
73 node,
74 "valtorgb_opti_constant",
75 in,
76 out,
77 GPU_uniform(&mul_bias[1]),
78 GPU_uniform(&coba->data[0].r),
79 GPU_uniform(&coba->data[1].r));
81 mul_bias[0] = 1.0f / (coba->data[1].pos - coba->data[0].pos);
82 mul_bias[1] = -mul_bias[0] * coba->data[0].pos;
83 return GPU_stack_link(mat,
84 node,
85 "valtorgb_opti_ease",
86 in,
87 out,
88 GPU_uniform(mul_bias),
89 GPU_uniform(&coba->data[0].r),
90 GPU_uniform(&coba->data[1].r));
91 default:
92 break;
93 }
94 }
95
97 GPUNodeLink *tex = GPU_color_band(mat, size, array, &layer);
98
99 if (coba->ipotype == COLBAND_INTERP_CONSTANT) {
100 return GPU_stack_link(mat, node, "valtorgb_nearest", in, out, tex, GPU_constant(&layer));
101 }
102
103 return GPU_stack_link(mat, node, "valtorgb", in, out, tex, GPU_constant(&layer));
104}
105
106class ColorBandFunction : public mf::MultiFunction {
107 private:
108 const ColorBand &color_band_;
109
110 public:
111 ColorBandFunction(const ColorBand &color_band) : color_band_(color_band)
112 {
113 static const mf::Signature signature = []() {
114 mf::Signature signature;
115 mf::SignatureBuilder builder{"Color Band", signature};
116 builder.single_input<float>("Value");
117 builder.single_output<ColorGeometry4f>("Color");
118 builder.single_output<float>("Alpha");
119 return signature;
120 }();
121 this->set_signature(&signature);
122 }
123
124 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const override
125 {
126 const VArray<float> &values = params.readonly_single_input<float>(0, "Value");
127 MutableSpan<ColorGeometry4f> colors = params.uninitialized_single_output<ColorGeometry4f>(
128 1, "Color");
129 MutableSpan<float> alphas = params.uninitialized_single_output<float>(2, "Alpha");
130
131 mask.foreach_index([&](const int64_t i) {
133 BKE_colorband_evaluate(&color_band_, values[i], color);
134 colors[i] = color;
135 alphas[i] = color.a;
136 });
137 }
138};
139
141{
142 const bNode &bnode = builder.node();
143 const ColorBand *color_band = (const ColorBand *)bnode.storage;
145}
146
148#ifdef WITH_MATERIALX
149{
150 /* TODO: Implement */
151 NodeItem res = empty();
152 return res;
153}
154#endif
156
157} // namespace blender::nodes::node_shader_color_ramp_cc
158
160{
162
163 static blender::bke::bNodeType ntype;
164
165 common_node_type_base(&ntype, "ShaderNodeValToRGB", SH_NODE_VALTORGB);
166 ntype.ui_name = "Color Ramp";
167 ntype.ui_description = "Map values to colors with the use of a gradient";
168 ntype.enum_name_legacy = "VALTORGB";
170 ntype.declare = file_ns::sh_node_valtorgb_declare;
171 ntype.initfunc = file_ns::node_shader_init_valtorgb;
175 ntype.gpu_fn = file_ns::gpu_shader_valtorgb;
176 ntype.build_multi_function = file_ns::sh_node_valtorgb_build_multi_function;
177 ntype.materialx_fn = file_ns::node_shader_materialx;
178
180}
void BKE_colorband_evaluate_table_rgba(const ColorBand *coba, float **array, int *size)
Definition colorband.cc:559
bool BKE_colorband_evaluate(const ColorBand *coba, float in, float out[4])
Definition colorband.cc:396
ColorBand * BKE_colorband_add(bool rangetype)
Definition colorband.cc:298
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:453
#define SH_NODE_VALTORGB
MINLINE float max_ff(float a, float b)
@ COLBAND_BLEND_RGB
@ COLBAND_INTERP_LINEAR
@ COLBAND_INTERP_CONSTANT
@ COLBAND_INTERP_EASE
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
GPUNodeLink * GPU_constant(const float *num)
GPUNodeLink * GPU_color_band(GPUMaterial *mat, int size, float *pixels, float *r_row)
bool GPU_link(GPUMaterial *mat, const char *name,...)
GPUNodeLink * GPU_uniform(const float *num)
@ PROP_FACTOR
Definition RNA_types.hh:251
long long int int64_t
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
void set_signature(const Signature *signature)
void call(const IndexMask &mask, mf::Params params, mf::Context) const override
#define in
#define out
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
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:5414
void node_type_size_preset(bNodeType &ntype, eNodeSizePreset size)
Definition node.cc:5396
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)
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
#define NODE_SHADER_MATERIALX_BEGIN
#define NODE_SHADER_MATERIALX_END
void register_node_type_sh_valtorgb()
void common_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
void node_free_standard_storage(bNode *node)
Definition node_util.cc:42
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:54
CBData data[32]
void * storage
Defines a node type.
Definition BKE_node.hh:238
NodeMaterialXFunction materialx_fn
Definition BKE_node.hh:344
std::string ui_description
Definition BKE_node.hh:244
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:289
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:342
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:351
const char * enum_name_legacy
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:362
i
Definition text_draw.cc:230