DebugModelMatrixPrimitive.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*global define*/
  2. define([
  3. '../Core/Cartesian3',
  4. '../Core/Color',
  5. '../Core/defaultValue',
  6. '../Core/defined',
  7. '../Core/destroyObject',
  8. '../Core/GeometryInstance',
  9. '../Core/Matrix4',
  10. '../Core/PolylineGeometry',
  11. './PolylineColorAppearance',
  12. './Primitive'
  13. ], function(
  14. Cartesian3,
  15. Color,
  16. defaultValue,
  17. defined,
  18. destroyObject,
  19. GeometryInstance,
  20. Matrix4,
  21. PolylineGeometry,
  22. PolylineColorAppearance,
  23. Primitive) {
  24. "use strict";
  25. /**
  26. * Draws the axes of a reference frame defined by a matrix that transforms to world
  27. * coordinates, i.e., Earth's WGS84 coordinates. The most prominent example is
  28. * a primitives <code>modelMatrix</code>.
  29. * <p>
  30. * The X axis is red; Y is green; and Z is blue.
  31. * </p>
  32. * <p>
  33. * This is for debugging only; it is not optimized for production use.
  34. * </p>
  35. *
  36. * @alias DebugModelMatrixPrimitive
  37. * @constructor
  38. *
  39. * @param {Object} [options] Object with the following properties:
  40. * @param {Number} [options.length=10000000.0] The length of the axes in meters.
  41. * @param {Number} [options.width=2.0] The width of the axes in pixels.
  42. * @param {Matrix4} [options.modelMatrix=Matrix4.IDENTITY] The 4x4 matrix that defines the reference frame, i.e., origin plus axes, to visualize.
  43. * @param {Boolean} [options.show=true] Determines if this primitive will be shown.
  44. * @param {Object} [options.id] A user-defined object to return when the instance is picked with {@link Scene#pick}
  45. *
  46. * @example
  47. * primitives.add(new Cesium.DebugModelMatrixPrimitive({
  48. * modelMatrix : primitive.modelMatrix, // primitive to debug
  49. * length : 100000.0,
  50. * width : 10.0
  51. * }));
  52. */
  53. var DebugModelMatrixPrimitive = function(options) {
  54. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  55. /**
  56. * The length of the axes in meters.
  57. *
  58. * @type {Number}
  59. * @default 10000000.0
  60. */
  61. this.length = defaultValue(options.length, 10000000.0);
  62. this._length = undefined;
  63. /**
  64. * The width of the axes in pixels.
  65. *
  66. * @type {Number}
  67. * @default 2.0
  68. */
  69. this.width = defaultValue(options.width, 2.0);
  70. this._width = undefined;
  71. /**
  72. * Determines if this primitive will be shown.
  73. *
  74. * @type Boolean
  75. * @default true
  76. */
  77. this.show = defaultValue(options.show, true);
  78. /**
  79. * The 4x4 matrix that defines the reference frame, i.e., origin plus axes, to visualize.
  80. *
  81. * @type {Matrix4}
  82. * @default {@link Matrix4.IDENTITY}
  83. */
  84. this.modelMatrix = Matrix4.clone(defaultValue(options.modelMatrix, Matrix4.IDENTITY));
  85. this._modelMatrix = new Matrix4();
  86. /**
  87. * User-defined object returned when the primitive is picked.
  88. *
  89. * @type {Object}
  90. * @default undefined
  91. *
  92. * @see Scene#pick
  93. */
  94. this.id = options.id;
  95. this._id = undefined;
  96. this._primitive = undefined;
  97. };
  98. /**
  99. * @private
  100. */
  101. DebugModelMatrixPrimitive.prototype.update = function(context, frameState, commandList) {
  102. if (!this.show) {
  103. return;
  104. }
  105. if (!defined(this._primitive) ||
  106. (!Matrix4.equals(this._modelMatrix, this.modelMatrix)) ||
  107. (this._length !== this.length) ||
  108. (this._width !== this.width) ||
  109. (this._id !== this.id)) {
  110. this._modelMatrix = Matrix4.clone(this.modelMatrix, this._modelMatrix);
  111. this._length = this.length;
  112. this._width = this.width;
  113. this._id = this.id;
  114. if (defined(this._primitive)) {
  115. this._primitive.destroy();
  116. }
  117. // Workaround projecting (0, 0, 0)
  118. if ((this.modelMatrix[12] === 0.0 && this.modelMatrix[13] === 0.0 && this.modelMatrix[14] === 0.0)) {
  119. this.modelMatrix[14] = 0.01;
  120. }
  121. var x = new GeometryInstance({
  122. geometry : new PolylineGeometry({
  123. positions : [
  124. Cartesian3.ZERO,
  125. Cartesian3.UNIT_X
  126. ],
  127. width : this.width,
  128. vertexFormat : PolylineColorAppearance.VERTEX_FORMAT,
  129. colors : [
  130. Color.RED,
  131. Color.RED
  132. ],
  133. followSurface: false
  134. }),
  135. modelMatrix : Matrix4.multiplyByUniformScale(this.modelMatrix, this.length, new Matrix4()),
  136. id : this.id,
  137. pickPrimitive : this
  138. });
  139. var y = new GeometryInstance({
  140. geometry : new PolylineGeometry({
  141. positions : [
  142. Cartesian3.ZERO,
  143. Cartesian3.UNIT_Y
  144. ],
  145. width : this.width,
  146. vertexFormat : PolylineColorAppearance.VERTEX_FORMAT,
  147. colors : [
  148. Color.GREEN,
  149. Color.GREEN
  150. ],
  151. followSurface: false
  152. }),
  153. modelMatrix : Matrix4.multiplyByUniformScale(this.modelMatrix, this.length, new Matrix4()),
  154. id : this.id,
  155. pickPrimitive : this
  156. });
  157. var z = new GeometryInstance({
  158. geometry : new PolylineGeometry({
  159. positions : [
  160. Cartesian3.ZERO,
  161. Cartesian3.UNIT_Z
  162. ],
  163. width : this.width,
  164. vertexFormat : PolylineColorAppearance.VERTEX_FORMAT,
  165. colors : [
  166. Color.BLUE,
  167. Color.BLUE
  168. ],
  169. followSurface: false
  170. }),
  171. modelMatrix : Matrix4.multiplyByUniformScale(this.modelMatrix, this.length, new Matrix4()),
  172. id : this.id,
  173. pickPrimitive : this
  174. });
  175. this._primitive = new Primitive({
  176. geometryInstances : [x, y, z],
  177. appearance : new PolylineColorAppearance(),
  178. asynchronous : false
  179. });
  180. }
  181. this._primitive.update(context, frameState, commandList);
  182. };
  183. /**
  184. * Returns true if this object was destroyed; otherwise, false.
  185. * <p>
  186. * If this object was destroyed, it should not be used; calling any function other than
  187. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  188. * </p>
  189. *
  190. * @returns {Boolean} <code>true</code> if this object was destroyed; otherwise, <code>false</code>.
  191. *
  192. * @see DebugModelMatrixPrimitive#destroy
  193. */
  194. DebugModelMatrixPrimitive.prototype.isDestroyed = function() {
  195. return false;
  196. };
  197. /**
  198. * Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
  199. * release of WebGL resources, instead of relying on the garbage collector to destroy this object.
  200. * <p>
  201. * Once an object is destroyed, it should not be used; calling any function other than
  202. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  203. * assign the return value (<code>undefined</code>) to the object as done in the example.
  204. * </p>
  205. *
  206. * @returns {undefined}
  207. *
  208. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  209. *
  210. * @example
  211. * p = p && p.destroy();
  212. *
  213. * @see DebugModelMatrixPrimitive#isDestroyed
  214. */
  215. DebugModelMatrixPrimitive.prototype.destroy = function() {
  216. this._primitive = this._primitive && this._primitive.destroy();
  217. return destroyObject(this);
  218. };
  219. return DebugModelMatrixPrimitive;
  220. });