Node.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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
  143. node.SetParent(this);
  144. }
  145. /// <summary>
  146. /// Remove a child node from this node.
  147. /// </summary>
  148. /// <param name="node">Node to add.</param>
  149. public void RemoveChildNode(Node node)
  150. {
  151. // make sure the node is a child of this node
  152. if (node._parent != this)
  153. {
  154. throw new System.Exception("Can't remove a node that don't belong to this parent.");
  155. }
  156. // remove node from children list
  157. _childNodes.Remove(node);
  158. // clear node parent
  159. node.SetParent(null);
  160. }
  161. /// <summary>
  162. /// Find and return first child node by identifier.
  163. /// </summary>
  164. /// <param name="identifier">Node identifier to search for.</param>
  165. /// <param name="searchInChildren">If true, will also search recurisvely in children.</param>
  166. /// <returns>Node with given identifier or null if not found.</returns>
  167. public Node FindChildNode(string identifier, bool searchInChildren = true)
  168. {
  169. foreach (Node node in _childNodes)
  170. {
  171. // search in direct children
  172. if (node.Identifier == identifier)
  173. {
  174. return node;
  175. }
  176. // recursive search
  177. if (searchInChildren)
  178. {
  179. Node foundInChild = node.FindChildNode(identifier, searchInChildren);
  180. if (foundInChild != null)
  181. {
  182. return foundInChild;
  183. }
  184. }
  185. }
  186. // if got here it means we didn't find any child node with given identifier
  187. return null;
  188. }
  189. /// <summary>
  190. /// Remove this node from its parent.
  191. /// </summary>
  192. public void RemoveFromParent()
  193. {
  194. // don't have a parent?
  195. if (_parent == null)
  196. {
  197. throw new System.Exception("Can't remove an orphan node from parent.");
  198. }
  199. // remove from parent
  200. _parent.RemoveChildNode(this);
  201. }
  202. /// <summary>
  203. /// Set this node as "dirty", eg that we need to update local transformations.
  204. /// </summary>
  205. protected virtual void OnWorldMatrixChange()
  206. {
  207. _isDirty = true;
  208. }
  209. /// <summary>
  210. /// Set the parent of this node.
  211. /// </summary>
  212. /// <param name="newParent">New parent node to set, or null for no parent.</param>
  213. protected virtual void SetParent(Node newParent)
  214. {
  215. // set parent
  216. _parent = newParent;
  217. // set our parents last transformations version to make sure we'll update world transformations next frame.
  218. _parentLastTransformVersion = newParent != null ? newParent._transformVersion - 1 : 1;
  219. }
  220. /// <summary>
  221. /// Calc final transformations for current frame.
  222. /// This uses an indicator to know if an update is needed, so no harm is done if you call it multiple times.
  223. /// </summary>
  224. protected virtual void UpdateTransformations()
  225. {
  226. // if local transformations are dirty, we need to update them
  227. if (_isDirty)
  228. {
  229. _localTransform = _transformations.BuildMatrix(_transformationsOrder, _rotationOrder);
  230. }
  231. // if local transformations are dirty, or parent transformations are out-of-date, update world transformations
  232. if (_isDirty ||
  233. (_parent != null && _parentLastTransformVersion != _parent._transformVersion) |
  234. (_parent == null && _parentLastTransformVersion != 0))
  235. {
  236. // if we got parent, apply its transformations
  237. if (_parent != null)
  238. {
  239. _worldTransform = _localTransform * _parent._worldTransform;
  240. _parentLastTransformVersion = _parent._transformVersion;
  241. }
  242. // if not, world transformations are the same as local, and reset parent last transformations version
  243. else
  244. {
  245. _worldTransform = _localTransform;
  246. _parentLastTransformVersion = 0;
  247. }
  248. // increase transformation version
  249. _transformVersion++;
  250. }
  251. // no longer dirty
  252. _isDirty = false;
  253. }
  254. /// <summary>
  255. /// Return local transformations matrix (note: will recalculate if needed).
  256. /// </summary>
  257. public Matrix LocalTransformations
  258. {
  259. get { UpdateTransformations(); return _localTransform; }
  260. }
  261. /// <summary>
  262. /// Return world transformations matrix (note: will recalculate if needed).
  263. /// </summary>
  264. public Matrix WorldTransformations
  265. {
  266. get { UpdateTransformations(); return _worldTransform; }
  267. }
  268. /// <summary>
  269. /// Reset all local transformations.
  270. /// </summary>
  271. public void ResetTransformations()
  272. {
  273. _transformations = new Transformations();
  274. OnWorldMatrixChange();
  275. }
  276. /// <summary>
  277. /// Get / Set the order in which we apply local transformations in this node.
  278. /// </summary>
  279. public TransformOrder TransformationsOrder
  280. {
  281. get { return _transformationsOrder; }
  282. set { _transformationsOrder = value; OnWorldMatrixChange(); }
  283. }
  284. /// <summary>
  285. /// Get / Set the order in which we apply local rotation in this node.
  286. /// </summary>
  287. public RotationOrder RotationOrder
  288. {
  289. get { return _rotationOrder; }
  290. set { _rotationOrder = value; OnWorldMatrixChange(); }
  291. }
  292. /// <summary>
  293. /// Get / Set node local position.
  294. /// </summary>
  295. public Vector3 Position
  296. {
  297. get { return _transformations.Position; }
  298. set { _transformations.Position = value; OnWorldMatrixChange(); }
  299. }
  300. /// <summary>
  301. /// Get / Set node local scale.
  302. /// </summary>
  303. public Vector3 Scale
  304. {
  305. get { return _transformations.Scale; }
  306. set { _transformations.Scale = value; OnWorldMatrixChange(); }
  307. }
  308. /// <summary>
  309. /// Get / Set node local rotation.
  310. /// </summary>
  311. public Vector3 Rotation
  312. {
  313. get { return _transformations.Rotation; }
  314. set { _transformations.Rotation = value; OnWorldMatrixChange(); }
  315. }
  316. /// <summary>
  317. /// Alias to access rotation X directly.
  318. /// </summary>
  319. public float RotationX
  320. {
  321. get { return _transformations.Rotation.X; }
  322. set { _transformations.Rotation.X = value; OnWorldMatrixChange(); }
  323. }
  324. /// <summary>
  325. /// Alias to access rotation Y directly.
  326. /// </summary>
  327. public float RotationY
  328. {
  329. get { return _transformations.Rotation.Y; }
  330. set { _transformations.Rotation.Y = value; OnWorldMatrixChange(); }
  331. }
  332. /// <summary>
  333. /// Alias to access rotation Z directly.
  334. /// </summary>
  335. public float RotationZ
  336. {
  337. get { return _transformations.Rotation.Z; }
  338. set { _transformations.Rotation.Z = value; OnWorldMatrixChange(); }
  339. }
  340. /// <summary>
  341. /// Alias to access scale X directly.
  342. /// </summary>
  343. public float ScaleX
  344. {
  345. get { return _transformations.Scale.X; }
  346. set { _transformations.Scale.X = value; OnWorldMatrixChange(); }
  347. }
  348. /// <summary>
  349. /// Alias to access scale Y directly.
  350. /// </summary>
  351. public float ScaleY
  352. {
  353. get { return _transformations.Scale.Y; }
  354. set { _transformations.Scale.Y = value; OnWorldMatrixChange(); }
  355. }
  356. /// <summary>
  357. /// Alias to access scale Z directly.
  358. /// </summary>
  359. public float ScaleZ
  360. {
  361. get { return _transformations.Scale.Z; }
  362. set { _transformations.Scale.Z = value; OnWorldMatrixChange(); }
  363. }
  364. /// <summary>
  365. /// Alias to access position X directly.
  366. /// </summary>
  367. public float PositionX
  368. {
  369. get { return _transformations.Position.X; }
  370. set { _transformations.Position.X = value; OnWorldMatrixChange(); }
  371. }
  372. /// <summary>
  373. /// Alias to access position Y directly.
  374. /// </summary>
  375. public float PositionY
  376. {
  377. get { return _transformations.Position.Y; }
  378. set { _transformations.Position.Y = value; OnWorldMatrixChange(); }
  379. }
  380. /// <summary>
  381. /// Alias to access position Z directly.
  382. /// </summary>
  383. public float PositionZ
  384. {
  385. get { return _transformations.Position.Z; }
  386. set { _transformations.Position.Z = value; OnWorldMatrixChange(); }
  387. }
  388. /// <summary>
  389. /// Move position by vector.
  390. /// </summary>
  391. /// <param name="moveBy">Vector to translate by.</param>
  392. public void Translate(Vector3 moveBy)
  393. {
  394. _transformations.Position += moveBy;
  395. OnWorldMatrixChange();
  396. }
  397. }
  398. }