three 数学库 / 插值

143 阅读3分钟

CubicInterpolant:使用三次多项式进行平滑插值,适合需要光滑过渡的场景,如动画轨迹。
DiscreteInterpolant:进行离散插值,仅使用最近的样本值,不保证平滑,适合离散数据。
LinearInterpolant:使用线性插值,简单高效,适合不需要复杂平滑的场景,如逐步移动物体。
QuaternionLinearInterpolant:专用于四元数的线性插值,确保旋转的平滑过渡,适合3D动画中的旋转插值。

CubicInterpolant 有五个属性一个方法

`CubicInterpolant( parameterPositions, sampleValues, sampleSize, resultBuffer ) parameterPositions -- 位置数组 sampleValues -- 样本数组 sampleSize -- 样本数量 resultBuffer -- 用于存储插值结果的缓冲区。CubicInterpolant 在 Three.js 中提供了一种高效、平滑的方式来进行立方插值,适用于各种动画和几何变换场景。通过控制点和时间参数,可以生成复杂且自然的运动轨迹 1729564855776.png

    const parameterPositions = [0, 1, 2, 3]; // 示例位置数组
    const sampleValues = [0, 1, 4, 9]; // 使用位置寻找的数据
    const sampleSize = 1; //  样本数量
    const resultBuffer = new Float32Array(1); // 结果缓冲区,准备存储插值结果
    const interpolant = new THREE.CubicInterpolant(parameterPositions, sampleValues, sampleSize, resultBuffer);
    // 假设我们想在位置 1.5 进行插值
    interpolant.evaluate(1.5); // resultBuffer 为对应位置的平方
    console.log(interpolant); // 输出插值结果 { "0": 2.25 }

属性

  • parameterPositions : parameterPositions 示例位置数组 示要注意的时evaluate 为位置数组的传值
  • resultBuffer : 对应参数中的 结果缓冲区
  • sampleValues : 对应示例数组
  • settings :
  • valueSize : 对应 样本数量 方法
  • evaluate ( t : Number ) : Array 评估位置t处的插值。

DiscreteInterpolant 离散插值 五个属性一个方法 属性与方法同上只是使用方式有些不同

DiscreteInterpolant( parameterPositions, sampleValues, sampleSize, resultBuffer ) parameterPositions -- 位置数组 sampleValues -- 样本数组 sampleSize -- 样本数量 resultBuffer -- 用于存储插值结果的缓冲区。 1729563287555.png

    // 定义参数位置和样本值
    const parameterPositions = [0, 1, 2, 3]; // 位置数组
    const sampleValues = [10, 20, 30, 40]; // 样本数组
    const sampleSize = 1; //  样本数量
    const resultBuffer = new Float32Array(1); // 用于存储结果的缓冲区
    // 创建 DiscreteInterpolant 实例
    const interpolant = new THREE.DiscreteInterpolant(parameterPositions, sampleValues, sampleSize, resultBuffer);
    // 进行插值,假设我们要在位置 1.5 进行插值
    const valueAt1_5 = interpolant.evaluate(1.5);
    console.log(valueAt1_5); // 输出插值结果 20

LinearInterpolant 线性插值 有五个属性一个方法 属性与方法同上只是使用方式有些不同

LinearInterpolant( parameterPositions, sampleValues, sampleSize, resultBuffer ) parameterPositions -- 位置数组 sampleValues -- 样本数组 sampleSize -- 样本数量 resultBuffer -- 用于存储插值结果的缓冲区。 1729563532653.png

    // 定义参数位置和样本值
    const parameterPositions = [0, 1, 2, 3]; // 位置数组
    const sampleValues = [10, 20, 30, 40]; // 样本数组
    const sampleSize = 1; // 样本数量(标量)
    const resultBuffer = new Float32Array(1); // 用于存储结果的缓冲区
    // 创建 LinearInterpolant 实例
    const interpolant = new THREE.LinearInterpolant(parameterPositions, sampleValues, sampleSize, resultBuffer);
    // 进行插值,假设我们要在位置 1.5 进行插值
    const valueAt1_5 = interpolant.evaluate(1.5);
    console.log(valueAt1_5); // 输出插值结果 25

QuaternionLinearInterpolant 四元数线性插值 属性与方法同上只是使用方式有些不同

QuaternionLinearInterpolant( parameterPositions, sampleValues, sampleSize, resultBuffer ) parameterPositions -- 位置数组 sampleValues -- 样本数组 sampleSize -- 样本数量 resultBuffer -- 用于存储插值结果的缓冲区。 1729564752473.png

    const q1 = new THREE.Quaternion(0, 0, 0, 1); // 起始四元数
    const q2 = new THREE.Quaternion(0, 1, 0, 0); // 结束四元数
    // 位置数组,定义插值的参数位置
    const parameterPositions = new Float32Array([0, 1]); // 从 0 到 1 的插值
    // 样本数组,包含两个四元数的分量
    const sampleValues = new Float32Array([
        q1.x, q1.y, q1.z, q1.w,  // 第一个四元数
        q2.x, q2.y, q2.z, q2.w   // 第二个四元数
    ]);
    const sampleSize = 4; // 每个四元数有 4 个分量
    const resultBuffer = new Float32Array(4); // 用于存储插值结果的缓冲区
    const interpolant = new THREE.QuaternionLinearInterpolant(
        parameterPositions,
        sampleValues,
        sampleSize,
        resultBuffer
    );
    // 在 t = 0.5 处评估插值
    interpolant.evaluate(0.5);
    console.log(resultBuffer, "插值结果"); // { "0": 0, "1": 0.7071067690849304, "2": 0, "3": 0.7071067690849304 }