简体   繁体   中英

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 real pixel position, this.mouse return local position -1..1, how get real world position like x: 250, y: 250 not -1,1,etc if i'am don't have mesh on cursor use camera offset. I am use this: mouse.x - cameraPos.x, mouse.y + cameraPos.y and get incorrect position.

ps Sorry for very bad english.

This code work for me:

let x = e.clientX - 400 + this.camera.position.x - cameraStartPos.x
let y = e.clientY - 100 + -this.camera.position.y - cameraStartPos.y

400 and 100 i'am don't know, mb it a magic!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM