2016-06-12 63 views
0

我正在做增強現實,需要將Object3D添加到另一個對象進行渲染。threejs Colladaloader:如何將場景變成object3d

我使用ColladaLoader導入對象;然而,返回類型是Scene - 不是我所需要的。

如何將場景轉換爲Object3D

這裏是我當前的代碼:

var mouse; 
var mouseObject; 

var loader = new THREE.ColladaLoader(); 

// Need to convert the axes so that our model does not stand upside-down: 
loader.options.convertUpAxis = true; 

// Load the 3D collada file (robot01.dae in my example), and specify 
// the callback function that is called once the model has loaded: 
loader.load('assets/mouse_v1_model/meshes/mouse_v1_model_animated.dae', function (collada) { 

    // Grab the collada scene data: 
    mouse = collada.scene.children[0]; 

    // No skin applied to my model so no need for the following: 
    // var skin = collada.skins[ 0 ]; 

    mouseObject = new THREE.Object3D(); 

    for (var j = 0; j < mouse.children.length; j++) { 
     mouseObject.add(new THREE.Mesh(mouse.children[j].geometry, mouse.children[j].material)); 
    } 

    mouseObject.matrixAutoUpdate = false; 
    mouseObject.position.z = 100; 

    // Scale-up the model so that we can see it: 
    /* mouse.scale.x = mouse.scale.y = mouse.scale.z = 3.0; 
    mouse.rotation.z = 360; 
    mouse.rotation.x = 180; 
    mouse.rotation.y = 180; 
    mouse.position.x = 0; 
    mouse.position.y = -10; 
    mouse.position.z = 100; 
    mouse.updateMatrix();*/ 

    //arScene.scene.add(mouseObject); 

}); 

// Load the marker to use. 
arController.loadMultiMarker('jsartoolkit5/examples/Data/multi/marker.dat', function(marker, markerNum) { 

    window.onclick = function() { 
     arScene.video.play(); 
    }; 

    // Create an object that tracks the marker transform. 
    var markerRoot = arController.createThreeMultiMarker(marker); 
    arScene.scene.add(markerRoot); 

    for (var i=0; i<markerNum; i++) { 

     markerRoot.markers[i] = mouseObject; 
     markerRoot.add(markerRoot.markers[i]); 
    } 

    // Call arScene.renderOn on each frame, 
    // it does marker detection, updates the Three.js scene and draws a new frame. 
    var tick = function() { 
     requestAnimationFrame(tick); 

     arScene.process(); 
     arScene.renderOn(renderer); 
    }; 
    tick(); 

});  
+0

您可以在現場得到你的對象加載器回調,如果你像這個'result.children'那樣訪問加載結果。 – Wilt

+0

我就是這麼做的;看到我爲問題添加的代碼片段。 –

+0

我用一個例子添加了一個答案... – Wilt

回答

0

您可以從加載的場景對象做出THREE.Group類型的容器對象。
事情是這樣的:

function onLoadCallback(result){ 
    // Create a group object as a container 
    var container = new THREE.Group(); 
    container.name = 'container'; 
    container.children = result.children; 

    // Add it to your existing scene 
    scene.add(container); 
} 

loader.load('assets/mouse_v1_model/meshes/mouse_v1_model_animated.dae', onLoadCallback); 
0

如果你把自己的COLLADA(例如,從攪拌機出口),那麼我真的會建議你試試ThreeJS出口(看here)。

或者,你可以看看你的COLLADA內(畢竟,它只是一個XML),只是你的對象添加到您的場景是這樣的:

scene.add(collada.children.YOUR_OBJECT);