Browse Source

Merge pull request #6609 from mehmetoguzderin/master

Add `Tools.CreateScreenshotAsync` and `Tools.CreateScreenshotUsingRenderTargetAsync`
David Catuhe 6 years ago
parent
commit
19bfb99285
3 changed files with 102 additions and 1 deletions
  1. 1 0
      dist/preview release/what's new.md
  2. 60 1
      src/Misc/screenshotTools.ts
  3. 41 0
      src/Misc/tools.ts

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

@@ -50,6 +50,7 @@
 - Added support for inspectable strings ([Deltakosh](https://github.com/deltakosh/))
 
 ### Tools
+- Added `Tools.CreateScreenshotAsync` and `Tools.CreateScreenshotUsingRenderTargetAsync` ([mehmetoguzderin](https://github.com/mehmetoguzderin/))
 - Added `Color3.toHSV()`, `Color3.toHSVToRef()` and `Color3.HSVtoRGBToRef()` ([Deltakosh](https://github.com/deltakosh/))
 - 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/))

+ 60 - 1
src/Misc/screenshotTools.ts

@@ -92,6 +92,33 @@ export class ScreenshotTools {
     }
 
     /**
+     * Captures a screenshot of the current rendering
+     * @see http://doc.babylonjs.com/how_to/render_scene_on_a_png
+     * @param engine defines the rendering engine
+     * @param camera defines the source camera
+     * @param size This parameter can be set to a single number or to an object with the
+     * following (optional) properties: precision, width, height. If a single number is passed,
+     * it will be used for both width and height. If an object is passed, the screenshot size
+     * will be derived from the parameters. The precision property is a multiplier allowing
+     * rendering at a higher or lower resolution
+     * @param mimeType defines the MIME type of the screenshot image (default: image/png).
+     * Check your browser for supported MIME types
+     * @returns screenshot as a string of base64-encoded characters. This string can be assigned
+     * to the src parameter of an <img> to display it
+     */
+    public static CreateScreenshotAsync(engine: Engine, camera: Camera, size: any, mimeType: string = "image/png"): Promise<string> {
+        return new Promise((resolve, reject) => {
+            ScreenshotTools.CreateScreenshot(engine, camera, size, (data) => {
+                if (typeof(data) !== "undefined") {
+                    resolve(data);
+                } else {
+                    reject(new Error("Data is undefined"));
+                }
+            }, mimeType);
+        });
+    }
+
+    /**
      * Generates an image screenshot from the specified camera.
      * @see http://doc.babylonjs.com/how_to/render_scene_on_a_png
      * @param engine The engine to use for rendering
@@ -186,7 +213,39 @@ export class ScreenshotTools {
         engine.setSize(originalSize.width, originalSize.height);
         camera.getProjectionMatrix(true); // Force cache refresh;
     }
+
+    /**
+     * Generates an image screenshot from the specified camera.
+     * @see http://doc.babylonjs.com/how_to/render_scene_on_a_png
+     * @param engine The engine to use for rendering
+     * @param camera The camera to use for rendering
+     * @param size This parameter can be set to a single number or to an object with the
+     * following (optional) properties: precision, width, height. If a single number is passed,
+     * it will be used for both width and height. If an object is passed, the screenshot size
+     * will be derived from the parameters. The precision property is a multiplier allowing
+     * rendering at a higher or lower resolution
+     * @param mimeType The MIME type of the screenshot image (default: image/png).
+     * Check your browser for supported MIME types
+     * @param samples Texture samples (default: 1)
+     * @param antialiasing Whether antialiasing should be turned on or not (default: false)
+     * @param fileName A name for for the downloaded file.
+     * @returns screenshot as a string of base64-encoded characters. This string can be assigned
+     * to the src parameter of an <img> to display it
+     */
+    public static CreateScreenshotUsingRenderTargetAsync(engine: Engine, camera: Camera, size: any, mimeType: string = "image/png", samples: number = 1, antialiasing: boolean = false, fileName?: string): Promise<string> {
+        return new Promise((resolve, reject) => {
+            ScreenshotTools.CreateScreenshotUsingRenderTarget(engine, camera, size, (data) => {
+                if (typeof(data) !== "undefined") {
+                    resolve(data);
+                } else {
+                    reject(new Error("Data is undefined"));
+                }
+            }, mimeType, samples, antialiasing, fileName);
+        });
+    }
 }
 
 Tools.CreateScreenshot = ScreenshotTools.CreateScreenshot;
-Tools.CreateScreenshotUsingRenderTarget = ScreenshotTools.CreateScreenshotUsingRenderTarget;
+Tools.CreateScreenshotAsync = ScreenshotTools.CreateScreenshotAsync;
+Tools.CreateScreenshotUsingRenderTarget = ScreenshotTools.CreateScreenshotUsingRenderTarget;
+Tools.CreateScreenshotUsingRenderTargetAsync = ScreenshotTools.CreateScreenshotUsingRenderTargetAsync;

+ 41 - 0
src/Misc/tools.ts

@@ -759,6 +759,25 @@ export class Tools {
     }
 
     /**
+     * Captures a screenshot of the current rendering
+     * @see http://doc.babylonjs.com/how_to/render_scene_on_a_png
+     * @param engine defines the rendering engine
+     * @param camera defines the source camera
+     * @param size This parameter can be set to a single number or to an object with the
+     * following (optional) properties: precision, width, height. If a single number is passed,
+     * it will be used for both width and height. If an object is passed, the screenshot size
+     * will be derived from the parameters. The precision property is a multiplier allowing
+     * rendering at a higher or lower resolution
+     * @param mimeType defines the MIME type of the screenshot image (default: image/png).
+     * Check your browser for supported MIME types
+     * @returns screenshot as a string of base64-encoded characters. This string can be assigned
+     * to the src parameter of an <img> to display it
+     */
+    public static CreateScreenshotAsync(engine: Engine, camera: Camera, size: any, mimeType: string = "image/png"): Promise<string> {
+        throw _DevTools.WarnImport("ScreenshotTools");
+    }
+
+    /**
      * Generates an image screenshot from the specified camera.
      * @see http://doc.babylonjs.com/how_to/render_scene_on_a_png
      * @param engine The engine to use for rendering
@@ -782,6 +801,28 @@ export class Tools {
     }
 
     /**
+     * Generates an image screenshot from the specified camera.
+     * @see http://doc.babylonjs.com/how_to/render_scene_on_a_png
+     * @param engine The engine to use for rendering
+     * @param camera The camera to use for rendering
+     * @param size This parameter can be set to a single number or to an object with the
+     * following (optional) properties: precision, width, height. If a single number is passed,
+     * it will be used for both width and height. If an object is passed, the screenshot size
+     * will be derived from the parameters. The precision property is a multiplier allowing
+     * rendering at a higher or lower resolution
+     * @param mimeType The MIME type of the screenshot image (default: image/png).
+     * Check your browser for supported MIME types
+     * @param samples Texture samples (default: 1)
+     * @param antialiasing Whether antialiasing should be turned on or not (default: false)
+     * @param fileName A name for for the downloaded file.
+     * @returns screenshot as a string of base64-encoded characters. This string can be assigned
+     * to the src parameter of an <img> to display it
+     */
+    public static CreateScreenshotUsingRenderTargetAsync(engine: Engine, camera: Camera, size: any, mimeType: string = "image/png", samples: number = 1, antialiasing: boolean = false, fileName?: string): Promise<string> {
+        throw _DevTools.WarnImport("ScreenshotTools");
+    }
+
+    /**
      * Implementation from http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/2117523#answer-2117523
      * Be aware Math.random() could cause collisions, but:
      * "All but 6 of the 128 bits of the ID are randomly generated, which means that for any two ids, there's a 1 in 2^^122 (or 5.3x10^^36) chance they'll collide"