Kaynağa Gözat

Merge pull request #6630 from Dok11/master

Defining typescript interface for options of method to make screenshots
David Catuhe 6 yıl önce
ebeveyn
işleme
eb74e0bc5d

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

@@ -75,6 +75,9 @@
 - Added `ShaderMaterial.setColor4Array` ([JonathanTron](https://github.com/JonathanTron/))
 - Added `ShaderMaterial.setArray4` ([JonathanTron](https://github.com/JonathanTron/))
 
+### ScreenshotTools
+- Added interface for argument `size` of screenshot methods ([Dok11](https://github.com/Dok11/))
+
 ### Sounds
 - Added `ISoundOptions.skipCodecCheck` to make `Sound` more flexible with URLs ([nbduke](https://github.com/nbduke))
 

+ 21 - 0
src/Misc/interfaces/screenshotSize.ts

@@ -0,0 +1,21 @@
+/**
+ * Interface for screenshot methods with describe argument called `size` as object with options
+ * @link https://doc.babylonjs.com/api/classes/babylon.screenshottools
+ */
+export interface IScreenshotSize {
+  /**
+   * number in pixels for canvas height
+   */
+  height?: number;
+
+  /**
+   * multiplier allowing render at a higher or lower resolution
+   * If value is defined then height and width will be ignored and taken from camera
+   */
+  precision?: number;
+
+  /**
+   * number in pixels for canvas width
+   */
+  width?: number;
+}

+ 68 - 49
src/Misc/screenshotTools.ts

@@ -6,7 +6,8 @@ import { FxaaPostProcess } from "../PostProcesses/fxaaPostProcess";
 import { Constants } from "../Engines/constants";
 import { Logger } from "./logger";
 import { _TypeStore } from "./typeStore";
-import { Tools } from './tools';
+import { Tools } from "./tools";
+import { IScreenshotSize } from './interfaces/screenshotSize';
 
 declare type Engine = import("../Engines/engine").Engine;
 
@@ -30,28 +31,35 @@ export class ScreenshotTools {
      * @param mimeType defines the MIME type of the screenshot image (default: image/png).
      * Check your browser for supported MIME types
      */
-    public static CreateScreenshot(engine: Engine, camera: Camera, size: any, successCallback?: (data: string) => void, mimeType: string = "image/png"): void {
-        var width: number;
-        var height: number;
-
-        // If a precision value is specified
-        if (size.precision) {
-            width = Math.round(engine.getRenderWidth() * size.precision);
-            height = Math.round(width / engine.getAspectRatio(camera));
-        }
-        else if (size.width && size.height) {
-            width = size.width;
-            height = size.height;
-        }
-        //If passing only width, computing height to keep display canvas ratio.
-        else if (size.width && !size.height) {
-            width = size.width;
-            height = Math.round(width / engine.getAspectRatio(camera));
-        }
-        //If passing only height, computing width to keep display canvas ratio.
-        else if (size.height && !size.width) {
-            height = size.height;
-            width = Math.round(height * engine.getAspectRatio(camera));
+    public static CreateScreenshot(engine: Engine, camera: Camera, size: IScreenshotSize | number, successCallback?: (data: string) => void, mimeType: string = "image/png"): void {
+        let width = 0;
+        let height = 0;
+
+        //If a size value defined as object
+        if (typeof(size) === 'object') {
+            // If a precision value is specified
+            if (size.precision) {
+                width = Math.round(engine.getRenderWidth() * size.precision);
+                height = Math.round(width / engine.getAspectRatio(camera));
+            }
+            else if (size.width && size.height) {
+                width = size.width;
+                height = size.height;
+            }
+            //If passing only width, computing height to keep display canvas ratio.
+            else if (size.width && !size.height) {
+                width = size.width;
+                height = Math.round(width / engine.getAspectRatio(camera));
+            }
+            //If passing only height, computing width to keep display canvas ratio.
+            else if (size.height && !size.width) {
+                height = size.height;
+                width = Math.round(height * engine.getAspectRatio(camera));
+            }
+            else {
+                width = Math.round(engine.getRenderWidth());
+                height = Math.round(width / engine.getAspectRatio(camera));
+            }
         }
         //Assuming here that "size" parameter is a number
         else if (!isNaN(size)) {
@@ -137,36 +145,47 @@ export class ScreenshotTools {
      * @param antialiasing Whether antialiasing should be turned on or not (default: false)
      * @param fileName A name for for the downloaded file.
      */
-    public static CreateScreenshotUsingRenderTarget(engine: Engine, camera: Camera, size: any, successCallback?: (data: string) => void, mimeType: string = "image/png", samples: number = 1, antialiasing: boolean = false, fileName?: string): void {
-        var width: number;
-        var height: number;
-
-        //If a precision value is specified
-        if (size.precision) {
-            width = Math.round(engine.getRenderWidth() * size.precision);
-            height = Math.round(width / engine.getAspectRatio(camera));
-            size = { width: width, height: height };
-        }
-        else if (size.width && size.height) {
-            width = size.width;
-            height = size.height;
-        }
-        //If passing only width, computing height to keep display canvas ratio.
-        else if (size.width && !size.height) {
-            width = size.width;
-            height = Math.round(width / engine.getAspectRatio(camera));
-            size = { width: width, height: height };
-        }
-        //If passing only height, computing width to keep display canvas ratio.
-        else if (size.height && !size.width) {
-            height = size.height;
-            width = Math.round(height * engine.getAspectRatio(camera));
-            size = { width: width, height: height };
+    public static CreateScreenshotUsingRenderTarget(engine: Engine, camera: Camera, size: IScreenshotSize | number, successCallback?: (data: string) => void, mimeType: string = "image/png", samples: number = 1, antialiasing: boolean = false, fileName?: string): void {
+        let width = 0;
+        let height = 0;
+        let targetTextureSize: number | { width: number, height: number } = 0;
+
+        //If a size value defined as object
+        if (typeof(size) === 'object') {
+            //If a precision value is specified
+            if (size.precision) {
+                width = Math.round(engine.getRenderWidth() * size.precision);
+                height = Math.round(width / engine.getAspectRatio(camera));
+                size = { width: width, height: height };
+            }
+            else if (size.width && size.height) {
+                width = size.width;
+                height = size.height;
+            }
+            //If passing only width, computing height to keep display canvas ratio.
+            else if (size.width && !size.height) {
+                width = size.width;
+                height = Math.round(width / engine.getAspectRatio(camera));
+                size = { width: width, height: height };
+            }
+            //If passing only height, computing width to keep display canvas ratio.
+            else if (size.height && !size.width) {
+                height = size.height;
+                width = Math.round(height * engine.getAspectRatio(camera));
+                size = { width: width, height: height };
+            }
+            else {
+                width = Math.round(engine.getRenderWidth());
+                height = Math.round(width / engine.getAspectRatio(camera));
+            }
+
+            targetTextureSize = {width, height};
         }
         //Assuming here that "size" parameter is a number
         else if (!isNaN(size)) {
             height = size;
             width = size;
+            targetTextureSize = size;
         }
         else {
             Logger.Error("Invalid 'size' parameter !");
@@ -192,7 +211,7 @@ export class ScreenshotTools {
         scene.render();
 
         // At this point size can be a number, or an object (according to engine.prototype.createRenderTargetTexture method)
-        var texture = new RenderTargetTexture("screenShot", size, scene, false, false, Constants.TEXTURETYPE_UNSIGNED_INT, false, Texture.NEAREST_SAMPLINGMODE);
+        var texture = new RenderTargetTexture("screenShot", targetTextureSize, scene, false, false, Constants.TEXTURETYPE_UNSIGNED_INT, false, Texture.NEAREST_SAMPLINGMODE);
         texture.renderList = null;
         texture.samples = samples;
         if (antialiasing) {

+ 5 - 4
src/Misc/tools.ts

@@ -15,6 +15,7 @@ import { PromisePolyfill } from './promise';
 import { TimingTools } from './timingTools';
 import { InstantiationTools } from './instantiationTools';
 import { GUID } from './guid';
+import { IScreenshotSize } from './interfaces/screenshotSize';
 
 declare type Camera = import("../Cameras/camera").Camera;
 declare type Engine = import("../Engines/engine").Engine;
@@ -754,7 +755,7 @@ export class Tools {
      * @param mimeType defines the MIME type of the screenshot image (default: image/png).
      * Check your browser for supported MIME types
      */
-    public static CreateScreenshot(engine: Engine, camera: Camera, size: any, successCallback?: (data: string) => void, mimeType: string = "image/png"): void {
+    public static CreateScreenshot(engine: Engine, camera: Camera, size: IScreenshotSize | number, successCallback?: (data: string) => void, mimeType: string = "image/png"): void {
         throw _DevTools.WarnImport("ScreenshotTools");
     }
 
@@ -773,7 +774,7 @@ export class Tools {
      * @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> {
+    public static CreateScreenshotAsync(engine: Engine, camera: Camera, size: IScreenshotSize | number, mimeType: string = "image/png"): Promise<string> {
         throw _DevTools.WarnImport("ScreenshotTools");
     }
 
@@ -796,7 +797,7 @@ export class Tools {
      * @param antialiasing Whether antialiasing should be turned on or not (default: false)
      * @param fileName A name for for the downloaded file.
      */
-    public static CreateScreenshotUsingRenderTarget(engine: Engine, camera: Camera, size: any, successCallback?: (data: string) => void, mimeType: string = "image/png", samples: number = 1, antialiasing: boolean = false, fileName?: string): void {
+    public static CreateScreenshotUsingRenderTarget(engine: Engine, camera: Camera, size: IScreenshotSize | number, successCallback?: (data: string) => void, mimeType: string = "image/png", samples: number = 1, antialiasing: boolean = false, fileName?: string): void {
         throw _DevTools.WarnImport("ScreenshotTools");
     }
 
@@ -818,7 +819,7 @@ export class Tools {
      * @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> {
+    public static CreateScreenshotUsingRenderTargetAsync(engine: Engine, camera: Camera, size: IScreenshotSize | number, mimeType: string = "image/png", samples: number = 1, antialiasing: boolean = false, fileName?: string): Promise<string> {
         throw _DevTools.WarnImport("ScreenshotTools");
     }