ModelNode.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import Matrix4 from "../Core/Matrix4.js";
  2. /**
  3. * A model node with a transform for user-defined animations. A glTF asset can
  4. * contain animations that target a node's transform. This class allows
  5. * changing a node's transform externally so animation can be driven by another
  6. * source, not just an animation in the glTF asset.
  7. * <p>
  8. * Use {@link Model#getNode} to create an instance.
  9. * </p>
  10. *
  11. * @alias ModelNode
  12. * @internalConstructor
  13. * @class
  14. *
  15. * @example
  16. * const node = model.getNode('LOD3sp');
  17. * node.matrix = Cesium.Matrix4.fromScale(new Cesium.Cartesian3(5.0, 1.0, 1.0), node.matrix);
  18. *
  19. * @see Model#getNode
  20. */
  21. function ModelNode(model, node, runtimeNode, id, matrix) {
  22. this._model = model;
  23. this._runtimeNode = runtimeNode;
  24. this._name = node.name;
  25. this._id = id;
  26. /**
  27. * @private
  28. */
  29. this.useMatrix = false;
  30. this._show = true;
  31. this._matrix = Matrix4.clone(matrix);
  32. this._originalMatrix = Matrix4.clone(matrix);
  33. }
  34. Object.defineProperties(ModelNode.prototype, {
  35. /**
  36. * The value of the <code>name</code> property of this node.
  37. *
  38. * @memberof ModelNode.prototype
  39. *
  40. * @type {String}
  41. * @readonly
  42. */
  43. name: {
  44. get: function () {
  45. return this._name;
  46. },
  47. },
  48. /**
  49. * The index of the node.
  50. *
  51. * @memberof ModelNode.prototype
  52. *
  53. * @type {String}
  54. * @readonly
  55. */
  56. id: {
  57. get: function () {
  58. return this._id;
  59. },
  60. },
  61. /**
  62. * Determines if this node and its children will be shown.
  63. *
  64. * @memberof ModelNode.prototype
  65. * @type {Boolean}
  66. *
  67. * @default true
  68. */
  69. show: {
  70. get: function () {
  71. return this._show;
  72. },
  73. set: function (value) {
  74. if (this._show !== value) {
  75. this._show = value;
  76. this._model._perNodeShowDirty = true;
  77. }
  78. },
  79. },
  80. /**
  81. * The node's 4x4 matrix transform from its local coordinates to
  82. * its parent's.
  83. * <p>
  84. * For changes to take effect, this property must be assigned to;
  85. * setting individual elements of the matrix will not work.
  86. * </p>
  87. *
  88. * @memberof ModelNode.prototype
  89. * @type {Matrix4}
  90. */
  91. matrix: {
  92. get: function () {
  93. return this._matrix;
  94. },
  95. set: function (value) {
  96. this._matrix = Matrix4.clone(value, this._matrix);
  97. this.useMatrix = true;
  98. const model = this._model;
  99. model._cesiumAnimationsDirty = true;
  100. this._runtimeNode.dirtyNumber = model._maxDirtyNumber;
  101. },
  102. },
  103. /**
  104. * Gets the node's original 4x4 matrix transform from its local coordinates to
  105. * its parent's, without any node transformations or articulations applied.
  106. *
  107. * @memberof ModelNode.prototype
  108. * @type {Matrix4}
  109. */
  110. originalMatrix: {
  111. get: function () {
  112. return this._originalMatrix;
  113. },
  114. },
  115. });
  116. /**
  117. * @private
  118. */
  119. ModelNode.prototype.setMatrix = function (matrix) {
  120. // Update matrix but do not set the dirty flag since this is used internally
  121. // to keep the matrix in-sync during a glTF animation.
  122. Matrix4.clone(matrix, this._matrix);
  123. };
  124. export default ModelNode;