Home Reference Source

cables_dev/cables/src/libs/cgl/copytexture/cgl_copytexture.js

  1. import { MESHES } from "../../../core/cgl/cgl_simplerect.js";
  2.  
  3. class CopyTexture
  4. {
  5. constructor(cgl, name, options)
  6. {
  7. this.cgl = cgl;
  8.  
  9. this._options = options;
  10. this.fb = null;
  11.  
  12. let shader = options.shader;
  13.  
  14. this._useDefaultShader = true;
  15. if (options.shader) this._useDefaultShader = false;
  16.  
  17. options.numRenderBuffers = options.numRenderBuffers || 1;
  18.  
  19. if (!shader)
  20. {
  21. shader = ""
  22. .endl() + "IN vec2 texCoord;";
  23.  
  24. for (let i = 0; i < options.numRenderBuffers; i++)
  25. {
  26. shader = shader.endl() + "UNI sampler2D tex" + i + ";".endl();
  27. }
  28.  
  29. shader = shader
  30. .endl() + "void main()"
  31. .endl() + "{";
  32.  
  33. if (options.numRenderBuffers == 1)
  34. {
  35. shader = shader.endl() + " outColor= texture(tex0,texCoord);".endl();
  36. }
  37.  
  38. else
  39. for (let i = 0; i < options.numRenderBuffers; i++)
  40. {
  41. shader = shader.endl() + "outColor" + i + " = texture(tex" + i + ",texCoord);".endl();
  42. }
  43.  
  44. shader = shader.endl() + "}";
  45. }
  46.  
  47. const verts = options.vertexShader || ""
  48. .endl() + "IN vec3 vPosition;"
  49. .endl() + "IN vec2 attrTexCoord;"
  50.  
  51. .endl() + "OUT vec2 texCoord;"
  52.  
  53. .endl() + "void main()"
  54. .endl() + "{"
  55. .endl() + " texCoord=attrTexCoord;"
  56. .endl() + " gl_Position = vec4(vPosition, 1.0);"
  57. .endl() + "}";
  58.  
  59.  
  60. this.bgShader = new CGL.Shader(cgl, "corelib copytexture " + name);
  61. this.bgShader.setSource(verts, shader);
  62.  
  63. if (!options.vertexShader)
  64. this.bgShader.ignoreMissingUniforms = true;
  65.  
  66. new CGL.Uniform(this.bgShader, "t", "tex", 0);
  67. new CGL.Uniform(this.bgShader, "t", "tex1", 1);
  68. new CGL.Uniform(this.bgShader, "t", "tex2", 2);
  69. new CGL.Uniform(this.bgShader, "t", "tex3", 3);
  70.  
  71. this.mesh = MESHES.getSimpleRect(this.cgl, "texEffectRect");
  72. }
  73.  
  74. setSize(w, h)
  75. {
  76. this._options.width = w;
  77. this._options.height = h;
  78. }
  79.  
  80. copy(tex, tex1, tex2, tex3, tex4)
  81. {
  82. const cgl = this.cgl;
  83. if (!tex) tex = CGL.Texture.getEmptyTexture(this.cgl);
  84. let
  85. w = this._options.width || tex.width,
  86. h = this._options.height || tex.height;
  87.  
  88. if (this.fb)
  89. {
  90. if (w <= 0)w = 8;
  91. if (h <= 0)h = 8;
  92. if (this.fb.getWidth() != w || this.fb.getHeight() != h) this.fb.setSize(w, h);
  93. }
  94. else
  95. {
  96. let filter = CGL.Texture.FILTER_LINEAR;
  97. let wrap = CGL.Texture.WRAP_CLAMP_TO_EDGE;
  98.  
  99. if (this._options.isFloatingPointTexture)filter = CGL.Texture.FILTER_NEAREST;
  100.  
  101. if (this._options.hasOwnProperty("filter"))filter = this._options.filter;
  102. if (this._options.hasOwnProperty("wrap"))wrap = this._options.wrap;
  103.  
  104. const options =
  105. {
  106. "isFloatingPointTexture": this._options.isFloatingPointTexture,
  107. "pixelFormat": this._options.pixelFormat,
  108. "numRenderBuffers": this._options.numRenderBuffers || 1,
  109. "filter": filter,
  110. "wrap": wrap,
  111. };
  112.  
  113. if (cgl.glVersion == 1) this.fb = new CGL.Framebuffer(cgl, w, h, options);
  114. else this.fb = new CGL.Framebuffer2(cgl, w, h, options);
  115. }
  116.  
  117. cgl.tempData.renderOffscreen = true;
  118. this.fb.renderStart(cgl);
  119.  
  120. cgl.setTexture(0, tex.tex);
  121. if (tex1) cgl.setTexture(1, tex1.tex);
  122. if (tex2) cgl.setTexture(2, tex2.tex);
  123. if (tex3) cgl.setTexture(3, tex3.tex);
  124. if (tex4) cgl.setTexture(4, tex4.tex);
  125.  
  126. cgl.pushShader(this.bgShader);
  127. this.mesh.render(this.bgShader);
  128. cgl.popShader();
  129.  
  130. this.fb.renderEnd();
  131. cgl.tempData.renderOffscreen = false;
  132.  
  133. return this.fb.getTextureColor();
  134. }
  135.  
  136. dispose()
  137. {
  138. if (this.fb) this.fb.dispose();
  139. if (this.bgShader) this.bgShader.dispose();
  140. if (this.mesh) this.mesh.dispose();
  141. }
  142. }
  143.  
  144. export { CopyTexture };