ModelExperimentalNode.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. import Cartesian3 from "../../Core/Cartesian3.js";
  2. import Check from "../../Core/Check.js";
  3. import defaultValue from "../../Core/defaultValue.js";
  4. import defined from "../../Core/defined.js";
  5. import DeveloperError from "../../Core/DeveloperError.js";
  6. import Matrix4 from "../../Core/Matrix4.js";
  7. import InstancingPipelineStage from "./InstancingPipelineStage.js";
  8. import ModelMatrixUpdateStage from "./ModelMatrixUpdateStage.js";
  9. import TranslationRotationScale from "../../Core/TranslationRotationScale.js";
  10. import Quaternion from "../../Core/Quaternion.js";
  11. /**
  12. * An in-memory representation of a node as part of the {@link ModelExperimentalSceneGraph}.
  13. *
  14. *
  15. * @param {Object} options An object containing the following options:
  16. * @param {ModelComponents.Node} options.node The corresponding node components from the 3D model
  17. * @param {Matrix4} options.transform The transform of this node, excluding transforms from the node's ancestors or children.
  18. * @param {Matrix4} options.transformToRoot The product of the transforms of all the node's ancestors, excluding the node's own transform.
  19. * @param {ModelExperimentalSceneGraph} options.sceneGraph The scene graph this node belongs to.
  20. * @param {Number[]} options.children The indices of the children of this node in the runtime nodes array of the scene graph.
  21. *
  22. * @alias ModelExperimentalNode
  23. * @constructor
  24. *
  25. * @private
  26. */
  27. export default function ModelExperimentalNode(options) {
  28. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  29. //>>includeStart('debug', pragmas.debug);
  30. Check.typeOf.object("options.node", options.node);
  31. Check.typeOf.object("options.transform", options.transform);
  32. Check.typeOf.object("options.transformToRoot", options.transformToRoot);
  33. Check.typeOf.object("options.sceneGraph", options.sceneGraph);
  34. Check.typeOf.object("options.children", options.children);
  35. //>>includeEnd('debug');
  36. const sceneGraph = options.sceneGraph;
  37. const transform = options.transform;
  38. const transformToRoot = options.transformToRoot;
  39. const node = options.node;
  40. this._sceneGraph = sceneGraph;
  41. this._children = options.children;
  42. this._node = node;
  43. this._name = node.name; // Helps with debugging
  44. this._originalTransform = Matrix4.clone(transform, this._originalTransform);
  45. this._transform = Matrix4.clone(transform, this._transform);
  46. this._transformToRoot = Matrix4.clone(transformToRoot, this._transformToRoot);
  47. this._originalTransform = Matrix4.clone(transform, this._originalTransform);
  48. const computedTransform = Matrix4.multiply(
  49. transformToRoot,
  50. transform,
  51. new Matrix4()
  52. );
  53. this._computedTransform = computedTransform;
  54. this._transformDirty = false;
  55. // Used for animation
  56. this._transformParameters = defined(node.matrix)
  57. ? undefined
  58. : new TranslationRotationScale(node.translation, node.rotation, node.scale);
  59. this._morphWeights = defined(node.morphWeights)
  60. ? node.morphWeights.slice()
  61. : [];
  62. // Will be set by the scene graph after the skins have been created
  63. this._runtimeSkin = undefined;
  64. this._computedJointMatrices = [];
  65. /**
  66. * Pipeline stages to apply across all the mesh primitives of this node. This
  67. * is an array of classes, each with a static method called
  68. * <code>process()</code>
  69. *
  70. * @type {Object[]}
  71. * @readonly
  72. *
  73. * @private
  74. */
  75. this.pipelineStages = [];
  76. /**
  77. * The mesh primitives that belong to this node
  78. *
  79. * @type {ModelExperimentalPrimitive[]}
  80. * @readonly
  81. *
  82. * @private
  83. */
  84. this.runtimePrimitives = [];
  85. /**
  86. * Update stages to apply to this primitive.
  87. *
  88. * @private
  89. */
  90. this.updateStages = [];
  91. this.configurePipeline();
  92. }
  93. Object.defineProperties(ModelExperimentalNode.prototype, {
  94. /**
  95. * The internal node this runtime node represents.
  96. *
  97. * @type {ModelComponents.Node}
  98. * @readonly
  99. *
  100. * @private
  101. */
  102. node: {
  103. get: function () {
  104. return this._node;
  105. },
  106. },
  107. /**
  108. * The scene graph this node belongs to.
  109. *
  110. * @type {ModelExperimentalSceneGraph}
  111. * @readonly
  112. *
  113. * @private
  114. */
  115. sceneGraph: {
  116. get: function () {
  117. return this._sceneGraph;
  118. },
  119. },
  120. /**
  121. * The indices of the children of this node in the scene graph.
  122. *
  123. * @type {Number[]}
  124. * @readonly
  125. */
  126. children: {
  127. get: function () {
  128. return this._children;
  129. },
  130. },
  131. /**
  132. * The node's local space transform. This can be changed externally so animation
  133. * can be driven by another source, not just an animation in the model's asset.
  134. * <p>
  135. * For changes to take effect, this property must be assigned to;
  136. * setting individual elements of the matrix will not work.
  137. * </p>
  138. *
  139. * @memberof ModelExperimentalNode.prototype
  140. * @type {Matrix4}
  141. */
  142. transform: {
  143. get: function () {
  144. return this._transform;
  145. },
  146. set: function (value) {
  147. if (Matrix4.equals(this._transform, value)) {
  148. return;
  149. }
  150. this._transformDirty = true;
  151. this._transform = Matrix4.clone(value, this._transform);
  152. },
  153. },
  154. /**
  155. * The transforms of all the node's ancestors, not including this node's
  156. * transform.
  157. *
  158. * @see ModelExperimentalNode#computedTransform
  159. *
  160. * @memberof ModelExperimentalNode.prototype
  161. * @type {Matrix4}
  162. * @readonly
  163. */
  164. transformToRoot: {
  165. get: function () {
  166. return this._transformToRoot;
  167. },
  168. },
  169. /**
  170. * A transform from the node's local space to the model's scene graph space.
  171. * This is the product of transformToRoot * transform.
  172. *
  173. * @memberof ModelExperimentalNode.prototype
  174. * @type {Matrix4}
  175. * @readonly
  176. */
  177. computedTransform: {
  178. get: function () {
  179. return this._computedTransform;
  180. },
  181. },
  182. /**
  183. * The node's original transform, as specified in the model. Does not include transformations from the node's ancestors.
  184. *
  185. * @memberof ModelExperimentalNode.prototype
  186. * @type {Matrix4}
  187. * @readonly
  188. */
  189. originalTransform: {
  190. get: function () {
  191. return this._originalTransform;
  192. },
  193. },
  194. /**
  195. * The node's local space translation. This is used internally to allow
  196. * animations in the model's asset to affect the node's properties.
  197. *
  198. * If the node's transformation was originally described using a matrix
  199. * in the model, then this will return undefined.
  200. *
  201. * @memberof ModelExperimentalNode.prototype
  202. * @type {Cartesian3}
  203. *
  204. * @exception {DeveloperError} The translation of a node cannot be set if it was defined using a matrix in the model.
  205. *
  206. * @private
  207. */
  208. translation: {
  209. get: function () {
  210. return defined(this._transformParameters)
  211. ? this._transformParameters.translation
  212. : undefined;
  213. },
  214. set: function (value) {
  215. const transformParameters = this._transformParameters;
  216. //>>includeStart('debug', pragmas.debug);
  217. if (!defined(transformParameters)) {
  218. throw new DeveloperError(
  219. "The translation of a node cannot be set if it was defined using a matrix in the model."
  220. );
  221. }
  222. //>>includeEnd('debug');
  223. const currentTranslation = transformParameters.translation;
  224. if (Cartesian3.equals(currentTranslation, value)) {
  225. return;
  226. }
  227. transformParameters.translation = Cartesian3.clone(
  228. value,
  229. transformParameters.translation
  230. );
  231. updateTransformFromParameters(this, transformParameters);
  232. },
  233. },
  234. /**
  235. * The node's local space rotation. This is used internally to allow
  236. * animations in the model's asset to affect the node's properties.
  237. *
  238. * If the node's transformation was originally described using a matrix
  239. * in the model, then this will return undefined.
  240. *
  241. * @memberof ModelExperimentalNode.prototype
  242. * @type {Quaternion}
  243. *
  244. * @exception {DeveloperError} The rotation of a node cannot be set if it was defined using a matrix in the model.
  245. *
  246. * @private
  247. */
  248. rotation: {
  249. get: function () {
  250. return defined(this._transformParameters)
  251. ? this._transformParameters.rotation
  252. : undefined;
  253. },
  254. set: function (value) {
  255. const transformParameters = this._transformParameters;
  256. //>>includeStart('debug', pragmas.debug);
  257. if (!defined(transformParameters)) {
  258. throw new DeveloperError(
  259. "The rotation of a node cannot be set if it was defined using a matrix in the model."
  260. );
  261. }
  262. //>>includeEnd('debug');
  263. const currentRotation = transformParameters.rotation;
  264. if (Quaternion.equals(currentRotation, value)) {
  265. return;
  266. }
  267. transformParameters.rotation = Quaternion.clone(
  268. value,
  269. transformParameters.rotation
  270. );
  271. updateTransformFromParameters(this, transformParameters);
  272. },
  273. },
  274. /**
  275. * The node's local space scale. This is used internally to allow
  276. * animations in the model's asset to affect the node's properties.
  277. *
  278. * If the node's transformation was originally described using a matrix
  279. * in the model, then this will return undefined.
  280. *
  281. * @memberof ModelExperimentalNode.prototype
  282. * @type {Cartesian3}
  283. *
  284. * @exception {DeveloperError} The scale of a node cannot be set if it was defined using a matrix in the model.
  285. * @private
  286. */
  287. scale: {
  288. get: function () {
  289. return defined(this._transformParameters)
  290. ? this._transformParameters.scale
  291. : undefined;
  292. },
  293. set: function (value) {
  294. const transformParameters = this._transformParameters;
  295. //>>includeStart('debug', pragmas.debug);
  296. if (!defined(transformParameters)) {
  297. throw new DeveloperError(
  298. "The scale of a node cannot be set if it was defined using a matrix in the model."
  299. );
  300. }
  301. //>>includeEnd('debug');
  302. const currentScale = transformParameters.scale;
  303. if (Cartesian3.equals(currentScale, value)) {
  304. return;
  305. }
  306. transformParameters.scale = Cartesian3.clone(
  307. value,
  308. transformParameters.scale
  309. );
  310. updateTransformFromParameters(this, transformParameters);
  311. },
  312. },
  313. /**
  314. * The node's morph weights. This is used internally to allow animations
  315. * in the model's asset to affect the node's properties.
  316. *
  317. * @memberof ModelExperimentalNode.prototype
  318. * @type {Number[]}
  319. *
  320. * @private
  321. */
  322. morphWeights: {
  323. get: function () {
  324. return this._morphWeights;
  325. },
  326. set: function (value) {
  327. const valueLength = value.length;
  328. //>>includeStart('debug', pragmas.debug);
  329. if (this._morphWeights.length !== valueLength) {
  330. throw new DeveloperError(
  331. "value must have the same length as the original weights array."
  332. );
  333. }
  334. //>>includeEnd('debug');
  335. for (let i = 0; i < valueLength; i++) {
  336. this._morphWeights[i] = value[i];
  337. }
  338. },
  339. },
  340. /**
  341. * The skin applied to this node, if it exists.
  342. *
  343. * @memberof ModelExperimentalNode.prototype
  344. * @type {ModelExperimentalSkin}
  345. * @readonly
  346. */
  347. runtimeSkin: {
  348. get: function () {
  349. return this._runtimeSkin;
  350. },
  351. },
  352. /**
  353. * The computed joint matrices of this node, derived from its skin.
  354. *
  355. * @memberof ModelExperimentalNode.prototype
  356. * @type {Matrix4[]}
  357. * @readonly
  358. */
  359. computedJointMatrices: {
  360. get: function () {
  361. return this._computedJointMatrices;
  362. },
  363. },
  364. });
  365. function updateTransformFromParameters(runtimeNode, transformParameters) {
  366. runtimeNode._transformDirty = true;
  367. runtimeNode._transform = Matrix4.fromTranslationRotationScale(
  368. transformParameters,
  369. runtimeNode._transform
  370. );
  371. }
  372. /**
  373. * Returns the child with the given index.
  374. *
  375. * @param {Number} index The index of the child.
  376. *
  377. * @returns {ModelExperimentalNode}
  378. *
  379. * @example
  380. * // Iterate through all children of a runtime node.
  381. * for (let i = 0; i < runtimeNode.children.length; i++)
  382. * {
  383. * const childNode = runtimeNode.getChild(i);
  384. * }
  385. */
  386. ModelExperimentalNode.prototype.getChild = function (index) {
  387. //>>includeStart('debug', pragmas.debug);
  388. Check.typeOf.number("index", index);
  389. if (index < 0 || index >= this.children.length) {
  390. throw new DeveloperError(
  391. "index must be greater than or equal to 0 and less than the number of children."
  392. );
  393. }
  394. //>>includeEnd('debug');
  395. return this.sceneGraph.runtimeNodes[this.children[index]];
  396. };
  397. /**
  398. * Configure the node pipeline stages. If the pipeline needs to be re-run, call
  399. * this method again to ensure the correct sequence of pipeline stages are
  400. * used.
  401. *
  402. * @private
  403. */
  404. ModelExperimentalNode.prototype.configurePipeline = function () {
  405. const node = this.node;
  406. const pipelineStages = this.pipelineStages;
  407. pipelineStages.length = 0;
  408. const updateStages = this.updateStages;
  409. updateStages.length = 0;
  410. if (defined(node.instances)) {
  411. pipelineStages.push(InstancingPipelineStage);
  412. }
  413. updateStages.push(ModelMatrixUpdateStage);
  414. };
  415. /**
  416. * Updates the computed transform used for rendering and instancing.
  417. *
  418. * @private
  419. */
  420. ModelExperimentalNode.prototype.updateComputedTransform = function () {
  421. this._computedTransform = Matrix4.multiply(
  422. this._transformToRoot,
  423. this._transform,
  424. this._computedTransform
  425. );
  426. };
  427. /**
  428. * Updates the joint matrices for this node, where each matrix is computed as
  429. * computedJointMatrix = nodeWorldTransform^(-1) * skinJointMatrix.
  430. *
  431. * @private
  432. */
  433. ModelExperimentalNode.prototype.updateJointMatrices = function () {
  434. const runtimeSkin = this._runtimeSkin;
  435. if (!defined(runtimeSkin)) {
  436. return;
  437. }
  438. runtimeSkin.updateJointMatrices();
  439. const computedJointMatrices = this._computedJointMatrices;
  440. const skinJointMatrices = runtimeSkin.jointMatrices;
  441. const length = skinJointMatrices.length;
  442. for (let i = 0; i < length; i++) {
  443. if (!defined(computedJointMatrices[i])) {
  444. computedJointMatrices[i] = new Matrix4();
  445. }
  446. const nodeWorldTransform = Matrix4.multiplyTransformation(
  447. this.transformToRoot,
  448. this.transform,
  449. computedJointMatrices[i]
  450. );
  451. const inverseNodeWorldTransform = Matrix4.inverseTransformation(
  452. nodeWorldTransform,
  453. computedJointMatrices[i]
  454. );
  455. computedJointMatrices[i] = Matrix4.multiplyTransformation(
  456. inverseNodeWorldTransform,
  457. skinJointMatrices[i],
  458. computedJointMatrices[i]
  459. );
  460. }
  461. };