textureTools.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. /**
  8. * Class used to host texture specific utilities
  9. */
  10. export class TextureTools {
  11. /**
  12. * Uses the GPU to create a copy texture rescaled at a given size
  13. * @param texture Texture to copy from
  14. * @param width defines the desired width
  15. * @param height defines the desired height
  16. * @param useBilinearMode defines if bilinear mode has to be used
  17. * @return the generated texture
  18. */
  19. public static CreateResizedCopy(texture: Texture, width: number, height: number, useBilinearMode: boolean = true): Texture {
  20. var scene = <Scene>texture.getScene();
  21. var engine = scene.getEngine();
  22. let rtt = new RenderTargetTexture(
  23. 'resized' + texture.name,
  24. { width: width, height: height },
  25. scene,
  26. !texture.noMipmap,
  27. true,
  28. (<InternalTexture>texture._texture).type,
  29. false,
  30. texture.samplingMode,
  31. false
  32. );
  33. rtt.wrapU = texture.wrapU;
  34. rtt.wrapV = texture.wrapV;
  35. rtt.uOffset = texture.uOffset;
  36. rtt.vOffset = texture.vOffset;
  37. rtt.uScale = texture.uScale;
  38. rtt.vScale = texture.vScale;
  39. rtt.uAng = texture.uAng;
  40. rtt.vAng = texture.vAng;
  41. rtt.wAng = texture.wAng;
  42. rtt.coordinatesIndex = texture.coordinatesIndex;
  43. rtt.level = texture.level;
  44. rtt.anisotropicFilteringLevel = texture.anisotropicFilteringLevel;
  45. (<InternalTexture>rtt._texture).isReady = false;
  46. texture.wrapU = Texture.CLAMP_ADDRESSMODE;
  47. texture.wrapV = Texture.CLAMP_ADDRESSMODE;
  48. let passPostProcess = new PassPostProcess("pass", 1, null, useBilinearMode ? Texture.BILINEAR_SAMPLINGMODE : Texture.NEAREST_SAMPLINGMODE, engine, false, Constants.TEXTURETYPE_UNSIGNED_INT);
  49. passPostProcess.getEffect().executeWhenCompiled(() => {
  50. passPostProcess.onApply = function(effect) {
  51. effect.setTexture("textureSampler", texture);
  52. };
  53. let internalTexture = rtt.getInternalTexture();
  54. if (internalTexture) {
  55. scene.postProcessManager.directRender([passPostProcess], internalTexture);
  56. engine.unBindFramebuffer(internalTexture);
  57. rtt.disposeFramebufferObjects();
  58. passPostProcess.dispose();
  59. internalTexture.isReady = true;
  60. }
  61. });
  62. return rtt;
  63. }
  64. }