SceneObject.cs 19 KB

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