Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
CameraComponent.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using SiliconStudio.Core.Serialization.Converters;
5 using SiliconStudio.Paradox.Effects;
6 using SiliconStudio.Paradox.EntityModel;
7 using SiliconStudio.Core;
8 using SiliconStudio.Core.Mathematics;
9 
10 namespace SiliconStudio.Paradox.Engine
11 {
12  /// <summary>
13  /// Describes the camera projection and view.
14  /// </summary>
15  [DataConverter(AutoGenerate = true)]
16  [DataContract("CameraComponent")]
17  public sealed class CameraComponent : EntityComponent
18  {
19  private float focusDistance;
20 
21  /// <summary>
22  /// The property key of this component.
23  /// </summary>
25 
26  /// <summary>
27  /// Create a new <see cref="CameraComponent"/> instance.
28  /// </summary>
29  public CameraComponent()
30  : this(null, 0 ,0)
31  {
32  }
33 
34  /// <summary>
35  /// Create a new <see cref="CameraComponent"/> instance with the provided target, near plane and far plane.
36  /// </summary>
37  /// <param name="target">The entity to use as target.</param>
38  /// <param name="nearPlane">The near plane value</param>
39  /// <param name="farPlane">The far plane value</param>
40  public CameraComponent(Entity target, float nearPlane, float farPlane)
41  {
42  AspectRatio = 16f / 9f;
43  Target = target;
44  TargetUp = Vector3.UnitY;
45  NearPlane = nearPlane;
46  FarPlane = farPlane;
47  VerticalFieldOfView = (float)Math.PI * 0.4f;
48  }
49 
50  /// <summary>
51  /// Associates an entity with this camera component.
52  /// </summary>
53  /// <param name="name">The name of entity.</param>
54  /// <returns>This CameraComponent.</returns>
55  [Obsolete("This method will be removed in a future release")]
56  public CameraComponent WithEntity(string name)
57  {
58  // By default create an entity on the CameraComponent
59  // This can be overrident later
60  var entity = new Entity(name);
61  entity.Add(this);
62 
63  Entity = entity;
64  return this;
65  }
66 
67  /// <summary>
68  /// Gets or sets the vertical field of view.
69  /// </summary>
70  /// <value>
71  /// The vertical field of view.
72  /// </value>
73  [DataMemberConvert]
74  public float VerticalFieldOfView { get; set; }
75 
76  /// <summary>
77  /// Gets or sets the near plane distance.
78  /// </summary>
79  /// <value>
80  /// The near plane distance.
81  /// </value>
82  [DataMemberConvert]
83  public float NearPlane { get; set; }
84 
85  /// <summary>
86  /// Gets or sets the far plane distance.
87  /// </summary>
88  /// <value>
89  /// The far plane distance.
90  /// </value>
91  [DataMemberConvert]
92  public float FarPlane { get; set; }
93 
94  /// <summary>
95  /// Gets or sets the aspect ratio.
96  /// </summary>
97  /// <value>
98  /// The aspect ratio.
99  /// </value>
100  [DataMemberConvert]
101  public float AspectRatio { get; set; }
102 
103  /// <summary>
104  /// Gets or sets the target this camera is pointing to. May be null.
105  /// </summary>
106  /// <value>The target.</value>
107  [DataMemberConvert]
108  public Entity Target { get; set; }
109 
110  /// <summary>
111  /// Gets or sets the up direction when using a target (for LookAt).
112  /// </summary>
113  /// <value>
114  /// The up direction when using a target (for LookAt).
115  /// </value>
116  [DataMemberConvert]
117  public Vector3 TargetUp { get; set; }
118 
119  /// <summary>
120  /// Gets or sets a value indicating whether [auto focus].
121  /// </summary>
122  /// <value><c>true</c> if [auto focus]; otherwise, <c>false</c>.</value>
123  [DataMemberConvert]
124  public bool AutoFocus { get; set; }
125 
126  /// <summary>
127  /// Gets or sets the focus distance.
128  /// </summary>
129  /// <value>The focus distance.</value>
130  [DataMemberConvert]
131  public float FocusDistance
132  {
133  get
134  {
135  if (AutoFocus)
136  return 0.0f;
137 
138  if (Entity != null && Target != null)
139  {
140  var eye = Entity.Transformation.WorldMatrix.TranslationVector;
141  var target = Target.Transformation.WorldMatrix.TranslationVector;
142  return Vector3.Distance(eye, target);
143  }
144 
145  return focusDistance;
146  }
147 
148  set
149  {
150  if (AutoFocus)
151  {
152  return;
153  }
154 
155  if (Entity == null || Target == null)
156  {
157  focusDistance = value;
158  }
159  }
160  }
161 
162  /// <summary>
163  /// Gets or sets a value indicating whether to use custom <see cref="ViewMatrix"/>. Default is <c>false</c>
164  /// </summary>
165  /// <value><c>true</c> if use custom <see cref="ViewMatrix"/>; otherwise, <c>false</c>.</value>
166  [DataMemberConvert]
167  public bool UseViewMatrix { get; set; }
168 
169  /// <summary>
170  /// Gets or sets the local view matrix, only used when <see cref="UseViewMatrix"/> is <c>true</c>.
171  /// </summary>
172  /// <value>The local view matrix.</value>
173  [DataMemberConvert]
174  public Matrix ViewMatrix { get; set; }
175 
176  /// <summary>
177  /// Gets or sets a value indicating whether to use custom <see cref="ProjectionMatrix"/>. Default is <c>false</c>
178  /// </summary>
179  /// <value><c>true</c> if use custom <see cref="ProjectionMatrix"/>; otherwise, <c>false</c>.</value>
180  [DataMemberConvert]
181  public bool UseProjectionMatrix { get; set; }
182 
183  /// <summary>
184  /// Gets or sets the local projection matrix, only used when <see cref="UseProjectionMatrix"/> is <c>true</c>.
185  /// </summary>
186  /// <value>The local projection matrix.</value>
187  [DataMemberConvert]
188  public Matrix ProjectionMatrix { get; set; }
189 
190  /// <summary>
191  /// Gets or sets the position.
192  /// </summary>
193  /// <value>The position.</value>
194  public Vector3 Position
195  {
196  get
197  {
198  return EnsureEntity.Transformation.Translation;
199  }
200 
201  set
202  {
203  EnsureEntity.Transformation.Translation = value;
204  }
205  }
206 
207  /// <summary>
208  /// Calculates the projection matrix and view matrix.
209  /// </summary>
210  /// <param name="projection">The projection matrix.</param>
211  /// <param name="viewMatrix">The view matrix.</param>
212  public void Calculate(out Matrix projection, out Matrix viewMatrix)
213  {
214  // Calculates the View
215  if (UseViewMatrix)
216  {
217  // We are using a ViewMatrix Matrix that is overriding Entity/Target matrices
218  viewMatrix = ViewMatrix;
219  }
220  else
221  {
222  if (Target != null)
223  {
224  // Build a view matrix from the Entity position and Target
225  // Currently use Y in camera local space as Up axis (need separate TargetUp that we multiply with WorldMatrix?)
226  var transformation = EnsureEntity.Transformation;
227  var targetUp = TargetUp;
228  Vector3.TransformNormal(ref targetUp, ref transformation.WorldMatrix, out targetUp);
229  viewMatrix = Matrix.LookAtRH(transformation.WorldMatrix.TranslationVector, Target.Transformation.WorldMatrix.TranslationVector, targetUp);
230  }
231  else
232  {
233  // TODO: determine which axis of the camera to look from
234  var worldMatrix = EnsureEntity.Transformation.WorldMatrix;
235  Matrix.Invert(ref worldMatrix, out viewMatrix);
236  }
237  }
238 
239  // Calculates the projection
240  if (UseProjectionMatrix)
241  {
242  projection = ProjectionMatrix;
243  }
244  else
245  {
246  Matrix.PerspectiveFovRH(VerticalFieldOfView, AspectRatio, NearPlane, FarPlane, out projection);
247  }
248  }
249 
250  public override PropertyKey DefaultKey
251  {
252  get { return Key; }
253  }
254  }
255 
256  public static class CameraComponentExtensions
257  {
258  public static CameraComponent GetCamera(this RenderPass pass)
259  {
260  return pass.GetProcessor<CameraSetter>().Camera;
261  }
262 
263  public static void SetCamera(this RenderPass pass, CameraComponent camera)
264  {
265  pass.GetProcessor<CameraSetter>().Camera = camera;
266  }
267  }
268 }
void Calculate(out Matrix projection, out Matrix viewMatrix)
Calculates the projection matrix and view matrix.
Game entity. It usually aggregates multiple EntityComponent
Definition: Entity.cs:28
static void SetCamera(this RenderPass pass, CameraComponent camera)
Base class for converters to/from a data type.
Represents a three dimensional mathematical vector.
Definition: Vector3.cs:42
static CameraComponent GetCamera(this RenderPass pass)
A processor that updates camera view and projection along the setup of RenderTargetSetter ...
Definition: CameraSetter.cs:16
Describes the camera projection and view.
CameraComponent(Entity target, float nearPlane, float farPlane)
Create a new CameraComponent instance with the provided target, near plane and far plane...
CameraComponent()
Create a new CameraComponent instance.
RenderPass is a hierarchy that defines how to collect and render meshes.
Definition: RenderPass.cs:19
A class that represents a tag propety.
Definition: PropertyKey.cs:17
CameraComponent WithEntity(string name)
Associates an entity with this camera component.
Represents a 4x4 mathematical matrix.
Definition: Matrix.cs:47