viewerLabs.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { PBREnvironment, EnvironmentDeserializer } from "./environmentSerializer";
  2. import { SceneManager } from '../viewer/sceneManager';
  3. import { Tools, Quaternion } from 'babylonjs';
  4. import { ViewerConfiguration } from "../configuration/configuration";
  5. import { TextureUtils } from "./texture";
  6. /**
  7. * The ViewerLabs class will hold functions that are not (!) backwards compatible.
  8. * The APIs in all labs-related classes and configuration might change.
  9. * Once stable, lab features will be moved to the publis API and configuration object.
  10. */
  11. export class ViewerLabs {
  12. constructor(private _sceneManager: SceneManager) { }
  13. public assetsRootURL: string;
  14. public environment: PBREnvironment = {
  15. //irradiance
  16. irradiancePolynomialCoefficients: {
  17. x: new BABYLON.Vector3(0, 0, 0),
  18. y: new BABYLON.Vector3(0, 0, 0),
  19. z: new BABYLON.Vector3(0, 0, 0),
  20. xx: new BABYLON.Vector3(0, 0, 0),
  21. yy: new BABYLON.Vector3(0, 0, 0),
  22. zz: new BABYLON.Vector3(0, 0, 0),
  23. yz: new BABYLON.Vector3(0, 0, 0),
  24. zx: new BABYLON.Vector3(0, 0, 0),
  25. xy: new BABYLON.Vector3(0, 0, 0)
  26. },
  27. textureIntensityScale: 1.0
  28. };
  29. /**
  30. * Loads an environment map from a given URL
  31. * @param url URL of environment map
  32. * @param onSuccess Callback fired after environment successfully applied to the scene
  33. * @param onProgress Callback fired at progress events while loading the environment map
  34. * @param onError Callback fired when the load fails
  35. */
  36. public loadEnvironment(url: string, onSuccess?: (env: PBREnvironment) => void, onProgress?: (bytesLoaded: number, bytesTotal: number) => void, onError?: (e: any) => void): void;
  37. /**
  38. * Loads an environment map from a given URL
  39. * @param buffer ArrayBuffer containing environment map
  40. * @param onSuccess Callback fired after environment successfully applied to the scene
  41. * @param onProgress Callback fired at progress events while loading the environment map
  42. * @param onError Callback fired when the load fails
  43. */
  44. public loadEnvironment(buffer: ArrayBuffer, onSuccess?: (env: PBREnvironment) => void, onProgress?: (bytesLoaded: number, bytesTotal: number) => void, onError?: (e: any) => void): void;
  45. /**
  46. * Sets the environment to an already loaded environment
  47. * @param env PBREnvironment instance
  48. * @param onSuccess Callback fired after environment successfully applied to the scene
  49. * @param onProgress Callback fired at progress events while loading the environment map
  50. * @param onError Callback fired when the load fails
  51. */
  52. public loadEnvironment(env: PBREnvironment, onSuccess?: (env: PBREnvironment) => void, onProgress?: (bytesLoaded: number, bytesTotal: number) => void, onError?: (e: any) => void): void;
  53. public loadEnvironment(data: string | ArrayBuffer | PBREnvironment, onSuccess?: (env: PBREnvironment) => void, onProgress?: (bytesLoaded: number, bytesTotal: number) => void, onError?: (e: any) => void): void {
  54. //@! todo: should loadEnvironment cancel any currently loading environments?
  55. if (data instanceof ArrayBuffer) {
  56. this.environment = EnvironmentDeserializer.Parse(data);
  57. if (onSuccess) onSuccess(this.environment);
  58. } else if (typeof data === 'string') {
  59. let url = this.getAssetUrl(data);
  60. this._sceneManager.scene._loadFile(
  61. url,
  62. (arrayBuffer: ArrayBuffer) => {
  63. this.environment = EnvironmentDeserializer.Parse(arrayBuffer);
  64. if (onSuccess) onSuccess(this.environment);
  65. },
  66. (progressEvent) => { if (onProgress) onProgress(progressEvent.loaded, progressEvent.total); },
  67. false,
  68. true,
  69. (r, e) => {
  70. if (onError) {
  71. onError(e);
  72. }
  73. }
  74. );
  75. } else {
  76. //data assumed to be PBREnvironment object
  77. this.environment = data;
  78. if (onSuccess) onSuccess(data);
  79. }
  80. }
  81. /**
  82. * Applies an `EnvironmentMapConfiguration` to the scene
  83. * @param environmentMapConfiguration Environment map configuration to apply
  84. */
  85. public applyEnvironmentMapConfiguration(rotationY?: number) {
  86. if (!this.environment) return;
  87. //set orientation
  88. let rotatquatRotationionY = Quaternion.RotationAxis(BABYLON.Axis.Y, rotationY || 0);
  89. // Add env texture to the scene.
  90. if (this.environment.specularTexture) {
  91. // IE crashes when disposing the old texture and setting a new one
  92. if (!this._sceneManager.scene.environmentTexture) {
  93. this._sceneManager.scene.environmentTexture = TextureUtils.GetBabylonCubeTexture(this._sceneManager.scene, this.environment.specularTexture, false, true);
  94. }
  95. if (this._sceneManager.scene.environmentTexture) {
  96. this._sceneManager.scene.environmentTexture.level = this.environment.textureIntensityScale;
  97. this._sceneManager.scene.environmentTexture.invertZ = true;
  98. this._sceneManager.scene.environmentTexture.lodLevelInAlpha = true;
  99. var poly = this._sceneManager.scene.environmentTexture.sphericalPolynomial || new BABYLON.SphericalPolynomial();
  100. poly.x = this.environment.irradiancePolynomialCoefficients.x;
  101. poly.y = this.environment.irradiancePolynomialCoefficients.y;
  102. poly.z = this.environment.irradiancePolynomialCoefficients.z;
  103. poly.xx = this.environment.irradiancePolynomialCoefficients.xx;
  104. poly.xy = this.environment.irradiancePolynomialCoefficients.xy;
  105. poly.yy = this.environment.irradiancePolynomialCoefficients.yy;
  106. poly.yz = this.environment.irradiancePolynomialCoefficients.yz;
  107. poly.zx = this.environment.irradiancePolynomialCoefficients.zx;
  108. poly.zz = this.environment.irradiancePolynomialCoefficients.zz;
  109. this._sceneManager.scene.environmentTexture.sphericalPolynomial = poly;
  110. //set orientation
  111. BABYLON.Matrix.FromQuaternionToRef(rotatquatRotationionY, this._sceneManager.scene.environmentTexture.getReflectionTextureMatrix());
  112. }
  113. }
  114. }
  115. /**
  116. * Get an environment asset url by using the configuration if the path is not absolute.
  117. * @param url Asset url
  118. * @returns The Asset url using the `environmentAssetsRootURL` if the url is not an absolute path.
  119. */
  120. public getAssetUrl(url: string): string {
  121. let returnUrl = url;
  122. if (url && url.toLowerCase().indexOf("//") === -1) {
  123. if (!this.assetsRootURL) {
  124. // Tools.Warn("Please, specify the root url of your assets before loading the configuration (labs.environmentAssetsRootURL) or disable the background through the viewer options.");
  125. return url;
  126. }
  127. returnUrl = this.assetsRootURL + returnUrl;
  128. }
  129. return returnUrl;
  130. }
  131. }