ModelNode.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import Check from "../../Core/Check.js";
  2. import defined from "../../Core/defined.js";
  3. /**
  4. * <div class="notice">
  5. * Use {@link Model#getNode} to get a node from a loaded model. Do not call the constructor directly.
  6. * </div>
  7. *
  8. * A model node with a modifiable transform to allow users to define their
  9. * own animations. While a model's asset can contain animations that target
  10. * a node's transform, this class allows users to change a node's transform
  11. * externally. In this way, animation can be driven by another source, not
  12. * just by the model's asset.
  13. *
  14. * @alias ModelNode
  15. * @internalConstructor
  16. * @class
  17. *
  18. * @example
  19. * const node = model.getNode("Hand");
  20. * node.matrix = Cesium.Matrix4.fromScale(new Cesium.Cartesian3(5.0, 1.0, 1.0), node.matrix);
  21. *
  22. * @see Model#getNode
  23. */
  24. function ModelNode(model, runtimeNode) {
  25. //>>includeStart('debug', pragmas.debug);
  26. Check.typeOf.object("model", model);
  27. Check.typeOf.object("runtimeNode", runtimeNode);
  28. //>>includeEnd('debug')
  29. this._model = model;
  30. this._runtimeNode = runtimeNode;
  31. }
  32. Object.defineProperties(ModelNode.prototype, {
  33. /**
  34. * The value of the <code>name</code> property of this node.
  35. *
  36. * @memberof ModelNode.prototype
  37. *
  38. * @type {string}
  39. * @readonly
  40. */
  41. name: {
  42. get: function () {
  43. return this._runtimeNode._name;
  44. },
  45. },
  46. /**
  47. * The index of the node in the glTF.
  48. *
  49. * @memberof ModelNode.prototype
  50. *
  51. * @type {number}
  52. * @readonly
  53. */
  54. id: {
  55. get: function () {
  56. return this._runtimeNode._id;
  57. },
  58. },
  59. /**
  60. * Determines if this node and its children will be shown.
  61. *
  62. * @memberof ModelNode.prototype
  63. * @type {boolean}
  64. *
  65. * @default true
  66. */
  67. show: {
  68. get: function () {
  69. return this._runtimeNode.show;
  70. },
  71. set: function (value) {
  72. this._runtimeNode.show = value;
  73. },
  74. },
  75. /**
  76. * The node's 4x4 matrix transform from its local coordinates to
  77. * its parent's. Setting the matrix to undefined will restore the
  78. * node's original transform, and allow the node to be animated by
  79. * any animations in the model again.
  80. * <p>
  81. * For changes to take effect, this property must be assigned to;
  82. * setting individual elements of the matrix will not work.
  83. * </p>
  84. *
  85. * @memberof ModelNode.prototype
  86. * @type {Matrix4}
  87. */
  88. matrix: {
  89. get: function () {
  90. return this._runtimeNode.transform;
  91. },
  92. set: function (value) {
  93. if (defined(value)) {
  94. this._runtimeNode.transform = value;
  95. this._runtimeNode.userAnimated = true;
  96. this._model._userAnimationDirty = true;
  97. } else {
  98. this._runtimeNode.transform = this.originalMatrix;
  99. this._runtimeNode.userAnimated = false;
  100. }
  101. },
  102. },
  103. /**
  104. * Gets the node's original 4x4 matrix transform from its local
  105. * coordinates to its parent's, without any node transformations
  106. * or articulations applied.
  107. *
  108. * @memberof ModelNode.prototype
  109. * @type {Matrix4}
  110. */
  111. originalMatrix: {
  112. get: function () {
  113. return this._runtimeNode.originalTransform;
  114. },
  115. },
  116. });
  117. export default ModelNode;