author Yannick Deubel (https://github.com/yandeu)
 *  copyright license https import type { WebGLRenderer } from 'three' ; import type { OrbitControls } from 'three/examples/jsm/controls/OrbitControls' ; import { Scene , OrthographicCamera , Vector2 } from 'three' ; import Cameras from '../plugins/cameras' ; import { setOrbitControls , setParent } from './_misc' ; import { updateEvents , setSize } from './_misc' ; export { updateEvents , getParent , destroy , setSize } from './_misc' ; export interface FlatInitConfig { width ? : number ; height ? : number ; /**
     * Size of the renderer.
     * Usually: renderer.getSize(new Vector2())
     */ size ? : { x : number ; y : number ; } ; } export interface FlatArea { camera : OrthographicCamera ; scene : Scene ; } /** Init the 2D element support. */ export const init = ( renderer , config = { } ) : FlatArea => { renderer . autoClear = false ; const { width = window . innerWidth , height = window . innerHeight , size = renderer . getSize ( new Vector2 ( ) ) } = config ; setSize ( size . x , size . y ) ; return { camera : Cameras . Orthographic ( { left : 0 , right : width , bottom : 0 , top : height } ) , scene : new Scene ( ) } ; } ; /** Use this if you need events on the 2D elements. */ export const initEvents = ( { canvas , orbitControls } ) => { setOrbitControls ( orbitControls ) ; setParent ( canvas ) ; } ; export const preRender = ( renderer ) => { renderer . clear ( ) ; } ; export const postRender = ( renderer , flatArea ) => { if ( flatArea && flatArea . scene && flatArea . camera ) { renderer . clearDepth ( ) ; renderer . render ( flatArea . scene , flatArea . camera ) ; updateEvents ( flatArea . camera ) ; } } ;