babylon.cubeTexture.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. module BABYLON {
  2. export class CubeTexture extends BaseTexture {
  3. public url: string;
  4. public coordinatesMode = Texture.CUBIC_MODE;
  5. /**
  6. * Gets or sets the center of the bounding box associated with the cube texture
  7. * It must define where the camera used to render the texture was set
  8. */
  9. public boundingBoxPosition = Vector3.Zero();
  10. private _boundingBoxSize: Vector3;
  11. /**
  12. * Gets or sets the size of the bounding box associated with the cube texture
  13. * When defined, the cubemap will switch to local mode
  14. * @see https://community.arm.com/graphics/b/blog/posts/reflections-based-on-local-cubemaps-in-unity
  15. * @example https://www.babylonjs-playground.com/#RNASML
  16. */
  17. public set boundingBoxSize(value: Vector3) {
  18. if (this._boundingBoxSize && this._boundingBoxSize.equals(value)) {
  19. return;
  20. }
  21. this._boundingBoxSize = value;
  22. let scene = this.getScene();
  23. if (scene) {
  24. scene.markAllMaterialsAsDirty(Material.TextureDirtyFlag);
  25. }
  26. }
  27. public get boundingBoxSize(): Vector3 {
  28. return this._boundingBoxSize;
  29. }
  30. private _noMipmap: boolean;
  31. private _files: string[];
  32. private _extensions: string[];
  33. private _textureMatrix: Matrix;
  34. private _format: number;
  35. private _prefiltered: boolean;
  36. public static CreateFromImages(files: string[], scene: Scene, noMipmap?: boolean) {
  37. let rootUrlKey = "";
  38. files.forEach(url => rootUrlKey += url);
  39. return new CubeTexture(rootUrlKey, scene, null, noMipmap, files);
  40. }
  41. public static CreateFromPrefilteredData(url: string, scene: Scene, forcedExtension: any = null) {
  42. return new CubeTexture(url, scene, null, false, null, null, null, undefined, true, forcedExtension);
  43. }
  44. constructor(rootUrl: string, scene: Scene, extensions: Nullable<string[]> = null, noMipmap: boolean = false, files: Nullable<string[]> = null,
  45. onLoad: Nullable<() => void> = null, onError: Nullable<(message?: string, exception?: any) => void> = null, format: number = Engine.TEXTUREFORMAT_RGBA, prefiltered = false, forcedExtension: any = null) {
  46. super(scene);
  47. this.name = rootUrl;
  48. this.url = rootUrl;
  49. this._noMipmap = noMipmap;
  50. this.hasAlpha = false;
  51. this._format = format;
  52. this._prefiltered = prefiltered;
  53. this.isCube = true;
  54. this._textureMatrix = Matrix.Identity();
  55. if (prefiltered) {
  56. this.gammaSpace = false;
  57. }
  58. if (!rootUrl && !files) {
  59. return;
  60. }
  61. this._texture = this._getFromCache(rootUrl, noMipmap);
  62. const lastDot = rootUrl.lastIndexOf(".");
  63. const extension = forcedExtension ? forcedExtension : (lastDot > -1 ? rootUrl.substring(lastDot).toLowerCase() : "");
  64. const isDDS = (extension === ".dds");
  65. if (!files) {
  66. if (!isDDS && !extensions) {
  67. extensions = ["_px.jpg", "_py.jpg", "_pz.jpg", "_nx.jpg", "_ny.jpg", "_nz.jpg"];
  68. }
  69. files = [];
  70. if (extensions) {
  71. for (var index = 0; index < extensions.length; index++) {
  72. files.push(rootUrl + extensions[index]);
  73. }
  74. }
  75. }
  76. this._files = files;
  77. if (!this._texture) {
  78. if (!scene.useDelayedTextureLoading) {
  79. if (prefiltered) {
  80. this._texture = scene.getEngine().createPrefilteredCubeTexture(rootUrl, scene, this.lodGenerationScale, this.lodGenerationOffset, onLoad, onError, format, forcedExtension);
  81. }
  82. else {
  83. this._texture = scene.getEngine().createCubeTexture(rootUrl, scene, files, noMipmap, onLoad, onError, this._format, forcedExtension);
  84. }
  85. } else {
  86. this.delayLoadState = Engine.DELAYLOADSTATE_NOTLOADED;
  87. }
  88. } else if (onLoad) {
  89. if (this._texture.isReady) {
  90. Tools.SetImmediate(() => onLoad());
  91. } else {
  92. this._texture.onLoadedObservable.add(onLoad);
  93. }
  94. }
  95. }
  96. // Methods
  97. public delayLoad(): void {
  98. if (this.delayLoadState !== Engine.DELAYLOADSTATE_NOTLOADED) {
  99. return;
  100. }
  101. let scene = this.getScene();
  102. if (!scene) {
  103. return;
  104. }
  105. this.delayLoadState = Engine.DELAYLOADSTATE_LOADED;
  106. this._texture = this._getFromCache(this.url, this._noMipmap);
  107. if (!this._texture) {
  108. if (this._prefiltered) {
  109. this._texture = scene.getEngine().createPrefilteredCubeTexture(this.url, scene, this.lodGenerationScale, this.lodGenerationOffset, undefined, undefined, this._format);
  110. }
  111. else {
  112. this._texture = scene.getEngine().createCubeTexture(this.url, scene, this._files, this._noMipmap, undefined, undefined, this._format);
  113. }
  114. }
  115. }
  116. public getReflectionTextureMatrix(): Matrix {
  117. return this._textureMatrix;
  118. }
  119. public setReflectionTextureMatrix(value: Matrix): void {
  120. this._textureMatrix = value;
  121. }
  122. public static Parse(parsedTexture: any, scene: Scene, rootUrl: string): CubeTexture {
  123. var texture = SerializationHelper.Parse(() => {
  124. return new CubeTexture(rootUrl + parsedTexture.name, scene, parsedTexture.extensions);
  125. }, parsedTexture, scene);
  126. // Local Cubemaps
  127. if (parsedTexture.boundingBoxPosition) {
  128. texture.boundingBoxPosition = BABYLON.Vector3.FromArray(parsedTexture.boundingBoxPosition);
  129. }
  130. if (parsedTexture.boundingBoxSize) {
  131. texture.boundingBoxSize = BABYLON.Vector3.FromArray(parsedTexture.boundingBoxSize);
  132. }
  133. // Animations
  134. if (parsedTexture.animations) {
  135. for (var animationIndex = 0; animationIndex < parsedTexture.animations.length; animationIndex++) {
  136. var parsedAnimation = parsedTexture.animations[animationIndex];
  137. texture.animations.push(Animation.Parse(parsedAnimation));
  138. }
  139. }
  140. return texture;
  141. }
  142. public clone(): CubeTexture {
  143. return SerializationHelper.Clone(() => {
  144. let scene = this.getScene();
  145. if (!scene) {
  146. return this;
  147. }
  148. return new CubeTexture(this.url, scene, this._extensions, this._noMipmap, this._files);
  149. }, this);
  150. }
  151. }
  152. }