Blender V4.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
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
24static void node_shader_init_tex_environment(bNodeTree * /*ntree*/, bNode *node)
25{
26 NodeTexEnvironment *tex = MEM_cnew<NodeTexEnvironment>("NodeTexEnvironment");
31
32 node->storage = tex;
33}
34
36 bNode *node,
37 bNodeExecData * /*execdata*/,
38 GPUNodeStack *in,
39 GPUNodeStack *out)
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) {
54 sampler.enable_filtering_flag(GPU_SAMPLER_FILTERING_MIPMAP);
55 }
56
57 GPUNodeLink *outalpha;
58
59 /* HACK(@fclem): For lookdev mode: do not compile an empty environment and just create an empty
60 * texture entry point. We manually bind to it after #DRW_shgroup_add_material_resources(). */
62 return GPU_stack_link(mat, node, "node_tex_environment_empty", in, out);
63 }
64
65 if (!in[0].link) {
66 GPU_link(mat, "node_tex_coord_position", &in[0].link);
67 node_shader_gpu_bump_tex_coord(mat, node, &in[0].link);
68 }
69
70 node_shader_gpu_tex_mapping(mat, node, in, out);
71
72 /* Compute texture coordinate. */
74 GPU_link(mat, "node_tex_environment_equirectangular", in[0].link, &in[0].link);
75 /* To fix pole issue we clamp the v coordinate. */
77 /* Force the highest mipmap and don't do anisotropic filtering.
78 * This is to fix the artifact caused by derivatives discontinuity. */
79 sampler.disable_filtering_flag(GPU_SAMPLER_FILTERING_MIPMAP |
81 }
82 else {
83 GPU_link(mat, "node_tex_environment_mirror_ball", in[0].link, &in[0].link);
84 /* Fix pole issue. */
87 }
88
89 const char *gpu_fn;
90 static const char *names[] = {
91 "node_tex_image_linear",
92 "node_tex_image_cubic",
93 };
94
95 switch (tex->interpolation) {
97 gpu_fn = names[0];
98 break;
101 gpu_fn = names[0];
102 break;
103 default:
104 gpu_fn = names[1];
105 break;
106 }
107
108 /* Sample texture with correct interpolation. */
109 GPU_link(mat, gpu_fn, in[0].link, GPU_image(mat, ima, iuser, sampler), &out[0].link, &outalpha);
110
111 if (out[0].hasoutput && ima) {
114 {
115 /* Don't let alpha affect color output in these cases. */
116 GPU_link(mat, "color_alpha_clear", out[0].link, &out[0].link);
117 }
118 else {
119 /* Always output with premultiplied alpha. */
120 if (ima->alpha_mode == IMA_ALPHA_PREMUL) {
121 GPU_link(mat, "color_alpha_clear", out[0].link, &out[0].link);
122 }
123 else {
124 GPU_link(mat, "color_alpha_premultiply", out[0].link, &out[0].link);
125 }
126 }
127 }
128
129 return true;
130}
131
133#ifdef WITH_MATERIALX
134{
135 NodeItem res = val(MaterialX::Color4(1.0f, 0.0f, 1.0f, 1.0f));
136
137 Image *image = (Image *)node_->id;
138 if (!image) {
139 return res;
140 }
141
142 NodeTexEnvironment *tex_env = static_cast<NodeTexEnvironment *>(node_->storage);
143
144 std::string image_path = image->id.name;
145 if (export_params_.image_fn) {
146 Scene *scene = DEG_get_input_scene(depsgraph_);
147 Main *bmain = DEG_get_bmain(depsgraph_);
148 image_path = export_params_.image_fn(bmain, scene, image, &tex_env->iuser);
149 }
150
151 NodeItem vector = get_input_link("Vector", NodeItem::Type::Vector2);
152 if (!vector) {
153 vector = texcoord_node();
154 }
155 /* TODO: texture-coordinates should be translated to spherical coordinates. */
156
157 std::string filtertype;
158 switch (tex_env->interpolation) {
160 filtertype = "linear";
161 break;
163 filtertype = "closest";
164 break;
165 case SHD_INTERP_CUBIC:
166 case SHD_INTERP_SMART:
167 filtertype = "cubic";
168 break;
169 default:
171 }
172
173 res = create_node("image", NodeItem::Type::Color4);
174 res.set_input("file", image_path, NodeItem::Type::Filename);
175 res.set_input("texcoord", vector);
176 res.set_input("filtertype", val(filtertype));
177
178 return res;
179}
180#endif
182
183} // namespace blender::nodes::node_shader_tex_environment_cc
184
185/* node type definition */
187{
189
190 static blender::bke::bNodeType ntype;
191
192 sh_node_type_base(&ntype, SH_NODE_TEX_ENVIRONMENT, "Environment Texture", NODE_CLASS_TEXTURE);
193 ntype.declare = file_ns::node_declare;
194 ntype.initfunc = file_ns::node_shader_init_tex_environment;
196 &ntype, "NodeTexEnvironment", node_free_standard_storage, node_copy_standard_storage);
197 ntype.gpu_fn = file_ns::node_shader_gpu_tex_environment;
200 ntype.materialx_fn = file_ns::node_shader_materialx;
201
203}
void BKE_imageuser_default(ImageUser *iuser)
#define SH_NODE_TEX_ENVIRONMENT
Definition BKE_node.hh:940
#define NODE_CLASS_TEXTURE
Definition BKE_node.hh:414
void BKE_texture_mapping_default(struct TexMapping *texmap, int type)
Definition texture.cc:247
void BKE_texture_colormapping_default(struct ColorMapping *colormap)
Definition texture.cc:350
#define BLI_assert_unreachable()
Definition BLI_assert.h:97
#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,...)
bool GPU_material_flag_get(const GPUMaterial *mat, eGPUMaterialFlag flag)
@ GPU_MATFLAG_LOOKDEV_HACK
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)
in reality light always falls off quadratically Particle Retrieve the data of the particle that spawned the object for example to give variation to multiple instances of an object Point Retrieve information about points in a point cloud Retrieve the edges of an object as it appears to Cycles topology will always appear triangulated Convert a blackbody temperature to an RGB value Normal Generate a perturbed normal from an RGB normal map image Typically used for faking highly detailed surfaces Generate an OSL shader from a file or text data block Image Sample an image file as a texture Gabor Generate Gabor noise Gradient Generate interpolated color and intensity values based on the input vector Magic Generate a psychedelic color texture Voronoi Generate Worley noise based on the distance to random points Typically used to generate textures such as or biological cells Brick Generate a procedural texture producing bricks Texture Retrieve multiple types of texture coordinates nTypically used as inputs for texture nodes Vector Convert a vector
input_tx image(0, GPU_RGBA16F, Qualifier::WRITE, ImageType::FLOAT_2D, "preview_img") .compute_source("compositor_compute_preview.glsl") .do_static_compilation(true)
local_group_size(16, 16) .push_constant(Type local_group_size(16, 16) .push_constant(Type input_tx sampler(1, ImageType::FLOAT_2D, "matte_tx") .image(0
local_group_size(16, 16) .push_constant(Type b
void node_type_size_preset(bNodeType *ntype, eNodeSizePreset size)
Definition node.cc:4614
void node_type_storage(bNodeType *ntype, const char *storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:4632
void node_register_type(bNodeType *ntype)
Definition node.cc:1708
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 sh_node_type_base(blender::bke::bNodeType *ntype, int type, const char *name, short nclass)
void node_shader_gpu_tex_mapping(GPUMaterial *mat, bNode *node, GPUNodeStack *in, GPUNodeStack *)
void node_shader_gpu_bump_tex_coord(GPUMaterial *mat, bNode *, GPUNodeLink **link)
void node_free_standard_storage(bNode *node)
Definition node_util.cc:46
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:58
void node_image_label(const bNodeTree *, const bNode *node, char *label, int label_maxncpy)
Definition node_util.cc:193
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:218
NodeMaterialXFunction materialx_fn
Definition BKE_node.hh:320
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:267
void(* labelfunc)(const bNodeTree *ntree, const bNode *node, char *label, int label_maxncpy)
Definition BKE_node.hh:249
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:318
NodeDeclareFunction declare
Definition BKE_node.hh:347