123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- /**
- * 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
- */
- export function GenerateBase64StringFromPixelData(pixels, size, invertY = false) {
- const width = size.width;
- const height = size.height;
- if (pixels instanceof Float32Array) {
- let len = pixels.byteLength / pixels.BYTES_PER_ELEMENT;
- const npixels = new Uint8Array(len);
- while (--len >= 0) {
- let val = pixels[len];
- if (val < 0) {
- val = 0;
- }
- else if (val > 1) {
- val = 1;
- }
- npixels[len] = val * 255;
- }
- pixels = npixels;
- }
- const canvas = document.createElement("canvas");
- canvas.width = width;
- canvas.height = height;
- const ctx = canvas.getContext("2d");
- if (!ctx) {
- return null;
- }
- const imageData = ctx.createImageData(width, height);
- const castData = imageData.data;
- castData.set(pixels);
- ctx.putImageData(imageData, 0, 0);
- if (invertY) {
- const canvas2 = document.createElement("canvas");
- canvas2.width = width;
- canvas2.height = height;
- const ctx2 = canvas2.getContext("2d");
- if (!ctx2) {
- return null;
- }
- ctx2.translate(0, height);
- ctx2.scale(1, -1);
- ctx2.drawImage(canvas, 0, 0);
- return canvas2.toDataURL("image/png");
- }
- 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
- */
- export function GenerateBase64StringFromTexture(texture, faceIndex = 0, level = 0) {
- const internalTexture = texture.getInternalTexture();
- if (!internalTexture) {
- return null;
- }
- const pixels = texture._readPixelsSync(faceIndex, level);
- if (!pixels) {
- return null;
- }
- return 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
- */
- export async function GenerateBase64StringFromTextureAsync(texture, faceIndex = 0, level = 0) {
- const internalTexture = texture.getInternalTexture();
- if (!internalTexture) {
- return null;
- }
- const pixels = await texture.readPixels(faceIndex, level);
- if (!pixels) {
- return null;
- }
- return GenerateBase64StringFromPixelData(pixels, texture.getSize(), internalTexture.invertY);
- }
- /**
- * Class used to host copy specific utilities
- * (Back-compat)
- */
- export const CopyTools = {
- /**
- * 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
- */
- GenerateBase64StringFromPixelData,
- /**
- * 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
- */
- GenerateBase64StringFromTexture,
- /**
- * 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
- */
- GenerateBase64StringFromTextureAsync,
- };
- //# sourceMappingURL=copyTools.js.map
|