copyTextureToTexture.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { EffectRenderer, EffectWrapper } from "../Materials/effectRenderer.js";
  2. import "../Shaders/copyTextureToTexture.fragment.js";
  3. /**
  4. * Conversion modes available when copying a texture into another one
  5. */
  6. export var ConversionMode;
  7. (function (ConversionMode) {
  8. ConversionMode[ConversionMode["None"] = 0] = "None";
  9. ConversionMode[ConversionMode["ToLinearSpace"] = 1] = "ToLinearSpace";
  10. ConversionMode[ConversionMode["ToGammaSpace"] = 2] = "ToGammaSpace";
  11. })(ConversionMode || (ConversionMode = {}));
  12. /**
  13. * Class used for fast copy from one texture to another
  14. */
  15. export class CopyTextureToTexture {
  16. _textureIsInternal(texture) {
  17. return texture.getInternalTexture === undefined;
  18. }
  19. /**
  20. * Constructs a new instance of the class
  21. * @param engine The engine to use for the copy
  22. * @param isDepthTexture True means that we should write (using gl_FragDepth) into the depth texture attached to the destination (default: false)
  23. */
  24. constructor(engine, isDepthTexture = false) {
  25. this._engine = engine;
  26. this._isDepthTexture = isDepthTexture;
  27. this._renderer = new EffectRenderer(engine);
  28. this._effectWrapper = new EffectWrapper({
  29. engine: engine,
  30. name: "CopyTextureToTexture",
  31. fragmentShader: "copyTextureToTexture",
  32. useShaderStore: true,
  33. uniformNames: ["conversion"],
  34. samplerNames: ["textureSampler"],
  35. defines: isDepthTexture ? ["#define DEPTH_TEXTURE"] : [],
  36. });
  37. this._effectWrapper.onApplyObservable.add(() => {
  38. if (isDepthTexture) {
  39. engine.setState(false);
  40. engine.setDepthBuffer(true);
  41. engine.depthCullingState.depthMask = true;
  42. engine.depthCullingState.depthFunc = 519;
  43. }
  44. if (this._textureIsInternal(this._source)) {
  45. this._effectWrapper.effect._bindTexture("textureSampler", this._source);
  46. }
  47. else {
  48. this._effectWrapper.effect.setTexture("textureSampler", this._source);
  49. }
  50. this._effectWrapper.effect.setFloat("conversion", this._conversion);
  51. });
  52. }
  53. /**
  54. * Indicates if the effect is ready to be used for the copy
  55. * @returns true if "copy" can be called without delay, else false
  56. */
  57. isReady() {
  58. return this._effectWrapper.effect.isReady();
  59. }
  60. /**
  61. * Copy one texture into another
  62. * @param source The source texture
  63. * @param destination The destination texture
  64. * @param conversion The conversion mode that should be applied when copying
  65. * @returns
  66. */
  67. copy(source, destination, conversion = ConversionMode.None) {
  68. if (!this.isReady()) {
  69. return false;
  70. }
  71. this._source = source;
  72. this._conversion = conversion;
  73. const engineDepthFunc = this._engine.depthCullingState.depthFunc;
  74. this._renderer.render(this._effectWrapper, destination);
  75. if (this._isDepthTexture && engineDepthFunc) {
  76. this._engine.depthCullingState.depthFunc = engineDepthFunc;
  77. }
  78. return true;
  79. }
  80. /**
  81. * Releases all the resources used by the class
  82. */
  83. dispose() {
  84. this._effectWrapper.dispose();
  85. this._renderer.dispose();
  86. }
  87. }
  88. //# sourceMappingURL=copyTextureToTexture.js.map