Currently, I am using ThreeJS to create a web application that showcases a collection of entities. Each entity is accompanied by a "View" and "Hide" button; for example, entityName View Hide. When the user clicks on the View button, a specific function gets invoked and successfully renders the entity on the screen.
function loadOBJFile(objFile){
/* material of OBJ model */
var OBJMaterial = new THREE.MeshPhongMaterial({color: 0x8888ff});
var loader = new THREE.OBJLoader();
loader.load(objFile, function (object){
object.traverse (function (child){
if (child instanceof THREE.Mesh) {
child.material = OBJMaterial;
}
});
object.position.y = 0.1;
scene.add(object);
});
}
function addEntity(object) {
loadOBJFile(object.name);
}
On the other hand, when the Hide button is clicked, a different function is triggered:
function removeEntity(object){
scene.remove(object.name);
}
The issue arises when an entity loaded with the "View" button does not disappear from the screen upon clicking the "Hide" button. How can I ensure that the "Hide" button functions correctly?
To troubleshoot this problem, I conducted a small experiment where I inserted scene.remove(object.name);
directly after scene.add(object);
in the addEntity
function. As a result, when the "View" button was clicked, no entity was displayed (as expected), indicating that scene.remove(object.name);
worked properly within addEntity
. However, I remain perplexed about implementing it in removeEntity(object).
Upon inspecting the contents of scene.children, the output showed: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
This is my complete code:
If you require more information, please feel free to ask. I have tested these functionalities with rev-59-dev and rev-60 versions of ThreeJS.
Thank you. :)