Node.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // A node is the basic container in the scene graph. Its basically a point in
  4. // transformations that can contain child nodes (and inherit transformations),
  5. // and contain renderable entities to draw inside.
  6. //
  7. // Author: Ronen Ness.
  8. // Since: 2017.
  9. //-----------------------------------------------------------------------------
  10. #endregion
  11. using Microsoft.Xna.Framework;
  12. using System.Collections.Generic;
  13. namespace MonoGameSceneGraph
  14. {
  15. /// <summary>
  16. /// MonoGameSceneGraph is the main namespace that contains all the MonoGame-SceneGraph entities.
  17. /// </summary>
  18. [System.Runtime.CompilerServices.CompilerGenerated]
  19. class NamespaceDoc
  20. {
  21. }
  22. /// <summary>
  23. /// A node with transformations, you can attach renderable entities to it, or append
  24. /// child nodes to inherit transformations.
  25. /// </summary>
  26. public class Node
  27. {
  28. /// <summary>
  29. /// Parent node.
  30. /// </summary>
  31. protected Node _parent = null;
  32. /// <summary>
  33. /// Node's transformations.
  34. /// </summary>
  35. protected Transformations _transformations = new Transformations();
  36. /// <summary>
  37. /// Is this node currently visible?
  38. /// </summary>
  39. public bool IsVisible = true;
  40. /// <summary>
  41. /// Optional identifier we can give to nodes.
  42. /// </summary>
  43. public string Identifier;
  44. /// <summary>
  45. /// Optional user data we can attach to nodes.
  46. /// </summary>
  47. public object UserData;
  48. /// <summary>
  49. /// The order in which we apply transformations when building the matrix for this node.
  50. /// </summary>
  51. protected TransformOrder _transformationsOrder = TransformOrder.ScaleRotationPosition;
  52. /// <summary>
  53. /// The order in which we apply rotation when building the matrix for this node.
  54. /// </summary>
  55. protected RotationOrder _rotationOrder = RotationOrder.RotateYXZ;
  56. /// <summary>
  57. /// Local transformations matrix, eg the result of the current local transformations.
  58. /// </summary>
  59. protected Matrix _localTransform = Matrix.Identity;
  60. /// <summary>
  61. /// World transformations matrix, eg the result of the local transformations multiplied with parent transformations.
  62. /// </summary>
  63. protected Matrix _worldTransform = Matrix.Identity;
  64. /// <summary>
  65. /// Child nodes under this node.
  66. /// </summary>
  67. protected List<Node> _childNodes = new List<Node>();
  68. /// <summary>
  69. /// Child entities under this node.
  70. /// </summary>
  71. protected List<IEntity> _childEntities = new List<IEntity>();
  72. /// <summary>
  73. /// Turns true when the transformations of this node changes.
  74. /// </summary>
  75. protected bool _isDirty = true;
  76. /// <summary>
  77. /// This number increment every time we update transformations.
  78. /// We use it to check if our parent's transformations had been changed since last
  79. /// time this node was rendered, and if so, we re-apply parent updated transformations.
  80. /// </summary>
  81. protected uint _transformVersion = 0;
  82. /// <summary>
  83. /// The last transformations version we got from our parent.
  84. /// </summary>
  85. protected uint _parentLastTransformVersion = 0;
  86. /// <summary>
  87. /// Get parent node.
  88. /// </summary>
  89. public Node Parent { get { return _parent; } }
  90. /// <summary>
  91. /// Draw the node and its children.
  92. /// </summary>
  93. public virtual void Draw()
  94. {
  95. // not visible? skip
  96. if (!IsVisible)
  97. {
  98. return;
  99. }
  100. // update transformations (only if needed, testing logic is inside)
  101. UpdateTransformations();
  102. // draw all child nodes
  103. foreach (Node node in _childNodes)
  104. {
  105. node.Draw();
  106. }
  107. // draw all child entities
  108. foreach (IEntity entity in _childEntities)
  109. {
  110. entity.Draw(this, _localTransform, _worldTransform);
  111. }
  112. }
  113. /// <summary>
  114. /// Add an entity to this node.
  115. /// </summary>
  116. /// <param name="entity">Entity to add.</param>
  117. public void AddEntity(IEntity entity)
  118. {
  119. _childEntities.Add(entity);
  120. }
  121. /// <summary>
  122. /// Remove an entity from this node.
  123. /// </summary>
  124. /// <param name="entity">Entity to add.</param>
  125. public void RemoveEntity(IEntity entity)
  126. {
  127. _childEntities.Remove(entity);
  128. }
  129. /// <summary>
  130. /// Add a child node to this node.
  131. /// </summary>
  132. /// <param name="node">Node to add.</param>
  133. public void AddChildNode(Node node)
  134. {
  135. // node already got a parent?
  136. if (node._parent != null)
  137. {
  138. throw new System.Exception("Can't add a node that already have a parent.");
  139. }
  140. // add node to children list
  141. _childNodes.Add(node);
  142. // set self as node's parent, and make sure it will update world transformations next draw call
  143. node._parent = this;
  144. node._parentLastTransformVersion = _transformVersion - 1;
  145. }
  146. /// <summary>
  147. /// Remove a child node from this node.
  148. /// </summary>
  149. /// <param name="node">Node to add.</param>
  150. public void RemoveChildNode(Node node)
  151. {
  152. // make sure the node is a child of this node
  153. if (node._parent != this)
  154. {
  155. throw new System.Exception("Can't remove a node that don't belong to this parent.");
  156. }
  157. // remove node from children list
  158. _childNodes.Remove(node);
  159. // clear node parent pointer and set parent transformations to 1, to make sure it will recalc world transform next draw
  160. node._parent = null;
  161. node._parentLastTransformVersion = 1;
  162. }
  163. /// <summary>
  164. /// Remove this node from its parent.
  165. /// </summary>
  166. public void RemoveFromParent()
  167. {
  168. // don't have a parent?
  169. if (_parent == null)
  170. {
  171. throw new System.Exception("Can't remove an orphan node from parent.");
  172. }
  173. // remove from parent
  174. _parent.RemoveChildNode(this);
  175. }
  176. /// <summary>
  177. /// Set this node as "dirty", eg that we need to update local transformations.
  178. /// </summary>
  179. protected void OnWorldMatrixChange()
  180. {
  181. _isDirty = true;
  182. }
  183. /// <summary>
  184. /// Calc final transformations for current frame.
  185. /// This uses an indicator to know if an update is needed, so no harm is done if you call it multiple times.
  186. /// </summary>
  187. protected void UpdateTransformations()
  188. {
  189. // if local transformations are dirty, we need to update them
  190. if (_isDirty)
  191. {
  192. _localTransform = _transformations.BuildMatrix(_transformationsOrder, _rotationOrder);
  193. _transformVersion++;
  194. }
  195. // if local transformations are dirty, or parent transformations are out-of-date, update global transformations
  196. if (_isDirty ||
  197. (_parent != null && _parentLastTransformVersion != _parent._transformVersion) |
  198. (_parent == null && _parentLastTransformVersion != 0))
  199. {
  200. // if we got parent, apply its transformations
  201. if (_parent != null)
  202. {
  203. _worldTransform = _localTransform * _parent._worldTransform;
  204. _parentLastTransformVersion = _parent._transformVersion;
  205. }
  206. // if not, world transformations are the same as local, and reset parent last transformations version
  207. else
  208. {
  209. _worldTransform = _localTransform;
  210. _parentLastTransformVersion = 0;
  211. }
  212. }
  213. // no longer dirty
  214. _isDirty = false;
  215. }
  216. /// <summary>
  217. /// Return local transformations matrix (note: will recalculate if needed).
  218. /// </summary>
  219. public Matrix LocalTransformations
  220. {
  221. get { UpdateTransformations(); return _localTransform; }
  222. }
  223. /// <summary>
  224. /// Return world transformations matrix (note: will recalculate if needed).
  225. /// </summary>
  226. public Matrix WorldTransformations
  227. {
  228. get { UpdateTransformations(); return _worldTransform; }
  229. }
  230. /// <summary>
  231. /// Reset all local transformations.
  232. /// </summary>
  233. public void ResetTransformations()
  234. {
  235. _transformations = new Transformations();
  236. OnWorldMatrixChange();
  237. }
  238. /// <summary>
  239. /// Get / Set the order in which we apply local transformations in this node.
  240. /// </summary>
  241. public TransformOrder TransformationsOrder
  242. {
  243. get { return _transformationsOrder; }
  244. set { _transformationsOrder = value; OnWorldMatrixChange(); }
  245. }
  246. /// <summary>
  247. /// Get / Set the order in which we apply local rotation in this node.
  248. /// </summary>
  249. public RotationOrder RotationOrder
  250. {
  251. get { return _rotationOrder; }
  252. set { _rotationOrder = value; OnWorldMatrixChange(); }
  253. }
  254. /// <summary>
  255. /// Get / Set node local position.
  256. /// </summary>
  257. public Vector3 Position
  258. {
  259. get { return _transformations.Position; }
  260. set { _transformations.Position = value; OnWorldMatrixChange(); }
  261. }
  262. /// <summary>
  263. /// Get / Set node local scale.
  264. /// </summary>
  265. public Vector3 Scale
  266. {
  267. get { return _transformations.Scale; }
  268. set { _transformations.Scale = value; OnWorldMatrixChange(); }
  269. }
  270. /// <summary>
  271. /// Get / Set node local rotation.
  272. /// </summary>
  273. public Vector3 Rotation
  274. {
  275. get { return _transformations.Rotation; }
  276. set { _transformations.Rotation = value; OnWorldMatrixChange(); }
  277. }
  278. /// <summary>
  279. /// Alias to access rotation X directly.
  280. /// </summary>
  281. public float RotationX
  282. {
  283. get { return _transformations.Rotation.X; }
  284. set { _transformations.Rotation.X = value; OnWorldMatrixChange(); }
  285. }
  286. /// <summary>
  287. /// Alias to access rotation Y directly.
  288. /// </summary>
  289. public float RotationY
  290. {
  291. get { return _transformations.Rotation.Y; }
  292. set { _transformations.Rotation.Y = value; OnWorldMatrixChange(); }
  293. }
  294. /// <summary>
  295. /// Alias to access rotation Z directly.
  296. /// </summary>
  297. public float RotationZ
  298. {
  299. get { return _transformations.Rotation.Z; }
  300. set { _transformations.Rotation.Z = value; OnWorldMatrixChange(); }
  301. }
  302. /// <summary>
  303. /// Alias to access scale X directly.
  304. /// </summary>
  305. public float ScaleX
  306. {
  307. get { return _transformations.Scale.X; }
  308. set { _transformations.Scale.X = value; OnWorldMatrixChange(); }
  309. }
  310. /// <summary>
  311. /// Alias to access scale Y directly.
  312. /// </summary>
  313. public float ScaleY
  314. {
  315. get { return _transformations.Scale.Y; }
  316. set { _transformations.Scale.Y = value; OnWorldMatrixChange(); }
  317. }
  318. /// <summary>
  319. /// Alias to access scale Z directly.
  320. /// </summary>
  321. public float ScaleZ
  322. {
  323. get { return _transformations.Scale.Z; }
  324. set { _transformations.Scale.Z = value; OnWorldMatrixChange(); }
  325. }
  326. /// <summary>
  327. /// Alias to access position X directly.
  328. /// </summary>
  329. public float PositionX
  330. {
  331. get { return _transformations.Position.X; }
  332. set { _transformations.Position.X = value; OnWorldMatrixChange(); }
  333. }
  334. /// <summary>
  335. /// Alias to access position Y directly.
  336. /// </summary>
  337. public float PositionY
  338. {
  339. get { return _transformations.Position.Y; }
  340. set { _transformations.Position.Y = value; OnWorldMatrixChange(); }
  341. }
  342. /// <summary>
  343. /// Alias to access position Z directly.
  344. /// </summary>
  345. public float PositionZ
  346. {
  347. get { return _transformations.Position.Z; }
  348. set { _transformations.Position.Z = value; OnWorldMatrixChange(); }
  349. }
  350. /// <summary>
  351. /// Move position by vector.
  352. /// </summary>
  353. /// <param name="moveBy">Vector to translate by.</param>
  354. public void Translate(Vector3 moveBy)
  355. {
  356. _transformations.Position += moveBy;
  357. OnWorldMatrixChange();
  358. }
  359. }
  360. }