Skip to content

Action click with Raycaster #7

@rtalon83

Description

@rtalon83

I'm doing a hit event over the 3D-Object. About the THREE.js documentation, it says to use Raycaster following the next code. But there are problems with de pitch of maps.

this._canvas.addEventListener('click', this.onCanvasClick);

this._camera = new THREE.PerspectiveCamera(
        this._mapView.map.transform.fov,
        this._mapView.map.transform.width / this._mapView.map.transform.height,
        0.1,
        //1000
        1e21
    );
this._camera.matrixAutoUpdate = false

this._raycaster = new THREE.Raycaster();
this._mouse = new THREE.Vector2( 1, 1 );

onCanvasClick(event) {
    event.preventDefault();       
    this._mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
    this._mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
    
    this._raycaster.setFromCamera(this._mouse, this._camera);
    const intersects = this._raycaster.intersectObjects(this._world.children, true);
    
    if (intersects.length > 0) {
        const intersectedObject = intersects[0].object;
        console.log('Object!', intersectedObject);
    }     
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions