gpt4 book ai didi

javascript 三个 OrbitControls : doubleclick Object

转载 作者:行者123 更新时间:2023-12-02 14:01:49 24 4
gpt4 key购买 nike

我正在使用 THREE.js 和 OrbitControls.js。我想双击一个对象然后做一些事情。这是我正在使用的代码。 cube1 是我要双击的对象。

var projector = new THREE.Projector();

document.addEventListener('dblclick', ondblclick, false);

function ondblclick(event)
{
var vector = new THREE.Vector3(
( event.clientX / window.innerWidth ) * 2 - 1,
- ( event.clientY / window.innerHeight ) * 2 + 1,
0.5);
projector.unprojectVector( vector, camera );

var ray = new THREE.Ray( camera.position,
vector.subSelf( camera.position ).normalize() );

var intersects = ray.intersectObjects( cube1 );

if ( intersects.length > 0 )
{
alert("hit");
}
}

不幸的是,该代码不起作用。我很感激任何帮助。这是我正在使用的相机:

var camera = new THREE.PerspectiveCamera( 45, WIDTH/HEIGHT, 0.1, 10000 );
camera.position.z = 1500;
camera.position.y = -1800;
scene.add(camera);

这是 OrbitControls:

var controls = new THREE.OrbitControls(camera, renderer.domElement);

最佳答案

我刚刚更正了 ondblclick 函数

function ondblclick(event) {
x = (event.clientX / window.innerWidth) * 2 - 1;
y = -(event.clientY / window.innerHeight) * 2 + 1;
dir = new THREE.Vector3(x, y, -1)
dir.unproject(camera)

ray = new THREE.Raycaster(camera.position, dir.sub(camera.position).normalize())
var intersects = ray.intersectObject(sphere);
if ( intersects.length > 0 )
{
alert("hit");
}
}

这是工作示例:http://jsfiddle.net/ddbTy/241/

关于javascript 三个 OrbitControls : doubleclick Object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40361670/

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