|
@@ -1,3 +1,4 @@
|
|
|
+import { ISize } from "../Maths/math.size";
|
|
|
import { Nullable } from "../types";
|
|
|
|
|
|
declare type BaseTexture = import("../Materials/Textures/baseTexture").BaseTexture;
|
|
@@ -7,25 +8,13 @@ declare type BaseTexture = import("../Materials/Textures/baseTexture").BaseTextu
|
|
|
*/
|
|
|
export class CopyTools {
|
|
|
/**
|
|
|
- * Reads the pixels stored in the webgl texture and returns them as a base64 string
|
|
|
- * @param texture defines the texture to read pixels from
|
|
|
- * @param faceIndex defines the face of the texture to read (in case of cube texture)
|
|
|
- * @param level defines the LOD level of the texture to read (in case of Mip Maps)
|
|
|
+ * Transform some pixel data to a base64 string
|
|
|
+ * @param pixels defines the pixel data to transform to base64
|
|
|
+ * @param size defines the width and height of the (texture) data
|
|
|
+ * @param invertY true if the data must be inverted for the Y coordinate during the conversion
|
|
|
* @returns The base64 encoded string or null
|
|
|
*/
|
|
|
- public static async GenerateBase64StringFromTexture(texture: BaseTexture, faceIndex = 0, level = 0): Promise<Nullable<string>> {
|
|
|
-
|
|
|
- var internalTexture = texture.getInternalTexture();
|
|
|
- if (!internalTexture) {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- var pixels = await texture.readPixels(faceIndex, level);
|
|
|
- if (!pixels) {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- var size = texture.getSize();
|
|
|
+ public static GenerateBase64StringFromPixelData(pixels: ArrayBufferView, size: ISize, invertY = false): Nullable<string> {
|
|
|
var width = size.width;
|
|
|
var height = size.height;
|
|
|
|
|
@@ -60,7 +49,7 @@ export class CopyTools {
|
|
|
castData.set(pixels);
|
|
|
ctx.putImageData(imageData, 0, 0);
|
|
|
|
|
|
- if (internalTexture.invertY) {
|
|
|
+ if (invertY) {
|
|
|
var canvas2 = document.createElement('canvas');
|
|
|
canvas2.width = width;
|
|
|
canvas2.height = height;
|
|
@@ -79,4 +68,46 @@ export class CopyTools {
|
|
|
|
|
|
return canvas.toDataURL('image/png');
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Reads the pixels stored in the webgl texture and returns them as a base64 string
|
|
|
+ * @param texture defines the texture to read pixels from
|
|
|
+ * @param faceIndex defines the face of the texture to read (in case of cube texture)
|
|
|
+ * @param level defines the LOD level of the texture to read (in case of Mip Maps)
|
|
|
+ * @returns The base64 encoded string or null
|
|
|
+ */
|
|
|
+ public static GenerateBase64StringFromTexture(texture: BaseTexture, faceIndex = 0, level = 0): Nullable<string> {
|
|
|
+ var internalTexture = texture.getInternalTexture();
|
|
|
+ if (!internalTexture) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ var pixels = texture._readPixelsSync(faceIndex, level);
|
|
|
+ if (!pixels) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ return CopyTools.GenerateBase64StringFromPixelData(pixels, texture.getSize(), internalTexture.invertY);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Reads the pixels stored in the webgl texture and returns them as a base64 string
|
|
|
+ * @param texture defines the texture to read pixels from
|
|
|
+ * @param faceIndex defines the face of the texture to read (in case of cube texture)
|
|
|
+ * @param level defines the LOD level of the texture to read (in case of Mip Maps)
|
|
|
+ * @returns The base64 encoded string or null wrapped in a promise
|
|
|
+ */
|
|
|
+ public static async GenerateBase64StringFromTextureAsync(texture: BaseTexture, faceIndex = 0, level = 0): Promise<Nullable<string>> {
|
|
|
+ var internalTexture = texture.getInternalTexture();
|
|
|
+ if (!internalTexture) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ var pixels = await texture.readPixels(faceIndex, level);
|
|
|
+ if (!pixels) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ return CopyTools.GenerateBase64StringFromPixelData(pixels, texture.getSize(), internalTexture.invertY);
|
|
|
+ }
|
|
|
}
|