2
0

SceneObject.cs 22 KB

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