markuslerner / THREE.Interactive

Fast and simple interaction manager for three.js for enabling mouse and touch events on 3D objects
MIT License
183 stars 26 forks source link

It is recommended to mention the `name` field in the documentation #22

Closed wang1212 closed 2 months ago

wang1212 commented 1 year ago

A common programming idiom is to call add() when the object (interactive) is created, and then call remove() when the object is destroyed.

However, due to the lack of understanding of the source code details, if the name field of the object is not assigned, it will cause all objects called remove() to be removed.

https://github.com/markuslerner/THREE.Interactive/blob/v1.6.1/src/index.ts#L223

remove() {
   if (!object) return;
   const filterSet = new Set<string>(
     childNames.length > 0 ? childNames : [object.name]  // If `name` is `undefined` ?
   );

  this.interactiveObjects = this.interactiveObjects.filter(
    (o) => !filterSet.has(o.name)  // If `name` is `undefined` ?
  );
}

So, if I understand correctly, the name field value of a 3D object should be unique? If yes, is this critical and should it be specifically mentioned in the docs?

markuslerner commented 1 year ago

Hi @wang1212, thank you for pointing this out. There was indeed a bug in the remove function. The intention was not to make object.name mandatory. I just published release 1.7.0, which fixes this. Now all objects, whose name is undefined won't be removed when calling InteractionManager.remove(...) with an object whose name is undefined.