Camera.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 coordinates relative to the camera's viewport.
  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 WorldToViewport(Vector3 value) { return native.WorldToViewport(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 coordinates to viewport coordinates.
  227. /// </summary>
  228. /// <param name="value">2D point on the screen, in pixels.</param>
  229. /// <returns>2D point on the render target attached to the camera, in pixels.</returns>
  230. public Vector2I ScreenToViewport(Vector2I value) { return native.ScreenToViewport(value); }
  231. /// <summary>
  232. /// Converts a point in viewport coordinates to a point in world space.
  233. /// </summary>
  234. /// <param name="value">2D point on the render target attached to the camera, in pixels.</param>
  235. /// <param name="depth">Depth at which place the world point at. The depth is applied to the vector going from
  236. /// camera origin to the point on the near plane.</param>
  237. /// <returns>3D point in world space.</returns>
  238. public Vector3 ViewportToWorld(Vector2I value, float depth = 0.5f) { return native.ViewportToWorld(value, depth); }
  239. /// <summary>
  240. /// Converts a point in viewport coordinates to a point in view space.
  241. /// </summary>
  242. /// <param name="value">2D point on the render target attached to the camera, in pixels.</param>
  243. /// <param name="depth">Depth at which place the view point at. The depth is applied to the vector going from
  244. /// camera origin to the point on the near plane.</param>
  245. /// <returns>3D point in view space.</returns>
  246. public Vector3 ViewportToView(Vector2I value, float depth = 0.5f) { return native.ViewportToView(value, depth); }
  247. /// <summary>
  248. /// Converts a point in viewport coordinates to a point in normalized device coordinates.
  249. /// </summary>
  250. /// <param name="value">2D point on the render target attached to the camera, in pixels.</param>
  251. /// <returns>2D point in normalized device coordinates ([-1, 1] range), relative to the camera's viewport.</returns>
  252. public Vector2 ViewportToNDC(Vector2I value) { return native.ViewportToNDC(value); }
  253. /// <summary>
  254. /// Converts a point relative to camera's coordinate system (view space) into a point in world space.
  255. /// </summary>
  256. /// <param name="value">3D point in view space.</param>
  257. /// <returns>3D point in world space.</returns>
  258. public Vector3 ViewToWorld(Vector3 value) { return native.ViewToWorld(value); }
  259. /// <summary>
  260. /// Converts a point relative to camera's coordinate system (view space) to screen coordinates.
  261. /// </summary>
  262. /// <param name="value">3D point in view space.</param>
  263. /// <returns>2D point on the render target attached to the camera, in pixels.</returns>
  264. public Vector2I ViewToViewport(Vector3 value) { return native.ViewToViewport(value); }
  265. /// <summary>
  266. /// Converts a point relative to camera's coordinate system (view space) to normalized device coordinates.
  267. /// </summary>
  268. /// <param name="value">3D point in view space.</param>
  269. /// <returns>2D point in normalized device coordinates ([-1, 1] range), relative to the camera's viewport.</returns>
  270. public Vector2 ViewToNDC(Vector3 value) { return native.ViewToNDC(value); }
  271. /// <summary>
  272. /// Converts a point relative to camera's viewport in normalized device coordinates ([-1, 1] range) into a point in
  273. /// world space.
  274. /// </summary>
  275. /// <param name="value">2D point in normalized device coordinates.</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 NDCToWorld(Vector2 value, float depth = 0.5f) { return native.NDCToWorld(value, depth); }
  280. /// <summary>
  281. /// Converts a point relative to camera's viewport in normalized device coordinates ([-1, 1] range) into a point in
  282. /// view space.
  283. /// </summary>
  284. /// <param name="value">2D point in normalized device coordinates.</param>
  285. /// <param name="depth">Depth at which place the world point at. The depth is applied to the vector going from
  286. /// camera origin to the point on the near plane.</param>
  287. /// <returns>3D point in view space.</returns>
  288. public Vector3 NDCToView(Vector2 value, float depth = 0.5f) { return native.NDCToView(value, depth); }
  289. /// <summary>
  290. /// Converts a point relative to camera's viewport in normalized device coordinates ([-1, 1] range) to viewport
  291. /// coordinates in pixels.
  292. /// </summary>
  293. /// <param name="value">2D point in normalized device coordinates.</param>
  294. /// <returns>2D point on the render target attached to the camera, in pixels.</returns>
  295. public Vector2I NDCToViewport(Vector2 value) { return native.NDCToViewport(value); }
  296. /// <summary>
  297. /// Converts a point in viewport coordinates to a ray in world space.
  298. /// </summary>
  299. /// <param name="value">2D point on the render target attached to the camera, in pixels.</param>
  300. /// <returns>A ray in world space with it's origin the selected point at the near frustum plane, pointing in the
  301. /// direction going from camera's origin towards a point on the near frustum plane.</returns>
  302. public Ray ViewportToWorldRay(Vector2I value) { return native.ViewportToWorldRay(value); }
  303. /// <summary>
  304. /// Converts a point in screen coordinates to a ray in world space.
  305. /// </summary>
  306. /// <param name="value">2D point on the screen, in pixels.</param>
  307. /// <returns>A ray in world space with it's origin the selected point at the near frustum plane, pointing in the
  308. /// direction going from camera's origin towards a point on the near frustum plane.</returns>
  309. public Ray ScreenToWorldRay(Vector2I value) { return native.ScreenToWorldRay(value); }
  310. /// <summary>
  311. /// Projects a point in view space to a point in normalized device coordinates. Similar to <see cref="ViewToNDC"/>
  312. /// but preserves the depth component.
  313. /// </summary>
  314. /// <param name="value">3D point in view space.</param>
  315. /// <returns>3D point in normalized device coordinates ([-1, 1] range), relative to the camera's viewport. Z value
  316. /// range depends on active render API.</returns>
  317. public Vector3 ProjectPoint(Vector3 value) { return native.ProjectPoint(value); }
  318. /// <summary>
  319. /// Un-rpojects a point in normalized device coordinates to a point in view space.
  320. /// </summary>
  321. /// <param name="value">3D point in normalized device coordinates ([-1, 1] range), relative to the camera's viewport.
  322. /// Z value range depends on active render API.</param>
  323. /// <returns>3D point in view space.</returns>
  324. public Vector3 UnprojectPoint(Vector3 value) { return native.UnprojectPoint(value); }
  325. /// <summary>
  326. /// Returns the width of the camera's frustum at the specified distance from the camera.
  327. /// </summary>
  328. /// <param name="distance">Distance along the axis the camera is looking at, in world units.</param>
  329. /// <returns>Frustum width, in world units. To find frustum height divide this by camera's aspect ratio. </returns>
  330. public float GetFrustumWidth(float distance)
  331. {
  332. if (ProjectionType == ProjectionType.Perspective)
  333. return distance * 2.0f * MathEx.Tan(FieldOfView * 0.5f);
  334. else
  335. return distance * 0.5f;
  336. }
  337. private void OnReset()
  338. {
  339. if (native != null)
  340. native.OnDestroy();
  341. native = new NativeCamera(SceneObject);
  342. // Restore saved values after reset
  343. native.aspectRatio = serializableData.aspectRatio;
  344. native.nearClipPlane = serializableData.nearClipPlane;
  345. native.farClipPlane = serializableData.farClipPlane;
  346. native.fieldOfView = serializableData.fieldOfView;
  347. native.viewportRect = serializableData.viewportRect;
  348. native.projectionType = serializableData.projectionType;
  349. native.orthoHeight = serializableData.orthoHeight;
  350. native.clearColor = serializableData.clearColor;
  351. native.clearDepth = serializableData.clearDepth;
  352. native.clearStencil = serializableData.clearStencil;
  353. native.clearFlags = serializableData.clearFlags;
  354. native.priority = serializableData.priority;
  355. native.layers = serializableData.layers;
  356. native.main = serializableData.main;
  357. // TODO - Make RenderTexture a resource so I can save/restore it?
  358. }
  359. private void OnUpdate()
  360. {
  361. native.UpdateView(SceneObject);
  362. }
  363. private void OnDestroy()
  364. {
  365. native.OnDestroy();
  366. }
  367. /// <summary>
  368. /// Holds all data the camera component needs to persist through serialization.
  369. /// </summary>
  370. [SerializeObject]
  371. internal class SerializableData
  372. {
  373. public float aspectRatio = 1.333f;
  374. public float nearClipPlane = 1.0f;
  375. public float farClipPlane = 1000.0f;
  376. public Degree fieldOfView = new Degree(90);
  377. public Rect2 viewportRect = new Rect2(0, 0, 1, 1);
  378. public ProjectionType projectionType = ProjectionType.Perspective;
  379. public float orthoHeight = 5.0f;
  380. public Color clearColor = new Color(83.0f / 255.0f, 83.0f / 255.0f, 83.0f / 255.0f);
  381. public float clearDepth = 1.0f;
  382. public ushort clearStencil;
  383. public ClearFlags clearFlags = ClearFlags.Color | ClearFlags.Depth | ClearFlags.Stencil;
  384. public int priority;
  385. public ulong layers = 0xFFFFFFFFFFFFFFFF;
  386. public bool main;
  387. }
  388. }
  389. }