rgbdTextureTools.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { PostProcess } from "../PostProcesses/postProcess.js";
  2. import "../Shaders/rgbdDecode.fragment.js";
  3. import "../Engines/Extensions/engine.renderTarget.js";
  4. import { ApplyPostProcess } from "./textureTools.js";
  5. /**
  6. * Class used to host RGBD texture specific utilities
  7. */
  8. export class RGBDTextureTools {
  9. /**
  10. * Expand the RGBD Texture from RGBD to Half Float if possible.
  11. * @param texture the texture to expand.
  12. */
  13. static ExpandRGBDTexture(texture) {
  14. const internalTexture = texture._texture;
  15. if (!internalTexture || !texture.isRGBD) {
  16. return;
  17. }
  18. // Gets everything ready.
  19. const engine = internalTexture.getEngine();
  20. const caps = engine.getCaps();
  21. const isReady = internalTexture.isReady;
  22. let expandTexture = false;
  23. // If half float available we can uncompress the texture
  24. if (caps.textureHalfFloatRender && caps.textureHalfFloatLinearFiltering) {
  25. expandTexture = true;
  26. internalTexture.type = 2;
  27. }
  28. // If full float available we can uncompress the texture
  29. else if (caps.textureFloatRender && caps.textureFloatLinearFiltering) {
  30. expandTexture = true;
  31. internalTexture.type = 1;
  32. }
  33. if (expandTexture) {
  34. // Do not use during decode.
  35. internalTexture.isReady = false;
  36. internalTexture._isRGBD = false;
  37. internalTexture.invertY = false;
  38. }
  39. const expandRGBDTexture = () => {
  40. // Expand the texture if possible
  41. // Simply run through the decode PP.
  42. const rgbdPostProcess = new PostProcess("rgbdDecode", "rgbdDecode", null, null, 1, null, 3, engine, false, undefined, internalTexture.type, undefined, null, false);
  43. rgbdPostProcess.externalTextureSamplerBinding = true;
  44. // Hold the output of the decoding.
  45. const expandedTexture = engine.createRenderTargetTexture(internalTexture.width, {
  46. generateDepthBuffer: false,
  47. generateMipMaps: false,
  48. generateStencilBuffer: false,
  49. samplingMode: internalTexture.samplingMode,
  50. type: internalTexture.type,
  51. format: 5,
  52. });
  53. rgbdPostProcess.getEffect().executeWhenCompiled(() => {
  54. // PP Render Pass
  55. rgbdPostProcess.onApply = (effect) => {
  56. effect._bindTexture("textureSampler", internalTexture);
  57. effect.setFloat2("scale", 1, 1);
  58. };
  59. texture.getScene().postProcessManager.directRender([rgbdPostProcess], expandedTexture, true);
  60. // Cleanup
  61. engine.restoreDefaultFramebuffer();
  62. engine._releaseTexture(internalTexture);
  63. if (rgbdPostProcess) {
  64. rgbdPostProcess.dispose();
  65. }
  66. // Internal Swap
  67. expandedTexture._swapAndDie(internalTexture);
  68. // Ready to get rolling again.
  69. internalTexture.isReady = true;
  70. });
  71. };
  72. if (expandTexture) {
  73. if (isReady) {
  74. expandRGBDTexture();
  75. }
  76. else {
  77. texture.onLoadObservable.addOnce(expandRGBDTexture);
  78. }
  79. }
  80. }
  81. /**
  82. * Encode the texture to RGBD if possible.
  83. * @param internalTexture the texture to encode
  84. * @param scene the scene hosting the texture
  85. * @param outputTextureType type of the texture in which the encoding is performed
  86. * @returns a promise with the internalTexture having its texture replaced by the result of the processing
  87. */
  88. static EncodeTextureToRGBD(internalTexture, scene, outputTextureType = 0) {
  89. return ApplyPostProcess("rgbdEncode", internalTexture, scene, outputTextureType, 1, 5);
  90. }
  91. }
  92. //# sourceMappingURL=rgbdTextureTools.js.map