Hello, I want to get count click position related to threeD obj, below is what I did:
play3D() {
const domElement = this.nativeElement.children[0];
domElement.innerHTML = '';
this.renderer = new THREE.WebGLRenderer({
antialias: true
});
this.renderer.setPixelRatio(window.devicePixelRatio);
this.renderer.setSize(domElement.offsetWidth, domElement.offsetHeight);
domElement.appendChild(this.renderer.domElement);
this.scene = new THREE.Scene();
this.camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 2000);
this.camera.add(new THREE.PointLight(0xffffff, 0.8));
this.scene.add(this.camera);
this.controls = new OrbitControls(this.camera, this.renderer.domElement);
const loader = new OBJLoader();
loader.load(this.threeDPath, obj => {
this.scene.add(obj);
this.controls.update();
}, this.onProgress);
this.raycaster = new THREE.Raycaster();
this.mouse = new THREE.Vector2();
window.addEventListener('resize', this.onWindowResize, false);
window.addEventListener( 'click', this.onMouseClick, false );
}
onMouseClick( event ) {
this.mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
this.mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
console.log(this.mouse.x, this.mouse.y);
this.raycaster.setFromCamera(this.mouse, this.camera);
// calculate objects intersecting the picking ray
const intersects = this.raycaster.intersectObjects(this.scene.children, true);
console.log(intersects);
for ( let i = 0; i < intersects.length; i++ ) {
intersects[ i ].object.material.color.set( 0xff0000 );
}
}
But when I run it, the intersects are always empty.
So may I ask what is the accurate way to get mouse click position x,y related to the 3D object?
Thanks