smouse
smouse

Reputation: 11

three.js change mesh from scene

I want to change a mesh from a group, trriged by a button.

I'm loading an external .obj file:

loader.load( obj, function ( object ) {

    createScene( object, mod.tipo, pid, cor.replace("#","0x") );

});

and add on a group

function createScene( geometry, name, id, cor ) {

    geometry.traverse( function ( child ) {

        if ( child instanceof THREE.Mesh ) {

            var material = new THREE.MeshPhongMaterial( {
                specular: 0xffffff,
                shininess: 10,
                side: THREE.DoubleSide,
                map: THREE.ImageUtils.loadTexture('/3d/js/texturas/white.jpg'),
                shading: THREE.SmoothShading
            } );

            material.color.setHex(cor);

            child.material = material;

            group.add( child );     

        }

and add this group in a scene: scene.add( group )

to change i set de visible of my mesh to false. But a want to remove it from scene and group.

I already try scene.remove('name') and scene.remove(mesh) but didnt work.

Someone know how to do this?

Upvotes: 1

Views: 1581

Answers (2)

smouse
smouse

Reputation: 11

I get a solution:

In my case i was using a group to join the meshes and rotate all of them to start position.

First i have to add the mashes directly in the scene. I could'n make it work on a group Second i have a global variable that contain an array of all meshes. And as was tring to remove use this reference. I have to change this logic.

So i create this function:

var clearScene = function(name) { var objsToRemove = scene.children;

for (var t = 0; t<objsToRemove.length; t++){

    if (objsToRemove[t] instanceof THREE.Mesh) {

      if (objsToRemove[t].name = name) {
        scene.remove(objsToRemove[t]);
        break;
      }

    }

}

}

it goes on each meshe looking for the mashe i want to delete. and simply call the remove frome the scene.

this works for me.

I have to change my group logic and mabe rotate mesh by mashe to default position, i think that i will have a better performance

Upvotes: 0

gaitat
gaitat

Reputation: 12642

You probably want:

var toRemove = scene.getNodeByName ( 'name', true );
if (toRemove !== undefined)
    scene.remove ( toRemove );

which searches in your scene for a node named name and then removes the resulting node from the scene.

Upvotes: 1

Related Questions