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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import Cartesian2 from "../Core/Cartesian2.js";
  2. import Check from "../Core/Check.js";
  3. import ClippingPlaneCollection from "./ClippingPlaneCollection.js";
  4. const textureResolutionScratch = new Cartesian2();
  5. /**
  6. * Gets the GLSL functions needed to retrieve clipping planes from a ClippingPlaneCollection's texture.
  7. *
  8. * @param {ClippingPlaneCollection} clippingPlaneCollection ClippingPlaneCollection with a defined texture.
  9. * @param {Context} context The current rendering context.
  10. * @returns {string} A string containing GLSL functions for retrieving clipping planes.
  11. * @private
  12. */
  13. function getClippingFunction(clippingPlaneCollection, context) {
  14. //>>includeStart('debug', pragmas.debug);
  15. Check.typeOf.object("clippingPlaneCollection", clippingPlaneCollection);
  16. Check.typeOf.object("context", context);
  17. //>>includeEnd('debug');
  18. const unionClippingRegions = clippingPlaneCollection.unionClippingRegions;
  19. const clippingPlanesLength = clippingPlaneCollection.length;
  20. const usingFloatTexture = ClippingPlaneCollection.useFloatTexture(context);
  21. const textureResolution = ClippingPlaneCollection.getTextureResolution(
  22. clippingPlaneCollection,
  23. context,
  24. textureResolutionScratch,
  25. );
  26. const width = textureResolution.x;
  27. const height = textureResolution.y;
  28. let functions = usingFloatTexture
  29. ? getClippingPlaneFloat(width, height)
  30. : getClippingPlaneUint8(width, height);
  31. functions += "\n";
  32. functions += unionClippingRegions
  33. ? clippingFunctionUnion(clippingPlanesLength)
  34. : clippingFunctionIntersect(clippingPlanesLength);
  35. return functions;
  36. }
  37. function clippingFunctionUnion(clippingPlanesLength) {
  38. return `float clip(vec4 fragCoord, sampler2D clippingPlanes, mat4 clippingPlanesMatrix)
  39. {
  40. vec4 position = czm_windowToEyeCoordinates(fragCoord);
  41. vec3 clipNormal = vec3(0.0);
  42. vec3 clipPosition = vec3(0.0);
  43. float clipAmount;
  44. float pixelWidth = czm_metersPerPixel(position);
  45. bool breakAndDiscard = false;
  46. for (int i = 0; i < ${clippingPlanesLength}; ++i)
  47. {
  48. vec4 clippingPlane = getClippingPlane(clippingPlanes, i, clippingPlanesMatrix);
  49. clipNormal = clippingPlane.xyz;
  50. clipPosition = -clippingPlane.w * clipNormal;
  51. float amount = dot(clipNormal, (position.xyz - clipPosition)) / pixelWidth;
  52. clipAmount = czm_branchFreeTernary(i == 0, amount, min(amount, clipAmount));
  53. if (amount <= 0.0)
  54. {
  55. breakAndDiscard = true;
  56. // HLSL compiler bug if we discard here: https://bugs.chromium.org/p/angleproject/issues/detail?id=1945#c6
  57. break;
  58. }
  59. }
  60. if (breakAndDiscard) {
  61. discard;
  62. }
  63. return clipAmount;
  64. }
  65. `;
  66. }
  67. function clippingFunctionIntersect(clippingPlanesLength) {
  68. return `float clip(vec4 fragCoord, sampler2D clippingPlanes, mat4 clippingPlanesMatrix)
  69. {
  70. bool clipped = true;
  71. vec4 position = czm_windowToEyeCoordinates(fragCoord);
  72. vec3 clipNormal = vec3(0.0);
  73. vec3 clipPosition = vec3(0.0);
  74. float clipAmount = 0.0;
  75. float pixelWidth = czm_metersPerPixel(position);
  76. for (int i = 0; i < ${clippingPlanesLength}; ++i)
  77. {
  78. vec4 clippingPlane = getClippingPlane(clippingPlanes, i, clippingPlanesMatrix);
  79. clipNormal = clippingPlane.xyz;
  80. clipPosition = -clippingPlane.w * clipNormal;
  81. float amount = dot(clipNormal, (position.xyz - clipPosition)) / pixelWidth;
  82. clipAmount = max(amount, clipAmount);
  83. clipped = clipped && (amount <= 0.0);
  84. }
  85. if (clipped)
  86. {
  87. discard;
  88. }
  89. return clipAmount;
  90. }
  91. `;
  92. }
  93. function getClippingPlaneFloat(width, height) {
  94. const pixelWidth = 1.0 / width;
  95. const pixelHeight = 1.0 / height;
  96. let pixelWidthString = `${pixelWidth}`;
  97. if (pixelWidthString.indexOf(".") === -1) {
  98. pixelWidthString += ".0";
  99. }
  100. let pixelHeightString = `${pixelHeight}`;
  101. if (pixelHeightString.indexOf(".") === -1) {
  102. pixelHeightString += ".0";
  103. }
  104. return `vec4 getClippingPlane(highp sampler2D packedClippingPlanes, int clippingPlaneNumber, mat4 transform)
  105. {
  106. int pixY = clippingPlaneNumber / ${width};
  107. int pixX = clippingPlaneNumber - (pixY * ${width});
  108. // Sample from center of pixel
  109. float u = (float(pixX) + 0.5) * ${pixelWidthString};
  110. float v = (float(pixY) + 0.5) * ${pixelHeightString};
  111. vec4 plane = texture(packedClippingPlanes, vec2(u, v));
  112. return czm_transformPlane(plane, transform);
  113. }
  114. `;
  115. }
  116. function getClippingPlaneUint8(width, height) {
  117. const pixelWidth = 1.0 / width;
  118. const pixelHeight = 1.0 / height;
  119. let pixelWidthString = `${pixelWidth}`;
  120. if (pixelWidthString.indexOf(".") === -1) {
  121. pixelWidthString += ".0";
  122. }
  123. let pixelHeightString = `${pixelHeight}`;
  124. if (pixelHeightString.indexOf(".") === -1) {
  125. pixelHeightString += ".0";
  126. }
  127. return `vec4 getClippingPlane(highp sampler2D packedClippingPlanes, int clippingPlaneNumber, mat4 transform)
  128. {
  129. int clippingPlaneStartIndex = clippingPlaneNumber * 2;
  130. int pixY = clippingPlaneStartIndex / ${width};
  131. int pixX = clippingPlaneStartIndex - (pixY * ${width});
  132. // Sample from center of pixel
  133. float u = (float(pixX) + 0.5) * ${pixelWidthString};
  134. float v = (float(pixY) + 0.5) * ${pixelHeightString};
  135. vec4 oct32 = texture(packedClippingPlanes, vec2(u, v)) * 255.0;
  136. vec2 oct = vec2(oct32.x * 256.0 + oct32.y, oct32.z * 256.0 + oct32.w);
  137. vec4 plane;
  138. plane.xyz = czm_octDecode(oct, 65535.0);
  139. plane.w = czm_unpackFloat(texture(packedClippingPlanes, vec2(u + ${pixelWidthString}, v)));
  140. return czm_transformPlane(plane, transform);
  141. }
  142. `;
  143. }
  144. export default getClippingFunction;