textureHelper.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 interface TextureChannelsToDisplay {
  10. R: boolean;
  11. G: boolean;
  12. B: boolean;
  13. A: boolean;
  14. }
  15. export class TextureHelper {
  16. private static _ProcessAsync(texture: BaseTexture, width: number, height: number, face: number, channels: TextureChannelsToDisplay, globalState: Nullable<GlobalState>, resolve: (result: Uint8Array) => void, reject: () => void) {
  17. var scene = texture.getScene()!;
  18. var engine = scene.getEngine();
  19. let passPostProcess: PostProcess;
  20. if (!texture.isCube) {
  21. passPostProcess = new PassPostProcess("pass", 1, null, Texture.NEAREST_SAMPLINGMODE, engine, false, Constants.TEXTURETYPE_UNSIGNED_INT);
  22. } else {
  23. var passCubePostProcess = new PassCubePostProcess("pass", 1, null, Texture.NEAREST_SAMPLINGMODE, engine, false, Constants.TEXTURETYPE_UNSIGNED_INT);
  24. passCubePostProcess.face = face;
  25. passPostProcess = passCubePostProcess;
  26. }
  27. if (!passPostProcess.getEffect().isReady()) {
  28. // Try again later
  29. passPostProcess.dispose();
  30. setTimeout(() => {
  31. this._ProcessAsync(texture, width, height, face, channels, globalState, resolve, reject);
  32. }, 250);
  33. return;
  34. }
  35. if (globalState) {
  36. globalState.blockMutationUpdates = true;
  37. }
  38. let rtt = new RenderTargetTexture(
  39. "temp",
  40. { width: width, height: height },
  41. scene, false);
  42. passPostProcess.onApply = function(effect) {
  43. effect.setTexture("textureSampler", texture);
  44. };
  45. let internalTexture = rtt.getInternalTexture();
  46. if (internalTexture) {
  47. scene.postProcessManager.directRender([passPostProcess], internalTexture);
  48. // Read the contents of the framebuffer
  49. var numberOfChannelsByLine = width * 4;
  50. var halfHeight = height / 2;
  51. //Reading datas from WebGL
  52. var data = engine.readPixels(0, 0, width, height);
  53. if (!channels.R || !channels.G || !channels.B || !channels.A) {
  54. for (var i = 0; i < width * height * 4; i += 4) {
  55. // If alpha is the only channel, just display alpha across all channels
  56. if (channels.A && !channels.R && !channels.G && !channels.B) {
  57. data[i] = data[i+3];
  58. data[i+1] = data[i+3];
  59. data[i+2] = data[i+3];
  60. data[i+3] = 255;
  61. continue;
  62. }
  63. let r = data[i], g = data[i+1], b = data[i+2], a = data[i+3];
  64. // If alpha is not visible, make everything 100% alpha
  65. if (!channels.A) {
  66. a = 255;
  67. }
  68. // If only one color channel is selected, map both colors to it. If two are selected, the unused one gets set to 0
  69. if (!channels.R) {
  70. if (channels.G && !channels.B) {
  71. r = g;
  72. } else if (channels.B && !channels.G) {
  73. r = b;
  74. } else {
  75. r = 0;
  76. }
  77. }
  78. if (!channels.G) {
  79. if (channels.R && !channels.B) {
  80. g = r;
  81. } else if (channels.B && !channels.R) {
  82. g = b;
  83. } else {
  84. g = 0;
  85. }
  86. }
  87. if (!channels.B) {
  88. if (channels.R && !channels.G) {
  89. b = r;
  90. } else if (channels.G && !channels.R) {
  91. b = g;
  92. } else {
  93. b = 0;
  94. }
  95. }
  96. data[i] = r;
  97. data[i + 1] = g;
  98. data[i + 2] = b;
  99. data[i + 3] = a;
  100. }
  101. }
  102. //To flip image on Y axis.
  103. if ((texture as Texture).invertY || texture.isCube) {
  104. for (var i = 0; i < halfHeight; i++) {
  105. for (var j = 0; j < numberOfChannelsByLine; j++) {
  106. var currentCell = j + i * numberOfChannelsByLine;
  107. var targetLine = height - i - 1;
  108. var targetCell = j + targetLine * numberOfChannelsByLine;
  109. var temp = data[currentCell];
  110. data[currentCell] = data[targetCell];
  111. data[targetCell] = temp;
  112. }
  113. }
  114. }
  115. resolve(data);
  116. // Unbind
  117. engine.unBindFramebuffer(internalTexture);
  118. } else {
  119. reject();
  120. }
  121. rtt.dispose();
  122. passPostProcess.dispose();
  123. if (globalState) {
  124. globalState.blockMutationUpdates = false;
  125. }
  126. }
  127. public static GetTextureDataAsync(texture: BaseTexture, width: number, height: number, face: number, channels: TextureChannelsToDisplay, globalState?: GlobalState): Promise<Uint8Array> {
  128. return new Promise((resolve, reject) => {
  129. if (!texture.isReady() && texture._texture) {
  130. texture._texture.onLoadedObservable.addOnce(() => {
  131. this._ProcessAsync(texture, width, height, face, channels, globalState || null, resolve, reject);
  132. });
  133. return;
  134. }
  135. this._ProcessAsync(texture, width, height, face, channels, globalState || null, resolve, reject);
  136. });
  137. }
  138. }