Blender V5.3
node_shader_tex_environment.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 "node_shader_util.hh"
10#include "node_util.hh"
11
12#include "BKE_image.hh"
13#include "BKE_node_runtime.hh"
14#include "BKE_texture.h"
15
17
19
20namespace blender {
21
23
25{
26 b.add_input<decl::Vector>("Vector"_ustr).hide_value();
27 b.add_output<decl::Color>("Color"_ustr).no_muted_links();
28}
29
31{
32 NodeTexEnvironment *tex = MEM_new<NodeTexEnvironment>("NodeTexEnvironment");
37
38 node->storage = tex;
39}
40
42 bNode *node,
43 bNodeExecData * /*execdata*/,
46{
47 Image *ima = id_cast<Image *>(node->id);
48 NodeTexEnvironment *tex = static_cast<NodeTexEnvironment *>(node->storage);
49
50 /* We get the image user from the original node, since GPU image keeps
51 * a pointer to it and the dependency refreshes the original. */
52 bNode *node_original = node->runtime->original ? node->runtime->original : node;
53 NodeTexImage *tex_original = static_cast<NodeTexImage *>(node_original->storage);
54 ImageUser *iuser = &tex_original->iuser;
55 /* Setting the GPU_SAMPLER_FILTERING_ANISOTROPIC_ENABLE enables anisotropic filtering. The
56 * exact number of samples are being determined at bind time by the engine.
57 * See #blender::draw::PassBase<T>::material_set */
59 GPU_SAMPLER_FILTERING_ANISOTROPIC_ENABLE,
62 /* TODO(@fclem): For now assume mipmap is always enabled. */
63 if (true) {
64 sampler.enable_filtering_flag(GPU_SAMPLER_FILTERING_MIPMAP);
65 }
66
67 GPUNodeLink *outalpha;
68
69 if (!ima) {
70 return GPU_stack_link(mat, node, "node_tex_environment_empty", in, out);
71 }
72
73 if (!in[0].link) {
74 GPU_link(mat, "node_tex_coord_position", GPU_shading_data(), &in[0].link);
76 }
77
79
80 /* Compute texture coordinate. */
82 GPU_link(mat, "node_tex_environment_equirectangular", in[0].link, &in[0].link);
83 /* To fix pole issue we clamp the v coordinate. */
85 /* Force the highest mipmap and don't do anisotropic filtering.
86 * This is to fix the artifact caused by derivatives discontinuity. */
87 sampler.disable_filtering_flag(GPU_SAMPLER_FILTERING_MIPMAP |
88 GPU_SAMPLER_FILTERING_ANISOTROPIC_MASK);
89 }
90 else {
91 GPU_link(mat, "node_tex_environment_mirror_ball", in[0].link, &in[0].link);
92 /* Fix pole issue. */
95 }
96
97 const char *gpu_fn;
98 static const char *names[] = {
99 "node_tex_image_linear",
100 "node_tex_image_cubic",
101 };
102
103 switch (tex->interpolation) {
105 gpu_fn = names[0];
106 break;
109 gpu_fn = names[0];
110 break;
111 default:
112 gpu_fn = names[1];
113 break;
114 }
115
116 /* Sample texture with correct interpolation. */
117 GPU_link(mat,
118 gpu_fn,
119 in[0].link,
120 GPU_image(mat, ima, iuser, sampler),
122 &out[0].link,
123 &outalpha);
124
125 if (out[0].hasoutput && out[0].link && ima) {
126 if (ELEM(ima->alpha_mode, IMA_ALPHA_IGNORE, IMA_ALPHA_CHANNEL_PACKED) ||
127 IMB_colormanagement_space_name_is_data(ima->colorspace_settings.name))
128 {
129 /* Don't let alpha affect color output in these cases. */
130 GPU_link(mat, "color_alpha_clear", out[0].link, &out[0].link);
131 }
132 else {
133 /* Always output with premultiplied alpha. */
134 if (ima->alpha_mode == IMA_ALPHA_PREMUL) {
135 GPU_link(mat, "color_alpha_clear", out[0].link, &out[0].link);
136 }
137 else {
138 GPU_link(mat, "color_alpha_premultiply", out[0].link, &out[0].link);
139 }
140 }
141 }
142
143 return true;
144}
145
147#ifdef WITH_MATERIALX
148{
149 NodeItem res = val(MaterialX::Color4(1.0f, 0.0f, 1.0f, 1.0f));
150
151 Image *image = (Image *)node_->id;
152 if (!image) {
153 return res;
154 }
155
156 NodeTexEnvironment *tex_env = static_cast<NodeTexEnvironment *>(node_->storage);
157
158 std::string image_path = image->id.name;
159 if (graph_.export_params.image_fn) {
160 Scene *scene = DEG_get_input_scene(graph_.depsgraph);
161 Main *bmain = DEG_get_bmain(graph_.depsgraph);
162 image_path = graph_.export_params.image_fn(bmain, scene, image, &tex_env->iuser);
163 }
164
165 NodeItem vector = get_input_link("Vector", NodeItem::Type::Vector2);
166 if (!vector) {
167 vector = texcoord_node();
168 }
169 /* TODO: texture-coordinates should be translated to spherical coordinates. */
170
171 std::string filtertype;
172 switch (tex_env->interpolation) {
174 filtertype = "linear";
175 break;
177 filtertype = "closest";
178 break;
179 case SHD_INTERP_CUBIC:
180 case SHD_INTERP_SMART:
181 filtertype = "cubic";
182 break;
183 default:
185 }
186
187 res = create_node("image", NodeItem::Type::Color4);
188 res.set_input("file", image_path, NodeItem::Type::Filename);
189 res.set_input("texcoord", vector);
190 res.set_input("filtertype", val(filtertype));
191
192 return res;
193}
194#endif
196
197} // namespace nodes::node_shader_tex_environment_cc
198
199/* node type definition */
201{
202 namespace file_ns = nodes::node_shader_tex_environment_cc;
203
204 static bke::bNodeType ntype;
205
206 sh_node_type_base(&ntype, "ShaderNodeTexEnvironment"_ustr, SH_NODE_TEX_ENVIRONMENT);
207 ntype.ui_name = "Environment Texture";
208 ntype.ui_description =
209 "Sample an image file as an environment texture. Typically used to light the scene with the "
210 "background node";
211 ntype.enum_name_legacy = "TEX_ENVIRONMENT";
213 ntype.declare = file_ns::node_declare;
214 ntype.initfunc = file_ns::node_shader_init_tex_environment;
216 ntype, "NodeTexEnvironment", node_free_standard_storage, node_copy_standard_storage);
217 ntype.gpu_fn = file_ns::node_shader_gpu_tex_environment;
220 ntype.materialx_fn = file_ns::node_shader_materialx;
221
223}
224
225} // namespace blender
#define NODE_CLASS_TEXTURE
Definition BKE_node.hh:488
#define SH_NODE_TEX_ENVIRONMENT
#define BLI_assert_unreachable()
#define ELEM(...)
#define sampler(slot)
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_declare(NodeDeclarationBuilder &b)
static int node_shader_gpu_tex_environment(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void node_shader_init_tex_environment(bNodeTree *, bNode *node)
BMesh Iterators.
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
void register_node_type_sh_tex_environment()
@ IMA_ALPHA_CHANNEL_PACKED
GPUNodeLink * GPU_image(GPUMaterial *mat, Image *ima, ImageUser *iuser, GPUSamplerState sampler_state)
@ GPU_SAMPLER_EXTEND_MODE_REPEAT
@ GPU_SAMPLER_EXTEND_MODE_EXTEND
@ GPU_SAMPLER_FILTERING_LINEAR
@ GPU_SAMPLER_FILTERING_MIPMAP
void sh_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
bool GPU_link(GPUMaterial *mat, const char *name,...)
void node_shader_gpu_tex_mapping(GPUMaterial *mat, bNode *node, GPUNodeStack *in, GPUNodeStack *)
Main * DEG_get_bmain(const Depsgraph *graph)
@ SHD_PROJ_EQUIRECTANGULAR
void BKE_texture_mapping_default(struct TexMapping *texmap, int type)
Definition texture.cc:234
void node_shader_gpu_bump_tex_coord(GPUMaterial *mat, bNode *, GPUNodeLink **link)
GPUNodeLink * GPU_shading_data()
Scene * DEG_get_input_scene(const Depsgraph *graph)
GPUNodeLink * GPU_kernel_globals()
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
void BKE_texture_colormapping_default(struct ColorMapping *colormap)
Definition texture.cc:337
bool IMB_colormanagement_space_name_is_data(const char *name)
void node_image_label(const bNodeTree *, const bNode *node, char *label, int label_maxncpy)
Definition node_util.cc:146
void BKE_imageuser_default(ImageUser *iuser)
#define NODE_SHADER_MATERIALX_BEGIN
#define NODE_SHADER_MATERIALX_END
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
void(*) labelfunc(const bNodeTree *ntree, const bNode *node, char *label, int label_maxncpy)
Definition BKE_node.hh:301
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:373
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