1

シーン上の古いオブジェクトを削除し、シーンを新しいオブジェクトを追加できるようにします

メッシュに追加された 3D カスタム オブジェクトを持つアプリケーションを開発しました。メッシュがシーンに追加されます。ページにもボタンがあります。クリックすると、既に作成されているオブジェクトがシーンの子であるメッシュから削除され、シーンは最初のように新しいオブジェクトを追加する準備ができているはずです。つまり、可能であればメッシュ全体を削除して、新しいオブジェクト セットを含む新しいメッシュをシーンに追加する必要があります。

function xyz() // Button click function
{
  // should remove the objects in the scene if exists and make scene available to add 
  // new objects
  for ex: scene.remove(mesh)....???
  // Also needed to clear the renderer??
   .
   .
   .
   do something
 }
 function init()
 {
   // adds the objects to the scene and instantiate renderer
    mesh = New THREE.Mesh({// some material});
    cube = new THREE.CubeGeometry(1,1,1);
    object = new THREE.Mesh(cube,material);
    mesh.add(object);
    scene.add(mesh);
 }
 function animate()
 {
        requestAnimationFrame(animate);
        render();
 }
 function render()
 {
   renderer.render(scene, camera);
 }
4

1 に答える 1

5

あなたが含めたスニペットに基づいて、あなたのコードについて多くの仮定を立てているので、試してみてください:

function xyz() {

    var l = scene.children.length
        ;

    //remove everything
    while (l--) {

        if(scene.children[l] instanceof THREE.Camera) continue; //leave camera in the scene

        scene.remove(scene.children[l]);

    }

    //reinitialise your stuff
    init();

}
于 2013-02-11T10:36:36.517 に答える