textureHelper.ts 6.9 KB

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