Camera.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.CompilerServices;
  5. using System.Text;
  6. namespace BansheeEngine
  7. {
  8. /// <summary>
  9. /// Camera component that determines how is world geometry projected onto a 2D surface. You may position and orient it
  10. /// in space, set options like aspect ratio and field or view and it outputs view and projection matrices required for
  11. /// rendering.
  12. /// </summary>
  13. public class Camera : Component
  14. {
  15. private NativeCamera native;
  16. [SerializeField]
  17. internal SerializableData serializableData = new SerializableData();
  18. /// <summary>
  19. /// Returns the non-component version of Camera that is wrapped by this component.
  20. /// </summary>
  21. internal NativeCamera Native
  22. {
  23. get { return native; }
  24. }
  25. /// <summary>
  26. /// Ratio between viewport width and height (width / height).
  27. /// </summary>
  28. public float AspectRatio
  29. {
  30. get { return native.aspectRatio; }
  31. set { native.aspectRatio = value; serializableData.aspectRatio = value; }
  32. }
  33. /// <summary>
  34. /// Distance from the frustum origin to the near clipping plane. Anything closer than the near clipping plane will
  35. /// not be rendered. Decreasing this value decreases depth buffer precision.
  36. /// </summary>
  37. public float NearClipPlane
  38. {
  39. get { return native.nearClipPlane; }
  40. set { native.nearClipPlane = value; serializableData.nearClipPlane = value; }
  41. }
  42. /// <summary>
  43. /// Distance from the frustum origin to the far clipping plane. Anything farther than the far clipping plane will
  44. /// not be rendered. Increasing this value decreases depth buffer precision.
  45. /// </summary>
  46. public float FarClipPlane
  47. {
  48. get { return native.farClipPlane; }
  49. set { native.farClipPlane = value; serializableData.farClipPlane = value; }
  50. }
  51. /// <summary>
  52. /// Horizontal field of view. This determines how wide the camera viewing angle is along the horizontal axis.
  53. /// Vertical FOV is calculated from the horizontal FOV and the aspect ratio.
  54. /// </summary>
  55. public Degree FieldOfView
  56. {
  57. get { return native.fieldOfView; }
  58. set { native.fieldOfView = value; serializableData.fieldOfView = value; }
  59. }
  60. /// <summary>
  61. /// Returns the area of the render target that the camera renders to, in normalized coordinates.
  62. /// </summary>
  63. public Rect2 ViewportRect
  64. {
  65. get { return native.viewportRect; }
  66. set { native.viewportRect = value; serializableData.viewportRect = value; }
  67. }
  68. /// <summary>
  69. /// Projection type that controls how is 3D geometry projected onto a 2D plane.
  70. /// </summary>
  71. public ProjectionType ProjectionType
  72. {
  73. get { return native.projectionType; }
  74. set { native.projectionType = value; serializableData.projectionType = value; }
  75. }
  76. /// <summary>
  77. /// Ortographic height that controls the size of the displayed objects. This value is only relevant when projection
  78. /// type is set to orthographic. Setting this value will automatically recalculate ortographic width depending on
  79. /// the aspect ratio.
  80. /// </summary>
  81. public float OrthoHeight
  82. {
  83. get { return native.orthoHeight; }
  84. set { native.orthoHeight = value; serializableData.orthoHeight = value; }
  85. }
  86. /// <summary>
  87. /// Returns the ortographic width that controls the size of the displayed object. To change this value modify
  88. /// <see cref="OrthoHeight"/> or <see cref="AspectRatio"/>.
  89. /// </summary>
  90. public float OrthoWidth
  91. {
  92. get { return native.orthoWidth; }
  93. }
  94. /// <summary>
  95. /// Color that will be used for clearing the camera's viewport before rendering. Only relevant if color clear is
  96. /// enabled.
  97. /// </summary>
  98. public Color ClearColor
  99. {
  100. get { return native.clearColor; }
  101. set { native.clearColor = value; serializableData.clearColor = value; }
  102. }
  103. /// <summary>
  104. /// Value that will be used for clearing the camera's depth buffer before rendering. Only relevant if depth clear
  105. /// is enabled.
  106. /// </summary>
  107. public float ClearDepth
  108. {
  109. get { return native.clearDepth; }
  110. set { native.clearDepth = value; serializableData.clearDepth = value; }
  111. }
  112. /// <summary>
  113. /// Value that will be used for clearing the camera's stencil buffer before rendering. Only relevant if stencil
  114. /// clear is enabled.
  115. /// </summary>
  116. public UInt16 ClearStencil
  117. {
  118. get { return native.clearStencil; }
  119. set { native.clearStencil = value; serializableData.clearStencil = value; }
  120. }
  121. /// <summary>
  122. /// Flags that control which portions of the camera viewport, if any, are cleared before rendering.
  123. /// </summary>
  124. public ClearFlags ClearFlags
  125. {
  126. get { return native.clearFlags; }
  127. set { native.clearFlags = value; serializableData.clearFlags = value; }
  128. }
  129. /// <summary>
  130. /// Determines in which orders are the cameras rendered. This only applies to cameras rendering to the same render
  131. /// target. Higher value means the camera will be processed sooner.
  132. /// </summary>
  133. public int Priority
  134. {
  135. get { return native.priority; }
  136. set { native.priority = value; serializableData.priority = value; }
  137. }
  138. /// <summary>
  139. /// Sets layer bitfield that is used when determining which object should the camera render. Renderable objects
  140. /// have their own layer flags that can be set depending on which camera you want to render them in.
  141. /// </summary>
  142. public UInt64 Layers
  143. {
  144. get { return native.layers; }
  145. set { native.layers = value; serializableData.layers = value; }
  146. }
  147. /// <summary>
  148. /// Returns the standard projection matrix that determines how are 3D points projected to two dimensions. The layout
  149. /// of this matrix depends on currently used render system.
  150. /// </summary>
  151. public Matrix4 ProjMatrix
  152. {
  153. get { return native.projMatrix; }
  154. }
  155. /// <summary>
  156. /// Returns the inverse of the standard projection matrix that determines how are 3D points projected to two
  157. /// dimensions. The layout of this matrix depends on currently used render system.
  158. /// </summary>
  159. public Matrix4 ProjMatrixInverse
  160. {
  161. get { return native.projMatrixInv; }
  162. }
  163. /// <summary>
  164. /// Returns the view matrix that controls camera position and orientation.
  165. /// </summary>
  166. public Matrix4 ViewMatrix
  167. {
  168. get { return native.viewMatrix; }
  169. }
  170. /// <summary>
  171. /// Returns the inverse of the view matrix that controls camera position and orientation.
  172. /// </summary>
  173. public Matrix4 ViewMatrixInverse
  174. {
  175. get { return native.viewMatrixInv; }
  176. }
  177. /// <summary>
  178. /// Returns the width of the camera's viewport, in pixels. Only valid if render target is currently set.
  179. /// </summary>
  180. public int WidthPixels
  181. {
  182. get { return native.widthPixels; }
  183. }
  184. /// <summary>
  185. /// Returns the height of the camera's viewport, in pixels. Only valid if render target is currently set.
  186. /// </summary>
  187. public int HeightPixels
  188. {
  189. get { return native.heightPixels; }
  190. }
  191. /// <summary>
  192. /// Returns the render target that the camera will output all rendered pixels to.
  193. /// </summary>
  194. public RenderTarget Target
  195. {
  196. get { return native.target; }
  197. set { native.target = value; }
  198. }
  199. /// <summary>
  200. /// Converts a point in world space to screen coordinates.
  201. /// </summary>
  202. /// <param name="value">3D point in world space.</param>
  203. /// <returns>2D point on the render target attached to the camera, in pixels.</returns>
  204. public Vector2I WorldToScreen(Vector3 value) { return native.WorldToScreen(value); }
  205. /// <summary>
  206. /// Converts a point in world space to clip space coordinates.
  207. /// </summary>
  208. /// <param name="value">3D point in world space.</param>
  209. /// <returns>2D point in normalized coordinates ([0, 1] range), relative to the camera's viewport.</returns>
  210. public Vector2 WorldToClip(Vector3 value) { return native.WorldToClip(value); }
  211. /// <summary>
  212. /// Converts a point in world space to view space coordinates.
  213. /// </summary>
  214. /// <param name="value">3D point in world space.</param>
  215. /// <returns>3D point relative to the camera's coordinate system.</returns>
  216. public Vector3 WorldToView(Vector3 value) { return native.WorldToView(value); }
  217. /// <summary>
  218. /// Converts a point in screen space to a point in world space.
  219. /// </summary>
  220. /// <param name="value">2D point on the render target attached to the camera, in pixels.</param>
  221. /// <param name="depth">Depth at which place the world point at. The depth is applied to the vector going from
  222. /// camera origin to the point on the near plane.</param>
  223. /// <returns>3D point in world space.</returns>
  224. public Vector3 ScreenToWorld(Vector2I value, float depth = 0.5f) { return native.ScreenToWorld(value, depth); }
  225. /// <summary>
  226. /// Converts a point in screen space to a point in view space.
  227. /// </summary>
  228. /// <param name="value">2D point on the render target attached to the camera, in pixels.</param>
  229. /// <param name="depth">Depth at which place the view point at. The depth is applied to the vector going from
  230. /// camera origin to the point on the near plane.</param>
  231. /// <returns>3D point in view space.</returns>
  232. public Vector3 ScreenToView(Vector2I value, float depth = 0.5f) { return native.ScreenToView(value, depth); }
  233. /// <summary>
  234. /// Converts a point in screen space to a point in normalized clip space.
  235. /// </summary>
  236. /// <param name="value">2D point on the render target attached to the camera, in pixels.</param>
  237. /// <returns>2D point in normalized cliped space ([0, 1] range), relative to the camera's viewport.</returns>
  238. public Vector2 ScreenToClip(Vector2I value) { return native.ScreenToClip(value); }
  239. /// <summary>
  240. /// Converts a point relative to camera's coordinate system (view space) into a point in world space.
  241. /// </summary>
  242. /// <param name="value">3D point in view space.</param>
  243. /// <returns>3D point in world space.</returns>
  244. public Vector3 ViewToWorld(Vector3 value) { return native.ViewToWorld(value); }
  245. /// <summary>
  246. /// Converts a point relative to camera's coordinate system (view space) to screen coordinates.
  247. /// </summary>
  248. /// <param name="value">3D point in view space.</param>
  249. /// <returns>2D point on the render target attached to the camera, in pixels.</returns>
  250. public Vector2I ViewToScreen(Vector3 value) { return native.ViewToScreen(value); }
  251. /// <summary>
  252. /// Converts a point relative to camera's coordinate system (view space) to normalized clip space.
  253. /// </summary>
  254. /// <param name="value">3D point in view space.</param>
  255. /// <returns>2D point in normalized cliped space ([0, 1] range), relative to the camera's viewport.</returns>
  256. public Vector2 ViewToClip(Vector3 value) { return native.ViewToClip(value); }
  257. /// <summary>
  258. /// Converts a point relative to camera's viewport in normalized clip space ([0, 1] range) into a point in
  259. /// world space.
  260. /// </summary>
  261. /// <param name="value">2D point in normalized clip space.</param>
  262. /// <param name="depth">Depth at which place the world point at. The depth is applied to the vector going from
  263. /// camera origin to the point on the near plane.</param>
  264. /// <returns>3D point in world space.</returns>
  265. public Vector3 ClipToWorld(Vector2 value, float depth = 0.5f) { return native.ClipToWorld(value, depth); }
  266. /// <summary>
  267. /// Converts a point relative to camera's viewport in normalized clip space ([0, 1] range) into a point in
  268. /// view space.
  269. /// </summary>
  270. /// <param name="value">2D point in normalized clip space.</param>
  271. /// <param name="depth">Depth at which place the world point at. The depth is applied to the vector going from
  272. /// camera origin to the point on the near plane.</param>
  273. /// <returns>3D point in view space.</returns>
  274. public Vector3 ClipToView(Vector2 value, float depth = 0.5f) { return native.ClipToView(value, depth); }
  275. /// <summary>
  276. /// Converts a point relative to camera's viewport in normalized clip space ([0, 1] range) to screen space.
  277. /// </summary>
  278. /// <param name="value">2D point in normalized clip space.</param>
  279. /// <returns>2D point on the render target attached to the camera, in pixels.</returns>
  280. public Vector2I ClipToScreen(Vector2 value) { return native.ClipToScreen(value); }
  281. /// <summary>
  282. /// Converts a point in screen space in a ray in world space.
  283. /// </summary>
  284. /// <param name="value">2D point on the render target attached to the camera, in pixels.</param>
  285. /// <returns>A ray in world space with it's origin the selected point at the near frustum plane, pointing in the
  286. /// direction going from camera's origin towards a point on the near frustum plane.</returns>
  287. public Ray ScreenToWorldRay(Vector2I value) { return native.ScreenToWorldRay(value); }
  288. /// <summary>
  289. /// Projects a point in view space to a point in clip space. Similar to <see cref="ViewToClip"/> but preserves
  290. /// the depth component.
  291. /// </summary>
  292. /// <param name="value">3D point in view space.</param>
  293. /// <returns>3D point in normalized cliped space ([0, 1] range), relative to the camera's viewport. Z value
  294. /// range depends on active render API.</returns>
  295. public Vector3 ProjectPoint(Vector3 value) { return native.ProjectPoint(value); }
  296. /// <summary>
  297. /// Un-rpojects a point in clip space to a point in view space.
  298. /// </summary>
  299. /// <param name="value">3D point in normalized cliped space ([0, 1] range), relative to the camera's viewport.
  300. /// Z value range depends on active render API.</param>
  301. /// <returns>3D point in view space.</returns>
  302. public Vector3 UnprojectPoint(Vector3 value) { return native.UnprojectPoint(value); }
  303. private void OnReset()
  304. {
  305. if (native != null)
  306. native.OnDestroy();
  307. native = new NativeCamera(SceneObject);
  308. // Restore saved values after reset
  309. native.aspectRatio = serializableData.aspectRatio;
  310. native.nearClipPlane = serializableData.nearClipPlane;
  311. native.farClipPlane = serializableData.farClipPlane;
  312. native.fieldOfView = serializableData.fieldOfView;
  313. native.viewportRect = serializableData.viewportRect;
  314. native.projectionType = serializableData.projectionType;
  315. native.orthoHeight = serializableData.orthoHeight;
  316. native.clearColor = serializableData.clearColor;
  317. native.clearDepth = serializableData.clearDepth;
  318. native.clearStencil = serializableData.clearStencil;
  319. native.clearFlags = serializableData.clearFlags;
  320. native.priority = serializableData.priority;
  321. native.layers = serializableData.layers;
  322. // TODO - Make RenderTexture a resource so I can save/restore it?
  323. }
  324. private void Update()
  325. {
  326. native.UpdateView(SceneObject);
  327. }
  328. private void OnDestroy()
  329. {
  330. native.OnDestroy();
  331. }
  332. /// <summary>
  333. /// Holds all data the camera component needs to persist through serialization.
  334. /// </summary>
  335. [SerializeObject]
  336. internal class SerializableData
  337. {
  338. public float aspectRatio = 1.333f;
  339. public float nearClipPlane = 1.0f;
  340. public float farClipPlane = 1000.0f;
  341. public Degree fieldOfView = new Degree(60);
  342. public Rect2 viewportRect = new Rect2(0, 0, 1, 1);
  343. public ProjectionType projectionType = ProjectionType.Perspective;
  344. public float orthoHeight;
  345. public Color clearColor = new Color(143.0f / 255.0f, 111.0f / 255.0f, 0);
  346. public float clearDepth = 1.0f;
  347. public UInt16 clearStencil;
  348. public ClearFlags clearFlags = ClearFlags.Color | ClearFlags.Depth | ClearFlags.Stencil;
  349. public int priority;
  350. public UInt64 layers = 0xFFFFFFFFFFFFFFFF;
  351. }
  352. }
  353. }