Changeset View
Changeset View
Standalone View
Standalone View
source/blender/nodes/geometry/nodes/node_geo_sample_uv_surface.cc
| /* SPDX-License-Identifier: GPL-2.0-or-later */ | /* SPDX-License-Identifier: GPL-2.0-or-later */ | ||||
| #include "BKE_attribute.hh" | |||||
| #include "BKE_attribute_math.hh" | #include "BKE_attribute_math.hh" | ||||
| #include "BKE_mesh.h" | #include "BKE_mesh.h" | ||||
| #include "BKE_type_conversions.hh" | #include "BKE_type_conversions.hh" | ||||
| #include "UI_interface.h" | #include "UI_interface.h" | ||||
| #include "UI_resources.h" | #include "UI_resources.h" | ||||
| #include "GEO_reverse_uv_sampler.hh" | #include "GEO_reverse_uv_sampler.hh" | ||||
| ▲ Show 20 Lines • Show All 182 Lines • ▼ Show 20 Lines | void evaluate_source() | ||||
| source_evaluator_->evaluate(); | source_evaluator_->evaluate(); | ||||
| source_uv_map_ = source_evaluator_->get_evaluated<float2>(0); | source_uv_map_ = source_evaluator_->get_evaluated<float2>(0); | ||||
| source_data_ = &source_evaluator_->get_evaluated(1); | source_data_ = &source_evaluator_->get_evaluated(1); | ||||
| reverse_uv_sampler_.emplace(source_uv_map_, mesh.looptris()); | reverse_uv_sampler_.emplace(source_uv_map_, mesh.looptris()); | ||||
| } | } | ||||
| }; | }; | ||||
| static GField get_input_attribute_field(GeoNodeExecParams ¶ms, const eCustomDataType data_type) | |||||
| { | |||||
| switch (data_type) { | |||||
| case CD_PROP_FLOAT: | |||||
| return params.extract_input<Field<float>>("Value_Float"); | |||||
| case CD_PROP_FLOAT3: | |||||
| return params.extract_input<Field<float3>>("Value_Vector"); | |||||
| case CD_PROP_COLOR: | |||||
| return params.extract_input<Field<ColorGeometry4f>>("Value_Color"); | |||||
| case CD_PROP_BOOL: | |||||
| return params.extract_input<Field<bool>>("Value_Bool"); | |||||
| case CD_PROP_INT32: | |||||
| return params.extract_input<Field<int>>("Value_Int"); | |||||
| default: | |||||
| BLI_assert_unreachable(); | |||||
| } | |||||
| return {}; | |||||
| } | |||||
| static void output_attribute_field(GeoNodeExecParams ¶ms, GField field) | |||||
| { | |||||
| switch (bke::cpp_type_to_custom_data_type(field.cpp_type())) { | |||||
| case CD_PROP_FLOAT: { | |||||
| params.set_output("Value_Float", Field<float>(field)); | |||||
| break; | |||||
| } | |||||
| case CD_PROP_FLOAT3: { | |||||
| params.set_output("Value_Vector", Field<float3>(field)); | |||||
| break; | |||||
| } | |||||
| case CD_PROP_COLOR: { | |||||
| params.set_output("Value_Color", Field<ColorGeometry4f>(field)); | |||||
| break; | |||||
| } | |||||
| case CD_PROP_BOOL: { | |||||
| params.set_output("Value_Bool", Field<bool>(field)); | |||||
| break; | |||||
| } | |||||
| case CD_PROP_INT32: { | |||||
| params.set_output("Value_Int", Field<int>(field)); | |||||
| break; | |||||
| } | |||||
| default: | |||||
| break; | |||||
| } | |||||
| } | |||||
| static void node_geo_exec(GeoNodeExecParams params) | static void node_geo_exec(GeoNodeExecParams params) | ||||
| { | { | ||||
| GeometrySet geometry = params.extract_input<GeometrySet>("Mesh"); | GeometrySet geometry = params.extract_input<GeometrySet>("Mesh"); | ||||
| const eCustomDataType data_type = eCustomDataType(params.node().custom1); | const eCustomDataType data_type = eCustomDataType(params.node().custom1); | ||||
| const Mesh *mesh = geometry.get_mesh_for_read(); | const Mesh *mesh = geometry.get_mesh_for_read(); | ||||
| if (mesh == nullptr) { | if (mesh == nullptr) { | ||||
| params.set_default_remaining_outputs(); | params.set_default_remaining_outputs(); | ||||
| return; | return; | ||||
| } | } | ||||
| if (mesh->totpoly == 0 && mesh->totvert != 0) { | if (mesh->totpoly == 0 && mesh->totvert != 0) { | ||||
| params.error_message_add(NodeWarningType::Error, TIP_("The source mesh must have faces")); | params.error_message_add(NodeWarningType::Error, TIP_("The source mesh must have faces")); | ||||
| params.set_default_remaining_outputs(); | params.set_default_remaining_outputs(); | ||||
| return; | return; | ||||
| } | } | ||||
| const std::string identifier = "Value_" + bke::type_name_identifier(data_type); | |||||
| const CPPType &float2_type = CPPType::get<float2>(); | const CPPType &float2_type = CPPType::get<float2>(); | ||||
| const bke::DataTypeConversions &conversions = bke::get_implicit_type_conversions(); | const bke::DataTypeConversions &conversions = bke::get_implicit_type_conversions(); | ||||
| Field<float2> source_uv_map = conversions.try_convert( | Field<float2> source_uv_map = conversions.try_convert( | ||||
| params.extract_input<Field<float3>>("Source UV Map"), float2_type); | params.extract_input<Field<float3>>("Source UV Map"), float2_type); | ||||
| GField field = get_input_attribute_field(params, data_type); | GField field; | ||||
| attribute_math::convert_to_static_type(data_type, [&](auto dummy) { | |||||
| using T = decltype(dummy); | |||||
| field = params.get_input<Field<T>>(identifier); | |||||
| }); | |||||
| Field<float2> sample_uvs = conversions.try_convert( | Field<float2> sample_uvs = conversions.try_convert( | ||||
| params.extract_input<Field<float3>>("Sample UV"), float2_type); | params.extract_input<Field<float3>>("Sample UV"), float2_type); | ||||
| auto fn = std::make_shared<SampleUVSurfaceFunction>( | auto fn = std::make_shared<SampleUVSurfaceFunction>( | ||||
| std::move(geometry), std::move(source_uv_map), std::move(field)); | std::move(geometry), std::move(source_uv_map), std::move(field)); | ||||
| auto op = FieldOperation::Create(std::move(fn), {std::move(sample_uvs)}); | auto op = FieldOperation::Create(std::move(fn), {std::move(sample_uvs)}); | ||||
| output_attribute_field(params, GField(op, 0)); | attribute_math::convert_to_static_type(data_type, [&](auto dummy) { | ||||
| using T = decltype(dummy); | |||||
| params.set_output(identifier, Field<T>(op, 0)); | |||||
| }); | |||||
| params.set_output("Is Valid", Field<bool>(op, 1)); | params.set_output("Is Valid", Field<bool>(op, 1)); | ||||
| } | } | ||||
| } // namespace blender::nodes::node_geo_sample_uv_surface_cc | } // namespace blender::nodes::node_geo_sample_uv_surface_cc | ||||
| void register_node_type_geo_sample_uv_surface() | void register_node_type_geo_sample_uv_surface() | ||||
| { | { | ||||
| namespace file_ns = blender::nodes::node_geo_sample_uv_surface_cc; | namespace file_ns = blender::nodes::node_geo_sample_uv_surface_cc; | ||||
| Show All 12 Lines | |||||