Blender V5.3
node_shader_tex_gradient.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2005 Blender Foundation. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "node_shader_util.hh"
10#include "node_util.hh"
11
12#include "BKE_texture.h"
13
14#include "BLI_math_constants.hh"
15#include "BLI_math_vector.hh"
16
17#include "NOD_multi_function.hh"
18
20#include "UI_resources.hh"
21
22namespace blender {
23
25
27{
28 b.is_function_node();
29
30 const bool is_compositor = b.tree_or_null() && b.tree_or_null()->type == NTREE_COMPOSIT;
31 const NodeDefaultInputType default_input_type =
34 b.add_input<decl::Vector>("Vector"_ustr).default_input_type(default_input_type);
35
36 b.add_output<decl::Color>("Color"_ustr).no_muted_links();
37 b.add_output<decl::Float>("Factor"_ustr, "Fac"_ustr).no_muted_links();
38}
39
41{
42 layout.prop(ptr, "gradient_type", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
43}
44
46{
47 NodeTexGradient *tex = MEM_new<NodeTexGradient>(__func__);
51
52 node->storage = tex;
53}
54
56 bNode *node,
57 bNodeExecData * /*execdata*/,
60{
63
64 NodeTexGradient *tex = static_cast<NodeTexGradient *>(node->storage);
65 float gradient_type = tex->gradient_type;
66 return GPU_stack_link(mat, node, "node_tex_gradient", in, out, GPU_constant(&gradient_type));
67}
68
69class GradientFunction : public mf::MultiFunction {
70 private:
71 int gradient_type_;
72
73 public:
74 GradientFunction(int gradient_type) : gradient_type_(gradient_type)
75 {
76 static const mf::Signature signature = []() {
77 mf::Signature signature;
78 mf::SignatureBuilder builder{"GradientFunction", signature};
79 builder.single_input<float3>("Vector");
80 builder.single_output<ColorGeometry4f>("Color", mf::ParamFlag::SupportsUnusedOutput);
81 builder.single_output<float>("Fac");
82 return signature;
83 }();
85 }
86
87 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const override
88 {
89 const VArray<float3> &vector = params.readonly_single_input<float3>(0, "Vector");
90
92 params.uninitialized_single_output_if_required<ColorGeometry4f>(1, "Color");
93 MutableSpan<float> fac = params.uninitialized_single_output<float>(2, "Fac");
94
95 const bool compute_color = !r_color.is_empty();
96
97 switch (gradient_type_) {
98 case SHD_BLEND_LINEAR: {
99 mask.foreach_index_optimized<int64_t>(
100 [&](const int64_t i) { fac[i] = math::clamp(vector[i].x, 0.0f, 1.0f); });
101 break;
102 }
103 case SHD_BLEND_QUADRATIC: {
104 mask.foreach_index_optimized<int64_t>([&](const int64_t i) {
105 const float r = std::max(vector[i].x, 0.0f);
106 fac[i] = math::clamp(r * r, 0.0f, 1.0f);
107 });
108 break;
109 }
110 case SHD_BLEND_EASING: {
111 mask.foreach_index_optimized<int64_t>([&](const int64_t i) {
112 const float r = std::min(std::max(vector[i].x, 0.0f), 1.0f);
113 const float t = r * r;
114 fac[i] = (3.0f * t - 2.0f * t * r);
115 });
116 break;
117 }
118 case SHD_BLEND_DIAGONAL: {
119 mask.foreach_index_optimized<int64_t>([&](const int64_t i) {
120 fac[i] = (vector[i].x + vector[i].y) * 0.5f;
121 fac[i] = math::clamp(fac[i], 0.0f, 1.0f);
122 });
123 break;
124 }
125 case SHD_BLEND_RADIAL: {
126 mask.foreach_index_optimized<int64_t>([&](const int64_t i) {
127 fac[i] = atan2f(vector[i].y, vector[i].x) / (M_PI * 2.0f) + 0.5f;
128 });
129 break;
130 }
132 mask.foreach_index_optimized<int64_t>([&](const int64_t i) {
133 /* Bias a little bit for the case where input is a unit length vector,
134 * to get exactly zero instead of a small random value depending
135 * on float precision. */
136 const float r = std::max(0.999999f - math::length(vector[i]), 0.0f);
137 fac[i] = r * r;
138 });
139 break;
140 }
141 case SHD_BLEND_SPHERICAL: {
142 mask.foreach_index_optimized<int64_t>([&](const int64_t i) {
143 /* Bias a little bit for the case where input is a unit length vector,
144 * to get exactly zero instead of a small random value depending
145 * on float precision. */
146 fac[i] = std::max(0.999999f - math::length(vector[i]), 0.0f);
147 });
148 break;
149 }
150 }
151 if (compute_color) {
152 mask.foreach_index_optimized<int64_t>(
153 [&](const int64_t i) { r_color[i] = ColorGeometry4f(fac[i], fac[i], fac[i], 1.0f); });
154 }
155 }
156
157 void hash_unique(UniqueHashBytes &hash) const override
158 {
159 static constexpr int8_t id = 0;
160 hash.add(&id);
161 hash.add(gradient_type_);
162 }
163};
164
166{
167 const bNode &node = builder.node();
168 NodeTexGradient *tex = static_cast<NodeTexGradient *>(node.storage);
170}
171
173#ifdef WITH_MATERIALX
174{
175 NodeTexGradient *tex = (NodeTexGradient *)node_->storage;
176 const int gradient_type = tex->gradient_type;
177 NodeItem vector = get_input_link("Vector", NodeItem::Type::Vector2);
178 if (!vector) {
179 vector = texcoord_node();
180 }
181 NodeItem res = empty();
182
183 switch (gradient_type) {
184 case SHD_BLEND_LINEAR:
185 res = vector[0];
186 break;
188 res = vector[0];
189 res = res * res;
190 break;
191 case SHD_BLEND_EASING:
192 res = vector[0].clamp();
193 res = res * res * (val(3.0f) - val(2.0f) * res);
194 break;
196 res = (vector[0] + vector[1]) * val(0.5f);
197 break;
198 case SHD_BLEND_RADIAL:
199 res = vector[1].atan2(vector[0]) / val(float(M_PI * 2.0f)) + val(0.5f);
200 break;
202 res = (val(1.0f) - vector.dotproduct(vector).sqrt()).max(val(0.0f));
203 res = res * res;
204 break;
206 res = (val(1.0f) - vector.dotproduct(vector).sqrt()).max(val(0.0f));
207 break;
208 default:
210 }
211 return res;
212}
213#endif
215
216} // namespace nodes::node_shader_tex_gradient_cc
217
219{
220 namespace file_ns = nodes::node_shader_tex_gradient_cc;
221
222 static bke::bNodeType ntype;
223
224 common_node_type_base(&ntype, "ShaderNodeTexGradient"_ustr, SH_NODE_TEX_GRADIENT);
225 ntype.ui_name = "Gradient Texture";
226 ntype.ui_description =
227 "Generate interpolated color and intensity values based on the input vector";
228 ntype.enum_name_legacy = "TEX_GRADIENT";
230 ntype.declare = file_ns::sh_node_tex_gradient_declare;
231 ntype.draw_buttons = file_ns::node_shader_buts_tex_gradient;
232 ntype.initfunc = file_ns::node_shader_init_tex_gradient;
234 ntype, "NodeTexGradient", node_free_standard_storage, node_copy_standard_storage);
235 ntype.gpu_fn = file_ns::node_shader_gpu_tex_gradient;
236 ntype.build_multi_function = file_ns::sh_node_gradient_tex_build_multi_function;
237 ntype.materialx_fn = file_ns::node_shader_materialx;
238
240}
241
242} // namespace blender
#define NODE_CLASS_TEXTURE
Definition BKE_node.hh:488
#define SH_NODE_TEX_GRADIENT
#define M_PI
#define BLI_assert_unreachable()
btScalar max
constexpr bool is_empty() const
Definition BLI_span.hh:515
void set_signature(const Signature *signature)
void call(const IndexMask &mask, mf::Params params, mf::Context) const override
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
T clamp(const T &a, const T &min, const T &max)
T length(const VecBase< T, Size > &a)
static void node_shader_init_tex_gradient(bNodeTree *, bNode *node)
static void sh_node_tex_gradient_declare(NodeDeclarationBuilder &b)
static int node_shader_gpu_tex_gradient(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void sh_node_gradient_tex_build_multi_function(NodeMultiFunctionBuilder &builder)
static void node_shader_buts_tex_gradient(ui::Layout &layout, bContext *, PointerRNA *ptr)
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
GPUNodeLink * GPU_constant(const float *num)
void node_shader_gpu_tex_mapping(GPUMaterial *mat, bNode *node, GPUNodeStack *in, GPUNodeStack *)
void node_shader_gpu_default_tex_coord(GPUMaterial *mat, bNode *node, GPUNodeLink **link)
void BKE_texture_mapping_default(struct TexMapping *texmap, int type)
Definition texture.cc:234
void register_node_type_sh_tex_gradient()
@ SHD_BLEND_QUADRATIC
@ SHD_BLEND_QUADRATIC_SPHERE
@ SHD_BLEND_SPHERICAL
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
VecBase< float, 3 > float3
void BKE_texture_colormapping_default(struct ColorMapping *colormap)
Definition texture.cc:337
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
#define atan2f
Defines a node type.
Definition BKE_node.hh:265
NodeMaterialXFunction materialx_fn
Definition BKE_node.hh:375
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
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
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)