Overloading of functions

Is there any downside of passing a Vector3 instead of three single values into functions like Matrix4,makeTranslation(x, y, z) ?

If not I would make a few PRs in the future where Matrix4.makeTranslation(x, y, z) and other functions would also accept a Vector3. Of course I would also at it in the documentation.

So I would suggest to allow “overriding” of functions, I know JavaScript can’t do this but it is possible to do it by code.

Example:

// three.js/src/math/Matrix4.js
  
// Before
makeTranslation: function ( x, y, z ) {

  this.set(
    1, 0, 0, x,
    0, 1, 0, y,
    0, 0, 1, z,
    0, 0, 0, 1
  );
  return this;
},...

// After (with "overriding")
makeTranslation: function ( x, y, z ) {

  if (x instanceof THREE.Vector3) { // haven't tested this line, but should work
    z = x.z;
    y = x.y;
    x = x.x;
  }

  this.set(
    1, 0, 0, x,
    0, 1, 0, y,
    0, 0, 1, z,
    0, 0, 0, 1
  );
  return this;
},...
Three.js version
  • [x ] Dev
Browser
  • All of them
OS
  • All of them

Author: Fantashit

1 thought on “Overloading of functions

  1. We support multiple types in certain cases. For example,

    Color.set( value ) // value can be of multiple types
    Mesh( geometry, material ) // material can be an array

    This method is particularly problematic for users:

    camera.lookAt( new THREE.Vector3( x, y, z ) );

    Users often do this, instead, and are confused when it doesn’t work.

    camera.lookAt( x, y, z );

    Furthermore, because a Vector3 is required, I have seen a Vector3 instantiated in the render loop

    camera.lookAt( new THREE.Vector3( x, y, z ) );

    I think supporting alternate types is fine in certain cases — lookAt() may be a good candidate.

Comments are closed.