Skip to content

Instantly share code, notes, and snippets.

@CodyJasonBennett
Created September 2, 2021 04:52
Show Gist options
  • Save CodyJasonBennett/456530eba0868c3f74f140e0ff6e4fa8 to your computer and use it in GitHub Desktop.
Save CodyJasonBennett/456530eba0868c3f74f140e0ff6e4fa8 to your computer and use it in GitHub Desktop.
3D de Casteljau algorithm
import { Vector3 } from 'https://cdn.skypack.dev/three';
const tempVector = new Vector3();
function deCasteljauAlgorithm(vectors, t) {
if (t === 1) return vectors[vectors.length - 1];
if (t === 0 || vectors.length === 1) return vectors[0];
const calculatedVectors = [];
for (let i = 1; i < vectors.length; i++) {
const vector1 = vectors[i - 1];
const vector2 = vectors[i];
const offset = tempVector.subVectors(vector2, vector1).multiplyScalar(t);
const target = vector1.add(offset);
calculatedVectors.push(target);
}
return deCasteljauAlgorithm(calculatedVectors, t);
}
// Usage
console.log(deCasteljauAlgorithm([new Vector3(0, 0, 0), new Vector3(0.5, 0.5, 0.5), new Vector3(1, 0, 0)], 0.5));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment