Camera.cs 22 KB

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