textureHelper.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 _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_SAMPLINGMODE, 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_SAMPLINGMODE, 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. let internalTexture = rtt.getInternalTexture();
  53. if (internalTexture) {
  54. scene.postProcessManager.directRender([lodPostProcess], internalTexture);
  55. // Read the contents of the framebuffer
  56. var numberOfChannelsByLine = width * 4;
  57. var halfHeight = height / 2;
  58. //Reading datas from WebGL
  59. var data = engine.readPixels(0, 0, width, height);
  60. if (!channels.R || !channels.G || !channels.B || !channels.A) {
  61. for (var i = 0; i < width * height * 4; i += 4) {
  62. // If alpha is the only channel, just display alpha across all channels
  63. if (channels.A && !channels.R && !channels.G && !channels.B) {
  64. data[i] = data[i+3];
  65. data[i+1] = data[i+3];
  66. data[i+2] = data[i+3];
  67. data[i+3] = 255;
  68. continue;
  69. }
  70. let r = data[i], g = data[i+1], b = data[i+2], a = data[i+3];
  71. // If alpha is not visible, make everything 100% alpha
  72. if (!channels.A) {
  73. a = 255;
  74. }
  75. // If only one color channel is selected, map both colors to it. If two are selected, the unused one gets set to 0
  76. if (!channels.R) {
  77. if (channels.G && !channels.B) {
  78. r = g;
  79. } else if (channels.B && !channels.G) {
  80. r = b;
  81. } else {
  82. r = 0;
  83. }
  84. }
  85. if (!channels.G) {
  86. if (channels.R && !channels.B) {
  87. g = r;
  88. } else if (channels.B && !channels.R) {
  89. g = b;
  90. } else {
  91. g = 0;
  92. }
  93. }
  94. if (!channels.B) {
  95. if (channels.R && !channels.G) {
  96. b = r;
  97. } else if (channels.G && !channels.R) {
  98. b = g;
  99. } else {
  100. b = 0;
  101. }
  102. }
  103. data[i] = r;
  104. data[i + 1] = g;
  105. data[i + 2] = b;
  106. data[i + 3] = a;
  107. }
  108. }
  109. //To flip image on Y axis.
  110. if ((texture as Texture).invertY || texture.isCube) {
  111. for (var i = 0; i < halfHeight; i++) {
  112. for (var j = 0; j < numberOfChannelsByLine; j++) {
  113. var currentCell = j + i * numberOfChannelsByLine;
  114. var targetLine = height - i - 1;
  115. var targetCell = j + targetLine * numberOfChannelsByLine;
  116. var temp = data[currentCell];
  117. data[currentCell] = data[targetCell];
  118. data[targetCell] = temp;
  119. }
  120. }
  121. }
  122. resolve(data);
  123. // Unbind
  124. engine.unBindFramebuffer(internalTexture);
  125. } else {
  126. reject();
  127. }
  128. rtt.dispose();
  129. lodPostProcess.dispose();
  130. if (globalState) {
  131. globalState.blockMutationUpdates = false;
  132. }
  133. }
  134. public static GetTextureDataAsync(texture: BaseTexture, width: number, height: number, face: number, channels: TextureChannelsToDisplay, globalState?: GlobalState, lod: number = 0): Promise<Uint8Array> {
  135. return new Promise((resolve, reject) => {
  136. if (!texture.isReady() && texture._texture) {
  137. texture._texture.onLoadedObservable.addOnce(() => {
  138. this._ProcessAsync(texture, width, height, face, channels, lod, globalState || null, resolve, reject);
  139. });
  140. return;
  141. }
  142. this._ProcessAsync(texture, width, height, face, channels, lod, globalState || null, resolve, reject);
  143. });
  144. }
  145. }