Three.js: Adding and Removing Children of Rotated Objects

前端 未结 1 1510
名媛妹妹
名媛妹妹 2020-11-28 12:44

I\'m trying to simulate a rubik\'s cube. I wanted to choose a face at random and rotate it. So I\'ve created 27 cube meshes and positioned them. You can see the working (err

相关标签:
1条回答
  • 2020-11-28 13:34

    The trick is to make use of THREE.Object3D.attach().

    You need to begin with all your 27 cubes as a child of the scene.

    Let pivot be an Object3D().

    Let active be an array containing the 9 cubes you want to rotate. Now make the cubes a child of the pivot:

    pivot.rotation.set( 0, 0, 0 );
    pivot.updateMatrixWorld();
    
    for ( var i in active ) {
    
        pivot.attach( active[ i ] );
    
    }
    

    Then, after the rotation, put the cubes back as children of the scene.

    pivot.updateMatrixWorld();
    
    for ( var i in active ) {
    
        scene.attach( active[ i ] );
    
    }
    

    three.js r.115

    0 讨论(0)
提交回复
热议问题