Blender  V2.93
node_geo_attribute_sample_texture.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #include "BLI_compiler_attrs.h"
18 
19 #include "DNA_texture_types.h"
20 
21 #include "BKE_texture.h"
22 
23 #include "RE_texture.h"
24 
25 #include "UI_interface.h"
26 #include "UI_resources.h"
27 
28 #include "node_geometry_util.hh"
29 
31  {SOCK_GEOMETRY, N_("Geometry")},
32  {SOCK_STRING, N_("Mapping")},
33  {SOCK_STRING, N_("Result")},
34  {-1, ""},
35 };
36 
38  {SOCK_GEOMETRY, N_("Geometry")},
39  {-1, ""},
40 };
41 
43  bContext *C,
44  PointerRNA *ptr)
45 {
46  uiTemplateID(layout, C, ptr, "texture", "texture.new", nullptr, nullptr, 0, ICON_NONE, nullptr);
47 }
48 
49 namespace blender::nodes {
50 
52  StringRef result_attribute_name,
53  StringRef map_attribute_name)
54 {
55  /* Use the domain of the result attribute if it already exists. */
56  ReadAttributePtr result_attribute = component.attribute_try_get_for_read(result_attribute_name);
57  if (result_attribute) {
58  return result_attribute->domain();
59  }
60 
61  /* Otherwise use the name of the map attribute. */
62  ReadAttributePtr map_attribute = component.attribute_try_get_for_read(map_attribute_name);
63  if (map_attribute) {
64  return map_attribute->domain();
65  }
66 
67  /* The node won't execute in this case, but we still have to return a value. */
68  return ATTR_DOMAIN_POINT;
69 }
70 
72 {
73  const bNode &node = params.node();
74  Tex *texture = reinterpret_cast<Tex *>(node.id);
75  if (texture == nullptr) {
76  return;
77  }
78 
79  const std::string result_attribute_name = params.get_input<std::string>("Result");
80  const std::string mapping_name = params.get_input<std::string>("Mapping");
81  if (!component.attribute_exists(mapping_name)) {
82  return;
83  }
84 
85  const AttributeDomain result_domain = get_result_domain(
86  component, result_attribute_name, mapping_name);
87 
88  OutputAttributePtr attribute_out = component.attribute_try_get_for_output(
89  result_attribute_name, result_domain, CD_PROP_COLOR);
90  if (!attribute_out) {
91  return;
92  }
93 
94  Float3ReadAttribute mapping_attribute = component.attribute_get_for_read<float3>(
95  mapping_name, result_domain, {0, 0, 0});
96 
97  MutableSpan<Color4f> colors = attribute_out->get_span<Color4f>();
98  for (const int i : IndexRange(mapping_attribute.size())) {
99  TexResult texture_result = {0};
100  const float3 position = mapping_attribute[i];
101  /* For legacy reasons we have to map [0, 1] to [-1, 1] to support uv mappings. */
102  const float3 remapped_position = position * 2.0f - float3(1.0f);
103  BKE_texture_get_value(nullptr, texture, remapped_position, &texture_result, false);
104  colors[i] = {texture_result.tr, texture_result.tg, texture_result.tb, texture_result.ta};
105  }
106  attribute_out.apply_span_and_save();
107 }
108 
110 {
111  GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");
112 
113  geometry_set = geometry_set_realize_instances(geometry_set);
114 
115  if (geometry_set.has<MeshComponent>()) {
117  }
118  if (geometry_set.has<PointCloudComponent>()) {
120  }
121 
122  params.set_output("Geometry", geometry_set);
123 }
124 
125 } // namespace blender::nodes
126 
128 {
129  static bNodeType ntype;
130 
131  geo_node_type_base(&ntype,
133  "Attribute Sample Texture",
135  0);
141  nodeRegisterType(&ntype);
142 }
AttributeDomain
Definition: BKE_attribute.h:41
@ ATTR_DOMAIN_POINT
Definition: BKE_attribute.h:43
void node_type_socket_templates(struct bNodeType *ntype, struct bNodeSocketTemplate *inputs, struct bNodeSocketTemplate *outputs)
Definition: node.cc:4527
void node_type_size_preset(struct bNodeType *ntype, eNodeSizePreset size)
Definition: node.cc:4577
#define NODE_CLASS_ATTRIBUTE
Definition: BKE_node.h:360
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1298
@ NODE_SIZE_LARGE
Definition: BKE_node.h:373
void BKE_texture_get_value(const struct Scene *scene, struct Tex *texture, const float *tex_co, struct TexResult *texres, bool use_color_management)
#define N_(msgid)
static uint8 component(Color32 c, uint i)
Definition: ColorBlock.cpp:126
@ CD_PROP_COLOR
@ SOCK_GEOMETRY
@ SOCK_STRING
Group RGB to Bright Vector Camera Vector Combine Material Light Line Style Layer Add Ambient Diffuse Glossy Refraction Transparent Toon Principled Hair Volume Principled Light Particle Volume Image Sky Noise Wave Voronoi Brick Texture Vector Combine Vertex Separate Vector White RGB Map Separate Set Z Dilate Combine Combine Color Channel Split ID Combine Luminance Directional Alpha Distance Hue Movie Ellipse Bokeh View Corner Anti Mix RGB Hue Separate TEX_NODE_PROC TEX_NODE_PROC TEX_NODE_PROC TEX_NODE_PROC TEX_NODE_PROC Boolean Random Edge Subdivision Point Object Attribute Attribute Attribute Color Attribute Attribute Vector Point GEO_NODE_ATTRIBUTE_SAMPLE_TEXTURE
#define C
Definition: RandGen.cpp:39
void uiTemplateID(uiLayout *layout, const struct bContext *C, struct PointerRNA *ptr, const char *propname, const char *newop, const char *openop, const char *unlinkop, int filter, const bool live_icon, const char *text)
OperationNode * node
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
TypedReadAttribute< float3 > Float3ReadAttribute
GeometrySet geometry_set_realize_instances(const GeometrySet &geometry_set)
std::unique_ptr< ReadAttribute > ReadAttributePtr
static void geo_node_attribute_sample_texture_exec(GeoNodeExecParams params)
static void execute_on_component(const GeoNodeExecParams &params, GeometryComponent &component)
static AttributeDomain get_result_domain(const GeometryComponent &component, StringRef source_name, StringRef result_name)
static bNodeSocketTemplate geo_node_attribute_sample_texture_out[]
static bNodeSocketTemplate geo_node_attribute_sample_texture_in[]
void register_node_type_geo_sample_texture()
static void geo_node_attribute_sample_texture_layout(uiLayout *layout, bContext *C, PointerRNA *ptr)
void geo_node_type_base(bNodeType *ntype, int type, const char *name, short nclass, short flag)
GeometryComponent & get_component_for_write(GeometryComponentType component_type)
bool has(const GeometryComponentType component_type) const
float tb
Definition: RE_texture.h:84
float ta
Definition: RE_texture.h:84
float tr
Definition: RE_texture.h:84
float tg
Definition: RE_texture.h:84
Compact definition of a node socket.
Definition: BKE_node.h:95
Defines a node type.
Definition: BKE_node.h:221
NodeGeometryExecFunction geometry_node_execute
Definition: BKE_node.h:327
void(* draw_buttons)(struct uiLayout *, struct bContext *C, struct PointerRNA *ptr)
Definition: BKE_node.h:253
PointerRNA * ptr
Definition: wm_files.c:3157