2
0

SceneObject.cs 23 KB

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