SceneObject.cs 22 KB

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