viewerLabs.ts 7.0 KB

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