EllipsoidPrimitive.js 17 KB

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