DepthPlane.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import BoundingSphere from "../Core/BoundingSphere.js";
  2. import Cartesian3 from "../Core/Cartesian3.js";
  3. import ComponentDatatype from "../Core/ComponentDatatype.js";
  4. import defined from "../Core/defined.js";
  5. import FeatureDetection from "../Core/FeatureDetection.js";
  6. import Geometry from "../Core/Geometry.js";
  7. import GeometryAttribute from "../Core/GeometryAttribute.js";
  8. import OrthographicFrustum from "../Core/OrthographicFrustum.js";
  9. import PrimitiveType from "../Core/PrimitiveType.js";
  10. import BufferUsage from "../Renderer/BufferUsage.js";
  11. import DrawCommand from "../Renderer/DrawCommand.js";
  12. import Pass from "../Renderer/Pass.js";
  13. import RenderState from "../Renderer/RenderState.js";
  14. import ShaderProgram from "../Renderer/ShaderProgram.js";
  15. import ShaderSource from "../Renderer/ShaderSource.js";
  16. import VertexArray from "../Renderer/VertexArray.js";
  17. import DepthPlaneFS from "../Shaders/DepthPlaneFS.js";
  18. import DepthPlaneVS from "../Shaders/DepthPlaneVS.js";
  19. import SceneMode from "./SceneMode.js";
  20. /**
  21. * @private
  22. */
  23. function DepthPlane() {
  24. this._rs = undefined;
  25. this._sp = undefined;
  26. this._va = undefined;
  27. this._command = undefined;
  28. this._mode = undefined;
  29. this._useLogDepth = false;
  30. }
  31. var depthQuadScratch = FeatureDetection.supportsTypedArrays()
  32. ? new Float32Array(12)
  33. : [];
  34. var scratchCartesian1 = new Cartesian3();
  35. var scratchCartesian2 = new Cartesian3();
  36. var scratchCartesian3 = new Cartesian3();
  37. var scratchCartesian4 = new Cartesian3();
  38. var scratchCartesian5 = new Cartesian3();
  39. function computeDepthQuad(ellipsoid, frameState) {
  40. var radii = ellipsoid.radii;
  41. var camera = frameState.camera;
  42. var center, eastOffset, northOffset;
  43. if (camera.frustum instanceof OrthographicFrustum) {
  44. center = Cartesian3.ZERO;
  45. eastOffset = camera.rightWC;
  46. northOffset = camera.upWC;
  47. } else {
  48. var p = camera.positionWC;
  49. // Find the corresponding position in the scaled space of the ellipsoid.
  50. var q = Cartesian3.multiplyComponents(
  51. ellipsoid.oneOverRadii,
  52. p,
  53. scratchCartesian1
  54. );
  55. var qUnit = Cartesian3.normalize(q, scratchCartesian2);
  56. // Determine the east and north directions at q.
  57. var eUnit = Cartesian3.normalize(
  58. Cartesian3.cross(Cartesian3.UNIT_Z, q, scratchCartesian3),
  59. scratchCartesian3
  60. );
  61. var nUnit = Cartesian3.normalize(
  62. Cartesian3.cross(qUnit, eUnit, scratchCartesian4),
  63. scratchCartesian4
  64. );
  65. var qMagnitude = Cartesian3.magnitude(q);
  66. // Determine the radius of the 'limb' of the ellipsoid.
  67. var wMagnitude = Math.sqrt(qMagnitude * qMagnitude - 1.0);
  68. // Compute the center and offsets.
  69. center = Cartesian3.multiplyByScalar(
  70. qUnit,
  71. 1.0 / qMagnitude,
  72. scratchCartesian1
  73. );
  74. var scalar = wMagnitude / qMagnitude;
  75. eastOffset = Cartesian3.multiplyByScalar(eUnit, scalar, scratchCartesian2);
  76. northOffset = Cartesian3.multiplyByScalar(nUnit, scalar, scratchCartesian3);
  77. }
  78. // A conservative measure for the longitudes would be to use the min/max longitudes of the bounding frustum.
  79. var upperLeft = Cartesian3.add(center, northOffset, scratchCartesian5);
  80. Cartesian3.subtract(upperLeft, eastOffset, upperLeft);
  81. Cartesian3.multiplyComponents(radii, upperLeft, upperLeft);
  82. Cartesian3.pack(upperLeft, depthQuadScratch, 0);
  83. var lowerLeft = Cartesian3.subtract(center, northOffset, scratchCartesian5);
  84. Cartesian3.subtract(lowerLeft, eastOffset, lowerLeft);
  85. Cartesian3.multiplyComponents(radii, lowerLeft, lowerLeft);
  86. Cartesian3.pack(lowerLeft, depthQuadScratch, 3);
  87. var upperRight = Cartesian3.add(center, northOffset, scratchCartesian5);
  88. Cartesian3.add(upperRight, eastOffset, upperRight);
  89. Cartesian3.multiplyComponents(radii, upperRight, upperRight);
  90. Cartesian3.pack(upperRight, depthQuadScratch, 6);
  91. var lowerRight = Cartesian3.subtract(center, northOffset, scratchCartesian5);
  92. Cartesian3.add(lowerRight, eastOffset, lowerRight);
  93. Cartesian3.multiplyComponents(radii, lowerRight, lowerRight);
  94. Cartesian3.pack(lowerRight, depthQuadScratch, 9);
  95. return depthQuadScratch;
  96. }
  97. DepthPlane.prototype.update = function (frameState) {
  98. this._mode = frameState.mode;
  99. if (frameState.mode !== SceneMode.SCENE3D) {
  100. return;
  101. }
  102. var context = frameState.context;
  103. var ellipsoid = frameState.mapProjection.ellipsoid;
  104. var useLogDepth = frameState.useLogDepth;
  105. if (!defined(this._command)) {
  106. this._rs = RenderState.fromCache({
  107. // Write depth, not color
  108. cull: {
  109. enabled: true,
  110. },
  111. depthTest: {
  112. enabled: true,
  113. },
  114. colorMask: {
  115. red: false,
  116. green: false,
  117. blue: false,
  118. alpha: false,
  119. },
  120. });
  121. this._command = new DrawCommand({
  122. renderState: this._rs,
  123. boundingVolume: new BoundingSphere(
  124. Cartesian3.ZERO,
  125. ellipsoid.maximumRadius
  126. ),
  127. pass: Pass.OPAQUE,
  128. owner: this,
  129. });
  130. }
  131. if (!defined(this._sp) || this._useLogDepth !== useLogDepth) {
  132. this._useLogDepth = useLogDepth;
  133. var vs = new ShaderSource({
  134. sources: [DepthPlaneVS],
  135. });
  136. var fs = new ShaderSource({
  137. sources: [DepthPlaneFS],
  138. });
  139. if (useLogDepth) {
  140. var extension =
  141. "#ifdef GL_EXT_frag_depth \n" +
  142. "#extension GL_EXT_frag_depth : enable \n" +
  143. "#endif \n\n";
  144. fs.sources.push(extension);
  145. fs.defines.push("LOG_DEPTH");
  146. vs.defines.push("LOG_DEPTH");
  147. }
  148. this._sp = ShaderProgram.replaceCache({
  149. shaderProgram: this._sp,
  150. context: context,
  151. vertexShaderSource: vs,
  152. fragmentShaderSource: fs,
  153. attributeLocations: {
  154. position: 0,
  155. },
  156. });
  157. this._command.shaderProgram = this._sp;
  158. }
  159. // update depth plane
  160. var depthQuad = computeDepthQuad(ellipsoid, frameState);
  161. // depth plane
  162. if (!defined(this._va)) {
  163. var geometry = new Geometry({
  164. attributes: {
  165. position: new GeometryAttribute({
  166. componentDatatype: ComponentDatatype.FLOAT,
  167. componentsPerAttribute: 3,
  168. values: depthQuad,
  169. }),
  170. },
  171. indices: [0, 1, 2, 2, 1, 3],
  172. primitiveType: PrimitiveType.TRIANGLES,
  173. });
  174. this._va = VertexArray.fromGeometry({
  175. context: context,
  176. geometry: geometry,
  177. attributeLocations: {
  178. position: 0,
  179. },
  180. bufferUsage: BufferUsage.DYNAMIC_DRAW,
  181. });
  182. this._command.vertexArray = this._va;
  183. } else {
  184. this._va.getAttribute(0).vertexBuffer.copyFromArrayView(depthQuad);
  185. }
  186. };
  187. DepthPlane.prototype.execute = function (context, passState) {
  188. if (this._mode === SceneMode.SCENE3D) {
  189. this._command.execute(context, passState);
  190. }
  191. };
  192. DepthPlane.prototype.isDestroyed = function () {
  193. return false;
  194. };
  195. DepthPlane.prototype.destroy = function () {
  196. this._sp = this._sp && this._sp.destroy();
  197. this._va = this._va && this._va.destroy();
  198. };
  199. export default DepthPlane;