Browse Source

Add a helper to apply a post process to a texture

Popov72 4 years ago
parent
commit
67bf602875
2 changed files with 74 additions and 47 deletions
  1. 3 47
      src/Misc/rgbdTextureTools.ts
  2. 71 0
      src/Misc/textureTools.ts

+ 3 - 47
src/Misc/rgbdTextureTools.ts

@@ -4,6 +4,7 @@ import "../Shaders/rgbdDecode.fragment";
 import { Engine } from '../Engines/engine';
 import { Engine } from '../Engines/engine';
 
 
 import "../Engines/Extensions/engine.renderTarget";
 import "../Engines/Extensions/engine.renderTarget";
+import { TextureTools } from './textureTools';
 
 
 declare type Texture = import("../Materials/Textures/texture").Texture;
 declare type Texture = import("../Materials/Textures/texture").Texture;
 declare type InternalTexture = import("../Materials/Textures/internalTexture").InternalTexture;
 declare type InternalTexture = import("../Materials/Textures/internalTexture").InternalTexture;
@@ -93,54 +94,9 @@ export class RGBDTextureTools {
      * @param internalTexture the texture to encode
      * @param internalTexture the texture to encode
      * @param scene the scene hosting the texture
      * @param scene the scene hosting the texture
      * @param outputTextureType type of the texture in which the encoding is performed
      * @param outputTextureType type of the texture in which the encoding is performed
+     * @return a promise with the internalTexture having its texture replaced by the result of the processing
      */
      */
     public static EncodeTextureToRGBD(internalTexture: InternalTexture, scene: Scene, outputTextureType = Constants.TEXTURETYPE_UNSIGNED_BYTE): Promise<InternalTexture> {
     public static EncodeTextureToRGBD(internalTexture: InternalTexture, scene: Scene, outputTextureType = Constants.TEXTURETYPE_UNSIGNED_BYTE): Promise<InternalTexture> {
-        // Gets everything ready.
-        const engine = internalTexture.getEngine() as Engine;
-
-        internalTexture.isReady = false;
-
-        return new Promise((resolve) => {
-            // Encode the texture if possible
-            // Simply run through the encode PP.
-            const rgbdPostProcess = new PostProcess("rgbdEncode", "rgbdEncode", null, null, 1, null, Constants.TEXTURE_NEAREST_SAMPLINGMODE, engine, false, undefined, outputTextureType, undefined, null, false);
-
-            // Hold the output of the decoding.
-            const encodedTexture = engine.createRenderTargetTexture({ width: internalTexture.width, height: internalTexture.height }, {
-                generateDepthBuffer: false,
-                generateMipMaps: false,
-                generateStencilBuffer: false,
-                samplingMode: Constants.TEXTURE_NEAREST_SAMPLINGMODE,
-                type: outputTextureType,
-                format: Constants.TEXTUREFORMAT_RGBA
-            });
-
-            rgbdPostProcess.getEffect().executeWhenCompiled(() => {
-                // PP Render Pass
-                rgbdPostProcess.onApply = (effect) => {
-                    effect._bindTexture("textureSampler", internalTexture);
-                    effect.setFloat2("scale", 1, 1);
-                };
-                scene.postProcessManager.directRender([rgbdPostProcess!], encodedTexture, true);
-
-                // Cleanup
-                engine.restoreDefaultFramebuffer();
-                engine._releaseTexture(internalTexture);
-                engine._releaseFramebufferObjects(encodedTexture);
-                if (rgbdPostProcess) {
-                    rgbdPostProcess.dispose();
-                }
-
-                // Internal Swap
-                encodedTexture._swapAndDie(internalTexture);
-
-                // Ready to get rolling again.
-                internalTexture.type = outputTextureType;
-                internalTexture.format = Constants.TEXTUREFORMAT_RGBA;
-                internalTexture.isReady = true;
-
-                resolve(internalTexture);
-            });
-        });
+        return TextureTools.ApplyPostProcess("rgbdEncode", internalTexture, scene, outputTextureType, Constants.TEXTURE_NEAREST_SAMPLINGMODE, Constants.TEXTUREFORMAT_RGBA);
     }
     }
 }
 }

+ 71 - 0
src/Misc/textureTools.ts

@@ -4,6 +4,8 @@ import { RenderTargetTexture } from "../Materials/Textures/renderTargetTexture";
 import { PassPostProcess } from "../PostProcesses/passPostProcess";
 import { PassPostProcess } from "../PostProcesses/passPostProcess";
 import { Constants } from "../Engines/constants";
 import { Constants } from "../Engines/constants";
 import { Scene } from "../scene";
 import { Scene } from "../scene";
+import { PostProcess } from '../PostProcesses/postProcess';
+import { Engine } from '../Engines/engine';
 
 
 /**
 /**
  * Class used to host texture specific utilities
  * Class used to host texture specific utilities
@@ -72,4 +74,73 @@ export class TextureTools {
 
 
         return rtt;
         return rtt;
     }
     }
+
+    /**
+     * Apply a post process to a texture
+     * @param postProcessName name of the fragment post process
+     * @param internalTexture the texture to encode
+     * @param scene the scene hosting the texture
+     * @param type type of the output texture. If not provided, use the one from internalTexture
+     * @param samplingMode sampling moode to use to sample the source texture. If not provided, use the one from internalTexture
+     * @param format format of the output texture. If not provided, use the one from internalTexture
+     * @return a promise with the internalTexture having its texture replaced by the result of the processing
+     */
+    public static ApplyPostProcess(postProcessName: string, internalTexture: InternalTexture, scene: Scene, type?: number, samplingMode?: number, format?: number): Promise<InternalTexture> {
+        // Gets everything ready.
+        const engine = internalTexture.getEngine() as Engine;
+
+        internalTexture.isReady = false;
+
+        samplingMode = samplingMode ?? internalTexture.samplingMode;
+        type = type ?? internalTexture.type;
+        format = format ?? internalTexture.format;
+
+        if (type === -1) {
+            type = Constants.TEXTURETYPE_UNSIGNED_BYTE;
+        }
+
+        return new Promise((resolve) => {
+            // Create the post process
+            const postProcess = new PostProcess("postprocess", postProcessName, null, null, 1, null, samplingMode, engine,
+                false, undefined, type, undefined, null, false, format);
+
+            // Hold the output of the decoding.
+            const encodedTexture = engine.createRenderTargetTexture({ width: internalTexture.width, height: internalTexture.height }, {
+                generateDepthBuffer: false,
+                generateMipMaps: false,
+                generateStencilBuffer: false,
+                samplingMode,
+                type,
+                format
+            });
+
+            postProcess.getEffect().executeWhenCompiled(() => {
+                // PP Render Pass
+                postProcess.onApply = (effect) => {
+                    effect._bindTexture("textureSampler", internalTexture);
+                    effect.setFloat2("scale", 1, 1);
+                };
+                scene.postProcessManager.directRender([postProcess!], encodedTexture, true);
+
+                // Cleanup
+                engine.restoreDefaultFramebuffer();
+                engine._releaseTexture(internalTexture);
+                engine._releaseFramebufferObjects(encodedTexture);
+                if (postProcess) {
+                    postProcess.dispose();
+                }
+
+                // Internal Swap
+                encodedTexture._swapAndDie(internalTexture);
+
+                // Ready to get rolling again.
+                internalTexture.type = type!;
+                internalTexture.format = Constants.TEXTUREFORMAT_RGBA;
+                internalTexture.isReady = true;
+
+                resolve(internalTexture);
+            });
+        });
+    }
+
 }
 }