Would Three consider a prototype.reset() function for its value classes (Vectors, Colors, etc)?

Being able to reset is a critical feature in deterministic design systems. For instance the DOM can do this:

const a = document.createElement("div")
a.style.color // -> ""

a.style.color = "red"
a.style.color // -> "red"

a.style.color = null
a.style.color // -> ""

This is why we can simply delete a style from Chrome Devtools and it goes back to its default.

or in React we can write:

<meshBasicMaterial color="red" />

Now we could simply remove the color, save the file, and hot module reload will tell the reconciler that “color” does not exist any longer on the same element:

<meshBasicMaterial />

But, since there is no way to know the default, it is stuck on red. I discussed this with Dan Abramov, and he was surprised that Three doesn’t have this. All other platforms seem to be able to do this, i asked around: Dom, Android, Ios, Konva, Ink, etc.

It would be a simple addition for all value types, so classes that have set/clone/copy. It could be a reset method for instance that puts the item back to the state it had:

const color = new THREE.Color()
color.set("red")
color.reset()
color.getHexString() // -> "ffffff"

Or a defaults collection on the prototype:

Color.prototype.defaultArguments = [0, 0, 0]
// ...
color.set(...color.defaultArguments)

This is not something that would only help React, it would also be relevant to any design system that is driven by Three, visual editors, IDEs and so on.

4 thoughts on “Would Three consider a prototype.reset() function for its value classes (Vectors, Colors, etc)?

  1. but color.identity() doesn’t sound good… ?

    Identity matrix makes sense. However, I’ve never heard of identity color and I’m afraid this semantic sounds quite uncommon. Same for all other math classes.

  2. To me, there is no obvious way to define a “default state” for math entities. For example the semantic of a vector mainly depends on its usage. A direction vector (which always has unit length) is something different than a vector defining a position in 3D space. There is no way to define a reset value appropriate for all possible use cases.

  3. One thought I had was to use Object.defineProperty to provide getters and setters of the properties

    I think getters and setters have too high a performance cost to be used for frequently accessed properties like position and scale.

  4. Thank you!

    As @donmccurdy said… Couldn’t react-three-fiber create internal defaults like this?

    const DEFAULT_OBJECT3D = new Object3D();

    And then do:

    mesh.position.copy( DEFAULT_OBJECT3D.position );