Skip to content

Instantly share code, notes, and snippets.

@calvin
Last active March 1, 2017 22:35
Show Gist options
  • Save calvin/ffc992a541d2e12b7936911c0ef989b4 to your computer and use it in GitHub Desktop.
Save calvin/ffc992a541d2e12b7936911c0ef989b4 to your computer and use it in GitHub Desktop.
Path3 implementation of three.js (r84)
Path3.prototype = Object.create( THREE.Curve.prototype );
Path3.prototype.constructor = Path3;
Path3.prototype.getPoint = function ( t ) {
var points = this.points;
var l = points.length;
if ( l < 2 ) console.log( 'duh, you need at least 2 points' );
var point = ( l - 1 ) * t;
var intPoint = Math.floor( point );
var weight = point - intPoint;
if ( weight === 0 && intPoint === l - 1 ) {
intPoint = l - 2;
weight = 1;
}
var p1 = points[ intPoint % l ];
var p2 = points[ ( intPoint + 1 ) % l ];
return p1.clone().lerp( p2, weight );
}
import * as THREE from "three";
export class Path3 extends THREE.CatmullRomCurve3 {
getPoint(t: number) {
const points = this.points;
const l = points.length;
if (l < 2) {
console.log("duh, you need at least 2 points");
}
const point = (l - 1) * t;
let intPoint = Math.floor(point);
let weight = point - intPoint;
if ( weight === 0 && intPoint === l - 1 ) {
intPoint = l - 2;
weight = 1;
}
const p1 = points[intPoint % l];
const p2 = points[(intPoint + 1) % l];
return p1.clone().lerp(p2, weight);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment