Camera.cs 20 KB

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