Camera.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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 : Component
  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. /// Determines should high dynamic range be enabled. High dynamic range allows light intensity to be more correctly
  142. /// recorded when rendering by allowing for a larger range of values. The stored light is then converted into
  143. /// visible color range using exposure and a tone mapping operator. Use <see cref="PostProcess"/> to customize
  144. /// those operations.
  145. /// </summary>
  146. public bool HDR
  147. {
  148. get { return native.HDR; }
  149. set { native.HDR = value; serializableData.HDR = value; }
  150. }
  151. /// <summary>
  152. /// Allows you to customize various post process operations that will be executed on the image produced by this
  153. /// camera.
  154. /// </summary>
  155. public PostProcessSettings PostProcess
  156. {
  157. get { return native.PostProcess; }
  158. set { native.PostProcess = value; serializableData.postProcessSettings = value; }
  159. }
  160. /// <summary>
  161. /// Sets layer bitfield that is used when determining which object should the camera render. Renderable objects
  162. /// have their own layer flags that can be set depending on which camera you want to render them in.
  163. /// </summary>
  164. public UInt64 Layers
  165. {
  166. get { return native.layers; }
  167. set { native.layers = value; serializableData.layers = value; }
  168. }
  169. /// <summary>
  170. /// Returns the standard projection matrix that determines how are 3D points projected to two dimensions. The layout
  171. /// of this matrix depends on currently used render system.
  172. /// </summary>
  173. public Matrix4 ProjMatrix
  174. {
  175. get { return native.projMatrix; }
  176. }
  177. /// <summary>
  178. /// Returns the inverse of the standard projection matrix that determines how are 3D points projected to two
  179. /// dimensions. The layout of this matrix depends on currently used render system.
  180. /// </summary>
  181. public Matrix4 ProjMatrixInverse
  182. {
  183. get { return native.projMatrixInv; }
  184. }
  185. /// <summary>
  186. /// Returns the view matrix that controls camera position and orientation.
  187. /// </summary>
  188. public Matrix4 ViewMatrix
  189. {
  190. get { return native.viewMatrix; }
  191. }
  192. /// <summary>
  193. /// Returns the inverse of the view matrix that controls camera position and orientation.
  194. /// </summary>
  195. public Matrix4 ViewMatrixInverse
  196. {
  197. get { return native.viewMatrixInv; }
  198. }
  199. /// <summary>
  200. /// Returns the width of the camera's viewport, in pixels. Only valid if render target is currently set.
  201. /// </summary>
  202. public int WidthPixels
  203. {
  204. get { return native.widthPixels; }
  205. }
  206. /// <summary>
  207. /// Returns the height of the camera's viewport, in pixels. Only valid if render target is currently set.
  208. /// </summary>
  209. public int HeightPixels
  210. {
  211. get { return native.heightPixels; }
  212. }
  213. /// <summary>
  214. /// Returns the render target that the camera will output all rendered pixels to.
  215. /// </summary>
  216. public RenderTarget Target
  217. {
  218. get { return native.target; }
  219. set { native.target = value; }
  220. }
  221. /// <summary>
  222. /// Determines if this is the main application camera. Main camera controls the final render surface that is
  223. /// displayed to the user.
  224. /// </summary>
  225. public bool Main
  226. {
  227. get { return native.main; }
  228. set { native.main = value; serializableData.main = value; }
  229. }
  230. /// <summary>
  231. /// Converts a point in world space to coordinates relative to the camera's viewport.
  232. /// </summary>
  233. /// <param name="value">3D point in world space.</param>
  234. /// <returns>2D point on the render target attached to the camera, in pixels.</returns>
  235. public Vector2I WorldToViewport(Vector3 value) { return native.WorldToViewport(value); }
  236. /// <summary>
  237. /// Converts a point in world space to normalized device coordinates.
  238. /// </summary>
  239. /// <param name="value">3D point in world space.</param>
  240. /// <returns>2D point in normalized device coordinates ([-1, 1] range), relative to the camera's viewport.</returns>
  241. public Vector2 WorldToNDC(Vector3 value) { return native.WorldToNDC(value); }
  242. /// <summary>
  243. /// Converts a point in world space to view space coordinates.
  244. /// </summary>
  245. /// <param name="value">3D point in world space.</param>
  246. /// <returns>3D point relative to the camera's coordinate system.</returns>
  247. public Vector3 WorldToView(Vector3 value) { return native.WorldToView(value); }
  248. /// <summary>
  249. /// Converts a point in screen coordinates to viewport coordinates.
  250. /// </summary>
  251. /// <param name="value">2D point on the screen, in pixels.</param>
  252. /// <returns>2D point on the render target attached to the camera, in pixels.</returns>
  253. public Vector2I ScreenToViewport(Vector2I value) { return native.ScreenToViewport(value); }
  254. /// <summary>
  255. /// Converts a point in viewport coordinates to a point in world space.
  256. /// </summary>
  257. /// <param name="value">2D point on the render target attached to the camera, in pixels.</param>
  258. /// <param name="depth">Depth at which place the world point at. The depth is applied to the vector going from
  259. /// camera origin to the point on the near plane.</param>
  260. /// <returns>3D point in world space.</returns>
  261. public Vector3 ViewportToWorld(Vector2I value, float depth = 0.5f) { return native.ViewportToWorld(value, depth); }
  262. /// <summary>
  263. /// Converts a point in viewport coordinates to a point in view space.
  264. /// </summary>
  265. /// <param name="value">2D point on the render target attached to the camera, in pixels.</param>
  266. /// <param name="depth">Depth at which place the view point at. The depth is applied to the vector going from
  267. /// camera origin to the point on the near plane.</param>
  268. /// <returns>3D point in view space.</returns>
  269. public Vector3 ViewportToView(Vector2I value, float depth = 0.5f) { return native.ViewportToView(value, depth); }
  270. /// <summary>
  271. /// Converts a point in viewport coordinates to a point in normalized device coordinates.
  272. /// </summary>
  273. /// <param name="value">2D point on the render target attached to the camera, in pixels.</param>
  274. /// <returns>2D point in normalized device coordinates ([-1, 1] range), relative to the camera's viewport.</returns>
  275. public Vector2 ViewportToNDC(Vector2I value) { return native.ViewportToNDC(value); }
  276. /// <summary>
  277. /// Converts a point relative to camera's coordinate system (view space) into a point in world space.
  278. /// </summary>
  279. /// <param name="value">3D point in view space.</param>
  280. /// <returns>3D point in world space.</returns>
  281. public Vector3 ViewToWorld(Vector3 value) { return native.ViewToWorld(value); }
  282. /// <summary>
  283. /// Converts a point relative to camera's coordinate system (view space) to screen coordinates.
  284. /// </summary>
  285. /// <param name="value">3D point in view space.</param>
  286. /// <returns>2D point on the render target attached to the camera, in pixels.</returns>
  287. public Vector2I ViewToViewport(Vector3 value) { return native.ViewToViewport(value); }
  288. /// <summary>
  289. /// Converts a point relative to camera's coordinate system (view space) to normalized device coordinates.
  290. /// </summary>
  291. /// <param name="value">3D point in view space.</param>
  292. /// <returns>2D point in normalized device coordinates ([-1, 1] range), relative to the camera's viewport.</returns>
  293. public Vector2 ViewToNDC(Vector3 value) { return native.ViewToNDC(value); }
  294. /// <summary>
  295. /// Converts a point relative to camera's viewport in normalized device coordinates ([-1, 1] range) into a point in
  296. /// world space.
  297. /// </summary>
  298. /// <param name="value">2D point in normalized device coordinates.</param>
  299. /// <param name="depth">Depth at which place the world point at. The depth is applied to the vector going from
  300. /// camera origin to the point on the near plane.</param>
  301. /// <returns>3D point in world space.</returns>
  302. public Vector3 NDCToWorld(Vector2 value, float depth = 0.5f) { return native.NDCToWorld(value, depth); }
  303. /// <summary>
  304. /// Converts a point relative to camera's viewport in normalized device coordinates ([-1, 1] range) into a point in
  305. /// view space.
  306. /// </summary>
  307. /// <param name="value">2D point in normalized device coordinates.</param>
  308. /// <param name="depth">Depth at which place the world point at. The depth is applied to the vector going from
  309. /// camera origin to the point on the near plane.</param>
  310. /// <returns>3D point in view space.</returns>
  311. public Vector3 NDCToView(Vector2 value, float depth = 0.5f) { return native.NDCToView(value, depth); }
  312. /// <summary>
  313. /// Converts a point relative to camera's viewport in normalized device coordinates ([-1, 1] range) to viewport
  314. /// coordinates in pixels.
  315. /// </summary>
  316. /// <param name="value">2D point in normalized device coordinates.</param>
  317. /// <returns>2D point on the render target attached to the camera, in pixels.</returns>
  318. public Vector2I NDCToViewport(Vector2 value) { return native.NDCToViewport(value); }
  319. /// <summary>
  320. /// Converts a point in viewport coordinates to a ray in world space.
  321. /// </summary>
  322. /// <param name="value">2D point on the render target attached to the camera, in pixels.</param>
  323. /// <returns>A ray in world space with it's origin the selected point at the near frustum plane, pointing in the
  324. /// direction going from camera's origin towards a point on the near frustum plane.</returns>
  325. public Ray ViewportToWorldRay(Vector2I value) { return native.ViewportToWorldRay(value); }
  326. /// <summary>
  327. /// Converts a point in screen coordinates to a ray in world space.
  328. /// </summary>
  329. /// <param name="value">2D point on the screen, in pixels.</param>
  330. /// <returns>A ray in world space with it's origin the selected point at the near frustum plane, pointing in the
  331. /// direction going from camera's origin towards a point on the near frustum plane.</returns>
  332. public Ray ScreenToWorldRay(Vector2I value) { return native.ScreenToWorldRay(value); }
  333. /// <summary>
  334. /// Projects a point in view space to a point in normalized device coordinates. Similar to <see cref="ViewToNDC"/>
  335. /// but preserves the depth component.
  336. /// </summary>
  337. /// <param name="value">3D point in view space.</param>
  338. /// <returns>3D point in normalized device coordinates ([-1, 1] range), relative to the camera's viewport. Z value
  339. /// range depends on active render API.</returns>
  340. public Vector3 ProjectPoint(Vector3 value) { return native.ProjectPoint(value); }
  341. /// <summary>
  342. /// Un-rpojects a point in normalized device coordinates to a point in view space.
  343. /// </summary>
  344. /// <param name="value">3D point in normalized device coordinates ([-1, 1] range), relative to the camera's viewport.
  345. /// Z value range depends on active render API.</param>
  346. /// <returns>3D point in view space.</returns>
  347. public Vector3 UnprojectPoint(Vector3 value) { return native.UnprojectPoint(value); }
  348. /// <summary>
  349. /// Returns the width of the camera's frustum at the specified distance from the camera.
  350. /// </summary>
  351. /// <param name="distance">Distance along the axis the camera is looking at, in world units.</param>
  352. /// <returns>Frustum width, in world units. To find frustum height divide this by camera's aspect ratio. </returns>
  353. public float GetFrustumWidth(float distance)
  354. {
  355. if (ProjectionType == ProjectionType.Perspective)
  356. return distance * 2.0f * MathEx.Tan(FieldOfView * 0.5f);
  357. else
  358. return distance * 0.5f;
  359. }
  360. private void OnReset()
  361. {
  362. if (native != null)
  363. native.OnDestroy();
  364. native = new NativeCamera(SceneObject);
  365. // Restore saved values after reset
  366. native.aspectRatio = serializableData.aspectRatio;
  367. native.nearClipPlane = serializableData.nearClipPlane;
  368. native.farClipPlane = serializableData.farClipPlane;
  369. native.fieldOfView = serializableData.fieldOfView;
  370. native.viewportRect = serializableData.viewportRect;
  371. native.projectionType = serializableData.projectionType;
  372. native.orthoHeight = serializableData.orthoHeight;
  373. native.clearColor = serializableData.clearColor;
  374. native.clearDepth = serializableData.clearDepth;
  375. native.clearStencil = serializableData.clearStencil;
  376. native.clearFlags = serializableData.clearFlags;
  377. native.priority = serializableData.priority;
  378. native.layers = serializableData.layers;
  379. native.main = serializableData.main;
  380. // TODO - Make RenderTexture a resource so I can save/restore it?
  381. }
  382. private void OnUpdate()
  383. {
  384. native.UpdateView(SceneObject);
  385. }
  386. private void OnDestroy()
  387. {
  388. native.OnDestroy();
  389. }
  390. /// <summary>
  391. /// Holds all data the camera component needs to persist through serialization.
  392. /// </summary>
  393. [SerializeObject]
  394. internal class SerializableData
  395. {
  396. internal SerializableData()
  397. {
  398. postProcessSettings = PostProcessSettings.CreateDefault();
  399. }
  400. public float aspectRatio = 1.333f;
  401. public float nearClipPlane = 1.0f;
  402. public float farClipPlane = 1000.0f;
  403. public Degree fieldOfView = new Degree(90);
  404. public Rect2 viewportRect = new Rect2(0, 0, 1, 1);
  405. public ProjectionType projectionType = ProjectionType.Perspective;
  406. public float orthoHeight = 5.0f;
  407. public Color clearColor = new Color(83.0f / 255.0f, 83.0f / 255.0f, 83.0f / 255.0f);
  408. public float clearDepth = 1.0f;
  409. public ushort clearStencil;
  410. public ClearFlags clearFlags = ClearFlags.Color | ClearFlags.Depth | ClearFlags.Stencil;
  411. public int priority;
  412. public bool HDR = true;
  413. public PostProcessSettings postProcessSettings;
  414. public ulong layers = 0xFFFFFFFFFFFFFFFF;
  415. public bool main;
  416. }
  417. }
  418. /** @} */
  419. }