Blender  V3.3
node_shader_color_ramp.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2005 Blender Foundation. All rights reserved. */
3 
8 #include "DNA_texture_types.h"
9 
10 #include "BLI_color.hh"
11 
12 #include "node_shader_util.hh"
13 
15 
17 {
18  b.is_function_node();
19  b.add_input<decl::Float>(N_("Fac")).default_value(0.5f).min(0.0f).max(1.0f).subtype(PROP_FACTOR);
20  b.add_output<decl::Color>(N_("Color"));
21  b.add_output<decl::Float>(N_("Alpha"));
22 }
23 
25 {
26  node->storage = BKE_colorband_add(true);
27 }
28 
30  bNode *node,
31  bNodeExecData *UNUSED(execdata),
32  GPUNodeStack *in,
34 {
35  struct ColorBand *coba = (ColorBand *)node->storage;
36  float *array, layer;
37  int size;
38 
39  /* Common / easy case optimization. */
40  if ((coba->tot <= 2) && (coba->color_mode == COLBAND_BLEND_RGB)) {
41  float mul_bias[2];
42  switch (coba->ipotype) {
44  mul_bias[0] = 1.0f / (coba->data[1].pos - coba->data[0].pos);
45  mul_bias[1] = -mul_bias[0] * coba->data[0].pos;
46  return GPU_stack_link(mat,
47  node,
48  "valtorgb_opti_linear",
49  in,
50  out,
51  GPU_uniform(mul_bias),
52  GPU_uniform(&coba->data[0].r),
53  GPU_uniform(&coba->data[1].r));
55  mul_bias[1] = max_ff(coba->data[0].pos, coba->data[1].pos);
56  return GPU_stack_link(mat,
57  node,
58  "valtorgb_opti_constant",
59  in,
60  out,
61  GPU_uniform(&mul_bias[1]),
62  GPU_uniform(&coba->data[0].r),
63  GPU_uniform(&coba->data[1].r));
65  mul_bias[0] = 1.0f / (coba->data[1].pos - coba->data[0].pos);
66  mul_bias[1] = -mul_bias[0] * coba->data[0].pos;
67  return GPU_stack_link(mat,
68  node,
69  "valtorgb_opti_ease",
70  in,
71  out,
72  GPU_uniform(mul_bias),
73  GPU_uniform(&coba->data[0].r),
74  GPU_uniform(&coba->data[1].r));
75  default:
76  break;
77  }
78  }
79 
81  GPUNodeLink *tex = GPU_color_band(mat, size, array, &layer);
82 
83  if (coba->ipotype == COLBAND_INTERP_CONSTANT) {
84  return GPU_stack_link(mat, node, "valtorgb_nearest", in, out, tex, GPU_constant(&layer));
85  }
86 
87  return GPU_stack_link(mat, node, "valtorgb", in, out, tex, GPU_constant(&layer));
88 }
89 
91  private:
92  const ColorBand &color_band_;
93 
94  public:
95  ColorBandFunction(const ColorBand &color_band) : color_band_(color_band)
96  {
98  this->set_signature(&signature);
99  }
100 
102  {
103  fn::MFSignatureBuilder signature{"Color Band"};
104  signature.single_input<float>("Value");
105  signature.single_output<ColorGeometry4f>("Color");
106  signature.single_output<float>("Alpha");
107  return signature.build();
108  }
109 
111  {
112  const VArray<float> &values = params.readonly_single_input<float>(0, "Value");
113  MutableSpan<ColorGeometry4f> colors = params.uninitialized_single_output<ColorGeometry4f>(
114  1, "Color");
115  MutableSpan<float> alphas = params.uninitialized_single_output<float>(2, "Alpha");
116 
117  for (int64_t i : mask) {
119  BKE_colorband_evaluate(&color_band_, values[i], color);
120  colors[i] = color;
121  alphas[i] = color.a;
122  }
123  }
124 };
125 
127 {
128  bNode &bnode = builder.node();
129  const ColorBand *color_band = (const ColorBand *)bnode.storage;
130  builder.construct_and_set_matching_fn<ColorBandFunction>(*color_band);
131 }
132 
133 } // namespace blender::nodes::node_shader_color_ramp_cc
134 
136 {
137  namespace file_ns = blender::nodes::node_shader_color_ramp_cc;
138 
139  static bNodeType ntype;
140 
148 
149  nodeRegisterType(&ntype);
150 }
struct ColorBand * BKE_colorband_add(bool rangetype)
Definition: colorband.c:296
bool BKE_colorband_evaluate(const struct ColorBand *coba, float in, float out[4])
void BKE_colorband_evaluate_table_rgba(const struct ColorBand *coba, float **array, int *size)
void node_type_gpu(struct bNodeType *ntype, NodeGPUExecFunction gpu_fn)
Definition: node.cc:4465
#define NODE_CLASS_CONVERTER
Definition: BKE_node.h:351
void node_type_init(struct bNodeType *ntype, void(*initfunc)(struct bNodeTree *ntree, struct bNode *node))
Definition: node.cc:4390
void node_type_size_preset(struct bNodeType *ntype, eNodeSizePreset size)
Definition: node.cc:4408
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:4426
#define SH_NODE_VALTORGB
Definition: BKE_node.h:1082
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1357
@ NODE_SIZE_LARGE
Definition: BKE_node.h:367
MINLINE float max_ff(float a, float b)
#define UNUSED(x)
@ COLBAND_BLEND_RGB
@ COLBAND_INTERP_LINEAR
@ COLBAND_INTERP_CONSTANT
@ COLBAND_INTERP_EASE
GPUNodeLink * GPU_color_band(GPUMaterial *mat, int size, float *pixels, float *row)
GPUNodeLink * GPU_constant(const float *num)
GPUNodeLink * GPU_uniform(const float *num)
bool GPU_stack_link(GPUMaterial *mat, struct bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Bright Control the brightness and contrast of the input color Vector Map an input vectors to used to fine tune the interpolation of the input Camera Retrieve information about the camera and how it relates to the current shading point s position Clamp a value between a minimum and a maximum Vector Perform vector math operation Invert a color
@ PROP_FACTOR
Definition: RNA_types.h:144
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
void set_signature(const MFSignature *signature)
const MFSignature & signature() const
void call(IndexMask mask, fn::MFParams params, fn::MFContext UNUSED(context)) const override
OperationNode * node
bNodeTree * ntree
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
ccl_device_inline float4 mask(const int4 &mask, const float4 &a)
Definition: math_float4.h:513
static int gpu_shader_valtorgb(GPUMaterial *mat, bNode *node, bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out)
static void sh_node_valtorgb_build_multi_function(nodes::NodeMultiFunctionBuilder &builder)
static void sh_node_valtorgb_declare(NodeDeclarationBuilder &b)
static void node_shader_init_valtorgb(bNodeTree *UNUSED(ntree), bNode *node)
static const pxr::TfToken out("out", pxr::TfToken::Immortal)
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
void register_node_type_sh_valtorgb()
void sh_fn_node_type_base(bNodeType *ntype, int type, const char *name, short nclass)
void node_copy_standard_storage(bNodeTree *UNUSED(dest_ntree), bNode *dest_node, const bNode *src_node)
Definition: node_util.c:55
void node_free_standard_storage(bNode *node)
Definition: node_util.c:43
__int64 int64_t
Definition: stdint.h:89
CBData data[32]
Defines a node type.
Definition: BKE_node.h:226
NodeMultiFunctionBuildFunction build_multi_function
Definition: BKE_node.h:313
NodeDeclareFunction declare
Definition: BKE_node.h:324
void * storage
#define N_(msgid)