Home Reference Source

cables_dev/cables/src/libs/cgl/texturemath/cgl_texturemath.js

  1. class ShaderTextureMath
  2. {
  3. constructor(cgl, name, options)
  4. {
  5. this._cgl = cgl;
  6. this._fb = null;
  7. this._mesh = CGL.MESHES.getSimpleRect(cgl, name + " shaderTexMath rect");
  8. this._prevViewPort = [0, 0, 0, 0];
  9. this._w = 0;
  10. this._h = 0;
  11.  
  12. if (options.hasOwnProperty("texturePort")) this._texPort = options.texturePort;
  13. if (options.hasOwnProperty("width")) this._w = options.width;
  14. if (options.hasOwnProperty("height")) this._h = options.height;
  15. }
  16.  
  17. dispose()
  18. {
  19. if (this._fb) this._fb.delete();
  20. this._fb = null;
  21. }
  22.  
  23. _initFb(w, h)
  24. {
  25. if (this._fb) this._fb.delete();
  26. this._fb = null;
  27.  
  28.  
  29. if (this._cgl.glVersion >= 2)
  30. {
  31. this._fb = new CGL.Framebuffer2(this._cgl, w, h,
  32. {
  33. "isFloatingPointTexture": true,
  34. "multisampling": false,
  35. "wrap": CGL.Texture.WRAP_REPEAT,
  36. "filter": CGL.Texture.FILTER_NEAREST,
  37. "depth": true,
  38. "multisamplingSamples": 0,
  39. "clear": true
  40. });
  41. }
  42. else
  43. {
  44. this._fb = new CGL.Framebuffer(this._cgl, w, h,
  45. {
  46. "isFloatingPointTexture": true,
  47. "filter": CGL.Texture.FILTER_NEAREST,
  48. "wrap": CGL.Texture.WRAP_REPEAT
  49. });
  50. }
  51. }
  52.  
  53. setSize(w, h)
  54. {
  55. this._w = w;
  56. this._h = h;
  57. }
  58.  
  59. render(shader)
  60. {
  61. if (this._texPort && !this._texPort.get()) return;
  62.  
  63. const vp = this._cgl.getViewPort();
  64.  
  65. let w = this._w;
  66. let h = this._h;
  67. if (this._texPort && this._texPort.get())
  68. {
  69. w = this._texPort.get().width;
  70. h = this._texPort.get().height;
  71. }
  72. if (!this._fb || this._fb.getWidth() != w || this._fb.getHeight() != h) this._initFb(w, h);
  73.  
  74. if (!shader)
  75. {
  76. return null;
  77. // outTex.set(null);
  78. }
  79. else
  80. {
  81. this._prevViewPort[0] = vp[0];
  82. this._prevViewPort[1] = vp[1];
  83. this._prevViewPort[2] = vp[2];
  84. this._prevViewPort[3] = vp[3];
  85.  
  86. this._fb.renderStart(this._cgl);
  87.  
  88. this._cgl.pushPMatrix();
  89. mat4.identity(this._cgl.pMatrix);
  90.  
  91. this._cgl.pushViewMatrix();
  92. mat4.identity(this._cgl.vMatrix);
  93.  
  94. this._cgl.pushModelMatrix();
  95. mat4.identity(this._cgl.mMatrix);
  96.  
  97. this._cgl.pushShader(shader);
  98. if (shader.bindTextures) shader.bindTextures();
  99.  
  100. if (this._texPort)
  101. this._cgl.setTexture(0, this._texPort.get().tex);
  102.  
  103. this._cgl.pushBlend(false);
  104.  
  105. this._mesh.render(shader);
  106.  
  107. this._cgl.popBlend();
  108.  
  109. this._cgl.popPMatrix();
  110. this._cgl.popModelMatrix();
  111. this._cgl.popViewMatrix();
  112. this._fb.renderEnd(this._cgl);
  113.  
  114. this._cgl.popShader();
  115.  
  116. this._cgl.gl.viewport(this._prevViewPort[0], this._prevViewPort[1], this._prevViewPort[2], this._prevViewPort[3]);
  117.  
  118. return this._fb.getTextureColor();
  119. }
  120. }
  121. }
  122.  
  123.  
  124. export { ShaderTextureMath };