我有一个使用sketchup 创建的建筑模型,导出为collada 文件并使用colladaloader 加载到three.js。一切正常,除了相机可以穿过墙壁。我该如何防止这种情况?到目前为止,我已经尝试过使用 Raycasting,但我认为有问题。
这就是模型的加载方式
loader.options.convertUpAxis = true;
loader.load( 'model/cityhall.dae', function ( collada ) {
dae = collada.scene;
skin = collada.skins[ 0 ];
model_geometry = collada.scene.children[ 0 ].children[0].geometry;
model_material = collada.scene.children[ 0 ].children[0].material;
dae.scale.x = dae.scale.y = dae.scale.z = 1;
dae.updateMatrix();
scene.add( dae );
dae.traverse(function (child) {
if (child instanceof THREE.Mesh) {
objects.push(child);
}
});
});
光线投射器
rayFloor = new THREE.Raycaster();
rayFloor.ray.direction.set( 0, -1, 0 );
rayWall = new THREE.Raycaster();
rayWall.ray.direction.set(0,0,1);
这是动画
function animate() {
requestAnimationFrame( animate );
controls.isOnObject( false );
controls.isWallCollision( false);
rayFloor.ray.origin.copy( controls.getObject().position );
rayFloor.ray.origin.x -= 10;
rayWall.ray.origin.copy( controls.getObject().position );
rayWall.ray.origin.y -= 8;
var intersections = rayFloor.intersectObjects( objects );
var intersections2 = rayWall.intersectObjects( objects );
if ( intersections.length > 0 ) {
//console.log('floor' + intersections);
var distance = intersections[ 0 ].distance;
if ( distance > 0 && distance < 10 ) {
controls.isOnObject( true );
}
}
if ( intersections2.length > 0 ) {
//console.log('wall' + intersections);
var distance = intersections2[ 0 ].distance;
if ( distance > 0 && distance < 10 ) {
controls.isWallCollision( true );
}
}
controls.update( Date.now() - time );
render();
time = Date.now();
}
问题是它无法正确检测墙壁和地板。