我正在使用three.js 框架,并将渲染器放在一个占据屏幕右侧50% 且高度为87.5% 的div 中。然后我试图在单击的任何位置放置一个球体,但是计算出的坐标在 div 内并不准确,并且球体出现在远离鼠标的各个位置。如何准确计算此 div 内的坐标?谢谢,这是代码
function onDocumentMouseMove( event ) {
event.preventDefault();
mouseX = (event.clientX / container.offsetWidth) * 2 - 1;
mouseY = -(event.clientY / container.offsetHeight) * 2 - 1;
}
function onDocumentMouseDown(event){
event.preventDefault()
alert("X: " + mouseX + " Y: " + mouseY);
var vector = new THREE.Vector3( mouseX, mouseY, 1 );
var sphere = new THREE.Mesh(new THREE.SphereGeometry(size / 4), new THREE.MeshLambertMaterial(intensity));
sphere.position.x = vector.x;
sphere.position.y = vector.y;
sphere.position.z = vector.z;
scene.add(sphere);
spheres.push(sphere);
}