ModelGraphics.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. * @property {Property | CustomShader} [customShader] A property specifying the {@link CustomShader} to apply to this model.
  44. */
  45. /**
  46. * A 3D model based on {@link https://github.com/KhronosGroup/glTF|glTF}, the runtime asset format for WebGL, OpenGL ES, and OpenGL.
  47. * The position and orientation of the model is determined by the containing {@link Entity}.
  48. * <p>
  49. * Cesium includes support for glTF geometry, materials, animations, and skinning.
  50. * Cameras and lights are not currently supported.
  51. * </p>
  52. *
  53. * @alias ModelGraphics
  54. * @constructor
  55. *
  56. * @param {ModelGraphics.ConstructorOptions} [options] Object describing initialization options
  57. *
  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._customShader = undefined;
  105. this._customShaderSubscription = undefined;
  106. this.merge(defaultValue(options, defaultValue.EMPTY_OBJECT));
  107. }
  108. Object.defineProperties(ModelGraphics.prototype, {
  109. /**
  110. * Gets the event that is raised whenever a property or sub-property is changed or modified.
  111. * @memberof ModelGraphics.prototype
  112. * @type {Event}
  113. * @readonly
  114. */
  115. definitionChanged: {
  116. get: function () {
  117. return this._definitionChanged;
  118. },
  119. },
  120. /**
  121. * Gets or sets the boolean Property specifying the visibility of the model.
  122. * @memberof ModelGraphics.prototype
  123. * @type {Property|undefined}
  124. * @default true
  125. */
  126. show: createPropertyDescriptor("show"),
  127. /**
  128. * Gets or sets the string Property specifying the URI of the glTF asset.
  129. * @memberof ModelGraphics.prototype
  130. * @type {Property|undefined}
  131. */
  132. uri: createPropertyDescriptor("uri"),
  133. /**
  134. * Gets or sets the numeric Property specifying a uniform linear scale
  135. * for this model. Values greater than 1.0 increase the size of the model while
  136. * values less than 1.0 decrease it.
  137. * @memberof ModelGraphics.prototype
  138. * @type {Property|undefined}
  139. * @default 1.0
  140. */
  141. scale: createPropertyDescriptor("scale"),
  142. /**
  143. * Gets or sets the numeric Property specifying the approximate minimum
  144. * pixel size of the model regardless of zoom. This can be used to ensure that
  145. * a model is visible even when the viewer zooms out. When <code>0.0</code>,
  146. * no minimum size is enforced.
  147. * @memberof ModelGraphics.prototype
  148. * @type {Property|undefined}
  149. * @default 0.0
  150. */
  151. minimumPixelSize: createPropertyDescriptor("minimumPixelSize"),
  152. /**
  153. * Gets or sets the numeric Property specifying the maximum scale
  154. * size of a model. This property is used as an upper limit for
  155. * {@link ModelGraphics#minimumPixelSize}.
  156. * @memberof ModelGraphics.prototype
  157. * @type {Property|undefined}
  158. */
  159. maximumScale: createPropertyDescriptor("maximumScale"),
  160. /**
  161. * Get or sets the boolean Property specifying whether textures
  162. * may continue to stream in after the model is loaded.
  163. * @memberof ModelGraphics.prototype
  164. * @type {Property|undefined}
  165. */
  166. incrementallyLoadTextures: createPropertyDescriptor(
  167. "incrementallyLoadTextures"
  168. ),
  169. /**
  170. * Gets or sets the boolean Property specifying if glTF animations should be run.
  171. * @memberof ModelGraphics.prototype
  172. * @type {Property|undefined}
  173. * @default true
  174. */
  175. runAnimations: createPropertyDescriptor("runAnimations"),
  176. /**
  177. * Gets or sets the boolean Property specifying if glTF animations should hold the last pose for time durations with no keyframes.
  178. * @memberof ModelGraphics.prototype
  179. * @type {Property|undefined}
  180. * @default true
  181. */
  182. clampAnimations: createPropertyDescriptor("clampAnimations"),
  183. /**
  184. * Get or sets the enum Property specifying whether the model
  185. * casts or receives shadows from light sources.
  186. * @memberof ModelGraphics.prototype
  187. * @type {Property|undefined}
  188. * @default ShadowMode.ENABLED
  189. */
  190. shadows: createPropertyDescriptor("shadows"),
  191. /**
  192. * Gets or sets the Property specifying the {@link HeightReference}.
  193. * @memberof ModelGraphics.prototype
  194. * @type {Property|undefined}
  195. * @default HeightReference.NONE
  196. */
  197. heightReference: createPropertyDescriptor("heightReference"),
  198. /**
  199. * Gets or sets the Property specifying the {@link Color} of the silhouette.
  200. * @memberof ModelGraphics.prototype
  201. * @type {Property|undefined}
  202. * @default Color.RED
  203. */
  204. silhouetteColor: createPropertyDescriptor("silhouetteColor"),
  205. /**
  206. * Gets or sets the numeric Property specifying the size of the silhouette in pixels.
  207. * @memberof ModelGraphics.prototype
  208. * @type {Property|undefined}
  209. * @default 0.0
  210. */
  211. silhouetteSize: createPropertyDescriptor("silhouetteSize"),
  212. /**
  213. * Gets or sets the Property specifying the {@link Color} that blends with the model's rendered color.
  214. * @memberof ModelGraphics.prototype
  215. * @type {Property|undefined}
  216. * @default Color.WHITE
  217. */
  218. color: createPropertyDescriptor("color"),
  219. /**
  220. * Gets or sets the enum Property specifying how the color blends with the model.
  221. * @memberof ModelGraphics.prototype
  222. * @type {Property|undefined}
  223. * @default ColorBlendMode.HIGHLIGHT
  224. */
  225. colorBlendMode: createPropertyDescriptor("colorBlendMode"),
  226. /**
  227. * A numeric Property specifying the color strength when the <code>colorBlendMode</code> is MIX.
  228. * A value of 0.0 results in the model's rendered color while a value of 1.0 results in a solid color, with
  229. * any value in-between resulting in a mix of the two.
  230. * @memberof ModelGraphics.prototype
  231. * @type {Property|undefined}
  232. * @default 0.5
  233. */
  234. colorBlendAmount: createPropertyDescriptor("colorBlendAmount"),
  235. /**
  236. * A property specifying the {@link Cartesian2} used to scale the diffuse and specular image-based lighting contribution to the final color.
  237. * @memberof ModelGraphics.prototype
  238. * @type {Property|undefined}
  239. */
  240. imageBasedLightingFactor: createPropertyDescriptor(
  241. "imageBasedLightingFactor"
  242. ),
  243. /**
  244. * A property specifying the {@link Cartesian3} light color when shading the model. When <code>undefined</code> the scene's light color is used instead.
  245. * @memberOf ModelGraphics.prototype
  246. * @type {Property|undefined}
  247. */
  248. lightColor: createPropertyDescriptor("lightColor"),
  249. /**
  250. * Gets or sets the {@link DistanceDisplayCondition} Property specifying at what distance from the camera that this model will be displayed.
  251. * @memberof ModelGraphics.prototype
  252. * @type {Property|undefined}
  253. */
  254. distanceDisplayCondition: createPropertyDescriptor(
  255. "distanceDisplayCondition"
  256. ),
  257. /**
  258. * Gets or sets the set of node transformations to apply to this model. This is represented as an {@link PropertyBag}, where keys are
  259. * names of nodes, and values are {@link TranslationRotationScale} Properties describing the transformation to apply to that node.
  260. * The transformation is applied after the node's existing transformation as specified in the glTF, and does not replace the node's existing transformation.
  261. * @memberof ModelGraphics.prototype
  262. * @type {PropertyBag}
  263. */
  264. nodeTransformations: createPropertyDescriptor(
  265. "nodeTransformations",
  266. undefined,
  267. createNodeTransformationPropertyBag
  268. ),
  269. /**
  270. * Gets or sets the set of articulation values to apply to this model. This is represented as an {@link PropertyBag}, where keys are
  271. * composed as the name of the articulation, a single space, and the name of the stage.
  272. * @memberof ModelGraphics.prototype
  273. * @type {PropertyBag}
  274. */
  275. articulations: createPropertyDescriptor(
  276. "articulations",
  277. undefined,
  278. createArticulationStagePropertyBag
  279. ),
  280. /**
  281. * A property specifying the {@link ClippingPlaneCollection} used to selectively disable rendering the model.
  282. * @memberof ModelGraphics.prototype
  283. * @type {Property|undefined}
  284. */
  285. clippingPlanes: createPropertyDescriptor("clippingPlanes"),
  286. /**
  287. * Gets or sets the {@link CustomShader} to apply to this model. When <code>undefined</code>, no custom shader code is used.
  288. * @memberof ModelGraphics.prototype
  289. * @type {Property|undefined}
  290. */
  291. customShader: createPropertyDescriptor("customShader"),
  292. });
  293. /**
  294. * Duplicates this instance.
  295. *
  296. * @param {ModelGraphics} [result] The object onto which to store the result.
  297. * @returns {ModelGraphics} The modified result parameter or a new instance if one was not provided.
  298. */
  299. ModelGraphics.prototype.clone = function (result) {
  300. if (!defined(result)) {
  301. return new ModelGraphics(this);
  302. }
  303. result.show = this.show;
  304. result.uri = this.uri;
  305. result.scale = this.scale;
  306. result.minimumPixelSize = this.minimumPixelSize;
  307. result.maximumScale = this.maximumScale;
  308. result.incrementallyLoadTextures = this.incrementallyLoadTextures;
  309. result.runAnimations = this.runAnimations;
  310. result.clampAnimations = this.clampAnimations;
  311. result.heightReference = this._heightReference;
  312. result.silhouetteColor = this.silhouetteColor;
  313. result.silhouetteSize = this.silhouetteSize;
  314. result.color = this.color;
  315. result.colorBlendMode = this.colorBlendMode;
  316. result.colorBlendAmount = this.colorBlendAmount;
  317. result.imageBasedLightingFactor = this.imageBasedLightingFactor;
  318. result.lightColor = this.lightColor;
  319. result.distanceDisplayCondition = this.distanceDisplayCondition;
  320. result.nodeTransformations = this.nodeTransformations;
  321. result.articulations = this.articulations;
  322. result.clippingPlanes = this.clippingPlanes;
  323. result.customShader = this.customShader;
  324. return result;
  325. };
  326. /**
  327. * Assigns each unassigned property on this object to the value
  328. * of the same property on the provided source object.
  329. *
  330. * @param {ModelGraphics} source The object to be merged into this object.
  331. */
  332. ModelGraphics.prototype.merge = function (source) {
  333. //>>includeStart('debug', pragmas.debug);
  334. if (!defined(source)) {
  335. throw new DeveloperError("source is required.");
  336. }
  337. //>>includeEnd('debug');
  338. this.show = defaultValue(this.show, source.show);
  339. this.uri = defaultValue(this.uri, source.uri);
  340. this.scale = defaultValue(this.scale, source.scale);
  341. this.minimumPixelSize = defaultValue(
  342. this.minimumPixelSize,
  343. source.minimumPixelSize
  344. );
  345. this.maximumScale = defaultValue(this.maximumScale, source.maximumScale);
  346. this.incrementallyLoadTextures = defaultValue(
  347. this.incrementallyLoadTextures,
  348. source.incrementallyLoadTextures
  349. );
  350. this.runAnimations = defaultValue(this.runAnimations, source.runAnimations);
  351. this.clampAnimations = defaultValue(
  352. this.clampAnimations,
  353. source.clampAnimations
  354. );
  355. this.shadows = defaultValue(this.shadows, source.shadows);
  356. this.heightReference = defaultValue(
  357. this.heightReference,
  358. source.heightReference
  359. );
  360. this.silhouetteColor = defaultValue(
  361. this.silhouetteColor,
  362. source.silhouetteColor
  363. );
  364. this.silhouetteSize = defaultValue(
  365. this.silhouetteSize,
  366. source.silhouetteSize
  367. );
  368. this.color = defaultValue(this.color, source.color);
  369. this.colorBlendMode = defaultValue(
  370. this.colorBlendMode,
  371. source.colorBlendMode
  372. );
  373. this.colorBlendAmount = defaultValue(
  374. this.colorBlendAmount,
  375. source.colorBlendAmount
  376. );
  377. this.imageBasedLightingFactor = defaultValue(
  378. this.imageBasedLightingFactor,
  379. source.imageBasedLightingFactor
  380. );
  381. this.lightColor = defaultValue(this.lightColor, source.lightColor);
  382. this.distanceDisplayCondition = defaultValue(
  383. this.distanceDisplayCondition,
  384. source.distanceDisplayCondition
  385. );
  386. this.clippingPlanes = defaultValue(
  387. this.clippingPlanes,
  388. source.clippingPlanes
  389. );
  390. this.customShader = defaultValue(this.customShader, source.customShader);
  391. const sourceNodeTransformations = source.nodeTransformations;
  392. if (defined(sourceNodeTransformations)) {
  393. const targetNodeTransformations = this.nodeTransformations;
  394. if (defined(targetNodeTransformations)) {
  395. targetNodeTransformations.merge(sourceNodeTransformations);
  396. } else {
  397. this.nodeTransformations = new PropertyBag(
  398. sourceNodeTransformations,
  399. createNodeTransformationProperty
  400. );
  401. }
  402. }
  403. const sourceArticulations = source.articulations;
  404. if (defined(sourceArticulations)) {
  405. const targetArticulations = this.articulations;
  406. if (defined(targetArticulations)) {
  407. targetArticulations.merge(sourceArticulations);
  408. } else {
  409. this.articulations = new PropertyBag(sourceArticulations);
  410. }
  411. }
  412. };
  413. export default ModelGraphics;