Node.cs 15 KB

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