29 .description(
"Texture coordinates from 0 to 1");
33 .inferred_structure_type()
34 .propagate_references();
37 .inferred_structure_type()
38 .propagate_references();
57 const int8_t interpolation_;
69 : interpolation_(interpolation),
70 extension_(extension),
72 image_user_(image_user)
74 static const mf::Signature
signature = []() {
76 mf::SignatureBuilder builder{
"ImageFunction",
signature};
77 builder.single_input<
float3>(
"Vector");
79 builder.single_output<
float>(
"Alpha", mf::ParamFlag::SupportsUnusedOutput);
85 if (image_buffer_ ==
nullptr) {
86 throw std::runtime_error(
"cannot acquire image buffer");
106 return std::clamp(
x, 0, width - 1);
111 const int m = std::abs(
x + (
x < 0)) % (2 * width);
113 return 2 * width - m - 1;
119 const float *buffer,
const int width,
const int height,
const int px,
const int py)
121 if (px < 0 || py < 0 || px >= width || py >= height) {
122 return float4(0.0f, 0.0f, 0.0f, 0.0f);
124 return ((
const float4 *)buffer)[size_t(px) + size_t(py) * size_t(width)];
128 const uint8_t *buffer,
const int width,
const int height,
const int px,
const int py)
130 if (px < 0 || py < 0 || px >= width || py >= height) {
131 return float4(0.0f, 0.0f, 0.0f, 0.0f);
134 const uint8_t *pixel_start = &buffer[(size_t(px) + size_t(py) * size_t(width)) * 4];
139 static float frac(
const float x,
int *ix)
141 const int i = int(
x) - ((
x < 0.0f) ? 1 : 0);
146 template<
typename BufferT>
153 int pix, piy, nix, niy;
154 const float tx =
frac(coord.x *
float(width) - 0.5f, &pix);
155 const float ty =
frac(coord.y *
float(height) - 0.5f, &piy);
156 int ppix, ppiy, nnix, nniy;
202 return float4(0.0f, 0.0f, 0.0f, 0.0f);
205 const int xc[4] = {ppix, pix, nix, nnix};
206 const int yc[4] = {ppiy, piy, niy, nniy};
209 u[0] = (((-1.0f / 6.0f) * tx + 0.5f) * tx - 0.5f) * tx + (1.0f / 6.0f);
210 u[1] = ((0.5f * tx - 1.0f) * tx) * tx + (2.0f / 3.0f);
211 u[2] = ((-0.5f * tx + 0.5f) * tx + 0.5f) * tx + (1.0f / 6.0f);
212 u[3] = (1.0f / 6.0f) * tx * tx * tx;
214 v[0] = (((-1.0f / 6.0f) * ty + 0.5f) * ty - 0.5f) * ty + (1.0f / 6.0f);
215 v[1] = ((0.5f * ty - 1.0f) * ty) * ty + (2.0f / 3.0f);
216 v[2] = ((-0.5f * ty + 0.5f) * ty + 0.5f) * ty + (1.0f / 6.0f);
217 v[3] = (1.0f / 6.0f) * ty * ty * ty;
237 template<
typename BufferT>
244 int pix, piy, nix, niy;
245 const float nfx =
frac(coord.x *
float(width) - 0.5f, &pix);
246 const float nfy =
frac(coord.y *
float(height) - 0.5f, &piy);
276 const float ptx = 1.0f - nfx;
277 const float pty = 1.0f - nfy;
285 template<
typename BufferT>
293 const float tx =
frac(coord.x *
float(width), &ix);
294 const float ty =
frac(coord.y *
float(height), &iy);
303 if (tx < 0.0f || ty < 0.0f || tx > 1.0f || ty > 1.0f) {
304 return float4(0.0f, 0.0f, 0.0f, 0.0f);
306 if (ix < 0 || iy < 0 || ix > width || iy > height) {
307 return float4(0.0f, 0.0f, 0.0f, 0.0f);
322 return float4(0.0f, 0.0f, 0.0f, 0.0f);
326 template<
typename BufferT>
335 switch (interpolation_) {
338 const float3 p = vectors[i];
344 const float3 p = vectors[i];
351 const float3 p = vectors[i];
363 MutableSpan<float> r_alpha = params.uninitialized_single_output_if_required<
float>(2,
"Alpha");
367 const int width = image_buffer_->x;
368 const int height = image_buffer_->y;
370 if (image_buffer_->float_data()) {
371 sample_buffer(image_buffer_->float_data(),
mask, vectors, width, height, color_data);
373 else if (image_buffer_->byte_data()) {
374 sample_buffer(image_buffer_->byte_data(),
mask, vectors, width, height, color_data);
380 int alpha_mode = image_.alpha_mode;
385 switch (alpha_mode) {
401 mask.foreach_index_optimized<
int64_t>([&](
const int64_t i) { color_data[i].w = 1.0f; });
406 if (!r_alpha.is_empty()) {
407 mask.foreach_index_optimized<
int64_t>([&](
const int64_t i) { r_alpha[i] = r_color[i].a; });
415 if (
image ==
nullptr) {
424 image_user.
cycl =
false;
429 std::unique_ptr<ImageFieldsFunction> image_fn;
431 image_fn = std::make_unique<ImageFieldsFunction>(
434 catch (
const std::runtime_error &) {
441 std::string error_message;
445 std::move(image_fn), {&sample_uv}, {&
color, &alpha}, params.
user_data(), error_message))
453 params.
set_output(
"Alpha"_ustr, std::move(alpha));
461 ntype.
ui_name =
"Image Texture";
#define NODE_STORAGE_FUNCS(StorageT)
#define NODE_CLASS_TEXTURE
#define GEO_NODE_IMAGE_TEXTURE
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define BLI_assert_unreachable()
const Signature & signature() const
void set_signature(const Signature *signature)
void error_message_add(const NodeWarningType type, StringRef message) const
T extract_input(const UString identifier)
GeoNodesUserData * user_data() const
const bNode & node() const
void set_default_remaining_outputs()
void set_output(const UString identifier, T &&value)
StructureType structure_type
~ImageFieldsFunction() override
static float frac(const float x, int *ix)
static int wrap_clamp(const int x, const int width)
void call(const IndexMask &mask, mf::Params params, mf::Context) const override
static float4 image_linear_texture_lookup(const BufferT *buffer, const int width, const int height, const float2 coord, const int8_t extension)
static int wrap_periodic(int x, const int width)
static float4 image_pixel_lookup(const uint8_t *buffer, const int width, const int height, const int px, const int py)
ImageFieldsFunction(const int8_t interpolation, const int8_t extension, Image &image, ImageUser image_user)
static float4 image_pixel_lookup(const float *buffer, const int width, const int height, const int px, const int py)
static int wrap_mirror(const int x, const int width)
void sample_buffer(const BufferT *buffer, const IndexMask &mask, const VArray< float3 > &vectors, int width, int height, MutableSpan< float4 > &color_data) const
static float4 image_closest_texture_lookup(const BufferT *buffer, const int width, const int height, const float2 coord, const int extension)
static float4 image_cubic_texture_lookup(const BufferT *buffer, const int width, const int height, const float2 coord, const int extension)
#define storage(slot, qualifiers)
void node_register_type(bNodeType &ntype)
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))
static void node_geo_exec(GeoNodeExecParams params)
static void node_register()
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_declare(NodeDeclarationBuilder &b)
static void node_init(bNodeTree *, bNode *node)
bool execute_multi_function_on_value_variant(const MultiFunction &fn, const std::shared_ptr< MultiFunction > &owned_fn, const Span< SocketValueVariant * > input_values, const Span< SocketValueVariant * > output_values, GeoNodesUserData *user_data, std::string &r_error_message)
@ ITEM_R_SPLIT_EMPTY_NAME
bool BKE_image_is_animated(Image *image)
MINLINE void rgba_uchar_to_float(float r_col[4], const unsigned char col_ub[4])
@ IMA_ALPHA_CHANNEL_PACKED
@ SHD_IMAGE_EXTENSION_EXTEND
@ SHD_IMAGE_EXTENSION_CLIP
@ SHD_IMAGE_EXTENSION_MIRROR
@ SHD_IMAGE_EXTENSION_REPEAT
ATTR_WARN_UNUSED_RESULT const BMVert * v
VecBase< float, 4 > float4
VecBase< float, 2 > float2
ImBuf * BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock)
@ NODE_DEFAULT_INPUT_POSITION_FIELD
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
void node_free_standard_storage(bNode *node)
VecBase< float, 3 > float3
bool IMB_colormanagement_space_name_is_data(const char *name)
void BKE_imageuser_default(ImageUser *iuser)
MINLINE void straight_to_premul_v4(float color[4])
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
void BKE_image_release_ibuf(Image *ima, ImBuf *ibuf, void *lock)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
static constexpr int _240
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
std::string ui_description
NodeGeometryExecFunction geometry_node_execute
void(*) initfunc(bNodeTree *ntree, bNode *node)
const char * enum_name_legacy
NodeDeclareFunction declare
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)