ModelArticulation.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import Check from "../../Core/Check.js";
  2. import defaultValue from "../../Core/defaultValue.js";
  3. import defined from "../../Core/defined.js";
  4. import Matrix4 from "../../Core/Matrix4.js";
  5. import ModelArticulationStage from "./ModelArticulationStage.js";
  6. /**
  7. * An in-memory representation of an articulation that affects nodes in the
  8. * {@link ModelSceneGraph}. This is defined in a model by the
  9. * <code>AGI_articulations</code> extension.
  10. *
  11. * @param {object} options An object containing the following options:
  12. * @param {ModelComponents.Articulation} options.articulation The articulation components from the 3D model.
  13. * @param {ModelSceneGraph} options.sceneGraph The scene graph this articulation belongs to.
  14. *
  15. * @alias ModelArticulation
  16. * @constructor
  17. *
  18. * @private
  19. */
  20. function ModelArticulation(options) {
  21. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  22. const articulation = options.articulation;
  23. const sceneGraph = options.sceneGraph;
  24. //>>includeStart('debug', pragmas.debug);
  25. Check.typeOf.object("options.articulation", articulation);
  26. Check.typeOf.object("options.sceneGraph", sceneGraph);
  27. //>>includeEnd('debug');
  28. this._articulation = articulation;
  29. this._sceneGraph = sceneGraph;
  30. this._name = articulation.name;
  31. this._runtimeStages = [];
  32. this._runtimeStagesByName = {};
  33. // Will be populated as the runtime nodes are created
  34. this._runtimeNodes = [];
  35. // Set to true so that the first call to
  36. // ModelSceneGraph.applyArticulations will work.
  37. this._dirty = true;
  38. initialize(this);
  39. }
  40. Object.defineProperties(ModelArticulation.prototype, {
  41. /**
  42. * The internal articulation that this runtime articulation represents.
  43. *
  44. * @memberof ModelArticulation.prototype
  45. * @type {ModelComponents.Articulation}
  46. * @readonly
  47. *
  48. * @private
  49. */
  50. articulation: {
  51. get: function () {
  52. return this._articulation;
  53. },
  54. },
  55. /**
  56. * The scene graph that this articulation belongs to.
  57. *
  58. * @memberof ModelArticulation.prototype
  59. * @type {ModelSceneGraph}
  60. * @readonly
  61. *
  62. * @private
  63. */
  64. sceneGraph: {
  65. get: function () {
  66. return this._sceneGraph;
  67. },
  68. },
  69. /**
  70. * The name of this articulation.
  71. *
  72. * @memberof ModelArticulation.prototype
  73. * @type {string}
  74. * @readonly
  75. *
  76. * @private
  77. */
  78. name: {
  79. get: function () {
  80. return this._name;
  81. },
  82. },
  83. /**
  84. * The runtime stages that belong to this articulation.
  85. *
  86. * @memberof ModelArticulation.prototype
  87. * @type {ModelArticulationStage[]}
  88. * @readonly
  89. *
  90. * @private
  91. */
  92. runtimeStages: {
  93. get: function () {
  94. return this._runtimeStages;
  95. },
  96. },
  97. /**
  98. * The runtime nodes that are affected by this articulation.
  99. *
  100. * @memberof ModelArticulation.prototype
  101. * @type {ModelRuntimeNode[]}
  102. * @readonly
  103. *
  104. * @private
  105. */
  106. runtimeNodes: {
  107. get: function () {
  108. return this._runtimeNodes;
  109. },
  110. },
  111. });
  112. function initialize(runtimeArticulation) {
  113. const articulation = runtimeArticulation.articulation;
  114. const stages = articulation.stages;
  115. const length = stages.length;
  116. const runtimeStages = runtimeArticulation._runtimeStages;
  117. const runtimeStagesByName = runtimeArticulation._runtimeStagesByName;
  118. for (let i = 0; i < length; i++) {
  119. const stage = stages[i];
  120. const runtimeStage = new ModelArticulationStage({
  121. stage: stage,
  122. runtimeArticulation: runtimeArticulation,
  123. });
  124. // Store the stages in an array to preserve the order in which
  125. // they appeared in the 3D model.
  126. runtimeStages.push(runtimeStage);
  127. // Store the stages in a dictionary for retrieval by name.
  128. const stageName = stage.name;
  129. runtimeStagesByName[stageName] = runtimeStage;
  130. }
  131. }
  132. /**
  133. * Sets the current value of an articulation stage.
  134. *
  135. * @param {string} stageName The name of the articulation stage.
  136. * @param {number} value The numeric value of this stage of the articulation.
  137. *
  138. * @private
  139. */
  140. ModelArticulation.prototype.setArticulationStage = function (stageName, value) {
  141. const stage = this._runtimeStagesByName[stageName];
  142. if (defined(stage)) {
  143. stage.currentValue = value;
  144. }
  145. };
  146. const scratchArticulationMatrix = new Matrix4();
  147. const scratchNodeMatrix = new Matrix4();
  148. /**
  149. * Applies the chain of articulation stages to the transform of each node that
  150. * participates in the articulation. This only recomputes the node transforms
  151. * if any stage in the articulation has been modified.
  152. * <p>
  153. * Note that this will overwrite any existing transformations on participating
  154. * nodes.
  155. * </p>
  156. *
  157. * @private
  158. */
  159. ModelArticulation.prototype.apply = function () {
  160. if (!this._dirty) {
  161. return;
  162. }
  163. this._dirty = false;
  164. let articulationMatrix = Matrix4.clone(
  165. Matrix4.IDENTITY,
  166. scratchArticulationMatrix
  167. );
  168. let i;
  169. const stages = this._runtimeStages;
  170. const stagesLength = stages.length;
  171. // Compute the result of the articulation stages...
  172. for (i = 0; i < stagesLength; i++) {
  173. const stage = stages[i];
  174. articulationMatrix = stage.applyStageToMatrix(articulationMatrix);
  175. }
  176. // ...then apply it to the transforms of the affected nodes.
  177. const nodes = this._runtimeNodes;
  178. const nodesLength = nodes.length;
  179. for (i = 0; i < nodesLength; i++) {
  180. const node = nodes[i];
  181. const transform = Matrix4.multiplyTransformation(
  182. node.originalTransform,
  183. articulationMatrix,
  184. scratchNodeMatrix
  185. );
  186. node.transform = transform;
  187. }
  188. };
  189. export default ModelArticulation;