智慧水务管理系统 - 精河县供水工程综合管理平台

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import defined from "../Core/defined.js";
  2. import destroyObject from "../Core/destroyObject.js";
  3. import PixelFormat from "../Core/PixelFormat.js";
  4. import Color from "../Core/Color.js";
  5. import PixelDatatype from "../Renderer/PixelDatatype.js";
  6. import FramebufferManager from "../Renderer/FramebufferManager.js";
  7. import ClearCommand from "../Renderer/ClearCommand.js";
  8. /**
  9. * Creates and manages framebuffers for edge visibility rendering.
  10. *
  11. * @param {Object} options Object with the following properties:
  12. *
  13. * @alias EdgeFramebuffer
  14. * @constructor
  15. *
  16. * @private
  17. */
  18. function EdgeFramebuffer(options) {
  19. options = options || {};
  20. // Create framebuffer manager with multiple render targets (MRT)
  21. // Color attachment 0: edge color output (visualization / debug)
  22. // Color attachment 1: R: edge type, G: featureId (metadata / ids)
  23. // Color attachment 2: packed depth (czm_packDepth) for edge fragments
  24. this._framebufferManager = new FramebufferManager({
  25. colorAttachmentsLength: 3, // MRT: Color + ID + Depth (packed RGBA)
  26. createColorAttachments: true,
  27. depthStencil: true,
  28. supportsDepthTexture: true,
  29. color: true,
  30. });
  31. this._framebuffer = undefined;
  32. this._colorTexture = undefined;
  33. this._idTexture = undefined;
  34. this._depthTexture = undefined; // packed depth color attachment (location = 2)
  35. this._depthStencilTexture = undefined;
  36. this._clearCommand = new ClearCommand({
  37. color: new Color(0.0, 0.0, 0.0, 0.0),
  38. depth: 1.0,
  39. stencil: 0,
  40. owner: this,
  41. });
  42. }
  43. Object.defineProperties(EdgeFramebuffer.prototype, {
  44. /**
  45. * Gets the framebuffer for edge rendering.
  46. * @memberof EdgeFramebuffer.prototype
  47. * @type {Framebuffer}
  48. * @readonly
  49. */
  50. framebuffer: {
  51. get: function () {
  52. return this._framebuffer;
  53. },
  54. },
  55. /**
  56. * Gets the color texture.
  57. * @memberof EdgeFramebuffer.prototype
  58. * @type {Texture}
  59. * @readonly
  60. */
  61. colorTexture: {
  62. get: function () {
  63. return this._colorTexture;
  64. },
  65. },
  66. /**
  67. * Gets the ID texture.
  68. * @memberof EdgeFramebuffer.prototype
  69. * @type {Texture}
  70. * @readonly
  71. */
  72. idTexture: {
  73. get: function () {
  74. return this._idTexture;
  75. },
  76. },
  77. /**
  78. * Gets the packed depth texture written during the edge pass.
  79. * @memberof EdgeFramebuffer.prototype
  80. * @type {Texture}
  81. * @readonly
  82. */
  83. depthTexture: {
  84. get: function () {
  85. return this._depthTexture;
  86. },
  87. },
  88. /**
  89. * Gets the depth-stencil texture.
  90. * @memberof EdgeFramebuffer.prototype
  91. * @type {Texture}
  92. * @readonly
  93. */
  94. depthStencilTexture: {
  95. get: function () {
  96. return this._depthStencilTexture;
  97. },
  98. },
  99. });
  100. /**
  101. * Updates the framebuffer.
  102. *
  103. * @param {Context} context The context.
  104. * @param {Viewport} viewport The viewport.
  105. * @param {boolean} hdr Whether HDR is enabled.
  106. * @param {Texture} [existingColorTexture] Optional existing color texture to reuse.
  107. * @param {Texture} [existingDepthTexture] Optional existing depth texture to reuse.
  108. *
  109. * @returns {boolean} True if the framebuffer was updated; otherwise, false.
  110. */
  111. EdgeFramebuffer.prototype.update = function (
  112. context,
  113. viewport,
  114. hdr,
  115. existingColorTexture,
  116. existingDepthTexture,
  117. ) {
  118. const width = viewport.width;
  119. const height = viewport.height;
  120. const pixelDatatype = hdr
  121. ? context.halfFloatingPointTexture
  122. ? PixelDatatype.HALF_FLOAT
  123. : PixelDatatype.FLOAT
  124. : PixelDatatype.UNSIGNED_BYTE;
  125. const changed = this._framebufferManager.update(
  126. context,
  127. width,
  128. height,
  129. 1, // No MSAA
  130. pixelDatatype,
  131. PixelFormat.RGBA,
  132. );
  133. // Always assign framebuffer if FramebufferManager has one
  134. if (this._framebufferManager.framebuffer) {
  135. this._framebuffer = this._framebufferManager.framebuffer;
  136. // Get the textures from the framebuffer manager or use existing ones
  137. this._colorTexture = defined(existingColorTexture)
  138. ? existingColorTexture
  139. : this._framebufferManager.getColorTexture(0); // Color attachment 0
  140. this._idTexture = this._framebufferManager.getColorTexture(1); // Color attachment 1: ID texture
  141. this._depthTexture = this._framebufferManager.getColorTexture(2); // Color attachment 2: packed depth
  142. this._depthStencilTexture = defined(existingDepthTexture)
  143. ? existingDepthTexture
  144. : this._framebufferManager.getDepthStencilTexture();
  145. }
  146. return changed;
  147. };
  148. /**
  149. * Clears the framebuffer using ClearCommand.
  150. * @deprecated Use getClearCommand() instead for proper MRT clearing.
  151. *
  152. * @param {Context} context The context.
  153. * @param {PassState} passState The pass state.
  154. * @param {Color} clearColor The clear color.
  155. */
  156. EdgeFramebuffer.prototype.clear = function (context, passState, clearColor) {
  157. const clearCommand = this.getClearCommand(clearColor);
  158. clearCommand.execute(context, passState);
  159. };
  160. /**
  161. * Gets the clear command for this framebuffer.
  162. *
  163. * @param {Color} [clearColor] The clear color to use. If undefined, uses the default.
  164. * @returns {ClearCommand} The clear command.
  165. */
  166. EdgeFramebuffer.prototype.getClearCommand = function (clearColor) {
  167. this._clearCommand.framebuffer = this._framebuffer;
  168. if (defined(clearColor)) {
  169. Color.clone(clearColor, this._clearCommand.color);
  170. }
  171. return this._clearCommand;
  172. };
  173. /**
  174. * Gets the edge framebuffer, creating it if necessary.
  175. *
  176. * @param {Context} context The context.
  177. * @param {Viewport} viewport The viewport.
  178. * @param {Texture} [existingColorTexture] Optional existing color texture to reuse.
  179. * @param {Texture} [existingDepthTexture] Optional existing depth texture to reuse.
  180. *
  181. * @returns {Framebuffer} The edge framebuffer.
  182. */
  183. EdgeFramebuffer.prototype.getFramebuffer = function (
  184. context,
  185. viewport,
  186. existingColorTexture,
  187. existingDepthTexture,
  188. ) {
  189. this.update(
  190. context,
  191. viewport,
  192. false,
  193. existingColorTexture,
  194. existingDepthTexture,
  195. );
  196. return this._framebuffer;
  197. };
  198. /**
  199. * Returns true if this object was destroyed; otherwise, false.
  200. *
  201. * @returns {boolean} True if this object was destroyed; otherwise, false.
  202. */
  203. EdgeFramebuffer.prototype.isDestroyed = function () {
  204. return false;
  205. };
  206. /**
  207. * Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
  208. * release of WebGL resources, instead of relying on the garbage collector to destroy this object.
  209. * <br /><br />
  210. * Once an object is destroyed, it should not be used; calling any function other than
  211. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  212. * assign the return value (<code>undefined</code>) to the object as done in the example.
  213. *
  214. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  215. */
  216. EdgeFramebuffer.prototype.destroy = function () {
  217. this._framebufferManager =
  218. this._framebufferManager && this._framebufferManager.destroy();
  219. this._clearCommand = undefined;
  220. return destroyObject(this);
  221. };
  222. export default EdgeFramebuffer;