RectanglePrimitive.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*global define*/
  2. define([
  3. '../Core/Color',
  4. '../Core/defaultValue',
  5. '../Core/defined',
  6. '../Core/destroyObject',
  7. '../Core/DeveloperError',
  8. '../Core/Ellipsoid',
  9. '../Core/GeometryInstance',
  10. '../Core/Math',
  11. '../Core/Rectangle',
  12. '../Core/RectangleGeometry',
  13. './EllipsoidSurfaceAppearance',
  14. './Material',
  15. './Primitive'
  16. ], function(
  17. Color,
  18. defaultValue,
  19. defined,
  20. destroyObject,
  21. DeveloperError,
  22. Ellipsoid,
  23. GeometryInstance,
  24. CesiumMath,
  25. Rectangle,
  26. RectangleGeometry,
  27. EllipsoidSurfaceAppearance,
  28. Material,
  29. Primitive) {
  30. "use strict";
  31. /**
  32. * A renderable rectangle.
  33. *
  34. * @alias RectanglePrimitive
  35. * @constructor
  36. *
  37. * @param {Object} [options] Object with the following properties:
  38. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid that the rectangle is drawn on.
  39. * @param {Rectangle} [options.rectangle] The rectangle, which defines the rectangular region to draw.
  40. * @param {Number} [options.granularity=CesiumMath.RADIANS_PER_DEGREE] The distance, in radians, between each latitude and longitude in the underlying geometry.
  41. * @param {Number} [options.height=0.0] The height, in meters, that the rectangle is raised above the {@link RectanglePrimitive#ellipsoid}.
  42. * @param {Number} [options.rotation=0.0] The angle, in radians, relative to north that the rectangle is rotated. Positive angles rotate counter-clockwise.
  43. * @param {Number} [options.textureRotationAngle=0.0] The rotation of the texture coordinates, in radians. A positive rotation is counter-clockwise.
  44. * @param {Boolean} [options.show=true] Determines if this primitive will be shown.
  45. * @param {Material} [options.material] The surface appearance of the primitive.
  46. * @param {Object} [options.id] A user-defined object to return when the instance is picked with {@link Scene#pick}
  47. * @param {Boolean} [options.asynchronous=true] Determines if the rectangle will be created asynchronously or block until ready.
  48. * @param {Boolean} [options.debugShowBoundingVolume=false] For debugging only. Determines if the primitive's commands' bounding spheres are shown.
  49. *
  50. * @example
  51. * var rectanglePrimitive = new Cesium.RectanglePrimitive({
  52. * rectangle : Cesium.Rectangle.fromDegrees(0.0, 20.0, 10.0, 30.0)
  53. * });
  54. * primitives.add(rectanglePrimitive);
  55. */
  56. var RectanglePrimitive = function(options) {
  57. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  58. /**
  59. * The ellipsoid that the rectangle is drawn on.
  60. *
  61. * @type Ellipsoid
  62. *
  63. * @default Ellipsoid.WGS84
  64. */
  65. this.ellipsoid = defaultValue(options.ellipsoid, Ellipsoid.WGS84);
  66. this._ellipsoid = undefined;
  67. /**
  68. * The rectangle, which defines the rectangular region to draw.
  69. *
  70. * @type Rectangle
  71. *
  72. * @default undefined
  73. */
  74. this.rectangle = Rectangle.clone(options.rectangle);
  75. this._rectangle = undefined;
  76. /**
  77. * The distance, in radians, between each latitude and longitude in the underlying geometry.
  78. * A lower granularity fits the curvature of the {@link RectanglePrimitive#ellipsoid} better,
  79. * but uses more triangles.
  80. *
  81. * @type Number
  82. *
  83. * @default CesiumMath.RADIANS_PER_DEGREE
  84. */
  85. this.granularity = defaultValue(options.granularity, CesiumMath.RADIANS_PER_DEGREE);
  86. this._granularity = undefined;
  87. /**
  88. * The height, in meters, that the rectangle is raised above the {@link RectanglePrimitive#ellipsoid}.
  89. *
  90. * @type Number
  91. *
  92. * @default 0.0
  93. */
  94. this.height = defaultValue(options.height, 0.0);
  95. this._height = undefined;
  96. /**
  97. * The angle, in radians, relative to north that the rectangle is rotated.
  98. * Positive angles rotate counter-clockwise.
  99. *
  100. * @type Number
  101. *
  102. * @default 0.0
  103. */
  104. this.rotation = defaultValue(options.rotation, 0.0);
  105. this._rotation = undefined;
  106. /**
  107. * The angle, in radians, relative to north that the primitive's texture is rotated.
  108. * Positive angles rotate counter-clockwise.
  109. *
  110. * @type Number
  111. *
  112. * @default 0.0
  113. */
  114. this.textureRotationAngle = defaultValue(options.textureRotationAngle, 0.0);
  115. this._textureRotationAngle = undefined;
  116. /**
  117. * Determines if this primitive will be shown.
  118. *
  119. * @type Boolean
  120. *
  121. * @default true
  122. */
  123. this.show = defaultValue(options.show, true);
  124. var material = Material.fromType(Material.ColorType, {
  125. color : new Color(1.0, 1.0, 0.0, 0.5)
  126. });
  127. /**
  128. * The surface appearance of the primitive. This can be one of several built-in {@link Material} objects or a custom material, scripted with
  129. * {@link https://github.com/AnalyticalGraphicsInc/cesium/wiki/Fabric|Fabric}.
  130. * <p>
  131. * The default material is <code>Material.ColorType</code>.
  132. * </p>
  133. *
  134. * @type Material
  135. *
  136. * @see {@link https://github.com/AnalyticalGraphicsInc/cesium/wiki/Fabric|Fabric}
  137. *
  138. * @example
  139. * // 1. Change the color of the default material to yellow
  140. * rectangle.material.uniforms.color = new Cesium.Color(1.0, 1.0, 0.0, 1.0);
  141. *
  142. * // 2. Change material to horizontal stripes
  143. * rectangle.material = Cesium.Material.fromType(Material.StripeType);
  144. */
  145. this.material = defaultValue(options.material, material);
  146. /**
  147. * User-defined object returned when the rectangle is picked.
  148. *
  149. * @type Object
  150. *
  151. * @default undefined
  152. *
  153. * @see Scene#pick
  154. */
  155. this.id = options.id;
  156. this._id = undefined;
  157. /**
  158. * Determines if the geometry instances will be created and batched on
  159. * a web worker.
  160. *
  161. * @type Boolean
  162. *
  163. * @default true
  164. */
  165. this.asynchronous = defaultValue(options.asynchronous, true);
  166. /**
  167. * This property is for debugging only; it is not for production use nor is it optimized.
  168. * <p>
  169. * Draws the bounding sphere for each draw command in the primitive.
  170. * </p>
  171. *
  172. * @type {Boolean}
  173. *
  174. * @default false
  175. */
  176. this.debugShowBoundingVolume = defaultValue(options.debugShowBoundingVolume, false);
  177. this._primitive = undefined;
  178. };
  179. /**
  180. * Called when {@link Viewer} or {@link CesiumWidget} render the scene to
  181. * get the draw commands needed to render this primitive.
  182. * <p>
  183. * Do not call this function directly. This is documented just to
  184. * list the exceptions that may be propagated when the scene is rendered:
  185. * </p>
  186. *
  187. * @exception {DeveloperError} this.ellipsoid must be defined.
  188. * @exception {DeveloperError} this.material must be defined.
  189. * @exception {DeveloperError} this.granularity must be defined.
  190. */
  191. RectanglePrimitive.prototype.update = function(context, frameState, commandList) {
  192. //>>includeStart('debug', pragmas.debug);
  193. if (!defined(this.ellipsoid)) {
  194. throw new DeveloperError('this.ellipsoid must be defined.');
  195. }
  196. if (!defined(this.material)) {
  197. throw new DeveloperError('this.material must be defined.');
  198. }
  199. if (this.granularity < 0.0) {
  200. throw new DeveloperError('this.granularity must be greater than zero.');
  201. }
  202. //>>includeEnd('debug');
  203. if (!this.show || (!defined(this.rectangle))) {
  204. return;
  205. }
  206. if (!Rectangle.equals(this._rectangle, this.rectangle) ||
  207. (this._ellipsoid !== this.ellipsoid) ||
  208. (this._granularity !== this.granularity) ||
  209. (this._height !== this.height) ||
  210. (this._rotation !== this.rotation) ||
  211. (this._textureRotationAngle !== this.textureRotationAngle) ||
  212. (this._id !== this.id)) {
  213. this._rectangle = Rectangle.clone(this.rectangle, this._rectangle);
  214. this._ellipsoid = this.ellipsoid;
  215. this._granularity = this.granularity;
  216. this._height = this.height;
  217. this._rotation = this.rotation;
  218. this._textureRotationAngle = this.textureRotationAngle;
  219. this._id = this.id;
  220. var instance = new GeometryInstance({
  221. geometry : new RectangleGeometry({
  222. rectangle : this.rectangle,
  223. vertexFormat : EllipsoidSurfaceAppearance.VERTEX_FORMAT,
  224. ellipsoid : this.ellipsoid,
  225. granularity : this.granularity,
  226. height : this.height,
  227. rotation : this.rotation,
  228. stRotation : this.textureRotationAngle
  229. }),
  230. id : this.id,
  231. pickPrimitive : this
  232. });
  233. if (defined(this._primitive)) {
  234. this._primitive.destroy();
  235. }
  236. this._primitive = new Primitive({
  237. geometryInstances : instance,
  238. appearance : new EllipsoidSurfaceAppearance({
  239. aboveGround : (this.height > 0.0)
  240. }),
  241. asynchronous : this.asynchronous
  242. });
  243. }
  244. var primitive = this._primitive;
  245. primitive.appearance.material = this.material;
  246. primitive.debugShowBoundingVolume = this.debugShowBoundingVolume;
  247. primitive.update(context, frameState, commandList);
  248. };
  249. /**
  250. * Returns true if this object was destroyed; otherwise, false.
  251. * <br /><br />
  252. * If this object was destroyed, it should not be used; calling any function other than
  253. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  254. *
  255. * @returns {Boolean} <code>true</code> if this object was destroyed; otherwise, <code>false</code>.
  256. *
  257. * @see Rectangle#destroy
  258. */
  259. RectanglePrimitive.prototype.isDestroyed = function() {
  260. return false;
  261. };
  262. /**
  263. * Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
  264. * release of WebGL resources, instead of relying on the garbage collector to destroy this object.
  265. * <br /><br />
  266. * Once an object is destroyed, it should not be used; calling any function other than
  267. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  268. * assign the return value (<code>undefined</code>) to the object as done in the example.
  269. *
  270. * @returns {undefined}
  271. *
  272. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  273. *
  274. * @example
  275. * rectangle = rectangle && rectangle.destroy();
  276. *
  277. * @see Rectangle#isDestroyed
  278. */
  279. RectanglePrimitive.prototype.destroy = function() {
  280. this._primitive = this._primitive && this._primitive.destroy();
  281. return destroyObject(this);
  282. };
  283. return RectanglePrimitive;
  284. });