Camera.cs 19 KB

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