Camera.cs 19 KB

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