copyTools.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * Transform some pixel data to a base64 string
  3. * @param pixels defines the pixel data to transform to base64
  4. * @param size defines the width and height of the (texture) data
  5. * @param invertY true if the data must be inverted for the Y coordinate during the conversion
  6. * @returns The base64 encoded string or null
  7. */
  8. export function GenerateBase64StringFromPixelData(pixels, size, invertY = false) {
  9. const width = size.width;
  10. const height = size.height;
  11. if (pixels instanceof Float32Array) {
  12. let len = pixels.byteLength / pixels.BYTES_PER_ELEMENT;
  13. const npixels = new Uint8Array(len);
  14. while (--len >= 0) {
  15. let val = pixels[len];
  16. if (val < 0) {
  17. val = 0;
  18. }
  19. else if (val > 1) {
  20. val = 1;
  21. }
  22. npixels[len] = val * 255;
  23. }
  24. pixels = npixels;
  25. }
  26. const canvas = document.createElement("canvas");
  27. canvas.width = width;
  28. canvas.height = height;
  29. const ctx = canvas.getContext("2d");
  30. if (!ctx) {
  31. return null;
  32. }
  33. const imageData = ctx.createImageData(width, height);
  34. const castData = imageData.data;
  35. castData.set(pixels);
  36. ctx.putImageData(imageData, 0, 0);
  37. if (invertY) {
  38. const canvas2 = document.createElement("canvas");
  39. canvas2.width = width;
  40. canvas2.height = height;
  41. const ctx2 = canvas2.getContext("2d");
  42. if (!ctx2) {
  43. return null;
  44. }
  45. ctx2.translate(0, height);
  46. ctx2.scale(1, -1);
  47. ctx2.drawImage(canvas, 0, 0);
  48. return canvas2.toDataURL("image/png");
  49. }
  50. return canvas.toDataURL("image/png");
  51. }
  52. /**
  53. * Reads the pixels stored in the webgl texture and returns them as a base64 string
  54. * @param texture defines the texture to read pixels from
  55. * @param faceIndex defines the face of the texture to read (in case of cube texture)
  56. * @param level defines the LOD level of the texture to read (in case of Mip Maps)
  57. * @returns The base64 encoded string or null
  58. */
  59. export function GenerateBase64StringFromTexture(texture, faceIndex = 0, level = 0) {
  60. const internalTexture = texture.getInternalTexture();
  61. if (!internalTexture) {
  62. return null;
  63. }
  64. const pixels = texture._readPixelsSync(faceIndex, level);
  65. if (!pixels) {
  66. return null;
  67. }
  68. return GenerateBase64StringFromPixelData(pixels, texture.getSize(), internalTexture.invertY);
  69. }
  70. /**
  71. * Reads the pixels stored in the webgl texture and returns them as a base64 string
  72. * @param texture defines the texture to read pixels from
  73. * @param faceIndex defines the face of the texture to read (in case of cube texture)
  74. * @param level defines the LOD level of the texture to read (in case of Mip Maps)
  75. * @returns The base64 encoded string or null wrapped in a promise
  76. */
  77. export async function GenerateBase64StringFromTextureAsync(texture, faceIndex = 0, level = 0) {
  78. const internalTexture = texture.getInternalTexture();
  79. if (!internalTexture) {
  80. return null;
  81. }
  82. const pixels = await texture.readPixels(faceIndex, level);
  83. if (!pixels) {
  84. return null;
  85. }
  86. return GenerateBase64StringFromPixelData(pixels, texture.getSize(), internalTexture.invertY);
  87. }
  88. /**
  89. * Class used to host copy specific utilities
  90. * (Back-compat)
  91. */
  92. export const CopyTools = {
  93. /**
  94. * Transform some pixel data to a base64 string
  95. * @param pixels defines the pixel data to transform to base64
  96. * @param size defines the width and height of the (texture) data
  97. * @param invertY true if the data must be inverted for the Y coordinate during the conversion
  98. * @returns The base64 encoded string or null
  99. */
  100. GenerateBase64StringFromPixelData,
  101. /**
  102. * Reads the pixels stored in the webgl texture and returns them as a base64 string
  103. * @param texture defines the texture to read pixels from
  104. * @param faceIndex defines the face of the texture to read (in case of cube texture)
  105. * @param level defines the LOD level of the texture to read (in case of Mip Maps)
  106. * @returns The base64 encoded string or null
  107. */
  108. GenerateBase64StringFromTexture,
  109. /**
  110. * Reads the pixels stored in the webgl texture and returns them as a base64 string
  111. * @param texture defines the texture to read pixels from
  112. * @param faceIndex defines the face of the texture to read (in case of cube texture)
  113. * @param level defines the LOD level of the texture to read (in case of Mip Maps)
  114. * @returns The base64 encoded string or null wrapped in a promise
  115. */
  116. GenerateBase64StringFromTextureAsync,
  117. };
  118. //# sourceMappingURL=copyTools.js.map