SceneObject.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. namespace BansheeEngine
  6. {
  7. /// <summary>
  8. /// An object in the scene graph. It has a position, place in the hierarchy and optionally a number of attached
  9. /// components.
  10. /// </summary>
  11. public sealed class SceneObject : GameObject
  12. {
  13. /// <summary>
  14. /// Name of the scene object.
  15. /// </summary>
  16. public string Name
  17. {
  18. get { return Internal_GetName(mCachedPtr); }
  19. set { Internal_SetName(mCachedPtr, value); }
  20. }
  21. /// <summary>
  22. /// Parent in the scene object hierarchy. Null for hierarchy root.
  23. /// </summary>
  24. public SceneObject Parent
  25. {
  26. get { return Internal_GetParent(mCachedPtr); }
  27. set { Internal_SetParent(mCachedPtr, value); }
  28. }
  29. /// <summary>
  30. /// Determines if the object's components are being updated or not.
  31. /// </summary>
  32. public bool Active
  33. {
  34. get { return Internal_GetActive(mCachedPtr); }
  35. set { Internal_SetActive(mCachedPtr, value); }
  36. }
  37. /// <summary>
  38. /// World position. This includes local position of this object, plus position offset of any parents.
  39. /// </summary>
  40. public Vector3 Position
  41. {
  42. get
  43. {
  44. Vector3 value;
  45. Internal_GetPosition(mCachedPtr, out value);
  46. return value;
  47. }
  48. set
  49. {
  50. Internal_SetPosition(mCachedPtr, ref value);
  51. }
  52. }
  53. /// <summary>
  54. /// Local space position (relative to the parent).
  55. /// </summary>
  56. public Vector3 LocalPosition
  57. {
  58. get
  59. {
  60. Vector3 value;
  61. Internal_GetLocalPosition(mCachedPtr, out value);
  62. return value;
  63. }
  64. set
  65. {
  66. Internal_SetLocalPosition(mCachedPtr, ref value);
  67. }
  68. }
  69. /// <summary>
  70. /// World rotation. This includes local rotation of this object, plus rotation of any parents.
  71. /// </summary>
  72. public Quaternion Rotation
  73. {
  74. get
  75. {
  76. Quaternion value;
  77. Internal_GetRotation(mCachedPtr, out value);
  78. return value;
  79. }
  80. set
  81. {
  82. Internal_SetRotation(mCachedPtr, ref value);
  83. }
  84. }
  85. /// <summary>
  86. /// Local rotation (relative to the parent).
  87. /// </summary>
  88. public Quaternion LocalRotation
  89. {
  90. get
  91. {
  92. Quaternion value;
  93. Internal_GetLocalRotation(mCachedPtr, out value);
  94. return value;
  95. }
  96. set
  97. {
  98. Internal_SetLocalRotation(mCachedPtr, ref value);
  99. }
  100. }
  101. /// <summary>
  102. /// World space scale. This includes local scale of this object, plus scale of any parent.
  103. /// </summary>
  104. public Vector3 Scale
  105. {
  106. get
  107. {
  108. Vector3 value;
  109. Internal_GetScale(mCachedPtr, out value);
  110. return value;
  111. }
  112. }
  113. /// <summary>
  114. /// Local scale (relative to the parent).
  115. /// </summary>
  116. public Vector3 LocalScale
  117. {
  118. get
  119. {
  120. Vector3 value;
  121. Internal_GetLocalScale(mCachedPtr, out value);
  122. return value;
  123. }
  124. set
  125. {
  126. Internal_SetLocalScale(mCachedPtr, ref value);
  127. }
  128. }
  129. /// <summary>
  130. /// Returns the world transform matrix. This matrix accounts for position, rotation and scale transformations
  131. /// relative to the world basis.
  132. /// </summary>
  133. public Matrix4 WorldTransform
  134. {
  135. get
  136. {
  137. Matrix4 value;
  138. Internal_GetWorldTransform(mCachedPtr, out value);
  139. return value;
  140. }
  141. }
  142. /// <summary>
  143. /// Returns the local transform matrix. This matrix accounts for position, rotation and scale transformations
  144. /// relative to the parent's basis.
  145. /// </summary>
  146. public Matrix4 LocalTransform
  147. {
  148. get
  149. {
  150. Matrix4 value;
  151. Internal_GetLocalTransform(mCachedPtr, out value);
  152. return value;
  153. }
  154. }
  155. /// <summary>
  156. /// Direction in world space that points along the local negative Z axis.
  157. /// </summary>
  158. public Vector3 Forward
  159. {
  160. get
  161. {
  162. Vector3 value;
  163. Internal_GetForward(mCachedPtr, out value);
  164. return value;
  165. }
  166. set
  167. {
  168. Internal_SetForward(mCachedPtr, ref value);
  169. }
  170. }
  171. /// <summary>
  172. /// Direction in world space that points along the local positive X axis.
  173. /// </summary>
  174. public Vector3 Right
  175. {
  176. get
  177. {
  178. Vector3 value;
  179. Internal_GetRight(mCachedPtr, out value);
  180. return value;
  181. }
  182. }
  183. /// <summary>
  184. /// Direction in world space that points along the local positive Y axis.
  185. /// </summary>
  186. public Vector3 Up
  187. {
  188. get
  189. {
  190. Vector3 value;
  191. Internal_GetUp(mCachedPtr, out value);
  192. return value;
  193. }
  194. }
  195. /// <summary>
  196. /// Constructor for internal use by the runtime.
  197. /// </summary>
  198. private SceneObject()
  199. {
  200. }
  201. /// <summary>
  202. /// Creates a new scene object. Object will initially be parented to scene root and placed at the world origin.
  203. /// </summary>
  204. /// <param name="name">Name of the scene object.</param>
  205. public SceneObject(string name)
  206. {
  207. Internal_CreateInstance(this, name, 0);
  208. }
  209. /// <summary>
  210. /// Creates a new scene object. Object will initially be parented to scene root and placed at the world origin.
  211. /// </summary>
  212. /// <param name="name">Name of the scene object.</param>
  213. /// <param name="isInternal">Specifies this object is for internal use by the runtime. Internal object will not
  214. /// get saved, nor will they be displayed in the editor during non-debug mode.</param>
  215. internal SceneObject(string name, bool isInternal)
  216. {
  217. if(isInternal)
  218. Internal_CreateInstance(this, name, (int)(SceneObjectEditorFlags.DontSave | SceneObjectEditorFlags.Internal | SceneObjectEditorFlags.Persistent));
  219. else
  220. Internal_CreateInstance(this, name, 0);
  221. }
  222. /// <summary>
  223. /// Constructs a new component of the specified type and adds it to the internal component list.
  224. /// </summary>
  225. /// <typeparam name="T">Type of component to create.</typeparam>
  226. /// <returns>Instance of the new component.</returns>
  227. public T AddComponent<T>() where T : Component
  228. {
  229. return (T)Component.Internal_AddComponent(this, typeof (T));
  230. }
  231. /// <summary>
  232. /// Constructs a new component of the specified type and adds it to the internal component list.
  233. /// </summary>
  234. /// <param name="type">Type of component to create.</param>
  235. /// <returns>Instance of the new component.</returns>
  236. public Component AddComponent(Type type)
  237. {
  238. return Component.Internal_AddComponent(this, type);
  239. }
  240. /// <summary>
  241. /// Searches for a component of a specific type. If there are multiple components matching the type, only the first
  242. /// one found is returned.
  243. /// </summary>
  244. /// <typeparam name="T">Type of the component to search for. Includes any components derived from the type.
  245. /// </typeparam>
  246. /// <returns>Component instance if found, null otherwise.</returns>
  247. public T GetComponent<T>() where T : Component
  248. {
  249. return (T)Component.Internal_GetComponent(this, typeof(T));
  250. }
  251. /// <summary>
  252. /// Searches for all components of a specific type.
  253. /// </summary>
  254. /// <typeparam name="T">Type of the component to search for. Includes any components derived from the type.
  255. /// </typeparam>
  256. /// <returns>All components matching the specified type.</returns>
  257. public T[] GetComponents<T>() where T : Component
  258. {
  259. return (T[])Component.Internal_GetComponentsPerType(this, typeof(T));
  260. }
  261. /// <summary>
  262. /// Returns a list of all components attached to this object.
  263. /// </summary>
  264. /// <returns>All components attached to this object.</returns>
  265. public Component[] GetComponents()
  266. {
  267. return Component.Internal_GetComponents(this);
  268. }
  269. /// <summary>
  270. /// Removes a component from the scene object. If there are multiple components matching the type, only the first
  271. /// one found is removed.
  272. /// </summary>
  273. /// <typeparam name="T">Type of the component to remove. Includes any components derived from the type.</typeparam>
  274. public void RemoveComponent<T>() where T : Component
  275. {
  276. Component.Internal_RemoveComponent(this, typeof(T));
  277. }
  278. /// <summary>
  279. /// Removes a component from the scene object. If there are multiple components matching the type, only the first
  280. /// one found is removed.
  281. /// </summary>
  282. /// <param name="type">Type of the component to remove. Includes any components derived from the type.</param>
  283. public void RemoveComponent(Type type)
  284. {
  285. Component.Internal_RemoveComponent(this, type);
  286. }
  287. /// <summary>
  288. /// Returns the number of child scene objects this object is parent to.
  289. /// </summary>
  290. /// <returns>Number of child scene objects.</returns>
  291. public int GetNumChildren()
  292. {
  293. int value;
  294. Internal_GetNumChildren(mCachedPtr, out value);
  295. return value;
  296. }
  297. /// <summary>
  298. /// Returns a child scene object.
  299. /// </summary>
  300. /// <param name="idx">Index of the child scene object to retrieve.</param>
  301. /// <returns>Instance of the child scene object, or null if index is out of range.</returns>
  302. public SceneObject GetChild(int idx)
  303. {
  304. return Internal_GetChild(mCachedPtr, idx);
  305. }
  306. /// <summary>
  307. /// Searches the child objects for an object matching the specified name.
  308. /// </summary>
  309. /// <param name="name">Name of the object to locate.</param>
  310. /// <param name="recursive">If true all descendants of the scene object will be searched, otherwise only immediate
  311. /// children.</param>
  312. /// <returns>First found scene object, or empty handle if none found.</returns>
  313. public SceneObject FindChild(string name, bool recursive = true)
  314. {
  315. return Internal_FindChild(mCachedPtr, name, recursive);
  316. }
  317. /// <summary>
  318. /// Searches the child objects for objects matching the specified name.
  319. /// </summary>
  320. /// <param name="name">Name of the objects to locate.</param>
  321. /// <param name="recursive">If true all descendants of the scene object will be searched, otherwise only immediate
  322. /// children.</param>
  323. /// <returns>All scene objects matching the specified name.</returns>
  324. public SceneObject[] FindChildren(string name, bool recursive = true)
  325. {
  326. return Internal_FindChildren(mCachedPtr, name, recursive);
  327. }
  328. /// <summary>
  329. /// Orients the object so it is looking at the provided location.
  330. /// </summary>
  331. /// <param name="position">Position in local space where to look at.</param>
  332. public void LookAt(Vector3 position)
  333. {
  334. Vector3 up = Vector3.YAxis;
  335. Internal_LookAt(mCachedPtr, ref position, ref up);
  336. }
  337. /// <summary>
  338. /// Orients the object so it is looking at the provided location.
  339. /// </summary>
  340. /// <param name="position">Position in world space where to look at.</param>
  341. /// <param name="up">Determines the object's Y axis orientation.</param>
  342. public void LookAt(Vector3 position, Vector3 up)
  343. {
  344. Internal_LookAt(mCachedPtr, ref position, ref up);
  345. }
  346. /// <summary>
  347. /// Moves the object's position by the vector offset provided along world axes.
  348. /// </summary>
  349. /// <param name="amount">Amount and direction to move the object along.</param>
  350. public void Move(Vector3 amount)
  351. {
  352. Internal_Move(mCachedPtr, ref amount);
  353. }
  354. /// <summary>
  355. /// Moves the object's position by the vector offset provided along local axes.
  356. /// </summary>
  357. /// <param name="amount">Amount and direction to move the object along.</param>
  358. public void MoveLocal(Vector3 amount)
  359. {
  360. Internal_MoveLocal(mCachedPtr, ref amount);
  361. }
  362. /// <summary>
  363. /// Rotates the object by the quaternion, in world space.
  364. /// </summary>
  365. /// <param name="amount">Quaternion that specifies the rotation.</param>
  366. public void Rotate(Quaternion amount)
  367. {
  368. Internal_Rotate(mCachedPtr, ref amount);
  369. }
  370. /// <summary>
  371. /// Rotates around local Z axis.
  372. /// </summary>
  373. /// <param name="angle">Angle to rotate by.</param>
  374. public void Roll(Degree angle)
  375. {
  376. Radian radianAngle = angle;
  377. Internal_Roll(mCachedPtr, ref radianAngle);
  378. }
  379. /// <summary>
  380. /// Rotates around local Y axis.
  381. /// </summary>
  382. /// <param name="angle">Angle to rotate by.</param>
  383. public void Yaw(Degree angle)
  384. {
  385. Radian radianAngle = angle;
  386. Internal_Yaw(mCachedPtr, ref radianAngle);
  387. }
  388. /// <summary>
  389. /// Rotates around local X axis.
  390. /// </summary>
  391. /// <param name="angle">Angle to rotate by.</param>
  392. public void Pitch(Degree angle)
  393. {
  394. Radian radianAngle = angle;
  395. Internal_Pitch(mCachedPtr, ref radianAngle);
  396. }
  397. /// <summary>
  398. /// Destroys the scene object, removing it from scene and stopping component updates.
  399. /// </summary>
  400. /// <param name="immediate">If true the scene object will be fully destroyed immediately. This means that objects
  401. /// that are still referencing this scene object might fail. Normally destruction is delayed
  402. /// until the end of the frame to give other objects a chance to stop using it.</param>
  403. public void Destroy(bool immediate = false)
  404. {
  405. Internal_Destroy(mCachedPtr, immediate);
  406. }
  407. [MethodImpl(MethodImplOptions.InternalCall)]
  408. private static extern void Internal_CreateInstance(SceneObject instance, string name, int flags);
  409. [MethodImpl(MethodImplOptions.InternalCall)]
  410. private static extern void Internal_SetName(IntPtr nativeInstance, string name);
  411. [MethodImpl(MethodImplOptions.InternalCall)]
  412. private static extern string Internal_GetName(IntPtr nativeInstance);
  413. [MethodImpl(MethodImplOptions.InternalCall)]
  414. private static extern void Internal_SetActive(IntPtr nativeInstance, bool value);
  415. [MethodImpl(MethodImplOptions.InternalCall)]
  416. private static extern bool Internal_GetActive(IntPtr nativeInstance);
  417. [MethodImpl(MethodImplOptions.InternalCall)]
  418. private static extern void Internal_SetParent(IntPtr nativeInstance, SceneObject parent);
  419. [MethodImpl(MethodImplOptions.InternalCall)]
  420. private static extern SceneObject Internal_GetParent(IntPtr nativeInstance);
  421. [MethodImpl(MethodImplOptions.InternalCall)]
  422. private static extern void Internal_GetNumChildren(IntPtr nativeInstance, out int value);
  423. [MethodImpl(MethodImplOptions.InternalCall)]
  424. private static extern SceneObject Internal_GetChild(IntPtr nativeInstance, int idx);
  425. [MethodImpl(MethodImplOptions.InternalCall)]
  426. private static extern SceneObject Internal_FindChild(IntPtr nativeInstance, string name, bool recursive);
  427. [MethodImpl(MethodImplOptions.InternalCall)]
  428. private static extern SceneObject[] Internal_FindChildren(IntPtr nativeInstance, string name, bool recursive);
  429. [MethodImpl(MethodImplOptions.InternalCall)]
  430. private static extern void Internal_GetPosition(IntPtr nativeInstance, out Vector3 value);
  431. [MethodImpl(MethodImplOptions.InternalCall)]
  432. private static extern void Internal_GetLocalPosition(IntPtr nativeInstance, out Vector3 value);
  433. [MethodImpl(MethodImplOptions.InternalCall)]
  434. private static extern void Internal_GetRotation(IntPtr nativeInstance, out Quaternion value);
  435. [MethodImpl(MethodImplOptions.InternalCall)]
  436. private static extern void Internal_GetLocalRotation(IntPtr nativeInstance, out Quaternion value);
  437. [MethodImpl(MethodImplOptions.InternalCall)]
  438. private static extern void Internal_GetScale(IntPtr nativeInstance, out Vector3 value);
  439. [MethodImpl(MethodImplOptions.InternalCall)]
  440. private static extern void Internal_GetLocalScale(IntPtr nativeInstance, out Vector3 value);
  441. [MethodImpl(MethodImplOptions.InternalCall)]
  442. private static extern void Internal_SetPosition(IntPtr nativeInstance, ref Vector3 value);
  443. [MethodImpl(MethodImplOptions.InternalCall)]
  444. private static extern void Internal_SetLocalPosition(IntPtr nativeInstance, ref Vector3 value);
  445. [MethodImpl(MethodImplOptions.InternalCall)]
  446. private static extern void Internal_SetRotation(IntPtr nativeInstance, ref Quaternion value);
  447. [MethodImpl(MethodImplOptions.InternalCall)]
  448. private static extern void Internal_SetLocalRotation(IntPtr nativeInstance, ref Quaternion value);
  449. [MethodImpl(MethodImplOptions.InternalCall)]
  450. private static extern void Internal_SetLocalScale(IntPtr nativeInstance, ref Vector3 value);
  451. [MethodImpl(MethodImplOptions.InternalCall)]
  452. private static extern void Internal_GetLocalTransform(IntPtr nativeInstance, out Matrix4 value);
  453. [MethodImpl(MethodImplOptions.InternalCall)]
  454. private static extern void Internal_GetWorldTransform(IntPtr nativeInstance, out Matrix4 value);
  455. [MethodImpl(MethodImplOptions.InternalCall)]
  456. private static extern void Internal_LookAt(IntPtr nativeInstance, ref Vector3 direction, ref Vector3 up);
  457. [MethodImpl(MethodImplOptions.InternalCall)]
  458. private static extern void Internal_Move(IntPtr nativeInstance, ref Vector3 value);
  459. [MethodImpl(MethodImplOptions.InternalCall)]
  460. private static extern void Internal_MoveLocal(IntPtr nativeInstance, ref Vector3 value);
  461. [MethodImpl(MethodImplOptions.InternalCall)]
  462. private static extern void Internal_Rotate(IntPtr nativeInstance, ref Quaternion value);
  463. [MethodImpl(MethodImplOptions.InternalCall)]
  464. private static extern void Internal_Roll(IntPtr nativeInstance, ref Radian value);
  465. [MethodImpl(MethodImplOptions.InternalCall)]
  466. private static extern void Internal_Yaw(IntPtr nativeInstance, ref Radian value);
  467. [MethodImpl(MethodImplOptions.InternalCall)]
  468. private static extern void Internal_Pitch(IntPtr nativeInstance, ref Radian value);
  469. [MethodImpl(MethodImplOptions.InternalCall)]
  470. private static extern void Internal_SetForward(IntPtr nativeInstance, ref Vector3 value);
  471. [MethodImpl(MethodImplOptions.InternalCall)]
  472. private static extern void Internal_GetForward(IntPtr nativeInstance, out Vector3 value);
  473. [MethodImpl(MethodImplOptions.InternalCall)]
  474. private static extern void Internal_GetUp(IntPtr nativeInstance, out Vector3 value);
  475. [MethodImpl(MethodImplOptions.InternalCall)]
  476. private static extern void Internal_GetRight(IntPtr nativeInstance, out Vector3 value);
  477. [MethodImpl(MethodImplOptions.InternalCall)]
  478. private static extern void Internal_Destroy(IntPtr nativeInstance, bool immediate);
  479. }
  480. /// <summary>
  481. /// Flags that can be used for controlling scene object behaviour.
  482. /// </summary>
  483. internal enum SceneObjectEditorFlags // Note: Must match C++ enum SceneObjectFlags
  484. {
  485. /// <summary>Object wont be in the main scene and its components won't receive updates.</summary>
  486. DontInstantiate = 0x01,
  487. /// <summary> Object will be skipped when saving the scene hierarchy or a prefab.</summary>
  488. DontSave = 0x02,
  489. /// <summary>
  490. /// Object will remain in the scene even after scene clear, unless destroyed directly. This only works with
  491. /// top-level objects.
  492. /// </summary>
  493. Persistent = 0x04,
  494. /// <summary>
  495. /// Provides a hint to external systems that his object is used by engine internals. For example, those systems
  496. /// might not want to display those objects together with the user created ones.
  497. /// </summary>
  498. Internal = 0x08
  499. }
  500. }