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

PickDepth.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import Cartesian4 from "../Core/Cartesian4.js";
  2. import defined from "../Core/defined.js";
  3. import destroyObject from "../Core/destroyObject.js";
  4. import FramebufferManager from "../Renderer/FramebufferManager.js";
  5. import RenderState from "../Renderer/RenderState.js";
  6. /**
  7. * @alias PickDepth
  8. * @constructor
  9. *
  10. * @private
  11. */
  12. function PickDepth() {
  13. this._framebuffer = new FramebufferManager();
  14. this._textureToCopy = undefined;
  15. this._copyDepthCommand = undefined;
  16. }
  17. Object.defineProperties(PickDepth.prototype, {
  18. framebuffer: {
  19. get: function () {
  20. return this._framebuffer.framebuffer;
  21. },
  22. },
  23. });
  24. function updateFramebuffers(pickDepth, context, depthTexture) {
  25. const { width, height } = depthTexture;
  26. pickDepth._framebuffer.update(context, width, height);
  27. }
  28. function updateCopyCommands(pickDepth, context, depthTexture) {
  29. if (!defined(pickDepth._copyDepthCommand)) {
  30. pickDepth._copyDepthCommand = context.createViewportQuadCommand(
  31. `uniform highp sampler2D colorTexture;
  32. in vec2 v_textureCoordinates;
  33. void main()
  34. {
  35. vec4 globeDepthPacked = texture(czm_globeDepthTexture, v_textureCoordinates);
  36. float globeDepth = czm_unpackDepth(globeDepthPacked);
  37. float depth = texture(colorTexture, v_textureCoordinates).r;
  38. out_FragColor = czm_branchFreeTernary(globeDepth <= 0.0 || globeDepth >= 1.0 || depth < globeDepth && depth > 0.0 && depth < 1.0,
  39. czm_packDepth(depth), globeDepthPacked);
  40. }
  41. `,
  42. {
  43. renderState: RenderState.fromCache(),
  44. uniformMap: {
  45. colorTexture: function () {
  46. return pickDepth._textureToCopy;
  47. },
  48. },
  49. owner: pickDepth,
  50. },
  51. );
  52. }
  53. pickDepth._textureToCopy = depthTexture;
  54. pickDepth._copyDepthCommand.framebuffer = pickDepth.framebuffer;
  55. }
  56. PickDepth.prototype.update = function (context, depthTexture) {
  57. updateFramebuffers(this, context, depthTexture);
  58. updateCopyCommands(this, context, depthTexture);
  59. };
  60. const scratchPackedDepth = new Cartesian4();
  61. const packedDepthScale = new Cartesian4(
  62. 1.0,
  63. 1.0 / 255.0,
  64. 1.0 / 65025.0,
  65. 1.0 / 16581375.0,
  66. );
  67. /**
  68. * Read the depth from the framebuffer at the given coordinate.
  69. *
  70. * @param {Context} context
  71. * @param {number} x The x-coordinate at which to read the depth.
  72. * @param {number} y The y-coordinate at which to read the depth.
  73. * @returns {number} The depth read from the framebuffer.
  74. *
  75. * @private
  76. */
  77. PickDepth.prototype.getDepth = function (context, x, y) {
  78. // If this function is called before the framebuffer is created, the depth is undefined.
  79. if (!defined(this.framebuffer)) {
  80. return undefined;
  81. }
  82. const pixels = context.readPixels({
  83. x: x,
  84. y: y,
  85. width: 1,
  86. height: 1,
  87. framebuffer: this.framebuffer,
  88. });
  89. const packedDepth = Cartesian4.unpack(pixels, 0, scratchPackedDepth);
  90. Cartesian4.divideByScalar(packedDepth, 255.0, packedDepth);
  91. return Cartesian4.dot(packedDepth, packedDepthScale);
  92. };
  93. PickDepth.prototype.executeCopyDepth = function (context, passState) {
  94. this._copyDepthCommand.execute(context, passState);
  95. };
  96. PickDepth.prototype.isDestroyed = function () {
  97. return false;
  98. };
  99. PickDepth.prototype.destroy = function () {
  100. this._framebuffer.destroy();
  101. if (defined(this._copyDepthCommand)) {
  102. this._copyDepthCommand.shaderProgram =
  103. defined(this._copyDepthCommand.shaderProgram) &&
  104. this._copyDepthCommand.shaderProgram.destroy();
  105. }
  106. return destroyObject(this);
  107. };
  108. export default PickDepth;