All files / lib math.js

97.18% Statements 69/71
60% Branches 24/40
100% Functions 22/22
96.82% Lines 61/63

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143    1x 1x   3x   3x   1x 6x   1x 4x     1x 3x     22x   4x   1x 5x     1x 111x     1x   51x     1x     6x   1x         2x 2x   2x   2x             1x 3x 3x   3x   3x             3x   1x 2x   3x   9x   1x 6x 6x 6x     1x               8x 8x   8x 8x 8x 8x     8x 8x 8x   8x 8x 8x     8x         8x     1x               2x 2x     1x 2x           1x 2x  
import { Vector3, MathUtils, Vector2 } from "three";
 
export const PI = Math.PI;
export const PI_2 = PI / 2;
 
export const identity = a => a;
 
export const pickRandom = (list = []) => list[Math.floor(Math.random() * list.length)];
 
export const degToRad = angle => {
    return angle * (PI / 180);
};
export const radToDeg = angle => {
    return angle * (180 / PI);
};
 
export const getProportion = (max1, b, max2) => {
    return (max1 * b) / max2;
};
 
export const clamp = (value, min, max) => (value < min ? min : value > max ? max : value);
 
export const cap = (value, max) => (value > max ? max : value);
 
export const isWithin = (value, min, max) => {
    return value >= min && value < max;
};
 
export const randomFloatFromInterval = (min, max) => {
    return Math.random() * (max - min + 1) + min;
};
 
export const randomIntFromInterval = (min, max) => {
    // min and max included
    return Math.floor(randomFloatFromInterval(min, max));
};
 
export const getDistance = (
    { x: xA = 0, y: yA = 0, z: zA = 0 } = {},
    { x: xB = 0, y: yB = 0, z: zB = 0 } = {},
) => Math.sqrt(Math.pow(xA - xB, 2) + Math.pow(yA - yB, 2) + Math.pow(zA - zB, 2));
 
export const findPointBetweenAtDistance = (
    { x: xC = 0, y: yC = 0, z: zC = 0 } = {},
    { x: xE = 0, y: yE = 0, z: zE = 0 } = {},
    distance = 1,
) => {
    const vCenter = new Vector3(xC, yC, zC);
    const vEnd = new Vector3(xE, yE, zE);
 
    const point = vCenter.add(vEnd.sub(vCenter).normalize().multiplyScalar(distance));
 
    return {
        x: point.x,
        y: point.y,
        z: point.z,
    };
};
 
export const lerpVectors = (origin, target, speed) => {
    const originVector = new Vector3(origin.x, origin.y, origin.z);
    const targetVector = new Vector3(target.x, target.y, target.z);
 
    originVector.lerp(targetVector, speed);
 
    return {
        x: originVector.x,
        y: originVector.y,
        z: originVector.z,
    };
};
 
export const lerp = (x, y, t) => MathUtils.lerp(x, y, t);
 
export const scaleVector = ({ x = 0, y = 0, z = 0 }, scale = 1) =>
    new Vector3(x, y, z).multiplyScalar(scale);
 
export const getSphereVolume = radius => (4 * Math.PI * Math.pow(radius, 3)) / 3;
 
export const repeat = (t, length) => clamp(t - Math.floor(t / length) * length, 0.0, length);
 
export const deltaAngle = (angle, target) => {
    let delta = repeat(target - angle, 360.0);
    if (delta > 180.0) delta -= 360.0;
    return delta;
};
 
export const smoothDamp = (
    current,
    target,
    currentVelocity,
    smoothTime,
    maxSpeed = Infinity,
    dt,
) => {
    smoothTime = Math.max(0.0001, smoothTime);
    const omega = 2 / smoothTime;
 
    const x = omega * dt;
    const exp = 1 / (1 + x + 0.48 * x * x + 0.235 * x * x * x);
    let change = current - target;
    const originalTo = target;
 
    // Clamp maximum speed
    const maxChange = maxSpeed * smoothTime;
    change = clamp(change, -maxChange, maxChange);
    target = current - change;
 
    const temp = (currentVelocity + omega * change) * dt;
    currentVelocity = (currentVelocity - omega * temp) * exp;
    let output = target + (change + temp) * exp;
 
    // Prevent overshooting
    Iif (originalTo - current > 0.0 == output > originalTo) {
        output = originalTo;
        currentVelocity = (output - originalTo) / dt;
    }
 
    return [output, currentVelocity];
};
 
export const smoothDampAngle = (
    current,
    target,
    currentVelocity,
    smoothTime,
    maxSpeed = Infinity,
    dt,
) => {
    target = current + deltaAngle(current, target);
    return smoothDamp(current, target, currentVelocity, smoothTime, maxSpeed, dt);
};
 
export const randomVector3 = (min = 0, max = 1) =>
    new Vector3(
        randomFloatFromInterval(min, max),
        randomFloatFromInterval(min, max),
        randomFloatFromInterval(min, max),
    );
 
export const randomVector2 = (min = 0, max = 1) =>
    new Vector2(randomFloatFromInterval(min, max), randomFloatFromInterval(min, max));