environmentTextureTools.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. import { Nullable } from "../types";
  2. import { Tools } from "./tools";
  3. import { Vector3 } from "../Maths/math";
  4. import { Scalar } from "../Maths/math.scalar";
  5. import { SphericalPolynomial } from "../Maths/sphericalPolynomial";
  6. import { InternalTexture } 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.renderTarget";
  14. import "../Shaders/rgbdEncode.fragment";
  15. import "../Shaders/rgbdDecode.fragment";
  16. /**
  17. * Raw texture data and descriptor sufficient for WebGL texture upload
  18. */
  19. export interface EnvironmentTextureInfo {
  20. /**
  21. * Version of the environment map
  22. */
  23. version: number;
  24. /**
  25. * Width of image
  26. */
  27. width: number;
  28. /**
  29. * Irradiance information stored in the file.
  30. */
  31. irradiance: any;
  32. /**
  33. * Specular information stored in the file.
  34. */
  35. specular: any;
  36. }
  37. /**
  38. * Defines One Image in the file. It requires only the position in the file
  39. * as well as the length.
  40. */
  41. interface BufferImageData {
  42. /**
  43. * Length of the image data.
  44. */
  45. length: number;
  46. /**
  47. * Position of the data from the null terminator delimiting the end of the JSON.
  48. */
  49. position: number;
  50. }
  51. /**
  52. * Defines the specular data enclosed in the file.
  53. * This corresponds to the version 1 of the data.
  54. */
  55. interface EnvironmentTextureSpecularInfoV1 {
  56. /**
  57. * Defines where the specular Payload is located. It is a runtime value only not stored in the file.
  58. */
  59. specularDataPosition?: number;
  60. /**
  61. * This contains all the images data needed to reconstruct the cubemap.
  62. */
  63. mipmaps: Array<BufferImageData>;
  64. /**
  65. * Defines the scale applied to environment texture. This manages the range of LOD level used for IBL according to the roughness.
  66. */
  67. lodGenerationScale: number;
  68. }
  69. /**
  70. * Defines the required storage to save the environment irradiance information.
  71. */
  72. interface EnvironmentTextureIrradianceInfoV1 {
  73. x: Array<number>;
  74. y: Array<number>;
  75. z: Array<number>;
  76. xx: Array<number>;
  77. yy: Array<number>;
  78. zz: Array<number>;
  79. yz: Array<number>;
  80. zx: Array<number>;
  81. xy: Array<number>;
  82. }
  83. /**
  84. * Sets of helpers addressing the serialization and deserialization of environment texture
  85. * stored in a BabylonJS env file.
  86. * Those files are usually stored as .env files.
  87. */
  88. export class EnvironmentTextureTools {
  89. /**
  90. * Magic number identifying the env file.
  91. */
  92. private static _MagicBytes = [0x86, 0x16, 0x87, 0x96, 0xf6, 0xd6, 0x96, 0x36];
  93. /**
  94. * Gets the environment info from an env file.
  95. * @param data The array buffer containing the .env bytes.
  96. * @returns the environment file info (the json header) if successfully parsed.
  97. */
  98. public static GetEnvInfo(data: ArrayBuffer): Nullable<EnvironmentTextureInfo> {
  99. let dataView = new DataView(data);
  100. let pos = 0;
  101. for (let i = 0; i < EnvironmentTextureTools._MagicBytes.length; i++) {
  102. if (dataView.getUint8(pos++) !== EnvironmentTextureTools._MagicBytes[i]) {
  103. Logger.Error('Not a babylon environment map');
  104. return null;
  105. }
  106. }
  107. // Read json manifest - collect characters up to null terminator
  108. let manifestString = '';
  109. let charCode = 0x00;
  110. while ((charCode = dataView.getUint8(pos++))) {
  111. manifestString += String.fromCharCode(charCode);
  112. }
  113. let manifest: EnvironmentTextureInfo = JSON.parse(manifestString);
  114. if (manifest.specular) {
  115. // Extend the header with the position of the payload.
  116. manifest.specular.specularDataPosition = pos;
  117. // Fallback to 0.8 exactly if lodGenerationScale is not defined for backward compatibility.
  118. manifest.specular.lodGenerationScale = manifest.specular.lodGenerationScale || 0.8;
  119. }
  120. return manifest;
  121. }
  122. /**
  123. * Creates an environment texture from a loaded cube texture.
  124. * @param texture defines the cube texture to convert in env file
  125. * @return a promise containing the environment data if succesfull.
  126. */
  127. public static CreateEnvTextureAsync(texture: CubeTexture): Promise<ArrayBuffer> {
  128. let internalTexture = texture.getInternalTexture();
  129. if (!internalTexture) {
  130. return Promise.reject("The cube texture is invalid.");
  131. }
  132. if (!texture._prefiltered) {
  133. return Promise.reject("The cube texture is invalid (not prefiltered).");
  134. }
  135. let engine = internalTexture.getEngine();
  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: CubeTexture): 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. * Uploads the texture info contained in the env file to the GPU.
  281. * @param texture defines the internal texture to upload to
  282. * @param arrayBuffer defines the buffer cotaining the data to load
  283. * @param info defines the texture info retrieved through the GetEnvInfo method
  284. * @returns a promise
  285. */
  286. public static UploadEnvLevelsAsync(texture: InternalTexture, arrayBuffer: any, info: EnvironmentTextureInfo): Promise<void> {
  287. if (info.version !== 1) {
  288. throw new Error(`Unsupported babylon environment map version "${info.version}"`);
  289. }
  290. let specularInfo = info.specular as EnvironmentTextureSpecularInfoV1;
  291. if (!specularInfo) {
  292. // Nothing else parsed so far
  293. return Promise.resolve();
  294. }
  295. // Double checks the enclosed info
  296. let mipmapsCount = Scalar.Log2(info.width);
  297. mipmapsCount = Math.round(mipmapsCount) + 1;
  298. if (specularInfo.mipmaps.length !== 6 * mipmapsCount) {
  299. throw new Error(`Unsupported specular mipmaps number "${specularInfo.mipmaps.length}"`);
  300. }
  301. texture._lodGenerationScale = specularInfo.lodGenerationScale;
  302. const imageData = new Array<Array<ArrayBufferView>>(mipmapsCount);
  303. for (let i = 0; i < mipmapsCount; i++) {
  304. imageData[i] = new Array<ArrayBufferView>(6);
  305. for (let face = 0; face < 6; face++) {
  306. const imageInfo = specularInfo.mipmaps[i * 6 + face];
  307. imageData[i][face] = new Uint8Array(arrayBuffer, specularInfo.specularDataPosition! + imageInfo.position, imageInfo.length);
  308. }
  309. }
  310. return EnvironmentTextureTools.UploadLevelsAsync(texture, imageData);
  311. }
  312. /**
  313. * Uploads the levels of image data to the GPU.
  314. * @param texture defines the internal texture to upload to
  315. * @param imageData defines the array buffer views of image data [mipmap][face]
  316. * @returns a promise
  317. */
  318. public static UploadLevelsAsync(texture: InternalTexture, imageData: ArrayBufferView[][]): Promise<void> {
  319. if (!Tools.IsExponentOfTwo(texture.width)) {
  320. throw new Error("Texture size must be a power of two");
  321. }
  322. const mipmapsCount = Math.round(Scalar.Log2(texture.width)) + 1;
  323. // Gets everything ready.
  324. let engine = texture.getEngine();
  325. let expandTexture = false;
  326. let generateNonLODTextures = false;
  327. let rgbdPostProcess: Nullable<PostProcess> = null;
  328. let cubeRtt: Nullable<InternalTexture> = null;
  329. let lodTextures: Nullable<{ [lod: number]: BaseTexture }> = null;
  330. let caps = engine.getCaps();
  331. texture.format = Constants.TEXTUREFORMAT_RGBA;
  332. texture.type = Constants.TEXTURETYPE_UNSIGNED_INT;
  333. texture.generateMipMaps = true;
  334. engine.updateTextureSamplingMode(Constants.TEXTURE_TRILINEAR_SAMPLINGMODE, texture);
  335. // Add extra process if texture lod is not supported
  336. if (!caps.textureLOD) {
  337. expandTexture = false;
  338. generateNonLODTextures = true;
  339. lodTextures = {};
  340. }
  341. // in webgl 1 there are no ways to either render or copy lod level information for float textures.
  342. else if (engine.webGLVersion < 2) {
  343. expandTexture = false;
  344. }
  345. // If half float available we can uncompress the texture
  346. else if (caps.textureHalfFloatRender && caps.textureHalfFloatLinearFiltering) {
  347. expandTexture = true;
  348. texture.type = Constants.TEXTURETYPE_HALF_FLOAT;
  349. }
  350. // If full float available we can uncompress the texture
  351. else if (caps.textureFloatRender && caps.textureFloatLinearFiltering) {
  352. expandTexture = true;
  353. texture.type = Constants.TEXTURETYPE_FLOAT;
  354. }
  355. // Expand the texture if possible
  356. if (expandTexture) {
  357. // Simply run through the decode PP
  358. rgbdPostProcess = new PostProcess("rgbdDecode", "rgbdDecode", null, null, 1, null, Constants.TEXTURE_TRILINEAR_SAMPLINGMODE, engine, false, undefined, texture.type, undefined, null, false);
  359. texture._isRGBD = false;
  360. texture.invertY = false;
  361. cubeRtt = engine.createRenderTargetCubeTexture(texture.width, {
  362. generateDepthBuffer: false,
  363. generateMipMaps: true,
  364. generateStencilBuffer: false,
  365. samplingMode: Constants.TEXTURE_TRILINEAR_SAMPLINGMODE,
  366. type: texture.type,
  367. format: Constants.TEXTUREFORMAT_RGBA
  368. });
  369. }
  370. else {
  371. texture._isRGBD = true;
  372. texture.invertY = true;
  373. // In case of missing support, applies the same patch than DDS files.
  374. if (generateNonLODTextures) {
  375. let mipSlices = 3;
  376. let scale = texture._lodGenerationScale;
  377. let offset = texture._lodGenerationOffset;
  378. for (let i = 0; i < mipSlices; i++) {
  379. //compute LOD from even spacing in smoothness (matching shader calculation)
  380. let smoothness = i / (mipSlices - 1);
  381. let roughness = 1 - smoothness;
  382. let minLODIndex = offset; // roughness = 0
  383. let maxLODIndex = (mipmapsCount - 1) * scale + offset; // roughness = 1 (mipmaps start from 0)
  384. let lodIndex = minLODIndex + (maxLODIndex - minLODIndex) * roughness;
  385. let mipmapIndex = Math.round(Math.min(Math.max(lodIndex, 0), maxLODIndex));
  386. let glTextureFromLod = new InternalTexture(engine, InternalTexture.DATASOURCE_TEMP);
  387. glTextureFromLod.isCube = true;
  388. glTextureFromLod.invertY = true;
  389. glTextureFromLod.generateMipMaps = false;
  390. engine.updateTextureSamplingMode(Constants.TEXTURE_LINEAR_LINEAR, glTextureFromLod);
  391. // Wrap in a base texture for easy binding.
  392. let lodTexture = new BaseTexture(null);
  393. lodTexture.isCube = true;
  394. lodTexture._texture = glTextureFromLod;
  395. lodTextures![mipmapIndex] = lodTexture;
  396. switch (i) {
  397. case 0:
  398. texture._lodTextureLow = lodTexture;
  399. break;
  400. case 1:
  401. texture._lodTextureMid = lodTexture;
  402. break;
  403. case 2:
  404. texture._lodTextureHigh = lodTexture;
  405. break;
  406. }
  407. }
  408. }
  409. }
  410. let promises: Promise<void>[] = [];
  411. // All mipmaps up to provided number of images
  412. for (let i = 0; i < imageData.length; i++) {
  413. // All faces
  414. for (let face = 0; face < 6; face++) {
  415. // Constructs an image element from image data
  416. let bytes = imageData[i][face];
  417. let blob = new Blob([bytes], { type: 'image/png' });
  418. let url = URL.createObjectURL(blob);
  419. let image = new Image();
  420. image.src = url;
  421. // Enqueue promise to upload to the texture.
  422. let promise = new Promise<void>((resolve, reject) => {
  423. image.onload = () => {
  424. if (expandTexture) {
  425. let tempTexture = engine.createTexture(null, true, true, null, Constants.TEXTURE_NEAREST_SAMPLINGMODE, null,
  426. (message) => {
  427. reject(message);
  428. },
  429. image);
  430. rgbdPostProcess!.getEffect().executeWhenCompiled(() => {
  431. // Uncompress the data to a RTT
  432. rgbdPostProcess!.onApply = (effect) => {
  433. effect._bindTexture("textureSampler", tempTexture);
  434. effect.setFloat2("scale", 1, 1);
  435. };
  436. engine.scenes[0].postProcessManager.directRender([rgbdPostProcess!], cubeRtt, true, face, i);
  437. // Cleanup
  438. engine.restoreDefaultFramebuffer();
  439. tempTexture.dispose();
  440. window.URL.revokeObjectURL(url);
  441. resolve();
  442. });
  443. }
  444. else {
  445. engine._uploadImageToTexture(texture, image, face, i);
  446. // Upload the face to the non lod texture support
  447. if (generateNonLODTextures) {
  448. let lodTexture = lodTextures![i];
  449. if (lodTexture) {
  450. engine._uploadImageToTexture(lodTexture._texture!, image, face, 0);
  451. }
  452. }
  453. resolve();
  454. }
  455. };
  456. image.onerror = (error) => {
  457. reject(error);
  458. };
  459. });
  460. promises.push(promise);
  461. }
  462. }
  463. // Fill remaining mipmaps with black textures.
  464. if (imageData.length < mipmapsCount) {
  465. let data: ArrayBufferView;
  466. const size = Math.pow(2, mipmapsCount - 1 - imageData.length);
  467. const dataLength = size * size * 4;
  468. switch (texture.type) {
  469. case Constants.TEXTURETYPE_UNSIGNED_INT: {
  470. data = new Uint8Array(dataLength);
  471. break;
  472. }
  473. case Constants.TEXTURETYPE_HALF_FLOAT: {
  474. data = new Uint16Array(dataLength);
  475. break;
  476. }
  477. case Constants.TEXTURETYPE_FLOAT: {
  478. data = new Float32Array(dataLength);
  479. break;
  480. }
  481. }
  482. for (let i = imageData.length; i < mipmapsCount; i++) {
  483. for (let face = 0; face < 6; face++) {
  484. engine._uploadArrayBufferViewToTexture(texture, data!, face, i);
  485. }
  486. }
  487. }
  488. // Once all done, finishes the cleanup and return
  489. return Promise.all(promises).then(() => {
  490. // Release temp RTT.
  491. if (cubeRtt) {
  492. engine._releaseFramebufferObjects(cubeRtt);
  493. cubeRtt._swapAndDie(texture);
  494. }
  495. // Release temp Post Process.
  496. if (rgbdPostProcess) {
  497. rgbdPostProcess.dispose();
  498. }
  499. // Flag internal texture as ready in case they are in use.
  500. if (generateNonLODTextures) {
  501. if (texture._lodTextureHigh && texture._lodTextureHigh._texture) {
  502. texture._lodTextureHigh._texture.isReady = true;
  503. }
  504. if (texture._lodTextureMid && texture._lodTextureMid._texture) {
  505. texture._lodTextureMid._texture.isReady = true;
  506. }
  507. if (texture._lodTextureLow && texture._lodTextureLow._texture) {
  508. texture._lodTextureLow._texture.isReady = true;
  509. }
  510. }
  511. });
  512. }
  513. /**
  514. * Uploads spherical polynomials information to the texture.
  515. * @param texture defines the texture we are trying to upload the information to
  516. * @param info defines the environment texture info retrieved through the GetEnvInfo method
  517. */
  518. public static UploadEnvSpherical(texture: InternalTexture, info: EnvironmentTextureInfo): void {
  519. if (info.version !== 1) {
  520. Logger.Warn('Unsupported babylon environment map version "' + info.version + '"');
  521. }
  522. let irradianceInfo = info.irradiance as EnvironmentTextureIrradianceInfoV1;
  523. if (!irradianceInfo) {
  524. return;
  525. }
  526. const sp = new SphericalPolynomial();
  527. Vector3.FromArrayToRef(irradianceInfo.x, 0, sp.x);
  528. Vector3.FromArrayToRef(irradianceInfo.y, 0, sp.y);
  529. Vector3.FromArrayToRef(irradianceInfo.z, 0, sp.z);
  530. Vector3.FromArrayToRef(irradianceInfo.xx, 0, sp.xx);
  531. Vector3.FromArrayToRef(irradianceInfo.yy, 0, sp.yy);
  532. Vector3.FromArrayToRef(irradianceInfo.zz, 0, sp.zz);
  533. Vector3.FromArrayToRef(irradianceInfo.yz, 0, sp.yz);
  534. Vector3.FromArrayToRef(irradianceInfo.zx, 0, sp.zx);
  535. Vector3.FromArrayToRef(irradianceInfo.xy, 0, sp.xy);
  536. texture._sphericalPolynomial = sp;
  537. }
  538. /** @hidden */
  539. public static _UpdateRGBDAsync(internalTexture: InternalTexture, data: ArrayBufferView[][], sphericalPolynomial: Nullable<SphericalPolynomial>, lodScale: number, lodOffset: number): Promise<void> {
  540. internalTexture._dataSource = InternalTexture.DATASOURCE_CUBERAW_RGBD;
  541. internalTexture._bufferViewArrayArray = data;
  542. internalTexture._lodGenerationScale = lodScale;
  543. internalTexture._lodGenerationOffset = lodOffset;
  544. internalTexture._sphericalPolynomial = sphericalPolynomial;
  545. return EnvironmentTextureTools.UploadLevelsAsync(internalTexture, data).then(() => {
  546. internalTexture.isReady = true;
  547. });
  548. }
  549. }
  550. // References the dependencies.
  551. InternalTexture._UpdateRGBDAsync = EnvironmentTextureTools._UpdateRGBDAsync;