private onMouseMove()

in tensorflow_similarity/visualization/projector_v2/views/projector.ts [264:303]


  private onMouseMove(event: MouseEvent) {
    // Raycaster expects the mouse to be a number between -1 and 1.
    if (!this.containerRect || !this.camera) return;
    const clientXInDom = event.clientX - this.containerRect.x;
    const clientYInDom = event.clientY - this.containerRect.y;
    const mouse = new THREE.Vector2(
      (clientXInDom / this.containerRect.width) * 2 - 1,
      -(clientYInDom / this.containerRect.height) * 2 + 1
    );

    this.raycaster.setFromCamera(mouse, this.camera);

    // Intersections pick up things like AxesHelper.
    const intersects = this.raycaster.intersectObjects(
      this.pointGroup.children
    );
    const firstMesh = intersects.find(
      (intersection) => intersection.object instanceof THREE.Mesh
    );

    if (this.hoveredMesh === firstMesh?.object) return;
    if (this.hoveredMesh) {
      const pointIndex = this.objectToIndex.get(this.hoveredMesh) ?? -1;
      const metadata = this.metadata[pointIndex];
      if (metadata) {
        this.updateColor(
          this.hoveredMesh,
          new THREE.Color(metadata.color ?? '#f00'),
          DEFAULT_OPACITY
        );
      }
    }

    if (firstMesh) {
      this.hoveredMesh = firstMesh.object as THREE.Mesh;
      this.updateColor(this.hoveredMesh, HOVERED_COLOR, 1);
    } else {
      this.hoveredMesh = null;
    }
  }