26#define MINIMUM_DISTORTION -0.999f
28#define HORIZONTAL_DISPERSION_SCALE 5.0f
30#define RADIAL_DISPERSION_SCALE 4.0f
32#define DISTORTION_SCALE 4.0f
41 N_(
"Radially distorts the image to create a barrel or a Pincushion distortion")},
46 N_(
"Horizontally distorts the image to create a channel/color shifting effect")},
47 {0,
nullptr, 0,
nullptr,
nullptr},
52 b.use_custom_socket_order();
53 b.allow_any_socket_order();
55 .default_value({1.0f, 1.0f, 1.0f, 1.0f})
58 b.add_output<
decl::Color>(
"Image").structure_type(StructureType::Dynamic).align_with_previous();
71 "The amount of distortion. 0 means no distortion, -1 means full Pincushion distortion, "
72 "and 1 means full Barrel distortion");
78 .description(
"The amount of chromatic aberration to add to the distortion");
83 "Introduces jitter while doing distortion, which can be faster but can produce grainy "
89 "Scales the image such that it fits entirely in the frame, leaving no empty spaces at "
118 const float distance_squared)
121 1.0f - chromatic_distortion * distance_squared)));
138 const bool use_jitter)
141 return distortion < 4.0f ? 2 : int(
math::sqrt(distortion + 1.0f));
143 return int(distortion + 1.0f);
154 const float distance_squared,
155 const bool use_jitter)
166 float distortion_red =
math::distance(distorted_uv_red, distorted_uv_green);
171 float distortion_blue =
math::distance(distorted_uv_green, distorted_uv_blue);
177 return int4(steps_red, steps_red + steps_blue, steps_blue, steps_red + steps_blue);
204 const float3 &chromatic_distortion,
207 const float distance_squared,
210 const bool use_jitter)
213 float distortion_amount = chromatic_distortion[end] - chromatic_distortion[start];
214 for (
int i = 0;
i < steps;
i++) {
217 float increment = (
i +
get_jitter(texel, start * steps +
i, use_jitter)) / steps;
218 float distortion = chromatic_distortion[start] + increment * distortion_amount;
225 accumulated_color[start] += (1.0f - increment) *
color[start];
226 accumulated_color[end] += increment *
color[end];
227 accumulated_color.w +=
color.w;
229 return accumulated_color;
236 const float3 &chromatic_distortion,
238 const bool use_jitter)
244 float distance_squared =
math::dot(uv, uv);
248 float3 distortion_bounds = chromatic_distortion * distance_squared;
249 if (distortion_bounds.x > 1.0f || distortion_bounds.y > 1.0f || distortion_bounds.z > 1.0f) {
257 chromatic_distortion,
size, uv, distance_squared, use_jitter);
267 chromatic_distortion,
277 chromatic_distortion,
327 if (this->
context().use_gpu()) {
369 output.allocate_texture(domain);
377 const float4 red =
input.sample_bilinear_zero(normalized_texel +
float2(dispersion, 0.0f));
379 const float4 blue =
input.sample_bilinear_zero(normalized_texel -
float2(dispersion, 0.0f));
383 output.store_pixel(texel,
float4(red.x, green.y, blue.z, alpha));
389 if (this->
context().use_gpu()) {
428 return "compositor_radial_lens_distortion_jitter";
430 return "compositor_radial_lens_distortion";
443 output.allocate_texture(domain);
460 return clamp_f(
input.get_single_value_default(0.0f), 0.0f, 1.0f);
483 const float maximum_distortion =
max_fff(distortion[0], distortion[1], distortion[2]);
485 if (
get_is_fit() && (maximum_distortion > 0.0f)) {
486 return 1.0f / (1.0f + 2.0f * maximum_distortion);
488 return 1.0f / (1.0f + maximum_distortion);
495 const MenuValue menu_value =
input.get_single_value_default(default_menu_value);
513 if (this->
get_input(
"Image").is_single_value()) {
548 ntype.
ui_name =
"Lens Distortion";
549 ntype.
ui_description =
"Simulate distortion and dispersion from camera lenses";
552 ntype.
declare = file_ns::cmp_node_lensdist_declare;
553 ntype.
initfunc = file_ns::node_composit_init_lensdist;
#define NODE_CLASS_DISTORT
#define CMP_NODE_LENSDIST
MINLINE float max_fff(float a, float b, float c)
MINLINE float clamp_f(float value, float min, float max)
CMPNodeLensDistortionType
@ CMP_NODE_LENS_DISTORTION_RADIAL
@ CMP_NODE_LENS_DISTORTION_HORIZONTAL
void GPU_shader_uniform_1f(blender::gpu::Shader *sh, const char *name, float value)
void GPU_shader_uniform_3fv(blender::gpu::Shader *sh, const char *name, const float data[3])
void GPU_shader_bind(blender::gpu::Shader *shader, const blender::gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_texture_extend_mode(blender::gpu::Texture *texture, GPUSamplerExtendMode extend_mode)
@ GPU_SAMPLER_EXTEND_MODE_CLAMP_TO_BORDER
void GPU_texture_filter_mode(blender::gpu::Texture *texture, bool use_filter)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
BMesh const char void * data
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
static unsigned long seed
gpu::Shader * get_shader(const char *info_name, ResultPrecision precision)
NodeOperation(Context &context, DNode node)
Result & get_result(StringRef identifier)
Context & context() const
Result & get_input(StringRef identifier) const
virtual Domain compute_domain()
void share_data(const Result &source)
T get_single_value_default(const T &default_value) const
void allocate_texture(const Domain domain, const bool from_pool=true, const std::optional< ResultStorageType > storage_type=std::nullopt)
void unbind_as_texture() const
void bind_as_texture(gpu::Shader *shader, const char *texture_name) const
void unbind_as_image() const
void bind_as_image(gpu::Shader *shader, const char *image_name, bool read=false) const
StructureType structure_type
void execute_horizontal_distortion()
CMPNodeLensDistortionType get_type()
float3 compute_chromatic_distortion()
void execute_horizontal_distortion_gpu()
void execute_radial_distortion_cpu()
void execute_radial_distortion_gpu()
NodeOperation(Context &context, DNode node)
void execute_horizontal_distortion_cpu()
const char * get_radial_distortion_shader()
void execute_radial_distortion()
void * MEM_callocN(size_t len, const char *str)
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))
void compute_dispatch_threads_at_least(gpu::Shader *shader, int2 threads_range, int2 local_size=int2(16))
void parallel_for(const int2 range, const Function &function)
T distance(const T &a, const T &b)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
T max(const T &a, const T &b)
static const EnumPropertyItem type_items[]
static int4 compute_number_of_integration_steps(const float3 &chromatic_distortion, const int2 &size, const float2 &uv, const float distance_squared, const bool use_jitter)
static NodeOperation * get_compositor_operation(Context &context, DNode node)
static float4 integrate_distortion(const int2 &texel, const Result &input, const int2 &size, const float3 &chromatic_distortion, const int start, const int end, const float distance_squared, const float2 &uv, const int steps, const bool use_jitter)
static float2 compute_distorted_uv(const float2 &uv, const float uv_scale, const int2 &size)
static float get_jitter(const int2 &texel, const int seed, const bool use_jitter)
static int compute_number_of_integration_steps_heuristic(const float distortion, const bool use_jitter)
static void cmp_node_lensdist_declare(NodeDeclarationBuilder &b)
static float compute_distortion_scale(const float distortion, const float distance_squared)
static void node_composit_init_lensdist(bNodeTree *, bNode *node)
static float3 compute_chromatic_distortion_scale(const float3 &chromatic_distortion, const float distance_squared)
static void radial_lens_distortion(const int2 texel, const Result &input, Result &output, const int2 &size, const float3 &chromatic_distortion, const float scale, const bool use_jitter)
float hash_to_float(uint32_t kx)
VecBase< int32_t, 4 > int4
VecBase< float, 4 > float4
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
VecBase< float, 3 > float3
#define HORIZONTAL_DISPERSION_SCALE
#define MINIMUM_DISTORTION
#define RADIAL_DISPERSION_SCALE
static void register_node_type_cmp_lensdist()
void cmp_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
void node_free_standard_storage(bNode *node)
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
std::string ui_description
NodeGetCompositorOperationFunction get_compositor_operation
void(* initfunc)(bNodeTree *ntree, bNode *node)
const char * enum_name_legacy
NodeDeclareFunction declare