import { Vector3Like } from "@here/harp-geoutils" ; import * as THREE from "three" ; param ndc function isOnScreen ( ndc ) { return ndc . z > - 1 && ndc . z < 1 && ndc . x >= - 1 && ndc . x <= 1 && ndc . y >= - 1 && ndc . y <= 1 ; } param ndc function isInRange ( ndc ) { return ndc . z > - 1 && ndc . z < 1 ; } hidden export class ScreenProjector { static tempV2 = new THREE . Vector2 ( ) ; static tempV3 = new THREE . Vector3 ( ) ; private m_width : number = 0 ; private m_height : number = 0 ; param m_camera constructor ( private m_camera ) { } /**
     * Height of the screen.
     */ get width ( ) : number { return this . m_width ; } /**
     * Width of the screen.
     */ get height ( ) : number { return this . m_height ; } param { ( Vector3Like ) } source param { THREE . Vector2 } target returns { THREE . Vector2 } project ( source , target = new THREE . Vector2 ( ) ) { const p = this . projectVector ( source , ScreenProjector . tempV3 ) ; return this . ndcToScreen ( p , target ) ; } param { ( Vector3Like ) } source param { THREE . Vector2 } target returns { THREE . Vector2 } projectToScreen ( source , target = new THREE . Vector2 ( ) ) { const p = this . projectVector ( source , ScreenProjector . tempV3 ) ; if ( isInRange ( p ) ) { return this . ndcToScreen ( p , target ) ; } return undefined ; } param { ( Vector3Like ) } source param { ( Number ) } halfWidth param { ( Number ) } halfHeight param { THREE . Vector2 } target returns { THREE . Vector2 } projectAreaToScreen ( source , halfWidth , halfHeight , target = new THREE . Vector2 ( ) ) { halfWidth *= 2 ; halfHeight *= 2 ; const p = this . projectVector ( source , ScreenProjector . tempV3 ) ; if ( isInRange ( p ) && p . x + halfWidth >= - 1 && p . x - halfWidth <= 1 && p . y + halfHeight >= - 1 && p . y - halfHeight <= 1 ) { return this . ndcToScreen ( p , target ) ; } return undefined ; } param { ( Vector3Like ) } source param { THREE . Vector3 } target returns { THREE . Vector3 } project3 ( source , target = new THREE . Vector3 ( ) ) { const p = this . projectVector ( source , ScreenProjector . tempV3 ) ; if ( p . z > - 1 && p . z < 1 ) { target . set ( ( p . x * this . m_width ) / 2 , ( p . y * this . m_height ) / 2 , p . z ) ; return target ; } return undefined ; } param { ( Vector3Like ) } source param { THREE . Vector3 } target returns { THREE . Vector3 } projectVector ( source , target ) { target . set ( source . x , source . y , source . z ) . project ( this . m_camera ) ; return target ; } returns { boolean } onScreen ( source ) { const p = this . projectVector ( source , ScreenProjector . tempV3 ) ; return isOnScreen ( p ) ; } param { THREE . Camera } camera param { number } width param { number } height update ( camera , width , height ) { this . m_camera = camera ; this . m_width = width ; this . m_height = height ; } private ndcToScreen ( ndc , screenCoords ) { return screenCoords . set ( ( ndc . x * this . m_width ) / 2 , ( ndc . y * this . m_height ) / 2 ) ; } }