gpt4 book ai didi

typescript - Raycaster 不检测旋转的物体

转载 作者:行者123 更新时间:2023-12-04 07:23:22 26 4
gpt4 key购买 nike

我是 Three.js 的新手,我想找出为什么光线转换找不到旋转网格对象的某些部分。例如,在下图中,检测到鼠标位于绿色圆圈的位置,就像它在盒子上一样,所以 intersectObjects 返回包含该盒子的数组。否则,鼠标在红色圆圈位置的行为就像它不在框上一样,intersectObjects 返回空数组。

BoxGeometry

import * as THREE from 'three'
import { Mesh, MeshLambertMaterial } from 'three';

var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, .1, 1000);
camera.position.z = 5;

var renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setClearColor("#e5e5e5");
renderer.setSize(window.innerWidth, window.innerHeight);

document.body.appendChild(renderer.domElement);

var raycaser = new THREE.Raycaster();
var mouse = new THREE.Vector2();

var geometry = new THREE.BoxGeometry(3.5, .5, 3);
var material = new THREE.MeshLambertMaterial({ color: 0xFFCC00 });
var mesh = new THREE.Mesh(geometry, material);
mesh.rotation.set(.5, .8, 0);
scene.add(mesh);

var light = new THREE.PointLight(0xFFFFFF, 1, 500);
light.position.set(0, 1, 7);
scene.add(light);

var render = function () {
requestAnimationFrame(render);
renderer.render(scene, camera);
}
render();

document.body.addEventListener("mousemove", (event) => {
event.preventDefault();

mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = (event.clientY / window.innerHeight) * 2 - 1;
raycaser.setFromCamera(mouse, camera);

var intersects = Array.from(raycaser.intersectObjects(scene.children, true)).map(inter => inter.object as Mesh);

document.body.style.cursor = intersects.length > 0 ? "pointer" : "default";

for (var i = 0; i < scene.children.length - 1; i++) {
var object = scene.children[i] as Mesh;
var material = object.material as MeshLambertMaterial;
material.emissive.set(intersects.includes(object) ? 0x666666 : 0x000000);
}
});

也许这不是因为对象的旋转,但仍然如何实现 Raycaster 的正确行为?

最佳答案

您必须通过使用 getBoundingClientRect() 来稍微不同地计算鼠标 坐标。查看以下实例:

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, .1, 1000);
camera.position.z = 5;

const renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setClearColor("#e5e5e5");
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);

document.body.appendChild(renderer.domElement);

const raycaser = new THREE.Raycaster();
const mouse = new THREE.Vector2();

const geometry = new THREE.BoxGeometry(3.5, .5, 3);
const material = new THREE.MeshLambertMaterial({
color: 0xFFCC00
});
const mesh = new THREE.Mesh(geometry, material);
mesh.rotation.set(.5, .8, 0);
scene.add(mesh);

const light = new THREE.PointLight(0xFFFFFF, 1, 500);
light.position.set(0, 1, 7);
scene.add(light);

function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
animate();

document.body.addEventListener("pointermove", (event) => {

const rect = renderer.domElement.getBoundingClientRect();
mouse.x = ((event.clientX - rect.left) / (rect.right - rect.left)) * 2 - 1;
mouse.y = -((event.clientY - rect.top) / (rect.bottom - rect.top)) * 2 + 1;

raycaser.setFromCamera(mouse, camera);

const intersects = raycaser.intersectObject(scene, true);

document.body.style.cursor = intersects.length > 0 ? "pointer" : "default";

});
body {
margin: 0;
}
<script src="https://cdn.jsdelivr.net/npm/three@0.130.1/build/three.js"></script>

关于typescript - Raycaster 不检测旋转的物体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68364000/

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