三个 JS 在光标上没有网格的情况下获得真实世界的位置?
threeJS get real worl position without mesh on cursor?
code.js
// ...
this.width = 2000
this.height = 2000
// ...
this.camera = new THREE.OrthographicCamera(this.width / -2, this.width / 2, this.height / 2, this.height / -2, 1, 1000 );
this.camera.position.z = 100;
this.camera.position.x = 600;
this.camera.position.y = -900;
this.raycaster = new THREE.Raycaster();
// ...
this.renderer = new THREE.WebGLRenderer({alpha: true, antialias: true});
// ...
this.mouse.x = ( event.clientX / this.renderer.domElement.width) * 2 - 1;
this.mouse.y = - ( event.clientY / this.renderer.domElement.height ) * 2 + 1;
// ...
this.raycaster.setFromCamera( this.mouse, this.camera );
this.intersects = this.raycaster.intersectObjects( this.allElements );
if (this.intersects.length == 0) {
// TODO: how get real world position if don't have mesh on cursor?
JS return 真实像素位置,this.mouse return 局部位置-1..1,如何获得像x: 250, y: 250 not -1 这样的真实世界位置, 1,等等,如果我在光标上没有网格,请使用相机偏移。
我正在使用这个:mouse.x - cameraPos.x, mouse.y + cameraPos.y 并得到不正确的位置。
p.s。对不起,英语太差了。
这段代码对我有用:
let x = e.clientX - 400 + this.camera.position.x - cameraStartPos.x
let y = e.clientY - 100 + -this.camera.position.y - cameraStartPos.y
400 和 100 我不知道,这很神奇!
code.js
// ...
this.width = 2000
this.height = 2000
// ...
this.camera = new THREE.OrthographicCamera(this.width / -2, this.width / 2, this.height / 2, this.height / -2, 1, 1000 );
this.camera.position.z = 100;
this.camera.position.x = 600;
this.camera.position.y = -900;
this.raycaster = new THREE.Raycaster();
// ...
this.renderer = new THREE.WebGLRenderer({alpha: true, antialias: true});
// ...
this.mouse.x = ( event.clientX / this.renderer.domElement.width) * 2 - 1;
this.mouse.y = - ( event.clientY / this.renderer.domElement.height ) * 2 + 1;
// ...
this.raycaster.setFromCamera( this.mouse, this.camera );
this.intersects = this.raycaster.intersectObjects( this.allElements );
if (this.intersects.length == 0) {
// TODO: how get real world position if don't have mesh on cursor?
JS return 真实像素位置,this.mouse return 局部位置-1..1,如何获得像x: 250, y: 250 not -1 这样的真实世界位置, 1,等等,如果我在光标上没有网格,请使用相机偏移。 我正在使用这个:mouse.x - cameraPos.x, mouse.y + cameraPos.y 并得到不正确的位置。
p.s。对不起,英语太差了。
这段代码对我有用:
let x = e.clientX - 400 + this.camera.position.x - cameraStartPos.x
let y = e.clientY - 100 + -this.camera.position.y - cameraStartPos.y
400 和 100 我不知道,这很神奇!