Skip to content

Commit 3cfaccc

Browse files
otdaviesKeavon
andauthored
New node: Remap (#3245)
* Added Remap node * Addressed feedback for mapping range * Removed extra defaults * Apply suggestion from @Keavon --------- Co-authored-by: Keavon Chambers <[email protected]>
1 parent 7e8c6cc commit 3cfaccc

File tree

1 file changed

+38
-0
lines changed
  • node-graph/gmath-nodes/src

1 file changed

+38
-0
lines changed

node-graph/gmath-nodes/src/lib.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,44 @@ impl TangentInverse for DVec2 {
325325
}
326326
}
327327

328+
#[node_macro::node(category("Math: Numeric"))]
329+
fn remap<U: num_traits::float::Float>(
330+
_: impl Ctx,
331+
#[implementations(f64, f32)] value: U,
332+
#[implementations(f64, f32)] input_min: U,
333+
#[implementations(f64, f32)]
334+
#[default(1.)]
335+
input_max: U,
336+
#[implementations(f64, f32)] output_min: U,
337+
#[implementations(f64, f32)]
338+
#[default(1.)]
339+
output_max: U,
340+
clamped: bool,
341+
) -> U {
342+
let input_range = input_max - input_min;
343+
344+
// Handle division by zero
345+
if input_range.abs() < U::epsilon() {
346+
return output_min;
347+
}
348+
349+
let normalized = (value - input_min) / input_range;
350+
let output_range = output_max - output_min;
351+
352+
let result = output_min + normalized * output_range;
353+
354+
if clamped {
355+
// Handle both normal and inverted ranges, since we want to allow the user to use this node to also reverse a range.
356+
if output_min <= output_max {
357+
result.clamp(output_min, output_max)
358+
} else {
359+
result.clamp(output_max, output_min)
360+
}
361+
} else {
362+
result
363+
}
364+
}
365+
328366
/// The random function (rand) converts a seed into a random number within the specified range, inclusive of the minimum and exclusive of the maximum. The minimum and maximum values are automatically swapped if they are reversed.
329367
#[node_macro::node(category("Math: Numeric"))]
330368
fn random(

0 commit comments

Comments
 (0)