Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/core/math/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,18 +253,18 @@ const math = {
*
* See https://en.wikipedia.org/wiki/Smoothstep for more details.
*
* @param {number} min - The lower bound of the interpolation range.
* @param {number} max - The upper bound of the interpolation range.
* @param {number} min - The value of x at which the result is 0. Can be greater than max for a
* falling ramp.
* @param {number} max - The value of x at which the result is 1.
* @param {number} x - The value to interpolate.
* @returns {number} The smoothly interpolated value clamped between zero and one.
* @example
* math.smoothstep(0, 10, 5); // returns 0.5
*/
smoothstep(min, max, x) {
if (x <= min) return 0;
if (x >= max) return 1;
if (min === max) return x <= min ? 0 : 1;

x = (x - min) / (max - min);
x = math.clamp((x - min) / (max - min), 0, 1);

return x * x * (3 - 2 * x);
},
Expand All @@ -275,18 +275,18 @@ const math = {
*
* See https://en.wikipedia.org/wiki/Smoothstep#Variations for more details.
*
* @param {number} min - The lower bound of the interpolation range.
* @param {number} max - The upper bound of the interpolation range.
* @param {number} min - The value of x at which the result is 0. Can be greater than max for a
* falling ramp.
* @param {number} max - The value of x at which the result is 1.
* @param {number} x - The value to interpolate.
* @returns {number} The smoothly interpolated value clamped between zero and one.
* @example
* math.smootherstep(0, 10, 5); // returns 0.5
*/
smootherstep(min, max, x) {
if (x <= min) return 0;
if (x >= max) return 1;
if (min === max) return x <= min ? 0 : 1;

x = (x - min) / (max - min);
x = math.clamp((x - min) / (max - min), 0, 1);

return x * x * x * (x * (x * 6 - 15) + 10);
},
Expand Down
38 changes: 38 additions & 0 deletions test/core/math/math.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,25 @@ describe('math', function () {
expect(math.smootherstep(0, 10, 10)).to.equal(1);
});

it('clamps x outside the range', function () {
expect(math.smootherstep(0, 10, -5)).to.equal(0);
expect(math.smootherstep(0, 10, 15)).to.equal(1);
});

it('returns a falling ramp when min is greater than max', function () {
expect(math.smootherstep(10, 0, 15)).to.equal(0);
expect(math.smootherstep(10, 0, 5)).to.equal(0.5);
expect(math.smootherstep(10, 0, -5)).to.equal(1);
expect(math.smootherstep(-0.3, -0.6, -0.2)).to.equal(0);
expect(math.smootherstep(-0.3, -0.6, -0.7)).to.equal(1);
});

it('returns a step when min equals max', function () {
expect(math.smootherstep(5, 5, 4)).to.equal(0);
expect(math.smootherstep(5, 5, 5)).to.equal(0);
expect(math.smootherstep(5, 5, 6)).to.equal(1);
});

});

describe('#smoothstep', function () {
Expand All @@ -340,6 +359,25 @@ describe('math', function () {
expect(math.smoothstep(0, 10, 10)).to.equal(1);
});

it('clamps x outside the range', function () {
expect(math.smoothstep(0, 10, -5)).to.equal(0);
expect(math.smoothstep(0, 10, 15)).to.equal(1);
});

it('returns a falling ramp when min is greater than max', function () {
expect(math.smoothstep(10, 0, 15)).to.equal(0);
expect(math.smoothstep(10, 0, 5)).to.equal(0.5);
expect(math.smoothstep(10, 0, -5)).to.equal(1);
expect(math.smoothstep(-0.3, -0.6, -0.2)).to.equal(0);
expect(math.smoothstep(-0.3, -0.6, -0.7)).to.equal(1);
});

it('returns a step when min equals max', function () {
expect(math.smoothstep(5, 5, 4)).to.equal(0);
expect(math.smoothstep(5, 5, 5)).to.equal(0);
expect(math.smoothstep(5, 5, 6)).to.equal(1);
});

});

});