EllipsoidPrimitive.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. import BoundingSphere from "../Core/BoundingSphere.js";
  2. import BoxGeometry from "../Core/BoxGeometry.js";
  3. import Cartesian3 from "../Core/Cartesian3.js";
  4. import combine from "../Core/combine.js";
  5. import defaultValue from "../Core/defaultValue.js";
  6. import defined from "../Core/defined.js";
  7. import destroyObject from "../Core/destroyObject.js";
  8. import DeveloperError from "../Core/DeveloperError.js";
  9. import Matrix4 from "../Core/Matrix4.js";
  10. import VertexFormat from "../Core/VertexFormat.js";
  11. import BufferUsage from "../Renderer/BufferUsage.js";
  12. import DrawCommand from "../Renderer/DrawCommand.js";
  13. import Pass from "../Renderer/Pass.js";
  14. import RenderState from "../Renderer/RenderState.js";
  15. import ShaderProgram from "../Renderer/ShaderProgram.js";
  16. import ShaderSource from "../Renderer/ShaderSource.js";
  17. import VertexArray from "../Renderer/VertexArray.js";
  18. import EllipsoidFS from "../Shaders/EllipsoidFS.js";
  19. import EllipsoidVS from "../Shaders/EllipsoidVS.js";
  20. import BlendingState from "./BlendingState.js";
  21. import CullFace from "./CullFace.js";
  22. import Material from "./Material.js";
  23. import SceneMode from "./SceneMode.js";
  24. const attributeLocations = {
  25. position: 0,
  26. };
  27. /**
  28. * A renderable ellipsoid. It can also draw spheres when the three {@link EllipsoidPrimitive#radii} components are equal.
  29. * <p>
  30. * This is only supported in 3D. The ellipsoid is not shown in 2D or Columbus view.
  31. * </p>
  32. *
  33. * @alias EllipsoidPrimitive
  34. * @constructor
  35. *
  36. * @param {object} [options] Object with the following properties:
  37. * @param {Cartesian3} [options.center=Cartesian3.ZERO] The center of the ellipsoid in the ellipsoid's model coordinates.
  38. * @param {Cartesian3} [options.radii] The radius of the ellipsoid along the <code>x</code>, <code>y</code>, and <code>z</code> axes in the ellipsoid's model coordinates.
  39. * @param {Matrix4} [options.modelMatrix=Matrix4.IDENTITY] The 4x4 transformation matrix that transforms the ellipsoid from model to world coordinates.
  40. * @param {boolean} [options.show=true] Determines if this primitive will be shown.
  41. * @param {Material} [options.material=Material.ColorType] The surface appearance of the primitive.
  42. * @param {object} [options.id] A user-defined object to return when the instance is picked with {@link Scene#pick}
  43. * @param {boolean} [options.debugShowBoundingVolume=false] For debugging only. Determines if this primitive's commands' bounding spheres are shown.
  44. *
  45. * @private
  46. */
  47. function EllipsoidPrimitive(options) {
  48. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  49. /**
  50. * The center of the ellipsoid in the ellipsoid's model coordinates.
  51. * <p>
  52. * The default is {@link Cartesian3.ZERO}.
  53. * </p>
  54. *
  55. * @type {Cartesian3}
  56. * @default {@link Cartesian3.ZERO}
  57. *
  58. * @see EllipsoidPrimitive#modelMatrix
  59. */
  60. this.center = Cartesian3.clone(defaultValue(options.center, Cartesian3.ZERO));
  61. this._center = new Cartesian3();
  62. /**
  63. * The radius of the ellipsoid along the <code>x</code>, <code>y</code>, and <code>z</code> axes in the ellipsoid's model coordinates.
  64. * When these are the same, the ellipsoid is a sphere.
  65. * <p>
  66. * The default is <code>undefined</code>. The ellipsoid is not drawn until a radii is provided.
  67. * </p>
  68. *
  69. * @type {Cartesian3}
  70. * @default undefined
  71. *
  72. *
  73. * @example
  74. * // A sphere with a radius of 2.0
  75. * e.radii = new Cesium.Cartesian3(2.0, 2.0, 2.0);
  76. *
  77. * @see EllipsoidPrimitive#modelMatrix
  78. */
  79. this.radii = Cartesian3.clone(options.radii);
  80. this._radii = new Cartesian3();
  81. this._oneOverEllipsoidRadiiSquared = new Cartesian3();
  82. this._boundingSphere = new BoundingSphere();
  83. /**
  84. * The 4x4 transformation matrix that transforms the ellipsoid from model to world coordinates.
  85. * When this is the identity matrix, the ellipsoid is drawn in world coordinates, i.e., Earth's WGS84 coordinates.
  86. * Local reference frames can be used by providing a different transformation matrix, like that returned
  87. * by {@link Transforms.eastNorthUpToFixedFrame}.
  88. *
  89. * @type {Matrix4}
  90. * @default {@link Matrix4.IDENTITY}
  91. *
  92. * @example
  93. * const origin = Cesium.Cartesian3.fromDegrees(-95.0, 40.0, 200000.0);
  94. * e.modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(origin);
  95. */
  96. this.modelMatrix = Matrix4.clone(
  97. defaultValue(options.modelMatrix, Matrix4.IDENTITY)
  98. );
  99. this._modelMatrix = new Matrix4();
  100. this._computedModelMatrix = new Matrix4();
  101. /**
  102. * Determines if the ellipsoid primitive will be shown.
  103. *
  104. * @type {boolean}
  105. * @default true
  106. */
  107. this.show = defaultValue(options.show, true);
  108. /**
  109. * The surface appearance of the ellipsoid. This can be one of several built-in {@link Material} objects or a custom material, scripted with
  110. * {@link https://github.com/CesiumGS/cesium/wiki/Fabric|Fabric}.
  111. * <p>
  112. * The default material is <code>Material.ColorType</code>.
  113. * </p>
  114. *
  115. * @type {Material}
  116. * @default Material.fromType(Material.ColorType)
  117. *
  118. *
  119. * @example
  120. * // 1. Change the color of the default material to yellow
  121. * e.material.uniforms.color = new Cesium.Color(1.0, 1.0, 0.0, 1.0);
  122. *
  123. * // 2. Change material to horizontal stripes
  124. * e.material = Cesium.Material.fromType(Cesium.Material.StripeType);
  125. *
  126. * @see {@link https://github.com/CesiumGS/cesium/wiki/Fabric|Fabric}
  127. */
  128. this.material = defaultValue(
  129. options.material,
  130. Material.fromType(Material.ColorType)
  131. );
  132. this._material = undefined;
  133. this._translucent = undefined;
  134. /**
  135. * User-defined object returned when the ellipsoid is picked.
  136. *
  137. * @type {object}
  138. *
  139. * @default undefined
  140. *
  141. * @see Scene#pick
  142. */
  143. this.id = options.id;
  144. this._id = undefined;
  145. /**
  146. * This property is for debugging only; it is not for production use nor is it optimized.
  147. * <p>
  148. * Draws the bounding sphere for each draw command in the primitive.
  149. * </p>
  150. *
  151. * @type {boolean}
  152. *
  153. * @default false
  154. */
  155. this.debugShowBoundingVolume = defaultValue(
  156. options.debugShowBoundingVolume,
  157. false
  158. );
  159. /**
  160. * @private
  161. */
  162. this.onlySunLighting = defaultValue(options.onlySunLighting, false);
  163. this._onlySunLighting = false;
  164. /**
  165. * @private
  166. */
  167. this._depthTestEnabled = defaultValue(options.depthTestEnabled, true);
  168. this._useLogDepth = false;
  169. this._sp = undefined;
  170. this._rs = undefined;
  171. this._va = undefined;
  172. this._pickSP = undefined;
  173. this._pickId = undefined;
  174. this._colorCommand = new DrawCommand({
  175. owner: defaultValue(options._owner, this),
  176. });
  177. this._pickCommand = new DrawCommand({
  178. owner: defaultValue(options._owner, this),
  179. pickOnly: true,
  180. });
  181. const that = this;
  182. this._uniforms = {
  183. u_radii: function () {
  184. return that.radii;
  185. },
  186. u_oneOverEllipsoidRadiiSquared: function () {
  187. return that._oneOverEllipsoidRadiiSquared;
  188. },
  189. };
  190. this._pickUniforms = {
  191. czm_pickColor: function () {
  192. return that._pickId.color;
  193. },
  194. };
  195. }
  196. function getVertexArray(context) {
  197. let vertexArray = context.cache.ellipsoidPrimitive_vertexArray;
  198. if (defined(vertexArray)) {
  199. return vertexArray;
  200. }
  201. const geometry = BoxGeometry.createGeometry(
  202. BoxGeometry.fromDimensions({
  203. dimensions: new Cartesian3(2.0, 2.0, 2.0),
  204. vertexFormat: VertexFormat.POSITION_ONLY,
  205. })
  206. );
  207. vertexArray = VertexArray.fromGeometry({
  208. context: context,
  209. geometry: geometry,
  210. attributeLocations: attributeLocations,
  211. bufferUsage: BufferUsage.STATIC_DRAW,
  212. interleave: true,
  213. });
  214. context.cache.ellipsoidPrimitive_vertexArray = vertexArray;
  215. return vertexArray;
  216. }
  217. /**
  218. * Called when {@link Viewer} or {@link CesiumWidget} render the scene to
  219. * get the draw commands needed to render this primitive.
  220. * <p>
  221. * Do not call this function directly. This is documented just to
  222. * list the exceptions that may be propagated when the scene is rendered:
  223. * </p>
  224. *
  225. * @exception {DeveloperError} this.material must be defined.
  226. */
  227. EllipsoidPrimitive.prototype.update = function (frameState) {
  228. if (
  229. !this.show ||
  230. frameState.mode !== SceneMode.SCENE3D ||
  231. !defined(this.center) ||
  232. !defined(this.radii)
  233. ) {
  234. return;
  235. }
  236. //>>includeStart('debug', pragmas.debug);
  237. if (!defined(this.material)) {
  238. throw new DeveloperError("this.material must be defined.");
  239. }
  240. //>>includeEnd('debug');
  241. const context = frameState.context;
  242. const translucent = this.material.isTranslucent();
  243. const translucencyChanged = this._translucent !== translucent;
  244. if (!defined(this._rs) || translucencyChanged) {
  245. this._translucent = translucent;
  246. // If this render state is ever updated to use a non-default
  247. // depth range, the hard-coded values in EllipsoidVS.glsl need
  248. // to be updated as well.
  249. this._rs = RenderState.fromCache({
  250. // Cull front faces - not back faces - so the ellipsoid doesn't
  251. // disappear if the viewer enters the bounding box.
  252. cull: {
  253. enabled: true,
  254. face: CullFace.FRONT,
  255. },
  256. depthTest: {
  257. enabled: this._depthTestEnabled,
  258. },
  259. // Only write depth when EXT_frag_depth is supported since the depth for
  260. // the bounding box is wrong; it is not the true depth of the ray casted ellipsoid.
  261. depthMask: !translucent && context.fragmentDepth,
  262. blending: translucent ? BlendingState.ALPHA_BLEND : undefined,
  263. });
  264. }
  265. if (!defined(this._va)) {
  266. this._va = getVertexArray(context);
  267. }
  268. let boundingSphereDirty = false;
  269. const radii = this.radii;
  270. if (!Cartesian3.equals(this._radii, radii)) {
  271. Cartesian3.clone(radii, this._radii);
  272. const r = this._oneOverEllipsoidRadiiSquared;
  273. r.x = 1.0 / (radii.x * radii.x);
  274. r.y = 1.0 / (radii.y * radii.y);
  275. r.z = 1.0 / (radii.z * radii.z);
  276. boundingSphereDirty = true;
  277. }
  278. if (
  279. !Matrix4.equals(this.modelMatrix, this._modelMatrix) ||
  280. !Cartesian3.equals(this.center, this._center)
  281. ) {
  282. Matrix4.clone(this.modelMatrix, this._modelMatrix);
  283. Cartesian3.clone(this.center, this._center);
  284. // Translate model coordinates used for rendering such that the origin is the center of the ellipsoid.
  285. Matrix4.multiplyByTranslation(
  286. this.modelMatrix,
  287. this.center,
  288. this._computedModelMatrix
  289. );
  290. boundingSphereDirty = true;
  291. }
  292. if (boundingSphereDirty) {
  293. Cartesian3.clone(Cartesian3.ZERO, this._boundingSphere.center);
  294. this._boundingSphere.radius = Cartesian3.maximumComponent(radii);
  295. BoundingSphere.transform(
  296. this._boundingSphere,
  297. this._computedModelMatrix,
  298. this._boundingSphere
  299. );
  300. }
  301. const materialChanged = this._material !== this.material;
  302. this._material = this.material;
  303. this._material.update(context);
  304. const lightingChanged = this.onlySunLighting !== this._onlySunLighting;
  305. this._onlySunLighting = this.onlySunLighting;
  306. const useLogDepth = frameState.useLogDepth;
  307. const useLogDepthChanged = this._useLogDepth !== useLogDepth;
  308. this._useLogDepth = useLogDepth;
  309. const colorCommand = this._colorCommand;
  310. let vs;
  311. let fs;
  312. // Recompile shader when material, lighting, or translucency changes
  313. if (
  314. materialChanged ||
  315. lightingChanged ||
  316. translucencyChanged ||
  317. useLogDepthChanged
  318. ) {
  319. vs = new ShaderSource({
  320. sources: [EllipsoidVS],
  321. });
  322. fs = new ShaderSource({
  323. sources: [this.material.shaderSource, EllipsoidFS],
  324. });
  325. if (this.onlySunLighting) {
  326. fs.defines.push("ONLY_SUN_LIGHTING");
  327. }
  328. if (!translucent && context.fragmentDepth) {
  329. fs.defines.push("WRITE_DEPTH");
  330. }
  331. if (this._useLogDepth) {
  332. vs.defines.push("LOG_DEPTH");
  333. fs.defines.push("LOG_DEPTH");
  334. }
  335. this._sp = ShaderProgram.replaceCache({
  336. context: context,
  337. shaderProgram: this._sp,
  338. vertexShaderSource: vs,
  339. fragmentShaderSource: fs,
  340. attributeLocations: attributeLocations,
  341. });
  342. colorCommand.vertexArray = this._va;
  343. colorCommand.renderState = this._rs;
  344. colorCommand.shaderProgram = this._sp;
  345. colorCommand.uniformMap = combine(this._uniforms, this.material._uniforms);
  346. colorCommand.executeInClosestFrustum = translucent;
  347. }
  348. const commandList = frameState.commandList;
  349. const passes = frameState.passes;
  350. if (passes.render) {
  351. colorCommand.boundingVolume = this._boundingSphere;
  352. colorCommand.debugShowBoundingVolume = this.debugShowBoundingVolume;
  353. colorCommand.modelMatrix = this._computedModelMatrix;
  354. colorCommand.pass = translucent ? Pass.TRANSLUCENT : Pass.OPAQUE;
  355. commandList.push(colorCommand);
  356. }
  357. if (passes.pick) {
  358. const pickCommand = this._pickCommand;
  359. if (!defined(this._pickId) || this._id !== this.id) {
  360. this._id = this.id;
  361. this._pickId = this._pickId && this._pickId.destroy();
  362. this._pickId = context.createPickId({
  363. primitive: this,
  364. id: this.id,
  365. });
  366. }
  367. // Recompile shader when material changes
  368. if (
  369. materialChanged ||
  370. lightingChanged ||
  371. !defined(this._pickSP) ||
  372. useLogDepthChanged
  373. ) {
  374. vs = new ShaderSource({
  375. sources: [EllipsoidVS],
  376. });
  377. fs = new ShaderSource({
  378. sources: [this.material.shaderSource, EllipsoidFS],
  379. pickColorQualifier: "uniform",
  380. });
  381. if (this.onlySunLighting) {
  382. fs.defines.push("ONLY_SUN_LIGHTING");
  383. }
  384. if (!translucent && context.fragmentDepth) {
  385. fs.defines.push("WRITE_DEPTH");
  386. }
  387. if (this._useLogDepth) {
  388. vs.defines.push("LOG_DEPTH");
  389. fs.defines.push("LOG_DEPTH");
  390. }
  391. this._pickSP = ShaderProgram.replaceCache({
  392. context: context,
  393. shaderProgram: this._pickSP,
  394. vertexShaderSource: vs,
  395. fragmentShaderSource: fs,
  396. attributeLocations: attributeLocations,
  397. });
  398. pickCommand.vertexArray = this._va;
  399. pickCommand.renderState = this._rs;
  400. pickCommand.shaderProgram = this._pickSP;
  401. pickCommand.uniformMap = combine(
  402. combine(this._uniforms, this._pickUniforms),
  403. this.material._uniforms
  404. );
  405. pickCommand.executeInClosestFrustum = translucent;
  406. }
  407. pickCommand.boundingVolume = this._boundingSphere;
  408. pickCommand.modelMatrix = this._computedModelMatrix;
  409. pickCommand.pass = translucent ? Pass.TRANSLUCENT : Pass.OPAQUE;
  410. commandList.push(pickCommand);
  411. }
  412. };
  413. /**
  414. * Returns true if this object was destroyed; otherwise, false.
  415. * <br /><br />
  416. * If this object was destroyed, it should not be used; calling any function other than
  417. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  418. *
  419. * @returns {boolean} <code>true</code> if this object was destroyed; otherwise, <code>false</code>.
  420. *
  421. * @see EllipsoidPrimitive#destroy
  422. */
  423. EllipsoidPrimitive.prototype.isDestroyed = function () {
  424. return false;
  425. };
  426. /**
  427. * Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
  428. * release of WebGL resources, instead of relying on the garbage collector to destroy this object.
  429. * <br /><br />
  430. * Once an object is destroyed, it should not be used; calling any function other than
  431. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  432. * assign the return value (<code>undefined</code>) to the object as done in the example.
  433. *
  434. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  435. *
  436. *
  437. * @example
  438. * e = e && e.destroy();
  439. *
  440. * @see EllipsoidPrimitive#isDestroyed
  441. */
  442. EllipsoidPrimitive.prototype.destroy = function () {
  443. this._sp = this._sp && this._sp.destroy();
  444. this._pickSP = this._pickSP && this._pickSP.destroy();
  445. this._pickId = this._pickId && this._pickId.destroy();
  446. return destroyObject(this);
  447. };
  448. export default EllipsoidPrimitive;