Page MenuHome

Math Node: Add median function
Needs ReviewPublic

Authored by Miles Conway (3DProgrammer) on Sep 11 2022, 10:51 AM.

Details

Summary

This patch adds the 'Median' function to the math node, which outputs the median of the three input numbers.

Currently, it only works on float math, because the median of vectors is not well defined.

While this can be done using the current nodes, it is messy and having a dedicated function is more convenient.

This patch implements the feature for Cycles, EEVEE, Geometry Nodes, and the Compositor.

Diff Detail

Repository
rB Blender

Event Timeline

Miles Conway (3DProgrammer) requested review of this revision.Sep 11 2022, 10:51 AM
Miles Conway (3DProgrammer) created this revision.
Brecht Van Lommel (brecht) requested changes to this revision.Sep 12 2022, 2:09 PM
Brecht Van Lommel (brecht) retitled this revision from Maths Node: Add median function to Math Node: Add median function.
Brecht Van Lommel (brecht) edited the summary of this revision. (Show Details)

The functionality seems useful.

The formulation using min/max is clever, but also leads to loss of precision. I would expect an operation like this to output a value exactly matching one of the 3 input values.

This revision now requires changes to proceed.Sep 12 2022, 2:11 PM

Changed to more accurate algorithm.

Brecht Van Lommel (brecht) requested changes to this revision.Sep 16 2022, 7:14 PM

Thanks for the update.

intern/cycles/kernel/osl/shaders/node_math.h
108–117

Can we simplify this to?

return (min(b, c) <= a && max(b, c) >= a) ? a : (min(a, c) <= b && max(a, c) >= b) ? b : c;

The third test seems redundant.

source/blender/compositor/operations/COM_MathBaseOperation.cc
1072–1103

For readability I suggest to read values into local variables a, b, c for these two implementations.

source/blender/gpu/shaders/common/gpu_shader_common_math.glsl
234

This should always output a result.

This revision now requires changes to proceed.Sep 16 2022, 7:14 PM