gpt4 book ai didi

javascript - 三.js |通过 blender 对导入的 .obj 模型进行光线转换

转载 作者:行者123 更新时间:2023-12-04 08:41:22 27 4
gpt4 key购买 nike

我已经通过 Blender 加载了一个 3D 地形 OBJLoader .我还创建了一个网格(下图中的黄色指针),我想在鼠标在地形上时跟随它。
我尝试使用 raycaster 方法,但我不知道如何将它应用到我的 .obj 中,因为我似乎无法在加载程序之外访问它。
我怎样才能让我的指针(黄色网格)在跟随鼠标时粘在我的地形(加载的 .obj)上?
请帮助一共三个.js菜鸟...

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();
My loaded .obj terrain

最佳答案

您的代码中有以下行:

var intersects = raycaster.intersectObjects( object );
我没看到 object声明为变量。我建议您在模块范围内声明此变量,将加载的 OBJ 文件分配给它,然后改用此代码:
var intersects = raycaster.intersectObject( object, true );
OBJLoader.load()返回 THREE.Group 类型的对象在某种意义上,它是一个包含任意数量网格的容器。因此,您想要递归地进行光线转换。

关于javascript - 三.js |通过 blender 对导入的 .obj 模型进行光线转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64551586/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com