Len White
Len White

Reputation: 992

Add Attribute to Three.js Object

I want to maintain some information with shapes I'm creating. I use an HTML canvas for the texture, so I know I can keep the values in the element attributes arrays of the canvases. I'm wondering though, is there a more straight-forward way of maintaining attributes that works for all Three.js shapes regardless of how they are constructed?

Upvotes: 1

Views: 3259

Answers (2)

Len White
Len White

Reputation: 992

To be clear as to the correct answer, prisoner849 provided it in the comment he made to another response: use the THREE.Mesh.userData property.

Upvotes: 2

Hellium
Hellium

Reputation: 7346

The advantage (or the problem) with Javascript, is that you can do almost everything with your objects. You can add, or even delete attributes as much as you want any time.

Thus, you can add every property you want to THREE objects by simply giving it a value :

 myThreeObject.myProperty = myValue ;

Be careful though, if you haven't assigned a value to the property, this property does not exist (yet), thus, don't forget to check if the value exist before reading it :

if( "undefined" !== typeof myThreeObject.myProperty )
    // ...

Upvotes: 3

Related Questions