DebugModelMatrixPrimitive.js 7.9 KB

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