SceneObject.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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.
  242. /// </summary>
  243. /// <typeparam name="T">Type of the component to search for.</typeparam>
  244. /// <returns>Component instance if found, null otherwise.</returns>
  245. public T GetComponent<T>() where T : Component
  246. {
  247. return (T)Component.Internal_GetComponent(this, typeof(T));
  248. }
  249. /// <summary>
  250. /// Returns a list of all components attached to this object.
  251. /// </summary>
  252. /// <returns>All components attached to this object.</returns>
  253. public Component[] GetComponents()
  254. {
  255. return Component.Internal_GetComponents(this);
  256. }
  257. /// <summary>
  258. /// Removes a component from the scene object.
  259. /// </summary>
  260. /// <typeparam name="T">Type of the component to remove.</typeparam>
  261. public void RemoveComponent<T>() where T : Component
  262. {
  263. Component.Internal_RemoveComponent(this, typeof(T));
  264. }
  265. /// <summary>
  266. /// Removes a component from the scene object.
  267. /// </summary>
  268. /// <param name="type">Type of the component to remove.</param>
  269. public void RemoveComponent(Type type)
  270. {
  271. Component.Internal_RemoveComponent(this, type);
  272. }
  273. /// <summary>
  274. /// Returns the number of child scene objects this object is parent to.
  275. /// </summary>
  276. /// <returns>Number of child scene objects.</returns>
  277. public int GetNumChildren()
  278. {
  279. int value;
  280. Internal_GetNumChildren(mCachedPtr, out value);
  281. return value;
  282. }
  283. /// <summary>
  284. /// Returns a child scene object.
  285. /// </summary>
  286. /// <param name="idx">Index of the child scene object to retrieve.</param>
  287. /// <returns>Instance of the child scene object, or null if index is out of range.</returns>
  288. public SceneObject GetChild(int idx)
  289. {
  290. return Internal_GetChild(mCachedPtr, idx);
  291. }
  292. /// <summary>
  293. /// Searches the child objects for an object matching the specified name.
  294. /// </summary>
  295. /// <param name="name">Name of the object to locate.</param>
  296. /// <param name="recursive">If true all descendants of the scene object will be searched, otherwise only immediate
  297. /// children.</param>
  298. /// <returns>First found scene object, or empty handle if none found.</returns>
  299. public SceneObject FindChild(string name, bool recursive = true)
  300. {
  301. return Internal_FindChild(mCachedPtr, name, recursive);
  302. }
  303. /// <summary>
  304. /// Searches the child objects for objects matching the specified name.
  305. /// </summary>
  306. /// <param name="name">Name of the objects to locate.</param>
  307. /// <param name="recursive">If true all descendants of the scene object will be searched, otherwise only immediate
  308. /// children.</param>
  309. /// <returns>All scene objects matching the specified name.</returns>
  310. public SceneObject[] FindChildren(string name, bool recursive = true)
  311. {
  312. return Internal_FindChildren(mCachedPtr, name, recursive);
  313. }
  314. /// <summary>
  315. /// Orients the object so it is looking at the provided location.
  316. /// </summary>
  317. /// <param name="position">Position in local space where to look at.</param>
  318. public void LookAt(Vector3 position)
  319. {
  320. Vector3 up = Vector3.YAxis;
  321. Internal_LookAt(mCachedPtr, ref position, ref up);
  322. }
  323. /// <summary>
  324. /// Orients the object so it is looking at the provided location.
  325. /// </summary>
  326. /// <param name="position">Position in world space where to look at.</param>
  327. /// <param name="up">Determines the object's Y axis orientation.</param>
  328. public void LookAt(Vector3 position, Vector3 up)
  329. {
  330. Internal_LookAt(mCachedPtr, ref position, ref up);
  331. }
  332. /// <summary>
  333. /// Moves the object's position by the vector offset provided along world axes.
  334. /// </summary>
  335. /// <param name="amount">Amount and direction to move the object along.</param>
  336. public void Move(Vector3 amount)
  337. {
  338. Internal_Move(mCachedPtr, ref amount);
  339. }
  340. /// <summary>
  341. /// Moves the object's position by the vector offset provided along local axes.
  342. /// </summary>
  343. /// <param name="amount">Amount and direction to move the object along.</param>
  344. public void MoveLocal(Vector3 amount)
  345. {
  346. Internal_MoveLocal(mCachedPtr, ref amount);
  347. }
  348. /// <summary>
  349. /// Rotates the object by the quaternion, in world space.
  350. /// </summary>
  351. /// <param name="amount">Quaternion that specifies the rotation.</param>
  352. public void Rotate(Quaternion amount)
  353. {
  354. Internal_Rotate(mCachedPtr, ref amount);
  355. }
  356. /// <summary>
  357. /// Rotates around local Z axis.
  358. /// </summary>
  359. /// <param name="angle">Angle to rotate by.</param>
  360. public void Roll(Degree angle)
  361. {
  362. Radian radianAngle = angle;
  363. Internal_Roll(mCachedPtr, ref radianAngle);
  364. }
  365. /// <summary>
  366. /// Rotates around local Y axis.
  367. /// </summary>
  368. /// <param name="angle">Angle to rotate by.</param>
  369. public void Yaw(Degree angle)
  370. {
  371. Radian radianAngle = angle;
  372. Internal_Yaw(mCachedPtr, ref radianAngle);
  373. }
  374. /// <summary>
  375. /// Rotates around local X axis.
  376. /// </summary>
  377. /// <param name="angle">Angle to rotate by.</param>
  378. public void Pitch(Degree angle)
  379. {
  380. Radian radianAngle = angle;
  381. Internal_Pitch(mCachedPtr, ref radianAngle);
  382. }
  383. /// <summary>
  384. /// Destroys the scene object, removing it from scene and stopping component updates.
  385. /// </summary>
  386. /// <param name="immediate">If true the scene object will be fully destroyed immediately. This means that objects
  387. /// that are still referencing this scene object might fail. Normally destruction is delayed
  388. /// until the end of the frame to give other object's a chance to stop using it.</param>
  389. public void Destroy(bool immediate = false)
  390. {
  391. Internal_Destroy(mCachedPtr, immediate);
  392. }
  393. [MethodImpl(MethodImplOptions.InternalCall)]
  394. private static extern void Internal_CreateInstance(SceneObject instance, string name, int flags);
  395. [MethodImpl(MethodImplOptions.InternalCall)]
  396. private static extern void Internal_SetName(IntPtr nativeInstance, string name);
  397. [MethodImpl(MethodImplOptions.InternalCall)]
  398. private static extern string Internal_GetName(IntPtr nativeInstance);
  399. [MethodImpl(MethodImplOptions.InternalCall)]
  400. private static extern void Internal_SetActive(IntPtr nativeInstance, bool value);
  401. [MethodImpl(MethodImplOptions.InternalCall)]
  402. private static extern bool Internal_GetActive(IntPtr nativeInstance);
  403. [MethodImpl(MethodImplOptions.InternalCall)]
  404. private static extern void Internal_SetParent(IntPtr nativeInstance, SceneObject parent);
  405. [MethodImpl(MethodImplOptions.InternalCall)]
  406. private static extern SceneObject Internal_GetParent(IntPtr nativeInstance);
  407. [MethodImpl(MethodImplOptions.InternalCall)]
  408. private static extern void Internal_GetNumChildren(IntPtr nativeInstance, out int value);
  409. [MethodImpl(MethodImplOptions.InternalCall)]
  410. private static extern SceneObject Internal_GetChild(IntPtr nativeInstance, int idx);
  411. [MethodImpl(MethodImplOptions.InternalCall)]
  412. private static extern SceneObject Internal_FindChild(IntPtr nativeInstance, string name, bool recursive);
  413. [MethodImpl(MethodImplOptions.InternalCall)]
  414. private static extern SceneObject[] Internal_FindChildren(IntPtr nativeInstance, string name, bool recursive);
  415. [MethodImpl(MethodImplOptions.InternalCall)]
  416. private static extern Prefab Internal_GetPrefab(IntPtr nativeInstance);
  417. [MethodImpl(MethodImplOptions.InternalCall)]
  418. private static extern void Internal_GetPosition(IntPtr nativeInstance, out Vector3 value);
  419. [MethodImpl(MethodImplOptions.InternalCall)]
  420. private static extern void Internal_GetLocalPosition(IntPtr nativeInstance, out Vector3 value);
  421. [MethodImpl(MethodImplOptions.InternalCall)]
  422. private static extern void Internal_GetRotation(IntPtr nativeInstance, out Quaternion value);
  423. [MethodImpl(MethodImplOptions.InternalCall)]
  424. private static extern void Internal_GetLocalRotation(IntPtr nativeInstance, out Quaternion value);
  425. [MethodImpl(MethodImplOptions.InternalCall)]
  426. private static extern void Internal_GetScale(IntPtr nativeInstance, out Vector3 value);
  427. [MethodImpl(MethodImplOptions.InternalCall)]
  428. private static extern void Internal_GetLocalScale(IntPtr nativeInstance, out Vector3 value);
  429. [MethodImpl(MethodImplOptions.InternalCall)]
  430. private static extern void Internal_SetPosition(IntPtr nativeInstance, ref Vector3 value);
  431. [MethodImpl(MethodImplOptions.InternalCall)]
  432. private static extern void Internal_SetLocalPosition(IntPtr nativeInstance, ref Vector3 value);
  433. [MethodImpl(MethodImplOptions.InternalCall)]
  434. private static extern void Internal_SetRotation(IntPtr nativeInstance, ref Quaternion value);
  435. [MethodImpl(MethodImplOptions.InternalCall)]
  436. private static extern void Internal_SetLocalRotation(IntPtr nativeInstance, ref Quaternion value);
  437. [MethodImpl(MethodImplOptions.InternalCall)]
  438. private static extern void Internal_SetLocalScale(IntPtr nativeInstance, ref Vector3 value);
  439. [MethodImpl(MethodImplOptions.InternalCall)]
  440. private static extern void Internal_GetLocalTransform(IntPtr nativeInstance, out Matrix4 value);
  441. [MethodImpl(MethodImplOptions.InternalCall)]
  442. private static extern void Internal_GetWorldTransform(IntPtr nativeInstance, out Matrix4 value);
  443. [MethodImpl(MethodImplOptions.InternalCall)]
  444. private static extern void Internal_LookAt(IntPtr nativeInstance, ref Vector3 direction, ref Vector3 up);
  445. [MethodImpl(MethodImplOptions.InternalCall)]
  446. private static extern void Internal_Move(IntPtr nativeInstance, ref Vector3 value);
  447. [MethodImpl(MethodImplOptions.InternalCall)]
  448. private static extern void Internal_MoveLocal(IntPtr nativeInstance, ref Vector3 value);
  449. [MethodImpl(MethodImplOptions.InternalCall)]
  450. private static extern void Internal_Rotate(IntPtr nativeInstance, ref Quaternion value);
  451. [MethodImpl(MethodImplOptions.InternalCall)]
  452. private static extern void Internal_Roll(IntPtr nativeInstance, ref Radian value);
  453. [MethodImpl(MethodImplOptions.InternalCall)]
  454. private static extern void Internal_Yaw(IntPtr nativeInstance, ref Radian value);
  455. [MethodImpl(MethodImplOptions.InternalCall)]
  456. private static extern void Internal_Pitch(IntPtr nativeInstance, ref Radian value);
  457. [MethodImpl(MethodImplOptions.InternalCall)]
  458. private static extern void Internal_SetForward(IntPtr nativeInstance, ref Vector3 value);
  459. [MethodImpl(MethodImplOptions.InternalCall)]
  460. private static extern void Internal_GetForward(IntPtr nativeInstance, out Vector3 value);
  461. [MethodImpl(MethodImplOptions.InternalCall)]
  462. private static extern void Internal_GetUp(IntPtr nativeInstance, out Vector3 value);
  463. [MethodImpl(MethodImplOptions.InternalCall)]
  464. private static extern void Internal_GetRight(IntPtr nativeInstance, out Vector3 value);
  465. [MethodImpl(MethodImplOptions.InternalCall)]
  466. private static extern void Internal_Destroy(IntPtr nativeInstance, bool immediate);
  467. }
  468. /// <summary>
  469. /// Flags that can be used for controlling scene object behaviour.
  470. /// </summary>
  471. internal enum SceneObjectEditorFlags // Note: Must match C++ enum SceneObjectFlags
  472. {
  473. /// <summary>Object wont be in the main scene and its components won't receive updates.</summary>
  474. DontInstantiate = 0x01,
  475. /// <summary> Object will be skipped when saving the scene hierarchy or a prefab.</summary>
  476. DontSave = 0x02,
  477. /// <summary>
  478. /// Object will remain in the scene even after scene clear, unless destroyed directly. This only works with
  479. /// top-level objects.
  480. /// </summary>
  481. Persistent = 0x04,
  482. /// <summary>
  483. /// Provides a hint to external systems that his object is used by engine internals. For example, those systems
  484. /// might not want to display those objects together with the user created ones.
  485. /// </summary>
  486. Internal = 0x08
  487. }
  488. }