Camera.cs 20 KB

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