environmentTextureTools.ts 28 KB

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