textureTools.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { InternalTexture } from "../Materials/Textures/internalTexture";
  2. import { Texture } from "../Materials/Textures/texture";
  3. import { RenderTargetTexture } from "../Materials/Textures/renderTargetTexture";
  4. import { PassPostProcess } from "../PostProcesses/passPostProcess";
  5. import { Constants } from "../Engines/constants";
  6. import { Scene } from "../scene";
  7. import { PostProcess } from '../PostProcesses/postProcess';
  8. import { Engine } from '../Engines/engine';
  9. /**
  10. * Class used to host texture specific utilities
  11. */
  12. export class TextureTools {
  13. /**
  14. * Uses the GPU to create a copy texture rescaled at a given size
  15. * @param texture Texture to copy from
  16. * @param width defines the desired width
  17. * @param height defines the desired height
  18. * @param useBilinearMode defines if bilinear mode has to be used
  19. * @return the generated texture
  20. */
  21. public static CreateResizedCopy(texture: Texture, width: number, height: number, useBilinearMode: boolean = true): Texture {
  22. var scene = <Scene>texture.getScene();
  23. var engine = scene.getEngine();
  24. let rtt = new RenderTargetTexture(
  25. 'resized' + texture.name,
  26. { width: width, height: height },
  27. scene,
  28. !texture.noMipmap,
  29. true,
  30. (<InternalTexture>texture._texture).type,
  31. false,
  32. texture.samplingMode,
  33. false
  34. );
  35. rtt.wrapU = texture.wrapU;
  36. rtt.wrapV = texture.wrapV;
  37. rtt.uOffset = texture.uOffset;
  38. rtt.vOffset = texture.vOffset;
  39. rtt.uScale = texture.uScale;
  40. rtt.vScale = texture.vScale;
  41. rtt.uAng = texture.uAng;
  42. rtt.vAng = texture.vAng;
  43. rtt.wAng = texture.wAng;
  44. rtt.coordinatesIndex = texture.coordinatesIndex;
  45. rtt.level = texture.level;
  46. rtt.anisotropicFilteringLevel = texture.anisotropicFilteringLevel;
  47. (<InternalTexture>rtt._texture).isReady = false;
  48. texture.wrapU = Texture.CLAMP_ADDRESSMODE;
  49. texture.wrapV = Texture.CLAMP_ADDRESSMODE;
  50. let passPostProcess = new PassPostProcess("pass", 1, null, useBilinearMode ? Texture.BILINEAR_SAMPLINGMODE : Texture.NEAREST_SAMPLINGMODE, engine, false, Constants.TEXTURETYPE_UNSIGNED_INT);
  51. passPostProcess.getEffect().executeWhenCompiled(() => {
  52. passPostProcess.onApply = function(effect) {
  53. effect.setTexture("textureSampler", texture);
  54. };
  55. let internalTexture = rtt.getInternalTexture();
  56. if (internalTexture) {
  57. scene.postProcessManager.directRender([passPostProcess], internalTexture);
  58. engine.unBindFramebuffer(internalTexture);
  59. rtt.disposeFramebufferObjects();
  60. passPostProcess.dispose();
  61. internalTexture.isReady = true;
  62. }
  63. });
  64. return rtt;
  65. }
  66. /**
  67. * Apply a post process to a texture
  68. * @param postProcessName name of the fragment post process
  69. * @param internalTexture the texture to encode
  70. * @param scene the scene hosting the texture
  71. * @param type type of the output texture. If not provided, use the one from internalTexture
  72. * @param samplingMode sampling moode to use to sample the source texture. If not provided, use the one from internalTexture
  73. * @param format format of the output texture. If not provided, use the one from internalTexture
  74. * @return a promise with the internalTexture having its texture replaced by the result of the processing
  75. */
  76. public static ApplyPostProcess(postProcessName: string, internalTexture: InternalTexture, scene: Scene, type?: number, samplingMode?: number, format?: number): Promise<InternalTexture> {
  77. // Gets everything ready.
  78. const engine = internalTexture.getEngine() as Engine;
  79. internalTexture.isReady = false;
  80. samplingMode = samplingMode ?? internalTexture.samplingMode;
  81. type = type ?? internalTexture.type;
  82. format = format ?? internalTexture.format;
  83. if (type === -1) {
  84. type = Constants.TEXTURETYPE_UNSIGNED_BYTE;
  85. }
  86. return new Promise((resolve) => {
  87. // Create the post process
  88. const postProcess = new PostProcess("postprocess", postProcessName, null, null, 1, null, samplingMode, engine,
  89. false, undefined, type, undefined, null, false, format);
  90. // Hold the output of the decoding.
  91. const encodedTexture = engine.createRenderTargetTexture({ width: internalTexture.width, height: internalTexture.height }, {
  92. generateDepthBuffer: false,
  93. generateMipMaps: false,
  94. generateStencilBuffer: false,
  95. samplingMode,
  96. type,
  97. format
  98. });
  99. postProcess.getEffect().executeWhenCompiled(() => {
  100. // PP Render Pass
  101. postProcess.onApply = (effect) => {
  102. effect._bindTexture("textureSampler", internalTexture);
  103. effect.setFloat2("scale", 1, 1);
  104. };
  105. scene.postProcessManager.directRender([postProcess!], encodedTexture, true);
  106. // Cleanup
  107. engine.restoreDefaultFramebuffer();
  108. engine._releaseTexture(internalTexture);
  109. engine._releaseFramebufferObjects(encodedTexture);
  110. if (postProcess) {
  111. postProcess.dispose();
  112. }
  113. // Internal Swap
  114. encodedTexture._swapAndDie(internalTexture);
  115. // Ready to get rolling again.
  116. internalTexture.type = type!;
  117. internalTexture.format = Constants.TEXTUREFORMAT_RGBA;
  118. internalTexture.isReady = true;
  119. resolve(internalTexture);
  120. });
  121. });
  122. }
  123. }