Give pack expressions clamp, linearstep, smoothstep and ridge

Ported from v3e (f1d7f92f5).
This commit is contained in:
piexel committed 2026-11-05 21:15:00 +01:00
1 parent a1d043b3c0
commit eb7a6e1044
3 files changed
+128 -1

No files matched your search

@@ -0,0 +1,66 @@
package art.arcane.iris.engine.object.noise.expression;
import com.dfsek.paralithic.eval.parser.Parser;
import com.dfsek.paralithic.functions.dynamic.DynamicFunction;
import com.dfsek.paralithic.node.Statefulness;
import org.jetbrains.annotations.NotNull;
import java.util.function.ToDoubleFunction;
/** Shaping functions every pack expression can call. */
final class ExpressionHelpers {
// Simplex noise is exactly 0.5 on its lattice, so a crest at 0.5 would trace the lattice as straight lines.
private static final double RIDGE_CREST = 0.44;
private static final double RIDGE_HALF_WIDTH = 1 / 2.2;
static final String DESCRIPTION = "Helpers: clamp(v, low, high); linearstep(from, to, v) rises 0 to 1 in a straight "
+ "line as v goes from 'from' to 'to'; smoothstep(from, to, v) does the same with soft ends; ridge(n) turns "
+ "noise from 0 to 1 into sharp crests of 1 where n is about 0.44, falling to 0 away from them.";
private ExpressionHelpers() {
}
/** Registers the helpers; a pack function registered afterwards with the same name replaces one. */
static void registerOn(Parser parser) {
parser.registerFunction("clamp", new Helper(3, args -> clamp(args[0], args[1], args[2])));
parser.registerFunction("linearstep", new Helper(3, args -> linearstep(args[0], args[1], args[2])));
parser.registerFunction("smoothstep", new Helper(3, args -> smoothstep(args[0], args[1], args[2])));
parser.registerFunction("ridge", new Helper(1, args -> ridge(args[0])));
}
static double clamp(double value, double low, double high) {
return Math.max(low, Math.min(high, value));
}
static double linearstep(double from, double to, double value) {
return clamp((value - from) / (to - from), 0, 1);
}
static double smoothstep(double from, double to, double value) {
double progress = linearstep(from, to, value);
return progress * progress * (3 - 2 * progress);
}
static double ridge(double noise) {
double crest = 1 - linearstep(0, RIDGE_HALF_WIDTH, Math.abs(noise - RIDGE_CREST));
return crest * crest;
}
private record Helper(int argNumber, ToDoubleFunction<double[]> body) implements DynamicFunction {
@Override
public double eval(double... args) {
return body.applyAsDouble(args);
}
@Override
public int getArgNumber() {
return argNumber;
}
@NotNull
@Override
public Statefulness statefulness() {
return Statefulness.STATELESS;
}
}
}
@@ -54,7 +54,8 @@ public class IrisExpression extends IrisRegistrant {
private KList<IrisExpressionFunction> functions = new KList<>();
@Required
@Desc("The expression. Inherited variables are x, y and z. Avoid using those variable names.")
@Desc("The expression. Inherited variables are x, y and z. Avoid using those variable names. "
+ ExpressionHelpers.DESCRIPTION)
private String expression;
private transient AtomicCache<Expression> expressionCache = new AtomicCache<>();
@@ -77,6 +78,7 @@ public class IrisExpression extends IrisRegistrant {
IrisLogging.reportError("Script variable load failed in " + getLoadFile().getPath() + ".", e);
}
ExpressionHelpers.registerOn(parser);
for (IrisExpressionFunction f : functions) {
if (!f.isValid()) continue;
f.setData(getLoader());
@@ -0,0 +1,59 @@
package art.arcane.iris.engine.object.noise.expression;
import com.dfsek.paralithic.eval.parser.Parser;
import com.dfsek.paralithic.eval.parser.Scope;
import art.arcane.volmlib.util.math.RNG;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ExpressionHelpersTest {
private static double evaluate(String expression, double n) throws Exception {
Parser parser = new Parser();
ExpressionHelpers.registerOn(parser);
Scope scope = new Scope();
scope.addInvocationVariable("n");
return parser.parse(expression, scope).evaluate(n);
}
@Test
public void packExpressionsCanCallTheHelpers() {
IrisExpression expression = new IrisExpression().setExpression("smoothstep(0, 10, x) + clamp(z, 0, 1)");
assertEquals(0.5D + 1D, expression.evaluate(new RNG(1), 5, 0, 7), 1e-9);
}
@Test
public void clampKeepsValuesInsideTheBounds() throws Exception {
assertEquals(0.2D, evaluate("clamp(n, 0.2, 0.8)", -3D), 1e-9);
assertEquals(0.5D, evaluate("clamp(n, 0.2, 0.8)", 0.5D), 1e-9);
assertEquals(0.8D, evaluate("clamp(n, 0.2, 0.8)", 3D), 1e-9);
}
@Test
public void linearstepRisesInAStraightLineBetweenItsEdges() throws Exception {
assertEquals(0D, evaluate("linearstep(0.4, 0.6, n)", 0.3D), 1e-9);
assertEquals(0.5D, evaluate("linearstep(0.4, 0.6, n)", 0.5D), 1e-9);
assertEquals(1D, evaluate("linearstep(0.4, 0.6, n)", 0.7D), 1e-9);
}
@Test
public void linearstepFallsWhenItsEdgesAreReversed() throws Exception {
assertEquals(1D, evaluate("linearstep(0.42, 0.17, n)", 0.1D), 1e-9);
assertEquals(0D, evaluate("linearstep(0.42, 0.17, n)", 0.5D), 1e-9);
}
@Test
public void smoothstepEasesInAndOut() throws Exception {
assertEquals(0.5D, evaluate("smoothstep(0, 1, n)", 0.5D), 1e-9);
assertEquals(0.028D, evaluate("smoothstep(0, 1, n)", 0.1D), 1e-9);
assertEquals(1D, evaluate("smoothstep(0, 1, n)", 2D), 1e-9);
}
@Test
public void ridgePeaksAtItsCrestAndFadesAway() throws Exception {
assertEquals(1D, evaluate("ridge(n)", 0.44D), 1e-9);
assertEquals(Math.pow(1 - 2.2 * 0.2, 2), evaluate("ridge(n)", 0.64D), 1e-9);
assertEquals(0D, evaluate("ridge(n)", 0.95D), 1e-9);
}
}