Node.cs 15 KB

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