Created
September 2, 2021 04:52
-
-
Save CodyJasonBennett/456530eba0868c3f74f140e0ff6e4fa8 to your computer and use it in GitHub Desktop.
3D de Casteljau algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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