Blender V5.0
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
5#include "node_shader_util.hh"
6#include "node_util.hh"
7
8#include "BKE_image.hh"
9#include "BKE_node_runtime.hh"
10#include "BKE_texture.h"
11
13
15
17
19{
20 b.add_input<decl::Vector>("Vector").hide_value();
21 b.add_output<decl::Color>("Color").no_muted_links();
22}
23
34
36 bNode *node,
37 bNodeExecData * /*execdata*/,
40{
41 Image *ima = (Image *)node->id;
43
44 /* We get the image user from the original node, since GPU image keeps
45 * a pointer to it and the dependency refreshes the original. */
46 bNode *node_original = node->runtime->original ? node->runtime->original : node;
47 NodeTexImage *tex_original = (NodeTexImage *)node_original->storage;
48 ImageUser *iuser = &tex_original->iuser;
52 /* TODO(@fclem): For now assume mipmap is always enabled. */
53 if (true) {
55 }
56
57 GPUNodeLink *outalpha;
58
59 if (!ima) {
60 return GPU_stack_link(mat, node, "node_tex_environment_empty", in, out);
61 }
62
63 if (!in[0].link) {
64 GPU_link(mat, "node_tex_coord_position", &in[0].link);
65 node_shader_gpu_bump_tex_coord(mat, node, &in[0].link);
66 }
67
69
70 /* Compute texture coordinate. */
72 GPU_link(mat, "node_tex_environment_equirectangular", in[0].link, &in[0].link);
73 /* To fix pole issue we clamp the v coordinate. */
75 /* Force the highest mipmap and don't do anisotropic filtering.
76 * This is to fix the artifact caused by derivatives discontinuity. */
79 }
80 else {
81 GPU_link(mat, "node_tex_environment_mirror_ball", in[0].link, &in[0].link);
82 /* Fix pole issue. */
85 }
86
87 const char *gpu_fn;
88 static const char *names[] = {
89 "node_tex_image_linear",
90 "node_tex_image_cubic",
91 };
92
93 switch (tex->interpolation) {
95 gpu_fn = names[0];
96 break;
99 gpu_fn = names[0];
100 break;
101 default:
102 gpu_fn = names[1];
103 break;
104 }
105
106 /* Sample texture with correct interpolation. */
107 GPU_link(mat, gpu_fn, in[0].link, GPU_image(mat, ima, iuser, sampler), &out[0].link, &outalpha);
108
109 if (out[0].hasoutput && ima) {
112 {
113 /* Don't let alpha affect color output in these cases. */
114 GPU_link(mat, "color_alpha_clear", out[0].link, &out[0].link);
115 }
116 else {
117 /* Always output with premultiplied alpha. */
118 if (ima->alpha_mode == IMA_ALPHA_PREMUL) {
119 GPU_link(mat, "color_alpha_clear", out[0].link, &out[0].link);
120 }
121 else {
122 GPU_link(mat, "color_alpha_premultiply", out[0].link, &out[0].link);
123 }
124 }
125 }
126
127 return true;
128}
129
131#ifdef WITH_MATERIALX
132{
133 NodeItem res = val(MaterialX::Color4(1.0f, 0.0f, 1.0f, 1.0f));
134
135 Image *image = (Image *)node_->id;
136 if (!image) {
137 return res;
138 }
139
140 NodeTexEnvironment *tex_env = static_cast<NodeTexEnvironment *>(node_->storage);
141
142 std::string image_path = image->id.name;
143 if (graph_.export_params.image_fn) {
144 Scene *scene = DEG_get_input_scene(graph_.depsgraph);
145 Main *bmain = DEG_get_bmain(graph_.depsgraph);
146 image_path = graph_.export_params.image_fn(bmain, scene, image, &tex_env->iuser);
147 }
148
149 NodeItem vector = get_input_link("Vector", NodeItem::Type::Vector2);
150 if (!vector) {
151 vector = texcoord_node();
152 }
153 /* TODO: texture-coordinates should be translated to spherical coordinates. */
154
155 std::string filtertype;
156 switch (tex_env->interpolation) {
158 filtertype = "linear";
159 break;
161 filtertype = "closest";
162 break;
163 case SHD_INTERP_CUBIC:
164 case SHD_INTERP_SMART:
165 filtertype = "cubic";
166 break;
167 default:
169 }
170
171 res = create_node("image", NodeItem::Type::Color4);
172 res.set_input("file", image_path, NodeItem::Type::Filename);
173 res.set_input("texcoord", vector);
174 res.set_input("filtertype", val(filtertype));
175
176 return res;
177}
178#endif
180
181} // namespace blender::nodes::node_shader_tex_environment_cc
182
183/* node type definition */
185{
187
188 static blender::bke::bNodeType ntype;
189
190 sh_node_type_base(&ntype, "ShaderNodeTexEnvironment", SH_NODE_TEX_ENVIRONMENT);
191 ntype.ui_name = "Environment Texture";
192 ntype.ui_description =
193 "Sample an image file as an environment texture. Typically used to light the scene with the "
194 "background node";
195 ntype.enum_name_legacy = "TEX_ENVIRONMENT";
197 ntype.declare = file_ns::node_declare;
198 ntype.initfunc = file_ns::node_shader_init_tex_environment;
200 ntype, "NodeTexEnvironment", node_free_standard_storage, node_copy_standard_storage);
201 ntype.gpu_fn = file_ns::node_shader_gpu_tex_environment;
204 ntype.materialx_fn = file_ns::node_shader_materialx;
205
207}
void BKE_imageuser_default(ImageUser *iuser)
#define NODE_CLASS_TEXTURE
Definition BKE_node.hh:457
#define SH_NODE_TEX_ENVIRONMENT
void BKE_texture_mapping_default(struct TexMapping *texmap, int type)
Definition texture.cc:234
void BKE_texture_colormapping_default(struct ColorMapping *colormap)
Definition texture.cc:337
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
#define ELEM(...)
Main * DEG_get_bmain(const Depsgraph *graph)
Scene * DEG_get_input_scene(const Depsgraph *graph)
@ IMA_ALPHA_IGNORE
@ IMA_ALPHA_PREMUL
@ IMA_ALPHA_CHANNEL_PACKED
@ SHD_PROJ_EQUIRECTANGULAR
@ SHD_INTERP_LINEAR
@ SHD_INTERP_SMART
@ SHD_INTERP_CUBIC
@ SHD_INTERP_CLOSEST
@ TEXMAP_TYPE_POINT
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
GPUNodeLink * GPU_image(GPUMaterial *mat, Image *ima, ImageUser *iuser, GPUSamplerState sampler_state)
bool GPU_link(GPUMaterial *mat, const char *name,...)
@ GPU_SAMPLER_EXTEND_MODE_REPEAT
@ GPU_SAMPLER_EXTEND_MODE_EXTEND
@ GPU_SAMPLER_FILTERING_MIPMAP
@ GPU_SAMPLER_FILTERING_ANISOTROPIC
@ GPU_SAMPLER_FILTERING_LINEAR
bool IMB_colormanagement_space_name_is_data(const char *name)
#define in
#define out
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
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_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)
#define NODE_SHADER_MATERIALX_BEGIN
#define NODE_SHADER_MATERIALX_END
void register_node_type_sh_tex_environment()
void node_shader_gpu_tex_mapping(GPUMaterial *mat, bNode *node, GPUNodeStack *in, GPUNodeStack *)
void sh_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
void node_shader_gpu_bump_tex_coord(GPUMaterial *mat, bNode *, GPUNodeLink **link)
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
void node_image_label(const bNodeTree *, const bNode *node, char *label, int label_maxncpy)
Definition node_util.cc:189
GPUSamplerExtendMode extend_yz
void enable_filtering_flag(GPUSamplerFiltering filtering_flags)
void disable_filtering_flag(GPUSamplerFiltering filtering_flags)
GPUSamplerExtendMode extend_x
ColorManagedColorspaceSettings colorspace_settings
char alpha_mode
TexMapping tex_mapping
ColorMapping color_mapping
struct ID * id
bNodeRuntimeHandle * runtime
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
void(* labelfunc)(const bNodeTree *ntree, const bNode *node, char *label, int label_maxncpy)
Definition BKE_node.hh:270
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:342
const char * enum_name_legacy
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:362