Curve - Direct change of attribute

Hi community!
Had an interesting question on Discord. How to change tension of CatmullRomCurve3 without re-creation of it?
The answer was obviouse: create an instance of the curve and re-use it, changing its tension.
But, what caught my attention this time, that we have a new array of Vector3 every time we call .getPoints().
For example, we want a line to follow curve, when control points and tension constantly change in time. Currently we get a new array of Vector3 and put values of its items into a buffer attribute for vertices. Wasting of time and resources.
Here is an option for the updating the geometry’s vertices directly via .getPoints(). The changes is quite minor:

let curve = new THREE.CatmullRomCurve3(pts, true, "catmullrom", 0);

curve.getPoints = function (attribute){
  let divisions = attribute.count - 1;
  let v3 = new THREE.Vector3();
  for ( let d = 0; d <= (divisions); d ++ ) {
    this.getPoint( d / (divisions), v3 );
    attribute.setXYZ(d, v3.x, v3.y, v3.z);
  }
  attribute.needsUpdate = true;
}

Demo: https://codepen.io/prisoner849/full/wvXBYKq

3 Likes

Do I see it correctly that you can also use this method to make example DynamicTubeGeometryCaps more efficient? :thinking:

Not this method specifically, but the approach in general. Re-use the things, when possible :slight_smile:

1 Like