import { PBREnvironment , EnvironmentDeserializer } from "./environmentSerializer" ; import { Scene } from "babylonjs/scene" ; import { Vector3 , Quaternion , Matrix , TmpVectors } from "babylonjs/Maths/math.vector" ; import { SphericalPolynomial } from "babylonjs/Maths/sphericalPolynomial" ; import { ShadowLight } from "babylonjs/Lights/shadowLight" ; import { TextureUtils } from "./texture" ; import { Axis } from "babylonjs/Maths/math.axis" ; /**
 * The ViewerLabs class will hold functions that are not (!) backwards compatible.
 * The APIs in all labs-related classes and configuration  might change.
 * Once stable, lab features will be moved to the publis API and configuration object.
 */ export class ViewerLabs { constructor ( private _scene ) { } public assetsRootURL : string ; public environment : PBREnvironment = { irradiancePolynomialCoefficients : { x : new Vector3 ( 0 , 0 , 0 ) , y : new Vector3 ( 0 , 0 , 0 ) , z : new Vector3 ( 0 , 0 , 0 ) , xx : new Vector3 ( 0 , 0 , 0 ) , yy : new Vector3 ( 0 , 0 , 0 ) , zz : new Vector3 ( 0 , 0 , 0 ) , yz : new Vector3 ( 0 , 0 , 0 ) , zx : new Vector3 ( 0 , 0 , 0 ) , xy : new Vector3 ( 0 , 0 , 0 ) } , textureIntensityScale : 1.0 } ; param url param onSuccess param onProgress param onError public loadEnvironment ( url , onSuccess ? , onProgress ? , onError ? ) ; param buffer param onSuccess param onProgress param onError public loadEnvironment ( buffer , onSuccess ? , onProgress ? , onError ? ) ; param env param onSuccess param onProgress param onError public loadEnvironment ( env , onSuccess ? , onProgress ? , onError ? ) ; public loadEnvironment ( data , onSuccess ? , onProgress ? , onError ? ) { if ( data instanceof ArrayBuffer ) { this . environment = EnvironmentDeserializer . Parse ( data ) ; if ( onSuccess ) { onSuccess ( this . environment ) ; } } else if ( typeof data === 'string' ) { let url = this . getAssetUrl ( data ) ; this . _scene . _loadFile ( url , ( arrayBuffer ) => { this . environment = EnvironmentDeserializer . Parse ( arrayBuffer ) ; if ( onSuccess ) { onSuccess ( this . environment ) ; } } , ( progressEvent ) => { if ( onProgress ) { onProgress ( progressEvent . loaded , progressEvent . total ) ; } } , false , true , ( r , e ) => { if ( onError ) { onError ( e ) ; } } ) ; } else { this . environment = data ; if ( onSuccess ) { onSuccess ( data ) ; } } } param environmentMapConfiguration public applyEnvironmentMapConfiguration ( rotationY ? ) { if ( ! this . environment ) { return ; } let rotatquatRotationionY = Quaternion . RotationAxis ( Axis . Y , rotationY || 0 ) ; if ( this . environment . specularTexture ) { if ( ! this . _scene . environmentTexture ) { this . _scene . environmentTexture = TextureUtils . GetBabylonCubeTexture ( this . _scene , this . environment . specularTexture , false , true ) ; } if ( this . _scene . environmentTexture ) { this . _scene . environmentTexture . level = this . environment . textureIntensityScale ; this . _scene . environmentTexture . invertZ = true ; this . _scene . environmentTexture . lodLevelInAlpha = true ; var poly = this . _scene . environmentTexture . sphericalPolynomial || new SphericalPolynomial ( ) ; poly . x = this . environment . irradiancePolynomialCoefficients . x ; poly . y = this . environment . irradiancePolynomialCoefficients . y ; poly . z = this . environment . irradiancePolynomialCoefficients . z ; poly . xx = this . environment . irradiancePolynomialCoefficients . xx ; poly . xy = this . environment . irradiancePolynomialCoefficients . xy ; poly . yy = this . environment . irradiancePolynomialCoefficients . yy ; poly . yz = this . environment . irradiancePolynomialCoefficients . yz ; poly . zx = this . environment . irradiancePolynomialCoefficients . zx ; poly . zz = this . environment . irradiancePolynomialCoefficients . zz ; this . _scene . environmentTexture . sphericalPolynomial = poly ; Matrix . FromQuaternionToRef ( rotatquatRotationionY , this . _scene . environmentTexture . getReflectionTextureMatrix ( ) ) ; } } } param url returns public getAssetUrl ( url ) { let returnUrl = url ; if ( url && url . toLowerCase ( ) . indexOf ( "//" ) === - 1 ) { if ( ! this . assetsRootURL ) { return url ; } returnUrl = this . assetsRootURL + returnUrl ; } return returnUrl ; } public rotateShadowLight ( shadowLight , amount , point = Vector3 . Zero ( ) , axis = Axis . Y , target = Vector3 . Zero ( ) ) { axis . normalize ( ) ; point . subtractToRef ( shadowLight . position , TmpVectors . Vector3 [ 0 ] ) ; Matrix . TranslationToRef ( TmpVectors . Vector3 [ 0 ] . x , TmpVectors . Vector3 [ 0 ] . y , TmpVectors . Vector3 [ 0 ] . z , TmpVectors . Matrix [ 0 ] ) ; TmpVectors . Matrix [ 0 ] . invertToRef ( TmpVectors . Matrix [ 2 ] ) ; Matrix . RotationAxisToRef ( axis , amount , TmpVectors . Matrix [ 1 ] ) ; TmpVectors . Matrix [ 2 ] . multiplyToRef ( TmpVectors . Matrix [ 1 ] , TmpVectors . Matrix [ 2 ] ) ; TmpVectors . Matrix [ 2 ] . multiplyToRef ( TmpVectors . Matrix [ 0 ] , TmpVectors . Matrix [ 2 ] ) ; TmpVectors . Matrix [ 2 ] . decompose ( TmpVectors . Vector3 [ 0 ] , TmpVectors . Quaternion [ 0 ] , TmpVectors . Vector3 [ 1 ] ) ; shadowLight . position . addInPlace ( TmpVectors . Vector3 [ 1 ] ) ; shadowLight . setDirectionToTarget ( target ) ; } }