ModelGraphics.js 18 KB

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