API Design: Object3D.position is read-only

Description of the problem

The position property of Object3D, and more importantly all it’s ancestors, is read-only. The default behavior of Object.defineProperties sets properties that way — that’s what’s used in Object3D.js.

Consider the scenario where a user wants to subclass Mesh.js.

var Particle = function (positionVector, mass, radius) {

    THREE.Mesh.call(this);

    // will cause read-only assignment error
    this.position = positionVector; 
   ...
    this.geometry = new THREE.SphereGeometry(this.radius, 10, 10);
    this.material = this.getMaterial({
        color: 0xff0000
    });
}

Particle.prototype = Object.create(THREE.Mesh.prototype);
Particle.prototype.constructor = Particle;

Copying or clone methods of Vector3 are problematic because there’s no way to preserve the original reference passed (or ‘injected’ if you want to be tedious) in the constructor above. This prevents elegant subclassing for child classes of Object3D like Mesh, and I think is a mistake in an otherwise very-well designed library. Please consider altering the relevant Object.defineProperties line to add writable: true.

Three.js version
  • Dev
  • r76
Browser
  • All of them
  • Chrome
  • Firefox
  • Internet Explorer
OS
  • All of them
  • Windows
  • Linux
  • Android
  • IOS
Hardware Requirements (graphics card, VR Device, …)

N/A

Author: Fantashit

2 thoughts on “API Design: Object3D.position is read-only

  1. @arctwelve

    The problem is… You start with position and you quickly end up doing the same with quaternion, matrix, matrixWorld, etc

    If you re-assign quaternion we can’t keep rotation in sync anymore. If you re-assign matrix, well… lots of horrible things can happen.

  2. @mrdoob

    Fair enough: protecting the internals from users is a good motivation. And it’s not difficult to keep a separate Vector3 to use for operations and then assign its values to position using the Vector3 methods. @aardgoose ‘s suggestion to document the read-only would be helpful.

    My compliments on an extraordinary and elegant library.

Comments are closed.