Three.js |光线投射通过混合器导入.obj模型



我已经通过带有OBJLoader的Blender加载了一个3D地形。我还创建了一个网格(下图中的黄色指针(,当鼠标在地形上时,我想跟随它。我尝试使用raycaster方法,但我不知道如何将其应用于我的.obj,因为我似乎无法在加载程序之外访问它。

当我的指针(黄色网格(跟随鼠标时,我如何让它粘在我的地形(loaded.obj(上?

请帮助总共三个.js noob。。。

var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 10000 );
var renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
camera.position.z = 150;
camera.position.y=300;
camera.position.x=350;
var light = new THREE.HemisphereLight( 0xffffbb, 0x080820, 1 );
scene.add( light );
var controls= new THREE.OrbitControls(camera,renderer.domElement);
controls.enableDamping=true;
controls.campingFactor=0.25;
controls.enableZoom=true;
controls.minDistance= 1;
controls.maxDistance=3000;
controls.minPolarAngle= -Math.PI/2;
controls.maxPolarAngle= Math.PI/2;
var terrain;
var mtlLoader = new THREE.MTLLoader();
mtlLoader.load('models/terrain.mtl',
    (materials) => {
        materials.preload();
    
        var loader = new THREE.OBJLoader();
        loader.setMaterials(materials);
        loader.load(
                'models/terrain.obj',
        function ( object ) {
        terrain = object;
        scene.add( terrain );
        });
    }
); 

var Cylindergeometry = new THREE.CylinderGeometry( 5, 0, 8, 32 );
var material = new THREE.MeshBasicMaterial( {color: 0xffff00} );
var cylinder = new THREE.Mesh( Cylindergeometry, material );
  
var Torusgeometry = new THREE.TorusGeometry( 7, 0.5, 8, 6);
var material = new THREE.MeshBasicMaterial( { color: 0xffff00 } );
var torus = new THREE.Mesh( Torusgeometry, material);
Torusgeometry.rotateX(1.5708);
Torusgeometry.translate(0,-4.5,0);
  //Merge the two parts to make it one mesh (yellow pivot)
var PivotGeometry = new THREE.Geometry();
PivotGeometry.merge(Cylindergeometry);
PivotGeometry.merge(Torusgeometry);
var pivot = new THREE.Mesh(PivotGeometry, material);
scene.add(pivot);  

// Trying to Raycast the terrain to make the pivot follow the mouse while it's on it 
var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2();
function onMouseMove( event ) {
    mouse.x = ( event.clientX / renderer.domElement.clientWidth ) * 2 - 1;
    mouse.y = - ( event.clientY / renderer.domElement.clientHeight ) * 2 + 1;
    raycaster.setFromCamera( mouse, camera );
    // See if the ray from the camera into the world hits one of our meshes
    var intersects = raycaster.intersectObject( terrain,true );
    // Toggle rotation bool for meshes that we clicked
    if ( intersects.length > 0 ) {
        
        pivot.position.set( 0, 0, 0 );
        pivot.lookAt( intersects[ 0 ].face.normal );
        pivot.position.copy( intersects[ 0 ].point );
    }
}
//Met à jour l'affichage de la scène
var animate = function () {
    requestAnimationFrame( animate );
    renderer.render( scene, camera );
}; 
animate(); 

我装载的.obj地形

您的代码中有以下行:

var intersects = raycaster.intersectObjects( object );

我看不出object作为变量是在哪里声明的。我建议您在模块范围中声明此变量,将加载的OBJ文件分配给它,然后使用以下代码:

var intersects = raycaster.intersectObject( object, true );

OBJLoader.load()返回类型为THREE.Group的对象,该对象在某种意义上是容纳任意数量网格的容器。因此,您希望递归地进行光线投射。

相关内容

  • 没有找到相关文章

最新更新