DebugModelMatrixPrimitive.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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(
  72. defaultValue(options.modelMatrix, Matrix4.IDENTITY)
  73. );
  74. this._modelMatrix = new Matrix4();
  75. /**
  76. * User-defined value returned when the primitive is picked.
  77. *
  78. * @type {*}
  79. * @default undefined
  80. *
  81. * @see Scene#pick
  82. */
  83. this.id = options.id;
  84. this._id = undefined;
  85. this._primitive = undefined;
  86. }
  87. /**
  88. * @private
  89. */
  90. DebugModelMatrixPrimitive.prototype.update = function (frameState) {
  91. if (!this.show) {
  92. return;
  93. }
  94. if (
  95. !defined(this._primitive) ||
  96. !Matrix4.equals(this._modelMatrix, this.modelMatrix) ||
  97. this._length !== this.length ||
  98. this._width !== this.width ||
  99. this._id !== this.id
  100. ) {
  101. this._modelMatrix = Matrix4.clone(this.modelMatrix, this._modelMatrix);
  102. this._length = this.length;
  103. this._width = this.width;
  104. this._id = this.id;
  105. if (defined(this._primitive)) {
  106. this._primitive.destroy();
  107. }
  108. // Workaround projecting (0, 0, 0)
  109. if (
  110. this.modelMatrix[12] === 0.0 &&
  111. this.modelMatrix[13] === 0.0 &&
  112. this.modelMatrix[14] === 0.0
  113. ) {
  114. this.modelMatrix[14] = 0.01;
  115. }
  116. const x = new GeometryInstance({
  117. geometry: new PolylineGeometry({
  118. positions: [Cartesian3.ZERO, Cartesian3.UNIT_X],
  119. width: this.width,
  120. vertexFormat: PolylineColorAppearance.VERTEX_FORMAT,
  121. colors: [Color.RED, Color.RED],
  122. arcType: ArcType.NONE,
  123. }),
  124. modelMatrix: Matrix4.multiplyByUniformScale(
  125. this.modelMatrix,
  126. this.length,
  127. new Matrix4()
  128. ),
  129. id: this.id,
  130. pickPrimitive: this,
  131. });
  132. const y = new GeometryInstance({
  133. geometry: new PolylineGeometry({
  134. positions: [Cartesian3.ZERO, Cartesian3.UNIT_Y],
  135. width: this.width,
  136. vertexFormat: PolylineColorAppearance.VERTEX_FORMAT,
  137. colors: [Color.GREEN, Color.GREEN],
  138. arcType: ArcType.NONE,
  139. }),
  140. modelMatrix: Matrix4.multiplyByUniformScale(
  141. this.modelMatrix,
  142. this.length,
  143. new Matrix4()
  144. ),
  145. id: this.id,
  146. pickPrimitive: this,
  147. });
  148. const z = new GeometryInstance({
  149. geometry: new PolylineGeometry({
  150. positions: [Cartesian3.ZERO, Cartesian3.UNIT_Z],
  151. width: this.width,
  152. vertexFormat: PolylineColorAppearance.VERTEX_FORMAT,
  153. colors: [Color.BLUE, Color.BLUE],
  154. arcType: ArcType.NONE,
  155. }),
  156. modelMatrix: Matrix4.multiplyByUniformScale(
  157. this.modelMatrix,
  158. this.length,
  159. new Matrix4()
  160. ),
  161. id: this.id,
  162. pickPrimitive: this,
  163. });
  164. this._primitive = new Primitive({
  165. geometryInstances: [x, y, z],
  166. appearance: new PolylineColorAppearance(),
  167. asynchronous: false,
  168. });
  169. }
  170. this._primitive.update(frameState);
  171. };
  172. /**
  173. * Returns true if this object was destroyed; otherwise, false.
  174. * <p>
  175. * If this object was destroyed, it should not be used; calling any function other than
  176. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  177. * </p>
  178. *
  179. * @returns {Boolean} <code>true</code> if this object was destroyed; otherwise, <code>false</code>.
  180. *
  181. * @see DebugModelMatrixPrimitive#destroy
  182. */
  183. DebugModelMatrixPrimitive.prototype.isDestroyed = function () {
  184. return false;
  185. };
  186. /**
  187. * Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
  188. * release of WebGL resources, instead of relying on the garbage collector to destroy this object.
  189. * <p>
  190. * Once an object is destroyed, it should not be used; calling any function other than
  191. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  192. * assign the return value (<code>undefined</code>) to the object as done in the example.
  193. * </p>
  194. *
  195. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  196. *
  197. * @example
  198. * p = p && p.destroy();
  199. *
  200. * @see DebugModelMatrixPrimitive#isDestroyed
  201. */
  202. DebugModelMatrixPrimitive.prototype.destroy = function () {
  203. this._primitive = this._primitive && this._primitive.destroy();
  204. return destroyObject(this);
  205. };
  206. export default DebugModelMatrixPrimitive;