environmentTextureTools.ts 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. import { Nullable } from "../types";
  2. import { Tools } from "./tools";
  3. import { Vector3 } from "../Maths/math.vector";
  4. import { Scalar } from "../Maths/math.scalar";
  5. import { SphericalPolynomial } from "../Maths/sphericalPolynomial";
  6. import { InternalTexture, InternalTextureSource } from "../Materials/Textures/internalTexture";
  7. import { BaseTexture } from "../Materials/Textures/baseTexture";
  8. import { Constants } from "../Engines/constants";
  9. import { Scene } from "../scene";
  10. import { PostProcess } from "../PostProcesses/postProcess";
  11. import { Logger } from "../Misc/logger";
  12. import "../Engines/Extensions/engine.renderTargetCube";
  13. import "../Engines/Extensions/engine.readTexture";
  14. import "../Materials/Textures/baseTexture.polynomial";
  15. import "../Shaders/rgbdEncode.fragment";
  16. import "../Shaders/rgbdDecode.fragment";
  17. import { Engine } from '../Engines/engine';
  18. import { ThinEngine } from '../Engines/thinEngine';
  19. /**
  20. * Raw texture data and descriptor sufficient for WebGL texture upload
  21. */
  22. export interface EnvironmentTextureInfo {
  23. /**
  24. * Version of the environment map
  25. */
  26. version: number;
  27. /**
  28. * Width of image
  29. */
  30. width: number;
  31. /**
  32. * Irradiance information stored in the file.
  33. */
  34. irradiance: any;
  35. /**
  36. * Specular information stored in the file.
  37. */
  38. specular: any;
  39. }
  40. /**
  41. * Defines One Image in the file. It requires only the position in the file
  42. * as well as the length.
  43. */
  44. interface BufferImageData {
  45. /**
  46. * Length of the image data.
  47. */
  48. length: number;
  49. /**
  50. * Position of the data from the null terminator delimiting the end of the JSON.
  51. */
  52. position: number;
  53. }
  54. /**
  55. * Defines the specular data enclosed in the file.
  56. * This corresponds to the version 1 of the data.
  57. */
  58. export interface EnvironmentTextureSpecularInfoV1 {
  59. /**
  60. * Defines where the specular Payload is located. It is a runtime value only not stored in the file.
  61. */
  62. specularDataPosition?: number;
  63. /**
  64. * This contains all the images data needed to reconstruct the cubemap.
  65. */
  66. mipmaps: Array<BufferImageData>;
  67. /**
  68. * Defines the scale applied to environment texture. This manages the range of LOD level used for IBL according to the roughness.
  69. */
  70. lodGenerationScale: number;
  71. }
  72. /**
  73. * Defines the required storage to save the environment irradiance information.
  74. */
  75. interface EnvironmentTextureIrradianceInfoV1 {
  76. x: Array<number>;
  77. y: Array<number>;
  78. z: Array<number>;
  79. xx: Array<number>;
  80. yy: Array<number>;
  81. zz: Array<number>;
  82. yz: Array<number>;
  83. zx: Array<number>;
  84. xy: Array<number>;
  85. }
  86. /**
  87. * Sets of helpers addressing the serialization and deserialization of environment texture
  88. * stored in a BabylonJS env file.
  89. * Those files are usually stored as .env files.
  90. */
  91. export class EnvironmentTextureTools {
  92. /**
  93. * Magic number identifying the env file.
  94. */
  95. private static _MagicBytes = [0x86, 0x16, 0x87, 0x96, 0xf6, 0xd6, 0x96, 0x36];
  96. /**
  97. * Gets the environment info from an env file.
  98. * @param data The array buffer containing the .env bytes.
  99. * @returns the environment file info (the json header) if successfully parsed.
  100. */
  101. public static GetEnvInfo(data: ArrayBufferView): Nullable<EnvironmentTextureInfo> {
  102. let dataView = new DataView(data.buffer, data.byteOffset, data.byteLength);
  103. let pos = 0;
  104. for (let i = 0; i < EnvironmentTextureTools._MagicBytes.length; i++) {
  105. if (dataView.getUint8(pos++) !== EnvironmentTextureTools._MagicBytes[i]) {
  106. Logger.Error('Not a babylon environment map');
  107. return null;
  108. }
  109. }
  110. // Read json manifest - collect characters up to null terminator
  111. let manifestString = '';
  112. let charCode = 0x00;
  113. while ((charCode = dataView.getUint8(pos++))) {
  114. manifestString += String.fromCharCode(charCode);
  115. }
  116. let manifest: EnvironmentTextureInfo = JSON.parse(manifestString);
  117. if (manifest.specular) {
  118. // Extend the header with the position of the payload.
  119. manifest.specular.specularDataPosition = pos;
  120. // Fallback to 0.8 exactly if lodGenerationScale is not defined for backward compatibility.
  121. manifest.specular.lodGenerationScale = manifest.specular.lodGenerationScale || 0.8;
  122. }
  123. return manifest;
  124. }
  125. /**
  126. * Creates an environment texture from a loaded cube texture.
  127. * @param texture defines the cube texture to convert in env file
  128. * @return a promise containing the environment data if succesfull.
  129. */
  130. public static CreateEnvTextureAsync(texture: BaseTexture): Promise<ArrayBuffer> {
  131. let internalTexture = texture.getInternalTexture();
  132. if (!internalTexture) {
  133. return Promise.reject("The cube texture is invalid.");
  134. }
  135. let engine = internalTexture.getEngine() as Engine;
  136. if (engine && engine.premultipliedAlpha) {
  137. return Promise.reject("Env texture can only be created when the engine is created with the premultipliedAlpha option set to false.");
  138. }
  139. if (texture.textureType === Constants.TEXTURETYPE_UNSIGNED_INT) {
  140. return Promise.reject("The cube texture should allow HDR (Full Float or Half Float).");
  141. }
  142. let canvas = engine.getRenderingCanvas();
  143. if (!canvas) {
  144. return Promise.reject("Env texture can only be created when the engine is associated to a canvas.");
  145. }
  146. let textureType = Constants.TEXTURETYPE_FLOAT;
  147. if (!engine.getCaps().textureFloatRender) {
  148. textureType = Constants.TEXTURETYPE_HALF_FLOAT;
  149. if (!engine.getCaps().textureHalfFloatRender) {
  150. return Promise.reject("Env texture can only be created when the browser supports half float or full float rendering.");
  151. }
  152. }
  153. let cubeWidth = internalTexture.width;
  154. let hostingScene = new Scene(engine);
  155. let specularTextures: { [key: number]: ArrayBuffer } = {};
  156. let promises: Promise<void>[] = [];
  157. // Read and collect all mipmaps data from the cube.
  158. let mipmapsCount = Scalar.Log2(internalTexture.width);
  159. mipmapsCount = Math.round(mipmapsCount);
  160. for (let i = 0; i <= mipmapsCount; i++) {
  161. let faceWidth = Math.pow(2, mipmapsCount - i);
  162. // All faces of the cube.
  163. for (let face = 0; face < 6; face++) {
  164. let data = texture.readPixels(face, i);
  165. // Creates a temp texture with the face data.
  166. let tempTexture = engine.createRawTexture(data, faceWidth, faceWidth, Constants.TEXTUREFORMAT_RGBA, false, false, Constants.TEXTURE_NEAREST_SAMPLINGMODE, null, textureType);
  167. // And rgbdEncode them.
  168. let promise = new Promise<void>((resolve, reject) => {
  169. let rgbdPostProcess = new PostProcess("rgbdEncode", "rgbdEncode", null, null, 1, null, Constants.TEXTURE_NEAREST_SAMPLINGMODE, engine, false, undefined, Constants.TEXTURETYPE_UNSIGNED_INT, undefined, null, false);
  170. rgbdPostProcess.getEffect().executeWhenCompiled(() => {
  171. rgbdPostProcess.onApply = (effect) => {
  172. effect._bindTexture("textureSampler", tempTexture);
  173. };
  174. // As the process needs to happen on the main canvas, keep track of the current size
  175. let currentW = engine.getRenderWidth();
  176. let currentH = engine.getRenderHeight();
  177. // Set the desired size for the texture
  178. engine.setSize(faceWidth, faceWidth);
  179. hostingScene.postProcessManager.directRender([rgbdPostProcess], null);
  180. // Reading datas from WebGL
  181. Tools.ToBlob(canvas!, (blob) => {
  182. let fileReader = new FileReader();
  183. fileReader.onload = (event: any) => {
  184. let arrayBuffer = event.target!.result as ArrayBuffer;
  185. specularTextures[i * 6 + face] = arrayBuffer;
  186. resolve();
  187. };
  188. fileReader.readAsArrayBuffer(blob!);
  189. });
  190. // Reapply the previous canvas size
  191. engine.setSize(currentW, currentH);
  192. });
  193. });
  194. promises.push(promise);
  195. }
  196. }
  197. // Once all the textures haves been collected as RGBD stored in PNGs
  198. return Promise.all(promises).then(() => {
  199. // We can delete the hosting scene keeping track of all the creation objects
  200. hostingScene.dispose();
  201. // Creates the json header for the env texture
  202. let info: EnvironmentTextureInfo = {
  203. version: 1,
  204. width: cubeWidth,
  205. irradiance: this._CreateEnvTextureIrradiance(texture),
  206. specular: {
  207. mipmaps: [],
  208. lodGenerationScale: texture.lodGenerationScale
  209. }
  210. };
  211. // Sets the specular image data information
  212. let position = 0;
  213. for (let i = 0; i <= mipmapsCount; i++) {
  214. for (let face = 0; face < 6; face++) {
  215. let byteLength = specularTextures[i * 6 + face].byteLength;
  216. info.specular.mipmaps.push({
  217. length: byteLength,
  218. position: position
  219. });
  220. position += byteLength;
  221. }
  222. }
  223. // Encode the JSON as an array buffer
  224. let infoString = JSON.stringify(info);
  225. let infoBuffer = new ArrayBuffer(infoString.length + 1);
  226. let infoView = new Uint8Array(infoBuffer); // Limited to ascii subset matching unicode.
  227. for (let i = 0, strLen = infoString.length; i < strLen; i++) {
  228. infoView[i] = infoString.charCodeAt(i);
  229. }
  230. // Ends up with a null terminator for easier parsing
  231. infoView[infoString.length] = 0x00;
  232. // Computes the final required size and creates the storage
  233. let totalSize = EnvironmentTextureTools._MagicBytes.length + position + infoBuffer.byteLength;
  234. let finalBuffer = new ArrayBuffer(totalSize);
  235. let finalBufferView = new Uint8Array(finalBuffer);
  236. let dataView = new DataView(finalBuffer);
  237. // Copy the magic bytes identifying the file in
  238. let pos = 0;
  239. for (let i = 0; i < EnvironmentTextureTools._MagicBytes.length; i++) {
  240. dataView.setUint8(pos++, EnvironmentTextureTools._MagicBytes[i]);
  241. }
  242. // Add the json info
  243. finalBufferView.set(new Uint8Array(infoBuffer), pos);
  244. pos += infoBuffer.byteLength;
  245. // Finally inserts the texture data
  246. for (let i = 0; i <= mipmapsCount; i++) {
  247. for (let face = 0; face < 6; face++) {
  248. let dataBuffer = specularTextures[i * 6 + face];
  249. finalBufferView.set(new Uint8Array(dataBuffer), pos);
  250. pos += dataBuffer.byteLength;
  251. }
  252. }
  253. // Voila
  254. return finalBuffer;
  255. });
  256. }
  257. /**
  258. * Creates a JSON representation of the spherical data.
  259. * @param texture defines the texture containing the polynomials
  260. * @return the JSON representation of the spherical info
  261. */
  262. private static _CreateEnvTextureIrradiance(texture: BaseTexture): Nullable<EnvironmentTextureIrradianceInfoV1> {
  263. let polynmials = texture.sphericalPolynomial;
  264. if (polynmials == null) {
  265. return null;
  266. }
  267. return {
  268. x: [polynmials.x.x, polynmials.x.y, polynmials.x.z],
  269. y: [polynmials.y.x, polynmials.y.y, polynmials.y.z],
  270. z: [polynmials.z.x, polynmials.z.y, polynmials.z.z],
  271. xx: [polynmials.xx.x, polynmials.xx.y, polynmials.xx.z],
  272. yy: [polynmials.yy.x, polynmials.yy.y, polynmials.yy.z],
  273. zz: [polynmials.zz.x, polynmials.zz.y, polynmials.zz.z],
  274. yz: [polynmials.yz.x, polynmials.yz.y, polynmials.yz.z],
  275. zx: [polynmials.zx.x, polynmials.zx.y, polynmials.zx.z],
  276. xy: [polynmials.xy.x, polynmials.xy.y, polynmials.xy.z]
  277. } as any;
  278. }
  279. /**
  280. * Creates the ArrayBufferViews used for initializing environment texture image data.
  281. * @param data the image data
  282. * @param info parameters that determine what views will be created for accessing the underlying buffer
  283. * @return the views described by info providing access to the underlying buffer
  284. */
  285. public static CreateImageDataArrayBufferViews(data: ArrayBufferView, info: EnvironmentTextureInfo): Array<Array<ArrayBufferView>> {
  286. if (info.version !== 1) {
  287. throw new Error(`Unsupported babylon environment map version "${info.version}"`);
  288. }
  289. const specularInfo = info.specular as EnvironmentTextureSpecularInfoV1;
  290. // Double checks the enclosed info
  291. let mipmapsCount = Scalar.Log2(info.width);
  292. mipmapsCount = Math.round(mipmapsCount) + 1;
  293. if (specularInfo.mipmaps.length !== 6 * mipmapsCount) {
  294. throw new Error(`Unsupported specular mipmaps number "${specularInfo.mipmaps.length}"`);
  295. }
  296. const imageData = new Array<Array<ArrayBufferView>>(mipmapsCount);
  297. for (let i = 0; i < mipmapsCount; i++) {
  298. imageData[i] = new Array<ArrayBufferView>(6);
  299. for (let face = 0; face < 6; face++) {
  300. const imageInfo = specularInfo.mipmaps[i * 6 + face];
  301. imageData[i][face] = new Uint8Array(data.buffer, data.byteOffset + specularInfo.specularDataPosition! + imageInfo.position, imageInfo.length);
  302. }
  303. }
  304. return imageData;
  305. }
  306. /**
  307. * Uploads the texture info contained in the env file to the GPU.
  308. * @param texture defines the internal texture to upload to
  309. * @param data defines the data to load
  310. * @param info defines the texture info retrieved through the GetEnvInfo method
  311. * @returns a promise
  312. */
  313. public static UploadEnvLevelsAsync(texture: InternalTexture, data: ArrayBufferView, info: EnvironmentTextureInfo): Promise<void> {
  314. if (info.version !== 1) {
  315. throw new Error(`Unsupported babylon environment map version "${info.version}"`);
  316. }
  317. const specularInfo = info.specular as EnvironmentTextureSpecularInfoV1;
  318. if (!specularInfo) {
  319. // Nothing else parsed so far
  320. return Promise.resolve();
  321. }
  322. texture._lodGenerationScale = specularInfo.lodGenerationScale;
  323. const imageData = EnvironmentTextureTools.CreateImageDataArrayBufferViews(data, info);
  324. return EnvironmentTextureTools.UploadLevelsAsync(texture, imageData);
  325. }
  326. private static _OnImageReadyAsync(image: HTMLImageElement | ImageBitmap, engine: Engine, expandTexture: boolean,
  327. rgbdPostProcess: Nullable<PostProcess>, url: string, face: number, i: number, generateNonLODTextures: boolean,
  328. lodTextures: Nullable<{ [lod: number]: BaseTexture }>, cubeRtt: Nullable<InternalTexture>, texture: InternalTexture
  329. ): Promise<void> {
  330. return new Promise((resolve, reject) => {
  331. if (expandTexture) {
  332. let tempTexture = engine.createTexture(null, true, true, null, Constants.TEXTURE_NEAREST_SAMPLINGMODE, null,
  333. (message) => {
  334. reject(message);
  335. },
  336. image);
  337. rgbdPostProcess!.getEffect().executeWhenCompiled(() => {
  338. // Uncompress the data to a RTT
  339. rgbdPostProcess!.onApply = (effect) => {
  340. effect._bindTexture("textureSampler", tempTexture);
  341. effect.setFloat2("scale", 1, 1);
  342. };
  343. engine.scenes[0].postProcessManager.directRender([rgbdPostProcess!], cubeRtt, true, face, i);
  344. // Cleanup
  345. engine.restoreDefaultFramebuffer();
  346. tempTexture.dispose();
  347. URL.revokeObjectURL(url);
  348. resolve();
  349. });
  350. }
  351. else {
  352. engine._uploadImageToTexture(texture, image, face, i);
  353. // Upload the face to the non lod texture support
  354. if (generateNonLODTextures) {
  355. let lodTexture = lodTextures![i];
  356. if (lodTexture) {
  357. engine._uploadImageToTexture(lodTexture._texture!, image, face, 0);
  358. }
  359. }
  360. resolve();
  361. }
  362. }
  363. );
  364. }
  365. /**
  366. * Uploads the levels of image data to the GPU.
  367. * @param texture defines the internal texture to upload to
  368. * @param imageData defines the array buffer views of image data [mipmap][face]
  369. * @returns a promise
  370. */
  371. public static UploadLevelsAsync(texture: InternalTexture, imageData: ArrayBufferView[][]): Promise<void> {
  372. if (!Tools.IsExponentOfTwo(texture.width)) {
  373. throw new Error("Texture size must be a power of two");
  374. }
  375. const mipmapsCount = Scalar.ILog2(texture.width) + 1;
  376. // Gets everything ready.
  377. let engine = texture.getEngine() as Engine;
  378. let expandTexture = false;
  379. let generateNonLODTextures = false;
  380. let rgbdPostProcess: Nullable<PostProcess> = null;
  381. let cubeRtt: Nullable<InternalTexture> = null;
  382. let lodTextures: Nullable<{ [lod: number]: BaseTexture }> = null;
  383. let caps = engine.getCaps();
  384. texture.format = Constants.TEXTUREFORMAT_RGBA;
  385. texture.type = Constants.TEXTURETYPE_UNSIGNED_INT;
  386. texture.generateMipMaps = true;
  387. texture._cachedAnisotropicFilteringLevel = null;
  388. engine.updateTextureSamplingMode(Constants.TEXTURE_TRILINEAR_SAMPLINGMODE, texture);
  389. // Add extra process if texture lod is not supported
  390. if (!caps.textureLOD) {
  391. expandTexture = false;
  392. generateNonLODTextures = true;
  393. lodTextures = {};
  394. }
  395. // in webgl 1 there are no ways to either render or copy lod level information for float textures.
  396. else if (!ThinEngine.Features.supportRenderAndCopyToLodForFloatTextures) {
  397. expandTexture = false;
  398. }
  399. // If half float available we can uncompress the texture
  400. else if (caps.textureHalfFloatRender && caps.textureHalfFloatLinearFiltering) {
  401. expandTexture = true;
  402. texture.type = Constants.TEXTURETYPE_HALF_FLOAT;
  403. }
  404. // If full float available we can uncompress the texture
  405. else if (caps.textureFloatRender && caps.textureFloatLinearFiltering) {
  406. expandTexture = true;
  407. texture.type = Constants.TEXTURETYPE_FLOAT;
  408. }
  409. // Expand the texture if possible
  410. if (expandTexture) {
  411. // Simply run through the decode PP
  412. rgbdPostProcess = new PostProcess("rgbdDecode", "rgbdDecode", null, null, 1, null, Constants.TEXTURE_TRILINEAR_SAMPLINGMODE, engine, false, undefined, texture.type, undefined, null, false);
  413. texture._isRGBD = false;
  414. texture.invertY = false;
  415. cubeRtt = engine.createRenderTargetCubeTexture(texture.width, {
  416. generateDepthBuffer: false,
  417. generateMipMaps: true,
  418. generateStencilBuffer: false,
  419. samplingMode: Constants.TEXTURE_TRILINEAR_SAMPLINGMODE,
  420. type: texture.type,
  421. format: Constants.TEXTUREFORMAT_RGBA
  422. });
  423. }
  424. else {
  425. texture._isRGBD = true;
  426. texture.invertY = true;
  427. // In case of missing support, applies the same patch than DDS files.
  428. if (generateNonLODTextures) {
  429. let mipSlices = 3;
  430. let scale = texture._lodGenerationScale;
  431. let offset = texture._lodGenerationOffset;
  432. for (let i = 0; i < mipSlices; i++) {
  433. //compute LOD from even spacing in smoothness (matching shader calculation)
  434. let smoothness = i / (mipSlices - 1);
  435. let roughness = 1 - smoothness;
  436. let minLODIndex = offset; // roughness = 0
  437. let maxLODIndex = (mipmapsCount - 1) * scale + offset; // roughness = 1 (mipmaps start from 0)
  438. let lodIndex = minLODIndex + (maxLODIndex - minLODIndex) * roughness;
  439. let mipmapIndex = Math.round(Math.min(Math.max(lodIndex, 0), maxLODIndex));
  440. let glTextureFromLod = new InternalTexture(engine, InternalTextureSource.Temp);
  441. glTextureFromLod.isCube = true;
  442. glTextureFromLod.invertY = true;
  443. glTextureFromLod.generateMipMaps = false;
  444. engine.updateTextureSamplingMode(Constants.TEXTURE_LINEAR_LINEAR, glTextureFromLod);
  445. // Wrap in a base texture for easy binding.
  446. let lodTexture = new BaseTexture(null);
  447. lodTexture.isCube = true;
  448. lodTexture._texture = glTextureFromLod;
  449. lodTextures![mipmapIndex] = lodTexture;
  450. switch (i) {
  451. case 0:
  452. texture._lodTextureLow = lodTexture;
  453. break;
  454. case 1:
  455. texture._lodTextureMid = lodTexture;
  456. break;
  457. case 2:
  458. texture._lodTextureHigh = lodTexture;
  459. break;
  460. }
  461. }
  462. }
  463. }
  464. let promises: Promise<void>[] = [];
  465. // All mipmaps up to provided number of images
  466. for (let i = 0; i < imageData.length; i++) {
  467. // All faces
  468. for (let face = 0; face < 6; face++) {
  469. // Constructs an image element from image data
  470. let bytes = imageData[i][face];
  471. let blob = new Blob([bytes], { type: 'image/png' });
  472. let url = URL.createObjectURL(blob);
  473. let promise: Promise<void>;
  474. if (typeof Image === "undefined" || ThinEngine.Features.forceBitmapOverHTMLImageElement) {
  475. promise = createImageBitmap(blob, { premultiplyAlpha: "none" }).then((img) => {
  476. return this._OnImageReadyAsync(img, engine, expandTexture, rgbdPostProcess, url, face, i, generateNonLODTextures, lodTextures, cubeRtt, texture);
  477. });
  478. } else {
  479. let image = new Image();
  480. image.src = url;
  481. // Enqueue promise to upload to the texture.
  482. promise = new Promise<void>((resolve, reject) => {
  483. image.onload = () => {
  484. this._OnImageReadyAsync(image, engine, expandTexture, rgbdPostProcess, url, face, i, generateNonLODTextures, lodTextures, cubeRtt, texture)
  485. .then(() => resolve())
  486. .catch((reason) => {
  487. reject(reason);
  488. });
  489. };
  490. image.onerror = (error) => {
  491. reject(error);
  492. };
  493. });
  494. }
  495. promises.push(promise);
  496. }
  497. }
  498. // Fill remaining mipmaps with black textures.
  499. if (imageData.length < mipmapsCount) {
  500. let data: ArrayBufferView;
  501. const size = Math.pow(2, mipmapsCount - 1 - imageData.length);
  502. const dataLength = size * size * 4;
  503. switch (texture.type) {
  504. case Constants.TEXTURETYPE_UNSIGNED_INT: {
  505. data = new Uint8Array(dataLength);
  506. break;
  507. }
  508. case Constants.TEXTURETYPE_HALF_FLOAT: {
  509. data = new Uint16Array(dataLength);
  510. break;
  511. }
  512. case Constants.TEXTURETYPE_FLOAT: {
  513. data = new Float32Array(dataLength);
  514. break;
  515. }
  516. }
  517. for (let i = imageData.length; i < mipmapsCount; i++) {
  518. for (let face = 0; face < 6; face++) {
  519. engine._uploadArrayBufferViewToTexture(texture, data!, face, i);
  520. }
  521. }
  522. }
  523. // Once all done, finishes the cleanup and return
  524. return Promise.all(promises).then(() => {
  525. // Release temp RTT.
  526. if (cubeRtt) {
  527. engine._releaseFramebufferObjects(cubeRtt);
  528. engine._releaseTexture(texture);
  529. cubeRtt._swapAndDie(texture);
  530. }
  531. // Release temp Post Process.
  532. if (rgbdPostProcess) {
  533. rgbdPostProcess.dispose();
  534. }
  535. // Flag internal texture as ready in case they are in use.
  536. if (generateNonLODTextures) {
  537. if (texture._lodTextureHigh && texture._lodTextureHigh._texture) {
  538. texture._lodTextureHigh._texture.isReady = true;
  539. }
  540. if (texture._lodTextureMid && texture._lodTextureMid._texture) {
  541. texture._lodTextureMid._texture.isReady = true;
  542. }
  543. if (texture._lodTextureLow && texture._lodTextureLow._texture) {
  544. texture._lodTextureLow._texture.isReady = true;
  545. }
  546. }
  547. });
  548. }
  549. /**
  550. * Uploads spherical polynomials information to the texture.
  551. * @param texture defines the texture we are trying to upload the information to
  552. * @param info defines the environment texture info retrieved through the GetEnvInfo method
  553. */
  554. public static UploadEnvSpherical(texture: InternalTexture, info: EnvironmentTextureInfo): void {
  555. if (info.version !== 1) {
  556. Logger.Warn('Unsupported babylon environment map version "' + info.version + '"');
  557. }
  558. let irradianceInfo = info.irradiance as EnvironmentTextureIrradianceInfoV1;
  559. if (!irradianceInfo) {
  560. return;
  561. }
  562. const sp = new SphericalPolynomial();
  563. Vector3.FromArrayToRef(irradianceInfo.x, 0, sp.x);
  564. Vector3.FromArrayToRef(irradianceInfo.y, 0, sp.y);
  565. Vector3.FromArrayToRef(irradianceInfo.z, 0, sp.z);
  566. Vector3.FromArrayToRef(irradianceInfo.xx, 0, sp.xx);
  567. Vector3.FromArrayToRef(irradianceInfo.yy, 0, sp.yy);
  568. Vector3.FromArrayToRef(irradianceInfo.zz, 0, sp.zz);
  569. Vector3.FromArrayToRef(irradianceInfo.yz, 0, sp.yz);
  570. Vector3.FromArrayToRef(irradianceInfo.zx, 0, sp.zx);
  571. Vector3.FromArrayToRef(irradianceInfo.xy, 0, sp.xy);
  572. texture._sphericalPolynomial = sp;
  573. }
  574. /** @hidden */
  575. public static _UpdateRGBDAsync(internalTexture: InternalTexture, data: ArrayBufferView[][], sphericalPolynomial: Nullable<SphericalPolynomial>, lodScale: number, lodOffset: number): Promise<void> {
  576. internalTexture._source = InternalTextureSource.CubeRawRGBD;
  577. internalTexture._bufferViewArrayArray = data;
  578. internalTexture._lodGenerationScale = lodScale;
  579. internalTexture._lodGenerationOffset = lodOffset;
  580. internalTexture._sphericalPolynomial = sphericalPolynomial;
  581. return EnvironmentTextureTools.UploadLevelsAsync(internalTexture, data).then(() => {
  582. internalTexture.isReady = true;
  583. });
  584. }
  585. }
  586. // References the dependencies.
  587. InternalTexture._UpdateRGBDAsync = EnvironmentTextureTools._UpdateRGBDAsync;