浏览代码

Merge pull request #9584 from sebavan/InspectorPBRUX

hdr 2d texture loader
David Catuhe 4 年之前
父节点
当前提交
d937cf8da1
共有 2 个文件被更改,包括 73 次插入0 次删除
  1. 72 0
      src/Materials/Textures/Loaders/hdrTextureLoader.ts
  2. 1 0
      src/Materials/Textures/Loaders/index.ts

+ 72 - 0
src/Materials/Textures/Loaders/hdrTextureLoader.ts

@@ -0,0 +1,72 @@
+import { HDRTools } from "../../../Misc/HighDynamicRange/hdr";
+import { Nullable } from "../../../types";
+import { Engine } from "../../../Engines/engine";
+import { InternalTexture } from "../../../Materials/Textures/internalTexture";
+import { IInternalTextureLoader } from "../../../Materials/Textures/internalTextureLoader";
+import { StringTools } from '../../../Misc/stringTools';
+import { Constants } from "../../../Engines/constants";
+
+/**
+ * Implementation of the HDR Texture Loader.
+ * @hidden
+ */
+export class _HDRTextureLoader implements IInternalTextureLoader {
+    /**
+     * Defines wether the loader supports cascade loading the different faces.
+     */
+    public readonly supportCascades = false;
+
+    /**
+     * This returns if the loader support the current file information.
+     * @param extension defines the file extension of the file being loaded
+     * @returns true if the loader can load the specified file
+     */
+    public canLoad(extension: string): boolean {
+        return StringTools.EndsWith(extension, ".hdr");
+    }
+
+    /**
+     * Uploads the cube texture data to the WebGL texture. It has already been bound.
+     * @param data contains the texture data
+     * @param texture defines the BabylonJS internal texture
+     * @param createPolynomials will be true if polynomials have been requested
+     * @param onLoad defines the callback to trigger once the texture is ready
+     * @param onError defines the callback to trigger in case of error
+     */
+    public loadCubeData(data: ArrayBufferView | ArrayBufferView[], texture: InternalTexture, createPolynomials: boolean, onLoad: Nullable<(data?: any) => void>, onError: Nullable<(message?: string, exception?: any) => void>): void {
+        throw ".env not supported in Cube.";
+    }
+
+    /**
+     * Uploads the 2D texture data to the WebGL texture. It has already been bound once in the callback.
+     * @param data contains the texture data
+     * @param texture defines the BabylonJS internal texture
+     * @param callback defines the method to call once ready to upload
+     */
+    public loadData(data: ArrayBufferView, texture: InternalTexture,
+        callback: (width: number, height: number, loadMipmap: boolean, isCompressed: boolean, done: () => void) => void): void {
+        const uint8array = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
+        const hdrInfo = HDRTools.RGBE_ReadHeader(uint8array);
+        const pixelsDataRGB32 = HDRTools.RGBE_ReadPixels(uint8array, hdrInfo);
+
+        const pixels = hdrInfo.width * hdrInfo.height;
+        const pixelsDataRGBA32 = new Float32Array(pixels * 4);
+        for (let i = 0; i < pixels; i += 1) {
+            pixelsDataRGBA32[i * 4] = pixelsDataRGB32[i * 3];
+            pixelsDataRGBA32[i * 4 + 1] = pixelsDataRGB32[i * 3 + 1];
+            pixelsDataRGBA32[i * 4 + 2] = pixelsDataRGB32[i * 3 + 2];
+            pixelsDataRGBA32[i * 4 + 3] = 1;
+        }
+
+        callback(hdrInfo.width, hdrInfo.height, texture.generateMipMaps, false, () => {
+            const engine = texture.getEngine();
+            texture.type = Constants.TEXTURETYPE_FLOAT;
+            texture.format = Constants.TEXTUREFORMAT_RGBA;
+            texture._gammaSpace = false;
+            engine._uploadDataToTextureDirectly(texture, pixelsDataRGBA32);
+        });
+    }
+}
+
+// Register the loader.
+Engine._TextureLoaders.push(new _HDRTextureLoader());

+ 1 - 0
src/Materials/Textures/Loaders/index.ts

@@ -2,4 +2,5 @@ export * from "./ddsTextureLoader";
 export * from "./envTextureLoader";
 export * from "./ktxTextureLoader";
 export * from "./tgaTextureLoader";
+export * from "./hdrTextureLoader";
 export * from "./basisTextureLoader";