Three.js: Proper way to add and remove child objects using THREE.SceneUtils.attach/detach functions

前端 未结 3 1696
無奈伤痛
無奈伤痛 2021-01-01 00:37

Using three.js, and adapting instructions from West Langley\'s post provided here: Three.js: Adding and Removing Children of Rotated Objects, I set up a WebGL scene to whic

3条回答
  •  别那么骄傲
    2021-01-01 00:46

    This is an old post but the sake of search engines here are my thoughts about this

    Looking at the code for THREE.SceneUtils.attach() it appears to me that the code assumes the object that you want to attach is parented to the scene object. This makes it problematic to work with when your objects are actually nested further down in the scene graph. To address this problem I wrote this function

    function reparentObject3D(subject, newParent)
    {
        subject.matrix.copy(subject.matrixWorld);
        subject.applyMatrix(new THREE.Matrix4().getInverse(newParent.matrixWorld));
        newParent.add(subject);
    }
    

    This allows you to reparent an object from any level of the scene graph to any other level in the scene graph. This dispenses with the need to "Detach" objects. You just reparent them to the scene if thats what you need

提交回复
热议问题