textureHelper.ts 6.9 KB

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