getClippingFunction.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. const functionString =
  39. `${
  40. "float clip(vec4 fragCoord, sampler2D clippingPlanes, mat4 clippingPlanesMatrix)\n" +
  41. "{\n" +
  42. " vec4 position = czm_windowToEyeCoordinates(fragCoord);\n" +
  43. " vec3 clipNormal = vec3(0.0);\n" +
  44. " vec3 clipPosition = vec3(0.0);\n" +
  45. " float clipAmount;\n" + // For union planes, we want to get the min distance. So we set the initial value to the first plane distance in the loop below.
  46. " float pixelWidth = czm_metersPerPixel(position);\n" +
  47. " bool breakAndDiscard = false;\n" +
  48. " for (int i = 0; i < "
  49. }${clippingPlanesLength}; ++i)\n` +
  50. ` {\n` +
  51. ` vec4 clippingPlane = getClippingPlane(clippingPlanes, i, clippingPlanesMatrix);\n` +
  52. ` clipNormal = clippingPlane.xyz;\n` +
  53. ` clipPosition = -clippingPlane.w * clipNormal;\n` +
  54. ` float amount = dot(clipNormal, (position.xyz - clipPosition)) / pixelWidth;\n` +
  55. ` clipAmount = czm_branchFreeTernary(i == 0, amount, min(amount, clipAmount));\n` +
  56. ` if (amount <= 0.0)\n` +
  57. ` {\n` +
  58. ` breakAndDiscard = true;\n` +
  59. ` break;\n` + // HLSL compiler bug if we discard here: https://bugs.chromium.org/p/angleproject/issues/detail?id=1945#c6
  60. ` }\n` +
  61. ` }\n` +
  62. ` if (breakAndDiscard) {\n` +
  63. ` discard;\n` +
  64. ` }\n` +
  65. ` return clipAmount;\n` +
  66. `}\n`;
  67. return functionString;
  68. }
  69. function clippingFunctionIntersect(clippingPlanesLength) {
  70. const functionString =
  71. `${
  72. "float clip(vec4 fragCoord, sampler2D clippingPlanes, mat4 clippingPlanesMatrix)\n" +
  73. "{\n" +
  74. " bool clipped = true;\n" +
  75. " vec4 position = czm_windowToEyeCoordinates(fragCoord);\n" +
  76. " vec3 clipNormal = vec3(0.0);\n" +
  77. " vec3 clipPosition = vec3(0.0);\n" +
  78. " float clipAmount = 0.0;\n" +
  79. " float pixelWidth = czm_metersPerPixel(position);\n" +
  80. " for (int i = 0; i < "
  81. }${clippingPlanesLength}; ++i)\n` +
  82. ` {\n` +
  83. ` vec4 clippingPlane = getClippingPlane(clippingPlanes, i, clippingPlanesMatrix);\n` +
  84. ` clipNormal = clippingPlane.xyz;\n` +
  85. ` clipPosition = -clippingPlane.w * clipNormal;\n` +
  86. ` float amount = dot(clipNormal, (position.xyz - clipPosition)) / pixelWidth;\n` +
  87. ` clipAmount = max(amount, clipAmount);\n` +
  88. ` clipped = clipped && (amount <= 0.0);\n` +
  89. ` }\n` +
  90. ` if (clipped)\n` +
  91. ` {\n` +
  92. ` discard;\n` +
  93. ` }\n` +
  94. ` return clipAmount;\n` +
  95. `}\n`;
  96. return functionString;
  97. }
  98. function getClippingPlaneFloat(width, height) {
  99. const pixelWidth = 1.0 / width;
  100. const pixelHeight = 1.0 / height;
  101. let pixelWidthString = `${pixelWidth}`;
  102. if (pixelWidthString.indexOf(".") === -1) {
  103. pixelWidthString += ".0";
  104. }
  105. let pixelHeightString = `${pixelHeight}`;
  106. if (pixelHeightString.indexOf(".") === -1) {
  107. pixelHeightString += ".0";
  108. }
  109. const functionString =
  110. `${
  111. "vec4 getClippingPlane(highp sampler2D packedClippingPlanes, int clippingPlaneNumber, mat4 transform)\n" +
  112. "{\n" +
  113. " int pixY = clippingPlaneNumber / "
  114. }${width};\n` +
  115. ` int pixX = clippingPlaneNumber - (pixY * ${width});\n` +
  116. ` float u = (float(pixX) + 0.5) * ${pixelWidthString};\n` + // sample from center of pixel
  117. ` float v = (float(pixY) + 0.5) * ${pixelHeightString};\n` +
  118. ` vec4 plane = texture(packedClippingPlanes, vec2(u, v));\n` +
  119. ` return czm_transformPlane(plane, transform);\n` +
  120. `}\n`;
  121. return functionString;
  122. }
  123. function getClippingPlaneUint8(width, height) {
  124. const pixelWidth = 1.0 / width;
  125. const pixelHeight = 1.0 / height;
  126. let pixelWidthString = `${pixelWidth}`;
  127. if (pixelWidthString.indexOf(".") === -1) {
  128. pixelWidthString += ".0";
  129. }
  130. let pixelHeightString = `${pixelHeight}`;
  131. if (pixelHeightString.indexOf(".") === -1) {
  132. pixelHeightString += ".0";
  133. }
  134. const functionString =
  135. `${
  136. "vec4 getClippingPlane(highp sampler2D packedClippingPlanes, int clippingPlaneNumber, mat4 transform)\n" +
  137. "{\n" +
  138. " int clippingPlaneStartIndex = clippingPlaneNumber * 2;\n" + // clipping planes are two pixels each
  139. " int pixY = clippingPlaneStartIndex / "
  140. }${width};\n` +
  141. ` int pixX = clippingPlaneStartIndex - (pixY * ${width});\n` +
  142. ` float u = (float(pixX) + 0.5) * ${pixelWidthString};\n` + // sample from center of pixel
  143. ` float v = (float(pixY) + 0.5) * ${pixelHeightString};\n` +
  144. ` vec4 oct32 = texture(packedClippingPlanes, vec2(u, v)) * 255.0;\n` +
  145. ` vec2 oct = vec2(oct32.x * 256.0 + oct32.y, oct32.z * 256.0 + oct32.w);\n` +
  146. ` vec4 plane;\n` +
  147. ` plane.xyz = czm_octDecode(oct, 65535.0);\n` +
  148. ` plane.w = czm_unpackFloat(texture(packedClippingPlanes, vec2(u + ${pixelWidthString}, v)));\n` +
  149. ` return czm_transformPlane(plane, transform);\n` +
  150. `}\n`;
  151. return functionString;
  152. }
  153. export default getClippingFunction;