textureHelper.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { PostProcess } from 'babylonjs/PostProcesses/postProcess';
  2. import { Texture } from 'babylonjs/Materials/Textures/texture';
  3. import { PassPostProcess, PassCubePostProcess } from 'babylonjs/PostProcesses/passPostProcess';
  4. import { Constants } from 'babylonjs/Engines/constants';
  5. import { GlobalState } from './components/globalState';
  6. import { RenderTargetTexture } from 'babylonjs/Materials/Textures/renderTargetTexture';
  7. import { BaseTexture } from 'babylonjs/Materials/Textures/baseTexture';
  8. import { Nullable } from 'babylonjs/types';
  9. export enum TextureChannelToDisplay {
  10. R,
  11. G,
  12. B,
  13. A,
  14. All
  15. }
  16. export class TextureHelper {
  17. private static _ProcessAsync(texture: BaseTexture, width: number, height: number, face: number, channel: TextureChannelToDisplay, globalState: Nullable<GlobalState>, resolve: (result: Uint8Array) => void, reject: () => void) {
  18. var scene = texture.getScene()!;
  19. var engine = scene.getEngine();
  20. let passPostProcess: PostProcess;
  21. if (!texture.isCube) {
  22. passPostProcess = new PassPostProcess("pass", 1, null, Texture.NEAREST_SAMPLINGMODE, engine, false, Constants.TEXTURETYPE_UNSIGNED_INT);
  23. } else {
  24. var passCubePostProcess = new PassCubePostProcess("pass", 1, null, Texture.NEAREST_SAMPLINGMODE, engine, false, Constants.TEXTURETYPE_UNSIGNED_INT);
  25. passCubePostProcess.face = face;
  26. passPostProcess = passCubePostProcess;
  27. }
  28. if (!passPostProcess.getEffect().isReady()) {
  29. // Try again later
  30. passPostProcess.dispose();
  31. setTimeout(() => {
  32. this._ProcessAsync(texture, width, height, face, channel, globalState, resolve, reject);
  33. }, 250);
  34. return;
  35. }
  36. if (globalState) {
  37. globalState.blockMutationUpdates = true;
  38. }
  39. let rtt = new RenderTargetTexture(
  40. "temp",
  41. { width: width, height: height },
  42. scene, false);
  43. passPostProcess.onApply = function(effect) {
  44. effect.setTexture("textureSampler", texture);
  45. };
  46. let internalTexture = rtt.getInternalTexture();
  47. if (internalTexture) {
  48. scene.postProcessManager.directRender([passPostProcess], internalTexture);
  49. // Read the contents of the framebuffer
  50. var numberOfChannelsByLine = width * 4;
  51. var halfHeight = height / 2;
  52. //Reading datas from WebGL
  53. var data = engine.readPixels(0, 0, width, height);
  54. if (!texture.isCube) {
  55. if (channel != TextureChannelToDisplay.All) {
  56. for (var i = 0; i < width * height * 4; i += 4) {
  57. switch (channel) {
  58. case TextureChannelToDisplay.R:
  59. data[i + 1] = data[i];
  60. data[i + 2] = data[i];
  61. data[i + 3] = 255;
  62. break;
  63. case TextureChannelToDisplay.G:
  64. data[i] = data[i + 1];
  65. data[i + 2] = data[i];
  66. data[i + 3] = 255;
  67. break;
  68. case TextureChannelToDisplay.B:
  69. data[i] = data[i + 2];
  70. data[i + 1] = data[i + 2];
  71. data[i + 3] = 255;
  72. break;
  73. case TextureChannelToDisplay.A:
  74. data[i] = data[i + 3];
  75. data[i + 1] = data[i + 3];
  76. data[i + 2] = data[i + 3];
  77. data[i + 3] = 255;
  78. break;
  79. }
  80. }
  81. }
  82. }
  83. //To flip image on Y axis.
  84. if ((texture as Texture).invertY || texture.isCube) {
  85. for (var i = 0; i < halfHeight; i++) {
  86. for (var j = 0; j < numberOfChannelsByLine; j++) {
  87. var currentCell = j + i * numberOfChannelsByLine;
  88. var targetLine = height - i - 1;
  89. var targetCell = j + targetLine * numberOfChannelsByLine;
  90. var temp = data[currentCell];
  91. data[currentCell] = data[targetCell];
  92. data[targetCell] = temp;
  93. }
  94. }
  95. }
  96. resolve(data);
  97. // Unbind
  98. engine.unBindFramebuffer(internalTexture);
  99. } else {
  100. reject();
  101. }
  102. rtt.dispose();
  103. passPostProcess.dispose();
  104. if (globalState) {
  105. globalState.blockMutationUpdates = false;
  106. }
  107. }
  108. public static GetTextureDataAsync(texture: BaseTexture, width: number, height: number, face: number, channel: TextureChannelToDisplay, globalState?: GlobalState): Promise<Uint8Array> {
  109. return new Promise((resolve, reject) => {
  110. if (!texture.isReady() && texture._texture) {
  111. texture._texture.onLoadedObservable.addOnce(() => {
  112. this._ProcessAsync(texture, width, height, face, channel, globalState || null, resolve, reject);
  113. });
  114. return;
  115. }
  116. this._ProcessAsync(texture, width, height, face, channel, globalState || null, resolve, reject);
  117. });
  118. }
  119. }