[Bf-blender-cvs] [acc18531424] temp-geometry-nodes-sample-texture: support sampling texture based on point positions
Jacques Lucke
noreply at git.blender.org
Tue Dec 15 15:20:31 CET 2020
Commit: acc18531424a4c0e7e8561ffda146a4485fb5ce0
Author: Jacques Lucke
Date: Tue Dec 15 15:20:04 2020 +0100
Branches: temp-geometry-nodes-sample-texture
https://developer.blender.org/rBacc18531424a4c0e7e8561ffda146a4485fb5ce0
support sampling texture based on point positions
===================================================================
M source/blender/editors/space_node/drawnode.c
M source/blender/makesdna/DNA_node_types.h
M source/blender/makesrna/intern/rna_nodetree.c
M source/blender/nodes/geometry/nodes/node_geo_sample_texture.cc
===================================================================
diff --git a/source/blender/editors/space_node/drawnode.c b/source/blender/editors/space_node/drawnode.c
index dc36b2d3788..b10962e8d3b 100644
--- a/source/blender/editors/space_node/drawnode.c
+++ b/source/blender/editors/space_node/drawnode.c
@@ -3212,6 +3212,7 @@ static void node_geometry_buts_sample_texture(uiLayout *layout,
bContext *UNUSED(C),
PointerRNA *ptr)
{
+ uiItemR(layout, ptr, "mode", DEFAULT_FLAGS, "", ICON_NONE);
uiItemR(layout, ptr, "texture", DEFAULT_FLAGS, "", ICON_NONE);
}
diff --git a/source/blender/makesdna/DNA_node_types.h b/source/blender/makesdna/DNA_node_types.h
index d512d8ffe34..af73cbdec66 100644
--- a/source/blender/makesdna/DNA_node_types.h
+++ b/source/blender/makesdna/DNA_node_types.h
@@ -1501,6 +1501,11 @@ typedef enum GeometryNodeAttributeInputMode {
GEO_NODE_ATTRIBUTE_INPUT_COLOR = 3,
} GeometryNodeAttributeInputMode;
+typedef enum GeometryNodeSampleTextureMode {
+ GEO_NODE_SAMPLE_TEXTURE_MODE_POSITION = 0,
+ GEO_NODE_SAMPLE_TEXTURE_MODE_UV = 1,
+} GeometryNodeSampleTextureMode;
+
#ifdef __cplusplus
}
#endif
diff --git a/source/blender/makesrna/intern/rna_nodetree.c b/source/blender/makesrna/intern/rna_nodetree.c
index 2bb7b67cd92..9736039d8ab 100644
--- a/source/blender/makesrna/intern/rna_nodetree.c
+++ b/source/blender/makesrna/intern/rna_nodetree.c
@@ -8483,6 +8483,20 @@ static void def_geo_attribute_mix(StructRNA *srna)
static void def_geo_sample_texture(StructRNA *srna)
{
+ static const EnumPropertyItem instance_type_items[] = {
+ {GEO_NODE_SAMPLE_TEXTURE_MODE_POSITION,
+ "POSITION",
+ ICON_NONE,
+ "Position",
+ "Use the position attribute to sample the texture"},
+ {GEO_NODE_SAMPLE_TEXTURE_MODE_UV,
+ "UV",
+ ICON_NONE,
+ "UV",
+ "Use a UV map attribute to sample the texture"},
+ {0, NULL, 0, NULL, NULL},
+ };
+
PropertyRNA *prop;
prop = RNA_def_property(srna, "texture", PROP_POINTER, PROP_NONE);
@@ -8492,6 +8506,12 @@ static void def_geo_sample_texture(StructRNA *srna)
RNA_def_property_override_flag(prop, PROPOVERRIDE_OVERRIDABLE_LIBRARY);
RNA_def_property_ui_text(prop, "Texture", "Texture to sample values from");
RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_update_relations");
+
+ prop = RNA_def_property(srna, "mode", PROP_ENUM, PROP_NONE);
+ RNA_def_property_enum_sdna(prop, NULL, "custom1");
+ RNA_def_property_enum_items(prop, instance_type_items);
+ RNA_def_property_ui_text(prop, "Mode", "");
+ RNA_def_property_update(prop, NC_NODE | NA_EDITED, "rna_Node_socket_update");
}
/* -------------------------------------------------------------------------- */
diff --git a/source/blender/nodes/geometry/nodes/node_geo_sample_texture.cc b/source/blender/nodes/geometry/nodes/node_geo_sample_texture.cc
index bd2f1ab661e..edca8a50dfb 100644
--- a/source/blender/nodes/geometry/nodes/node_geo_sample_texture.cc
+++ b/source/blender/nodes/geometry/nodes/node_geo_sample_texture.cc
@@ -14,6 +14,14 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
+#include "BLI_compiler_attrs.h"
+
+#include "DNA_texture_types.h"
+
+#include "BKE_texture.h"
+
+#include "RE_texture.h"
+
#include "node_geometry_util.hh"
static bNodeSocketTemplate geo_node_sample_texture_in[] = {
@@ -29,9 +37,56 @@ static bNodeSocketTemplate geo_node_sample_texture_out[] = {
namespace blender::nodes {
+static void execute_on_component(GeometryComponent &component, const GeoNodeExecParams ¶ms)
+{
+ const bNode &node = params.node();
+ Tex *texture = reinterpret_cast<Tex *>(node.id);
+ const GeometryNodeSampleTextureMode mode = static_cast<GeometryNodeSampleTextureMode>(
+ node.custom1);
+ const std::string result_attribute_name = params.get_input<std::string>("Result");
+
+ if (texture == nullptr) {
+ return;
+ }
+
+ WriteAttributePtr attribute_out = component.attribute_try_ensure_for_write(
+ result_attribute_name, ATTR_DOMAIN_POINT, CD_PROP_COLOR);
+ if (!attribute_out) {
+ return;
+ }
+
+ MutableSpan<Color4f> colors = attribute_out->get_span().typed<Color4f>();
+
+ const int tot_points = component.attribute_domain_size(ATTR_DOMAIN_POINT);
+ if (mode == GEO_NODE_SAMPLE_TEXTURE_MODE_POSITION) {
+ Float3ReadAttribute attribute_position = component.attribute_get_for_read<float3>(
+ "position", ATTR_DOMAIN_POINT, {0, 0, 0});
+
+ for (const int i : IndexRange(tot_points)) {
+ TexResult texture_result = {0};
+ const float3 position = attribute_position[i];
+ BKE_texture_get_value(nullptr, texture, position, &texture_result, false);
+ colors[i] = {texture_result.tr, texture_result.tg, texture_result.tb, texture_result.ta};
+ }
+ }
+ if (mode == GEO_NODE_SAMPLE_TEXTURE_MODE_UV) {
+ colors.fill({0, 0, 0, 1});
+ }
+
+ attribute_out->apply_span();
+}
+
static void geo_node_sample_texture_exec(GeoNodeExecParams params)
{
GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");
+
+ if (geometry_set.has<MeshComponent>()) {
+ execute_on_component(geometry_set.get_component_for_write<MeshComponent>(), params);
+ }
+ if (geometry_set.has<PointCloudComponent>()) {
+ execute_on_component(geometry_set.get_component_for_write<PointCloudComponent>(), params);
+ }
+
params.set_output("Geometry", geometry_set);
}
} // namespace blender::nodes
More information about the Bf-blender-cvs
mailing list