GeometryInstance.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import defaultValue from "./defaultValue.js";
  2. import defined from "./defined.js";
  3. import DeveloperError from "./DeveloperError.js";
  4. import Matrix4 from "./Matrix4.js";
  5. /**
  6. * Geometry instancing allows one {@link Geometry} object to be positions in several
  7. * different locations and colored uniquely. For example, one {@link BoxGeometry} can
  8. * be instanced several times, each with a different <code>modelMatrix</code> to change
  9. * its position, rotation, and scale.
  10. *
  11. * @alias GeometryInstance
  12. * @constructor
  13. *
  14. * @param {object} options Object with the following properties:
  15. * @param {Geometry|GeometryFactory} options.geometry The geometry to instance.
  16. * @param {Matrix4} [options.modelMatrix=Matrix4.IDENTITY] The model matrix that transforms to transform the geometry from model to world coordinates.
  17. * @param {object} [options.id] A user-defined object to return when the instance is picked with {@link Scene#pick} or get/set per-instance attributes with {@link Primitive#getGeometryInstanceAttributes}.
  18. * @param {object} [options.attributes] Per-instance attributes like a show or color attribute shown in the example below.
  19. *
  20. *
  21. * @example
  22. * // Create geometry for a box, and two instances that refer to it.
  23. * // One instance positions the box on the bottom and colored aqua.
  24. * // The other instance positions the box on the top and color white.
  25. * const geometry = Cesium.BoxGeometry.fromDimensions({
  26. * vertexFormat : Cesium.VertexFormat.POSITION_AND_NORMAL,
  27. * dimensions : new Cesium.Cartesian3(1000000.0, 1000000.0, 500000.0)
  28. * });
  29. * const instanceBottom = new Cesium.GeometryInstance({
  30. * geometry : geometry,
  31. * modelMatrix : Cesium.Matrix4.multiplyByTranslation(Cesium.Transforms.eastNorthUpToFixedFrame(
  32. * Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883)), new Cesium.Cartesian3(0.0, 0.0, 1000000.0), new Cesium.Matrix4()),
  33. * attributes : {
  34. * color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.AQUA)
  35. * },
  36. * id : 'bottom'
  37. * });
  38. * const instanceTop = new Cesium.GeometryInstance({
  39. * geometry : geometry,
  40. * modelMatrix : Cesium.Matrix4.multiplyByTranslation(Cesium.Transforms.eastNorthUpToFixedFrame(
  41. * Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883)), new Cesium.Cartesian3(0.0, 0.0, 3000000.0), new Cesium.Matrix4()),
  42. * attributes : {
  43. * color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.AQUA)
  44. * },
  45. * id : 'top'
  46. * });
  47. *
  48. * @see Geometry
  49. */
  50. function GeometryInstance(options) {
  51. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  52. //>>includeStart('debug', pragmas.debug);
  53. if (!defined(options.geometry)) {
  54. throw new DeveloperError("options.geometry is required.");
  55. }
  56. //>>includeEnd('debug');
  57. /**
  58. * The geometry being instanced.
  59. *
  60. * @type Geometry
  61. *
  62. * @default undefined
  63. */
  64. this.geometry = options.geometry;
  65. /**
  66. * The 4x4 transformation matrix that transforms the geometry from model to world coordinates.
  67. * When this is the identity matrix, the geometry is drawn in world coordinates, i.e., Earth's WGS84 coordinates.
  68. * Local reference frames can be used by providing a different transformation matrix, like that returned
  69. * by {@link Transforms.eastNorthUpToFixedFrame}.
  70. *
  71. * @type Matrix4
  72. *
  73. * @default Matrix4.IDENTITY
  74. */
  75. this.modelMatrix = Matrix4.clone(
  76. defaultValue(options.modelMatrix, Matrix4.IDENTITY)
  77. );
  78. /**
  79. * User-defined object returned when the instance is picked or used to get/set per-instance attributes.
  80. *
  81. * @type {object}
  82. *
  83. * @default undefined
  84. *
  85. * @see Scene#pick
  86. * @see Primitive#getGeometryInstanceAttributes
  87. */
  88. this.id = options.id;
  89. /**
  90. * Used for picking primitives that wrap geometry instances.
  91. *
  92. * @private
  93. */
  94. this.pickPrimitive = options.pickPrimitive;
  95. /**
  96. * Per-instance attributes like {@link ColorGeometryInstanceAttribute} or {@link ShowGeometryInstanceAttribute}.
  97. * {@link Geometry} attributes varying per vertex; these attributes are constant for the entire instance.
  98. *
  99. * @type {object}
  100. *
  101. * @default undefined
  102. */
  103. this.attributes = defaultValue(options.attributes, {});
  104. /**
  105. * @private
  106. */
  107. this.westHemisphereGeometry = undefined;
  108. /**
  109. * @private
  110. */
  111. this.eastHemisphereGeometry = undefined;
  112. }
  113. export default GeometryInstance;