Spline.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /**
  2. * Spline from Tween.js, slightly optimized
  3. * http://sole.github.com/tween.js/examples/05_spline.html
  4. *
  5. * @author mrdoob / http://mrdoob.com/
  6. */
  7. THREE.Spline = function () {
  8. var c = [], v3 = { x: 0, y: 0, z: 0 },
  9. point, intPoint, weight, w2, w3,
  10. pa, pb, pc, pd;
  11. this.get2DPoint = function ( points, k ) {
  12. point = ( points.length - 1 ) * k;
  13. intPoint = Math.floor( point );
  14. weight = point - intPoint;
  15. c[ 0 ] = intPoint == 0 ? intPoint : intPoint - 1;
  16. c[ 1 ] = intPoint;
  17. c[ 2 ] = intPoint > points.length - 2 ? intPoint : intPoint + 1;
  18. c[ 3 ] = intPoint > points.length - 3 ? intPoint : intPoint + 2;
  19. pa = points[ c[ 0 ] ];
  20. pb = points[ c[ 1 ] ];
  21. pc = points[ c[ 2 ] ];
  22. pd = points[ c[ 3 ] ];
  23. w2 = weight * weight;
  24. w3 = weight * w2;
  25. v3.x = interpolate( pa.x, pb.x, pc.x, pd.x, weight, w2, w3 );
  26. v3.y = interpolate( pa.y, pb.y, pc.y, pd.y, weight, w2, w3 );
  27. v3.z = interpolate( pa.z, pb.z, pc.z, pd.z, weight, w2, w3 );
  28. return v3;
  29. }
  30. // Catmull-Rom
  31. function interpolate( p0, p1, p2, p3, t, t2, t3 ) {
  32. var v0 = ( p2 - p0 ) * 0.5,
  33. v1 = ( p3 - p1 ) * 0.5;
  34. return ( 2 * ( p1 - p2 ) + v0 + v1 ) * t3 + ( - 3 * ( p1 - p2 ) - 2 * v0 - v1 ) * t2 + v0 * t + p1;
  35. }
  36. };
粤ICP备19079148号