Bläddra i källkod

Merge pull request #6787 from sebavan/master

Update RGBD Tools
sebavan 6 år sedan
förälder
incheckning
7f8219473f

+ 1 - 0
dist/preview release/what's new.md

@@ -63,6 +63,7 @@
 - Added `ShadowGenerator.onAfterShadowMapRenderObservable` and `ShadowGenerator.onAfterShadowMapMeshRenderObservable` ([Deltakosh](https://github.com/deltakosh/))
 - Added support for side by side and top bottom images in the `PhotoDome` ([Deltakosh](https://github.com/deltakosh/))
 - Added playground ts-local (TypeScript support for local playground) ([pjoe](https://github.com/pjoe/))
+- Added RGBD Texture tools [Sebavan](https://github.com/sebavan/)
 
 ### Meshes
 - Added `TransformNode.instantiateHierarchy()` which try to instantiate (or clone) a node and its entire hiearchy ([Deltakosh](https://github.com/deltakosh/))

+ 2 - 0
src/Materials/PBR/pbrBaseMaterial.ts

@@ -114,6 +114,7 @@ export class PBRMaterialDefines extends MaterialDefines
     public LIGHTMAPDIRECTUV = 0;
     public USELIGHTMAPASSHADOWMAP = false;
     public GAMMALIGHTMAP = false;
+    public RGBDLIGHTMAP = false;
 
     public REFLECTION = false;
     public REFLECTIONMAP_3D = false;
@@ -1396,6 +1397,7 @@ export abstract class PBRBaseMaterial extends PushMaterial {
                     MaterialHelper.PrepareDefinesForMergedUV(this._lightmapTexture, defines, "LIGHTMAP");
                     defines.USELIGHTMAPASSHADOWMAP = this._useLightmapAsShadowmap;
                     defines.GAMMALIGHTMAP = this._lightmapTexture.gammaSpace;
+                    defines.RGBDLIGHTMAP = this._lightmapTexture.isRGBD;
                 } else {
                     defines.LIGHTMAP = false;
                 }

+ 4 - 1
src/Materials/Textures/baseTexture.ts

@@ -209,11 +209,14 @@ export class BaseTexture implements IAnimatable {
     public gammaSpace = true;
 
     /**
-     * Gets whether or not the texture contains RGBD data.
+     * Gets or sets whether or not the texture contains RGBD data.
      */
     public get isRGBD(): boolean {
         return this._texture != null && this._texture._isRGBD;
     }
+    public set isRGBD(value: boolean) {
+        if (this._texture) { this._texture._isRGBD = value; }
+    }
 
     /**
      * Is Z inverted in the texture (useful in a cube texture).

+ 3 - 75
src/Misc/brdfTextureTools.ts

@@ -1,82 +1,12 @@
-import { InternalTexture } from "../Materials/Textures/internalTexture";
 import { BaseTexture } from "../Materials/Textures/baseTexture";
 import { Texture } from "../Materials/Textures/texture";
-import { Constants } from "../Engines/constants";
 import { Scene } from "../scene";
-import { PostProcess } from "../PostProcesses/postProcess";
-import "../Shaders/rgbdDecode.fragment";
+import { RGBDTextureTools } from "./rgbdTextureTools";
 
 /**
  * Class used to host texture specific utilities
  */
 export class BRDFTextureTools {
-
-    /**
-     * Expand the BRDF Texture from RGBD to Half Float if necessary.
-     * @param texture the texture to expand.
-     */
-    private static _ExpandDefaultBRDFTexture(texture: InternalTexture) {
-        // Gets everything ready.
-        let engine = texture.getEngine();
-        let caps = engine.getCaps();
-        let expandTexture = false;
-
-        // If half float available we can uncompress the texture
-        if (caps.textureHalfFloatRender && caps.textureHalfFloatLinearFiltering) {
-            expandTexture = true;
-            texture.type = Constants.TEXTURETYPE_HALF_FLOAT;
-        }
-        // If full float available we can uncompress the texture
-        else if (caps.textureFloatRender && caps.textureFloatLinearFiltering) {
-            expandTexture = true;
-            texture.type = Constants.TEXTURETYPE_FLOAT;
-        }
-
-        // Expand the texture if possible
-        if (expandTexture) {
-            // Do not use during decode.
-            texture.isReady = false;
-
-            // Simply run through the decode PP.
-            const rgbdPostProcess = new PostProcess("rgbdDecode", "rgbdDecode", null, null, 1, null, Constants.TEXTURE_TRILINEAR_SAMPLINGMODE, engine, false, undefined, texture.type, undefined, null, false);
-            texture._isRGBD = false;
-            texture.invertY = false;
-
-            // Hold the output of the decoding.
-            const expandedTexture = engine.createRenderTargetTexture(texture.width, {
-                generateDepthBuffer: false,
-                generateMipMaps: false,
-                generateStencilBuffer: false,
-                samplingMode: Constants.TEXTURE_BILINEAR_SAMPLINGMODE,
-                type: texture.type,
-                format: Constants.TEXTUREFORMAT_RGBA
-            });
-
-            rgbdPostProcess.getEffect().executeWhenCompiled(() => {
-                // PP Render Pass
-                rgbdPostProcess.onApply = (effect) => {
-                    effect._bindTexture("textureSampler", texture);
-                    effect.setFloat2("scale", 1, 1);
-                };
-                engine.scenes[0].postProcessManager.directRender([rgbdPostProcess!], expandedTexture, true);
-
-                // Cleanup
-                engine.restoreDefaultFramebuffer();
-                engine._releaseTexture(texture);
-                engine._releaseFramebufferObjects(expandedTexture);
-                if (rgbdPostProcess) {
-                    rgbdPostProcess.dispose();
-                }
-
-                // Internal Swap
-                expandedTexture._swapAndDie(texture);
-
-                // Ready to get rolling again.
-                texture.isReady = true;
-            });
-        }
-    }
-
     /**
      * Gets a default environment BRDF for MS-BRDF Height Correlated BRDF
      * @param scene defines the hosting scene
@@ -89,16 +19,14 @@ export class BRDFTextureTools {
             scene.useDelayedTextureLoading = false;
 
             var texture = Texture.CreateFromBase64String(this._environmentBRDFBase64Texture, "EnvironmentBRDFTexture", scene, true, false, Texture.BILINEAR_SAMPLINGMODE);
-            texture._texture!._isRGBD = true;
+            texture.isRGBD = true;
             texture.wrapU = Texture.CLAMP_ADDRESSMODE;
             texture.wrapV = Texture.CLAMP_ADDRESSMODE;
             scene.environmentBRDFTexture = texture;
 
             scene.useDelayedTextureLoading = useDelayedTextureLoading;
 
-            texture.onLoadObservable.addOnce(() => {
-                this._ExpandDefaultBRDFTexture(texture._texture!);
-            });
+            RGBDTextureTools.ExpandRGBDTexture(texture);
         }
 
         return scene.environmentBRDFTexture;

+ 1 - 0
src/Misc/index.ts

@@ -33,6 +33,7 @@ export * from "./typeStore";
 export * from "./webRequest";
 export * from "./iInspectable";
 export * from "./brdfTextureTools";
+export * from "./rgbdTextureTools";
 export * from "./gradients";
 export * from "./perfCounter";
 export * from "./fileRequest";

+ 83 - 0
src/Misc/rgbdTextureTools.ts

@@ -0,0 +1,83 @@
+import { Constants } from "../Engines/constants";
+import { PostProcess } from "../PostProcesses/postProcess";
+import "../Shaders/rgbdDecode.fragment";
+
+declare type Texture = import("../Materials/Textures/texture").Texture;
+
+/**
+ * Class used to host RGBD texture specific utilities
+ */
+export class RGBDTextureTools {
+    /**
+     * Expand the RGBD Texture from RGBD to Half Float if possible.
+     * @param texture the texture to expand.
+     */
+    public static ExpandRGBDTexture(texture: Texture) {
+        const internalTexture = texture._texture;
+        if (!internalTexture || !texture.isRGBD) {
+            return;
+        }
+
+        texture.onLoadObservable.addOnce(() => {
+            // Gets everything ready.
+            let engine = internalTexture.getEngine();
+            let caps = engine.getCaps();
+            let expandTexture = false;
+
+            // If half float available we can uncompress the texture
+            if (caps.textureHalfFloatRender && caps.textureHalfFloatLinearFiltering) {
+                expandTexture = true;
+                internalTexture.type = Constants.TEXTURETYPE_HALF_FLOAT;
+            }
+            // If full float available we can uncompress the texture
+            else if (caps.textureFloatRender && caps.textureFloatLinearFiltering) {
+                expandTexture = true;
+                internalTexture.type = Constants.TEXTURETYPE_FLOAT;
+            }
+
+            // Expand the texture if possible
+            if (expandTexture) {
+                // Do not use during decode.
+                internalTexture.isReady = false;
+
+                // Simply run through the decode PP.
+                const rgbdPostProcess = new PostProcess("rgbdDecode", "rgbdDecode", null, null, 1, null, Constants.TEXTURE_TRILINEAR_SAMPLINGMODE, engine, false, undefined, internalTexture.type, undefined, null, false);
+                internalTexture._isRGBD = false;
+                internalTexture.invertY = false;
+
+                // Hold the output of the decoding.
+                const expandedTexture = engine.createRenderTargetTexture(internalTexture.width, {
+                    generateDepthBuffer: false,
+                    generateMipMaps: false,
+                    generateStencilBuffer: false,
+                    samplingMode: internalTexture.samplingMode,
+                    type: internalTexture.type,
+                    format: Constants.TEXTUREFORMAT_RGBA
+                });
+
+                rgbdPostProcess.getEffect().executeWhenCompiled(() => {
+                    // PP Render Pass
+                    rgbdPostProcess.onApply = (effect) => {
+                        effect._bindTexture("textureSampler", internalTexture);
+                        effect.setFloat2("scale", 1, 1);
+                    };
+                    engine.scenes[0].postProcessManager.directRender([rgbdPostProcess!], expandedTexture, true);
+
+                    // Cleanup
+                    engine.restoreDefaultFramebuffer();
+                    engine._releaseTexture(internalTexture);
+                    engine._releaseFramebufferObjects(expandedTexture);
+                    if (rgbdPostProcess) {
+                        rgbdPostProcess.dispose();
+                    }
+
+                    // Internal Swap
+                    expandedTexture._swapAndDie(internalTexture);
+
+                    // Ready to get rolling again.
+                    internalTexture.isReady = true;
+                });
+            }
+        });
+    }
+}

+ 3 - 3
src/Shaders/ShadersInclude/lightFragment.fx

@@ -185,15 +185,15 @@
                 specularBase += computeCustomSpecularLighting(info, specularBase, shadow);
             #endif
         #elif defined(LIGHTMAP) && defined(LIGHTMAPEXCLUDED{X})
-            diffuseBase += lightmapColor * shadow;
+            diffuseBase += lightmapColor.rgb * shadow;
             #ifdef SPECULARTERM
                 #ifndef LIGHTMAPNOSPECULAR{X}
-                    specularBase += info.specular * shadow * lightmapColor;
+                    specularBase += info.specular * shadow * lightmapColor.rgb;
                 #endif
             #endif
             #ifdef CLEARCOAT
                 #ifndef LIGHTMAPNOSPECULAR{X}
-                    clearCoatBase += info.clearCoat.rgb * shadow * lightmapColor;
+                    clearCoatBase += info.clearCoat.rgb * shadow * lightmapColor.rgb;
                 #endif
             #endif
             #ifdef SHEEN

+ 10 - 5
src/Shaders/pbr.fragment.fx

@@ -832,11 +832,16 @@ void main(void) {
     #endif
 
     #ifdef LIGHTMAP
-        vec3 lightmapColor = texture2D(lightmapSampler, vLightmapUV + uvOffset).rgb;
+        vec4 lightmapColor = texture2D(lightmapSampler, vLightmapUV + uvOffset);
+
+        #ifdef RGBDLIGHTMAP
+            lightmapColor.rgb = fromRGBD(lightmapColor);
+        #endif
+
         #ifdef GAMMALIGHTMAP
-            lightmapColor = toLinearSpace(lightmapColor);
+            lightmapColor.rgb = toLinearSpace(lightmapColor.rgb);
         #endif
-        lightmapColor *= vLightmapInfos.y;
+        lightmapColor.rgb *= vLightmapInfos.y;
     #endif
 
     // Direct Lighting Variables
@@ -1215,9 +1220,9 @@ void main(void) {
 #ifdef LIGHTMAP
     #ifndef LIGHTMAPEXCLUDED
         #ifdef USELIGHTMAPASSHADOWMAP
-            finalColor.rgb *= lightmapColor;
+            finalColor.rgb *= lightmapColor.rgb;
         #else
-            finalColor.rgb += lightmapColor;
+            finalColor.rgb += lightmapColor.rgb;
         #endif
     #endif
 #endif