Skip to content

Commit

Permalink
CameraComponent now can be updated by ignorant third party without an…
Browse files Browse the repository at this point in the history
…y harm. Up to now you had to reason about the aspect ratio. A helper visualizing the frustum can now just call Update without any side effect.
  • Loading branch information
gregsn committed Sep 11, 2020
1 parent aab1aa8 commit c4dffd8
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions sources/engine/Stride.Engine/Engine/CameraComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,25 @@ public CameraComponent(float nearClipPlane, float farClipPlane)
public bool UseCustomAspectRatio { get; set; }

/// <summary>
/// Gets or sets the aspect ratio.
/// Gets or sets the custom aspect ratio.
/// </summary>
/// <value>
/// The aspect ratio.
/// The custom aspect ratio.
/// </value>
/// <userdoc>The aspect ratio for the camera (when the Custom aspect ratio option is selected)</userdoc>
[DataMember(40)]
[DefaultValue(DefaultAspectRatio)]
public float AspectRatio { get; set; }
public float AspectRatio { get; set; } // TODO: this should be called CustomAspectRatio

/// <summary>
/// Gets the last used aspect ratio.
/// </summary>
/// <remarks>
/// This value is updated when calling <see cref="Update"/>.
/// It either holds the aspect ratio of the window that called Update or the manually set aspect ratio <see cref="AspectRatio"/> if <see cref="UseCustomAspectRatio"/> is <c>true</c>.
/// </remarks>
[DataMemberIgnore]
public float ActuallyUsedAspectRatio { get; private set; } = 1; // TODO: this should be called AspectRatio

/// <userdoc>The camera slot used in the graphics compositor)</userdoc>
[DataMember(50)]
Expand Down Expand Up @@ -203,8 +213,14 @@ public void Update()
/// <param name="screenAspectRatio">The current screen aspect ratio. If null, use the <see cref="AspectRatio"/> even if <see cref="UseCustomAspectRatio"/> is false.</param>
public void Update(float? screenAspectRatio)
{
// Calculates the aspect ratio
var aspectRatio = (screenAspectRatio.HasValue && !UseCustomAspectRatio) ? screenAspectRatio.Value : AspectRatio;
// Calculates the aspect ratio. We only set a new aspect ratio when instructed to. Don't fall back on the custom value, if not instructed to.
// By caching the actually used aspect ratio we are now free to call Update(null) at any time.
// A helper visualizing the state of the camera will not change it's state.
if (UseCustomAspectRatio)
ActuallyUsedAspectRatio = AspectRatio;
else
if (screenAspectRatio.HasValue)
ActuallyUsedAspectRatio = screenAspectRatio.Value;

// Calculates the View
if (!UseCustomViewMatrix)
Expand All @@ -228,10 +244,10 @@ public void Update(float? screenAspectRatio)
// TODO: Should we throw an error if Projection is not set?
if (!UseCustomProjectionMatrix)
{
// Calculates the aspect ratio
// Calculates the projection matrix
ProjectionMatrix = Projection == CameraProjectionMode.Perspective ?
Matrix.PerspectiveFovRH(MathUtil.DegreesToRadians(VerticalFieldOfView), aspectRatio, NearClipPlane, FarClipPlane) :
Matrix.OrthoRH(aspectRatio * OrthographicSize, OrthographicSize, NearClipPlane, FarClipPlane);
Matrix.PerspectiveFovRH(MathUtil.DegreesToRadians(VerticalFieldOfView), ActuallyUsedAspectRatio, NearClipPlane, FarClipPlane) :
Matrix.OrthoRH(ActuallyUsedAspectRatio * OrthographicSize, OrthographicSize, NearClipPlane, FarClipPlane);
}

// Update ViewProjectionMatrix
Expand Down

0 comments on commit c4dffd8

Please sign in to comment.