Camera.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.CompilerServices;
  5. using System.Text;
  6. namespace BansheeEngine
  7. {
  8. public class Camera : Component
  9. {
  10. private CameraHandler handler;
  11. [SerializeField]
  12. private SerializableData serializableData = new SerializableData();
  13. internal CameraHandler Handler
  14. {
  15. get { return handler; }
  16. }
  17. public float AspectRatio
  18. {
  19. get { return handler.aspectRatio; }
  20. set { handler.aspectRatio = value; serializableData.aspectRatio = value; }
  21. }
  22. public float NearClipPlane
  23. {
  24. get { return handler.nearClipPlane; }
  25. set { handler.nearClipPlane = value; serializableData.nearClipPlane = value; }
  26. }
  27. public float FarClipPlane
  28. {
  29. get { return handler.farClipPlane; }
  30. set { handler.farClipPlane = value; serializableData.farClipPlane = value; }
  31. }
  32. public Degree FieldOfView
  33. {
  34. get { return handler.fieldOfView; }
  35. set { handler.fieldOfView = value; serializableData.fieldOfView = value; }
  36. }
  37. public Rect2 ViewportRect
  38. {
  39. get { return handler.viewportRect; }
  40. set { handler.viewportRect = value; serializableData.viewportRect = value; }
  41. }
  42. public ProjectionType ProjectionType
  43. {
  44. get { return handler.projectionType; }
  45. set { handler.projectionType = value; serializableData.projectionType = value; }
  46. }
  47. public float OrthoHeight
  48. {
  49. get { return handler.orthoHeight; }
  50. set { handler.orthoHeight = value; serializableData.orthoHeight = value; }
  51. }
  52. public float OrthoWidth
  53. {
  54. get { return handler.orthoWidth; }
  55. }
  56. public Color ClearColor
  57. {
  58. get { return handler.clearColor; }
  59. set { handler.clearColor = value; serializableData.clearColor = value; }
  60. }
  61. public float ClearDepth
  62. {
  63. get { return handler.clearDepth; }
  64. set { handler.clearDepth = value; serializableData.clearDepth = value; }
  65. }
  66. public UInt16 ClearStencil
  67. {
  68. get { return handler.clearStencil; }
  69. set { handler.clearStencil = value; serializableData.clearStencil = value; }
  70. }
  71. public ClearFlags ClearFlags
  72. {
  73. get { return handler.clearFlags; }
  74. set { handler.clearFlags = value; serializableData.clearFlags = value; }
  75. }
  76. public int Priority
  77. {
  78. get { return handler.priority; }
  79. set { handler.priority = value; serializableData.priority = value; }
  80. }
  81. public UInt64 Layers
  82. {
  83. get { return handler.layers; }
  84. set { handler.layers = value; serializableData.layers = value; }
  85. }
  86. public Matrix4 ProjMatrix
  87. {
  88. get { return handler.projMatrix; }
  89. }
  90. public Matrix4 ProjMatrixInverse
  91. {
  92. get { return handler.projMatrixInv; }
  93. }
  94. public Matrix4 ViewMatrix
  95. {
  96. get { return handler.viewMatrix; }
  97. }
  98. public Matrix4 ViewMatrixInverse
  99. {
  100. get { return handler.viewMatrixInv; }
  101. }
  102. public int WidthPixels
  103. {
  104. get { return handler.widthPixels; }
  105. }
  106. public int HeightPixels
  107. {
  108. get { return handler.heightPixels; }
  109. }
  110. public RenderTarget Target
  111. {
  112. get { return handler.target; }
  113. set { handler.target = value; }
  114. }
  115. public Vector2I WorldToScreen(Vector3 value) { return handler.WorldToScreen(value); }
  116. public Vector2 WorldToClip(Vector3 value) { return handler.WorldToClip(value); }
  117. public Vector3 WorldToView(Vector3 value) { return handler.WorldToView(value); }
  118. public Vector3 ScreenToWorld(Vector2I value) { return handler.ScreenToWorld(value); }
  119. public Vector3 ScreenToView(Vector2I value) { return handler.ScreenToView(value); }
  120. public Vector2 ScreenToClip(Vector2I value) { return handler.ScreenToClip(value); }
  121. public Vector3 ViewToWorld(Vector3 value) { return handler.ViewToWorld(value); }
  122. public Vector2I ViewToScreen(Vector3 value) { return handler.ViewToScreen(value); }
  123. public Vector2 ViewToClip(Vector3 value) { return handler.ViewToClip(value); }
  124. public Vector3 ClipToWorld(Vector2 value) { return handler.ClipToWorld(value); }
  125. public Vector3 ClipToView(Vector2 value) { return handler.ClipToView(value); }
  126. public Vector2I ClipToScreen(Vector2 value) { return handler.ClipToScreen(value); }
  127. public Ray ScreenToWorldRay(Vector2I value) { return handler.ScreenToWorldRay(value); }
  128. public Vector3 ProjectPoint(Vector3 value) { return handler.ProjectPoint(value); }
  129. public Vector3 UnprojectPoint(Vector3 value) { return handler.UnprojectPoint(value); }
  130. private void OnInitialize()
  131. {
  132. serializableData.aspectRatio = 1.333f;
  133. serializableData.nearClipPlane = 1.0f;
  134. serializableData.farClipPlane = 1000.0f;
  135. serializableData.fieldOfView = new Degree(60);
  136. serializableData.viewportRect = new Rect2(0, 0, 1, 1);
  137. serializableData.projectionType = ProjectionType.Perspective;
  138. serializableData.layers = 0xFFFFFFFFFFFFFFFF;
  139. serializableData.clearColor = new Color(143.0f / 255.0f, 111.0f / 255.0f, 0);
  140. serializableData.clearDepth = 1.0f;
  141. serializableData.clearFlags = ClearFlags.Color | ClearFlags.Depth | ClearFlags.Stencil;
  142. }
  143. private void OnReset()
  144. {
  145. if (handler != null)
  146. handler.OnDestroy();
  147. handler = new CameraHandler(sceneObject);
  148. // Restore saved values after reset
  149. handler.aspectRatio = serializableData.aspectRatio;
  150. handler.nearClipPlane = serializableData.nearClipPlane;
  151. handler.farClipPlane = serializableData.farClipPlane;
  152. handler.fieldOfView = serializableData.fieldOfView;
  153. handler.viewportRect = serializableData.viewportRect;
  154. handler.projectionType = serializableData.projectionType;
  155. handler.orthoHeight = serializableData.orthoHeight;
  156. handler.clearColor = serializableData.clearColor;
  157. handler.clearDepth = serializableData.clearDepth;
  158. handler.clearStencil = serializableData.clearStencil;
  159. handler.clearFlags = serializableData.clearFlags;
  160. handler.priority = serializableData.priority;
  161. handler.layers = serializableData.layers;
  162. // TODO - Make RenderTexture a resource so I can save/restore it?
  163. }
  164. private void Update()
  165. {
  166. handler.UpdateView(sceneObject);
  167. }
  168. private void OnDestroy()
  169. {
  170. handler.OnDestroy();
  171. }
  172. [SerializeObject]
  173. private struct SerializableData
  174. {
  175. public float aspectRatio;
  176. public float nearClipPlane;
  177. public float farClipPlane;
  178. public Degree fieldOfView;
  179. public Rect2 viewportRect;
  180. public ProjectionType projectionType;
  181. public float orthoHeight;
  182. public Color clearColor;
  183. public float clearDepth;
  184. public UInt16 clearStencil;
  185. public ClearFlags clearFlags;
  186. public int priority;
  187. public UInt64 layers;
  188. }
  189. }
  190. }