textureHelper.ts 6.8 KB

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