ModelGraphics.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. import defaultValue from "../Core/defaultValue.js";
  2. import defined from "../Core/defined.js";
  3. import DeveloperError from "../Core/DeveloperError.js";
  4. import Event from "../Core/Event.js";
  5. import createPropertyDescriptor from "./createPropertyDescriptor.js";
  6. import NodeTransformationProperty from "./NodeTransformationProperty.js";
  7. import PropertyBag from "./PropertyBag.js";
  8. function createNodeTransformationProperty(value) {
  9. return new NodeTransformationProperty(value);
  10. }
  11. function createNodeTransformationPropertyBag(value) {
  12. return new PropertyBag(value, createNodeTransformationProperty);
  13. }
  14. function createArticulationStagePropertyBag(value) {
  15. return new PropertyBag(value);
  16. }
  17. /**
  18. * @typedef {Object} ModelGraphics.ConstructorOptions
  19. *
  20. * Initialization options for the ModelGraphics constructor
  21. *
  22. * @property {Property | boolean} [show=true] A boolean Property specifying the visibility of the model.
  23. * @property {Property | string | Resource} [uri] A string or Resource Property specifying the URI of the glTF asset.
  24. * @property {Property | number} [scale=1.0] A numeric Property specifying a uniform linear scale.
  25. * @property {Property | number} [minimumPixelSize=0.0] A numeric Property specifying the approximate minimum pixel size of the model regardless of zoom.
  26. * @property {Property | number} [maximumScale] The maximum scale size of a model. An upper limit for minimumPixelSize.
  27. * @property {Property | boolean} [incrementallyLoadTextures=true] Determine if textures may continue to stream in after the model is loaded.
  28. * @property {Property | boolean} [runAnimations=true] A boolean Property specifying if glTF animations specified in the model should be started.
  29. * @property {Property | boolean} [clampAnimations=true] A boolean Property specifying if glTF animations should hold the last pose for time durations with no keyframes.
  30. * @property {Property | ShadowMode} [shadows=ShadowMode.ENABLED] An enum Property specifying whether the model casts or receives shadows from light sources.
  31. * @property {Property | HeightReference} [heightReference=HeightReference.NONE] A Property specifying what the height is relative to.
  32. * @property {Property | Color} [silhouetteColor=Color.RED] A Property specifying the {@link Color} of the silhouette.
  33. * @property {Property | number} [silhouetteSize=0.0] A numeric Property specifying the size of the silhouette in pixels.
  34. * @property {Property | Color} [color=Color.WHITE] A Property specifying the {@link Color} that blends with the model's rendered color.
  35. * @property {Property | ColorBlendMode} [colorBlendMode=ColorBlendMode.HIGHLIGHT] An enum Property specifying how the color blends with the model.
  36. * @property {Property | number} [colorBlendAmount=0.5] A numeric Property specifying the color strength when the <code>colorBlendMode</code> is <code>MIX</code>. A value of 0.0 results in the model's rendered color while a value of 1.0 results in a solid color, with any value in-between resulting in a mix of the two.
  37. * @property {Property | Cartesian2} [imageBasedLightingFactor=new Cartesian2(1.0, 1.0)] A property specifying the contribution from diffuse and specular image-based lighting.
  38. * @property {Property | Color} [lightColor] A property specifying the light color when shading the model. When <code>undefined</code> the scene's light color is used instead.
  39. * @property {Property | DistanceDisplayCondition} [distanceDisplayCondition] A Property specifying at what distance from the camera that this model will be displayed.
  40. * @property {PropertyBag | Object.<string, TranslationRotationScale>} [nodeTransformations] An object, where keys are names of nodes, and values are {@link TranslationRotationScale} Properties describing the transformation to apply to that node. The transformation is applied after the node's existing transformation as specified in the glTF, and does not replace the node's existing transformation.
  41. * @property {PropertyBag | Object.<string, number>} [articulations] An object, where keys are composed of an articulation name, a single space, and a stage name, and the values are numeric properties.
  42. * @property {Property | ClippingPlaneCollection} [clippingPlanes] A property specifying the {@link ClippingPlaneCollection} used to selectively disable rendering the model.
  43. */
  44. /**
  45. * A 3D model based on {@link https://github.com/KhronosGroup/glTF|glTF}, the runtime asset format for WebGL, OpenGL ES, and OpenGL.
  46. * The position and orientation of the model is determined by the containing {@link Entity}.
  47. * <p>
  48. * Cesium includes support for glTF geometry, materials, animations, and skinning.
  49. * Cameras and lights are not currently supported.
  50. * </p>
  51. *
  52. * @alias ModelGraphics
  53. * @constructor
  54. *
  55. * @param {ModelGraphics.ConstructorOptions} [options] Object describing initialization options
  56. *
  57. * @demo {@link https://sandcastle.cesium.com/index.html?src=3D%20Models.html|Cesium Sandcastle 3D Models Demo}
  58. */
  59. function ModelGraphics(options) {
  60. this._definitionChanged = new Event();
  61. this._show = undefined;
  62. this._showSubscription = undefined;
  63. this._uri = undefined;
  64. this._uriSubscription = undefined;
  65. this._scale = undefined;
  66. this._scaleSubscription = undefined;
  67. this._minimumPixelSize = undefined;
  68. this._minimumPixelSizeSubscription = undefined;
  69. this._maximumScale = undefined;
  70. this._maximumScaleSubscription = undefined;
  71. this._incrementallyLoadTextures = undefined;
  72. this._incrementallyLoadTexturesSubscription = undefined;
  73. this._runAnimations = undefined;
  74. this._runAnimationsSubscription = undefined;
  75. this._clampAnimations = undefined;
  76. this._clampAnimationsSubscription = undefined;
  77. this._shadows = undefined;
  78. this._shadowsSubscription = undefined;
  79. this._heightReference = undefined;
  80. this._heightReferenceSubscription = undefined;
  81. this._silhouetteColor = undefined;
  82. this._silhouetteColorSubscription = undefined;
  83. this._silhouetteSize = undefined;
  84. this._silhouetteSizeSubscription = undefined;
  85. this._color = undefined;
  86. this._colorSubscription = undefined;
  87. this._colorBlendMode = undefined;
  88. this._colorBlendModeSubscription = undefined;
  89. this._colorBlendAmount = undefined;
  90. this._colorBlendAmountSubscription = undefined;
  91. this._imageBasedLightingFactor = undefined;
  92. this._imageBasedLightingFactorSubscription = undefined;
  93. this._lightColor = undefined;
  94. this._lightColorSubscription = undefined;
  95. this._distanceDisplayCondition = undefined;
  96. this._distanceDisplayConditionSubscription = undefined;
  97. this._nodeTransformations = undefined;
  98. this._nodeTransformationsSubscription = undefined;
  99. this._articulations = undefined;
  100. this._articulationsSubscription = undefined;
  101. this._clippingPlanes = undefined;
  102. this._clippingPlanesSubscription = undefined;
  103. this.merge(defaultValue(options, defaultValue.EMPTY_OBJECT));
  104. }
  105. Object.defineProperties(ModelGraphics.prototype, {
  106. /**
  107. * Gets the event that is raised whenever a property or sub-property is changed or modified.
  108. * @memberof ModelGraphics.prototype
  109. * @type {Event}
  110. * @readonly
  111. */
  112. definitionChanged: {
  113. get: function () {
  114. return this._definitionChanged;
  115. },
  116. },
  117. /**
  118. * Gets or sets the boolean Property specifying the visibility of the model.
  119. * @memberof ModelGraphics.prototype
  120. * @type {Property|undefined}
  121. * @default true
  122. */
  123. show: createPropertyDescriptor("show"),
  124. /**
  125. * Gets or sets the string Property specifying the URI of the glTF asset.
  126. * @memberof ModelGraphics.prototype
  127. * @type {Property|undefined}
  128. */
  129. uri: createPropertyDescriptor("uri"),
  130. /**
  131. * Gets or sets the numeric Property specifying a uniform linear scale
  132. * for this model. Values greater than 1.0 increase the size of the model while
  133. * values less than 1.0 decrease it.
  134. * @memberof ModelGraphics.prototype
  135. * @type {Property|undefined}
  136. * @default 1.0
  137. */
  138. scale: createPropertyDescriptor("scale"),
  139. /**
  140. * Gets or sets the numeric Property specifying the approximate minimum
  141. * pixel size of the model regardless of zoom. This can be used to ensure that
  142. * a model is visible even when the viewer zooms out. When <code>0.0</code>,
  143. * no minimum size is enforced.
  144. * @memberof ModelGraphics.prototype
  145. * @type {Property|undefined}
  146. * @default 0.0
  147. */
  148. minimumPixelSize: createPropertyDescriptor("minimumPixelSize"),
  149. /**
  150. * Gets or sets the numeric Property specifying the maximum scale
  151. * size of a model. This property is used as an upper limit for
  152. * {@link ModelGraphics#minimumPixelSize}.
  153. * @memberof ModelGraphics.prototype
  154. * @type {Property|undefined}
  155. */
  156. maximumScale: createPropertyDescriptor("maximumScale"),
  157. /**
  158. * Get or sets the boolean Property specifying whether textures
  159. * may continue to stream in after the model is loaded.
  160. * @memberof ModelGraphics.prototype
  161. * @type {Property|undefined}
  162. */
  163. incrementallyLoadTextures: createPropertyDescriptor(
  164. "incrementallyLoadTextures"
  165. ),
  166. /**
  167. * Gets or sets the boolean Property specifying if glTF animations should be run.
  168. * @memberof ModelGraphics.prototype
  169. * @type {Property|undefined}
  170. * @default true
  171. */
  172. runAnimations: createPropertyDescriptor("runAnimations"),
  173. /**
  174. * Gets or sets the boolean Property specifying if glTF animations should hold the last pose for time durations with no keyframes.
  175. * @memberof ModelGraphics.prototype
  176. * @type {Property|undefined}
  177. * @default true
  178. */
  179. clampAnimations: createPropertyDescriptor("clampAnimations"),
  180. /**
  181. * Get or sets the enum Property specifying whether the model
  182. * casts or receives shadows from light sources.
  183. * @memberof ModelGraphics.prototype
  184. * @type {Property|undefined}
  185. * @default ShadowMode.ENABLED
  186. */
  187. shadows: createPropertyDescriptor("shadows"),
  188. /**
  189. * Gets or sets the Property specifying the {@link HeightReference}.
  190. * @memberof ModelGraphics.prototype
  191. * @type {Property|undefined}
  192. * @default HeightReference.NONE
  193. */
  194. heightReference: createPropertyDescriptor("heightReference"),
  195. /**
  196. * Gets or sets the Property specifying the {@link Color} of the silhouette.
  197. * @memberof ModelGraphics.prototype
  198. * @type {Property|undefined}
  199. * @default Color.RED
  200. */
  201. silhouetteColor: createPropertyDescriptor("silhouetteColor"),
  202. /**
  203. * Gets or sets the numeric Property specifying the size of the silhouette in pixels.
  204. * @memberof ModelGraphics.prototype
  205. * @type {Property|undefined}
  206. * @default 0.0
  207. */
  208. silhouetteSize: createPropertyDescriptor("silhouetteSize"),
  209. /**
  210. * Gets or sets the Property specifying the {@link Color} that blends with the model's rendered color.
  211. * @memberof ModelGraphics.prototype
  212. * @type {Property|undefined}
  213. * @default Color.WHITE
  214. */
  215. color: createPropertyDescriptor("color"),
  216. /**
  217. * Gets or sets the enum Property specifying how the color blends with the model.
  218. * @memberof ModelGraphics.prototype
  219. * @type {Property|undefined}
  220. * @default ColorBlendMode.HIGHLIGHT
  221. */
  222. colorBlendMode: createPropertyDescriptor("colorBlendMode"),
  223. /**
  224. * A numeric Property specifying the color strength when the <code>colorBlendMode</code> is MIX.
  225. * A value of 0.0 results in the model's rendered color while a value of 1.0 results in a solid color, with
  226. * any value in-between resulting in a mix of the two.
  227. * @memberof ModelGraphics.prototype
  228. * @type {Property|undefined}
  229. * @default 0.5
  230. */
  231. colorBlendAmount: createPropertyDescriptor("colorBlendAmount"),
  232. /**
  233. * A property specifying the {@link Cartesian2} used to scale the diffuse and specular image-based lighting contribution to the final color.
  234. * @memberof ModelGraphics.prototype
  235. * @type {Property|undefined}
  236. */
  237. imageBasedLightingFactor: createPropertyDescriptor(
  238. "imageBasedLightingFactor"
  239. ),
  240. /**
  241. * A property specifying the {@link Cartesian3} light color when shading the model. When <code>undefined</code> the scene's light color is used instead.
  242. * @memberOf ModelGraphics.prototype
  243. * @type {Property|undefined}
  244. */
  245. lightColor: createPropertyDescriptor("lightColor"),
  246. /**
  247. * Gets or sets the {@link DistanceDisplayCondition} Property specifying at what distance from the camera that this model will be displayed.
  248. * @memberof ModelGraphics.prototype
  249. * @type {Property|undefined}
  250. */
  251. distanceDisplayCondition: createPropertyDescriptor(
  252. "distanceDisplayCondition"
  253. ),
  254. /**
  255. * Gets or sets the set of node transformations to apply to this model. This is represented as an {@link PropertyBag}, where keys are
  256. * names of nodes, and values are {@link TranslationRotationScale} Properties describing the transformation to apply to that node.
  257. * The transformation is applied after the node's existing transformation as specified in the glTF, and does not replace the node's existing transformation.
  258. * @memberof ModelGraphics.prototype
  259. * @type {PropertyBag}
  260. */
  261. nodeTransformations: createPropertyDescriptor(
  262. "nodeTransformations",
  263. undefined,
  264. createNodeTransformationPropertyBag
  265. ),
  266. /**
  267. * Gets or sets the set of articulation values to apply to this model. This is represented as an {@link PropertyBag}, where keys are
  268. * composed as the name of the articulation, a single space, and the name of the stage.
  269. * @memberof ModelGraphics.prototype
  270. * @type {PropertyBag}
  271. */
  272. articulations: createPropertyDescriptor(
  273. "articulations",
  274. undefined,
  275. createArticulationStagePropertyBag
  276. ),
  277. /**
  278. * A property specifying the {@link ClippingPlaneCollection} used to selectively disable rendering the model.
  279. * @memberof ModelGraphics.prototype
  280. * @type {Property|undefined}
  281. */
  282. clippingPlanes: createPropertyDescriptor("clippingPlanes"),
  283. });
  284. /**
  285. * Duplicates this instance.
  286. *
  287. * @param {ModelGraphics} [result] The object onto which to store the result.
  288. * @returns {ModelGraphics} The modified result parameter or a new instance if one was not provided.
  289. */
  290. ModelGraphics.prototype.clone = function (result) {
  291. if (!defined(result)) {
  292. return new ModelGraphics(this);
  293. }
  294. result.show = this.show;
  295. result.uri = this.uri;
  296. result.scale = this.scale;
  297. result.minimumPixelSize = this.minimumPixelSize;
  298. result.maximumScale = this.maximumScale;
  299. result.incrementallyLoadTextures = this.incrementallyLoadTextures;
  300. result.runAnimations = this.runAnimations;
  301. result.clampAnimations = this.clampAnimations;
  302. result.heightReference = this._heightReference;
  303. result.silhouetteColor = this.silhouetteColor;
  304. result.silhouetteSize = this.silhouetteSize;
  305. result.color = this.color;
  306. result.colorBlendMode = this.colorBlendMode;
  307. result.colorBlendAmount = this.colorBlendAmount;
  308. result.imageBasedLightingFactor = this.imageBasedLightingFactor;
  309. result.lightColor = this.lightColor;
  310. result.distanceDisplayCondition = this.distanceDisplayCondition;
  311. result.nodeTransformations = this.nodeTransformations;
  312. result.articulations = this.articulations;
  313. result.clippingPlanes = this.clippingPlanes;
  314. return result;
  315. };
  316. /**
  317. * Assigns each unassigned property on this object to the value
  318. * of the same property on the provided source object.
  319. *
  320. * @param {ModelGraphics} source The object to be merged into this object.
  321. */
  322. ModelGraphics.prototype.merge = function (source) {
  323. //>>includeStart('debug', pragmas.debug);
  324. if (!defined(source)) {
  325. throw new DeveloperError("source is required.");
  326. }
  327. //>>includeEnd('debug');
  328. this.show = defaultValue(this.show, source.show);
  329. this.uri = defaultValue(this.uri, source.uri);
  330. this.scale = defaultValue(this.scale, source.scale);
  331. this.minimumPixelSize = defaultValue(
  332. this.minimumPixelSize,
  333. source.minimumPixelSize
  334. );
  335. this.maximumScale = defaultValue(this.maximumScale, source.maximumScale);
  336. this.incrementallyLoadTextures = defaultValue(
  337. this.incrementallyLoadTextures,
  338. source.incrementallyLoadTextures
  339. );
  340. this.runAnimations = defaultValue(this.runAnimations, source.runAnimations);
  341. this.clampAnimations = defaultValue(
  342. this.clampAnimations,
  343. source.clampAnimations
  344. );
  345. this.shadows = defaultValue(this.shadows, source.shadows);
  346. this.heightReference = defaultValue(
  347. this.heightReference,
  348. source.heightReference
  349. );
  350. this.silhouetteColor = defaultValue(
  351. this.silhouetteColor,
  352. source.silhouetteColor
  353. );
  354. this.silhouetteSize = defaultValue(
  355. this.silhouetteSize,
  356. source.silhouetteSize
  357. );
  358. this.color = defaultValue(this.color, source.color);
  359. this.colorBlendMode = defaultValue(
  360. this.colorBlendMode,
  361. source.colorBlendMode
  362. );
  363. this.colorBlendAmount = defaultValue(
  364. this.colorBlendAmount,
  365. source.colorBlendAmount
  366. );
  367. this.imageBasedLightingFactor = defaultValue(
  368. this.imageBasedLightingFactor,
  369. source.imageBasedLightingFactor
  370. );
  371. this.lightColor = defaultValue(this.lightColor, source.lightColor);
  372. this.distanceDisplayCondition = defaultValue(
  373. this.distanceDisplayCondition,
  374. source.distanceDisplayCondition
  375. );
  376. this.clippingPlanes = defaultValue(
  377. this.clippingPlanes,
  378. source.clippingPlanes
  379. );
  380. const sourceNodeTransformations = source.nodeTransformations;
  381. if (defined(sourceNodeTransformations)) {
  382. const targetNodeTransformations = this.nodeTransformations;
  383. if (defined(targetNodeTransformations)) {
  384. targetNodeTransformations.merge(sourceNodeTransformations);
  385. } else {
  386. this.nodeTransformations = new PropertyBag(
  387. sourceNodeTransformations,
  388. createNodeTransformationProperty
  389. );
  390. }
  391. }
  392. const sourceArticulations = source.articulations;
  393. if (defined(sourceArticulations)) {
  394. const targetArticulations = this.articulations;
  395. if (defined(targetArticulations)) {
  396. targetArticulations.merge(sourceArticulations);
  397. } else {
  398. this.articulations = new PropertyBag(sourceArticulations);
  399. }
  400. }
  401. };
  402. export default ModelGraphics;