Skip to content

Commit

Permalink
support for dynamically toggling perspective/ortho on cameras, auto-f…
Browse files Browse the repository at this point in the history
…itting for ortho
  • Loading branch information
rms80 committed Jun 21, 2018
1 parent adf7dfb commit 09265c0
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
52 changes: 48 additions & 4 deletions animation/CameraAnimator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,41 @@ public void AnimatePanZoomFocus(Vector3f focusPoint, CoordSpace eSpace, float di
}



/// <summary>
/// </summary>
public void AnimatePanZoomFocusOrtho(Vector3f focusPoint, CoordSpace eSpace, float targetHeight, float duration)
{
if (duration > 0 && ShowTargetDuringAnimations)
UseCamera.SetTargetVisible(true);

Vector3f focusPointS = (eSpace == CoordSpace.WorldCoords) ? UseScene.ToSceneP(focusPoint) : focusPoint;
Vector3f startFocusS = UseScene.ToSceneP(UseCamera.GetTarget());
float startHeight = UseCamera.OrthoHeight;

Action<float> tweenF = (t) => {
float smooth_t = MathUtil.WyvillRise01(t);
Vector3f newTargetS = Vector3f.Lerp(startFocusS, focusPointS, smooth_t);
UseCamera.Manipulator().PanFocusOnScenePoint(UseScene, UseCamera, newTargetS);

float toHeight = MathUtil.Lerp(startHeight, targetHeight, t);
float curHeight = UseCamera.OrthoHeight;
float dh = toHeight - curHeight;
UseCamera.Manipulator().SceneZoom(UseScene, UseCamera, -dh);
};

if (duration > 0) {
TweenAnimator anim = new TweenAnimator(tweenF, duration) {
OnCompletedF = () => { UseCamera.SetTargetVisible(false); }
};
UseScene.ObjectAnimator.Register(anim);
} else
tweenF(1.0f);
}




/// <summary>
/// Animate camera so that centerPt moves to center of camera, and width is visible.
/// Camera target is also set to centerPt
Expand All @@ -129,9 +164,14 @@ public void AnimateFitWidthToView(Vector3f centerPt, float width, CoordSpace eSp
{
if (eSpace != CoordSpace.WorldCoords)
width = UseScene.ToWorldDimension(width);
float fFitDistW = UseCamera.Manipulator().GetFitWidthCameraDistance(width);
Vector3f focusPointW = (eSpace == CoordSpace.WorldCoords) ? centerPt : UseScene.ToWorldP(centerPt);
AnimatePanZoomFocus(focusPointW, CoordSpace.WorldCoords, fFitDistW, duration);
if (UseCamera.IsOrthographic) {
float targetHeight = UseCamera.AspectRatio * width;
AnimatePanZoomFocusOrtho(focusPointW, CoordSpace.WorldCoords, targetHeight, duration);
} else {
float fFitDistW = UseCamera.Manipulator().GetFitWidthCameraDistance(width);
AnimatePanZoomFocus(focusPointW, CoordSpace.WorldCoords, fFitDistW, duration);
}
}


Expand All @@ -143,9 +183,13 @@ public void AnimateFitHeightToView(Vector3f centerPt, float height, CoordSpace e
{
if (eSpace != CoordSpace.WorldCoords)
height = UseScene.ToWorldDimension(height);
float fFitDistW = UseCamera.Manipulator().GetFitHeightCameraDistance(height);
Vector3f focusPointW = (eSpace == CoordSpace.WorldCoords) ? centerPt : UseScene.ToWorldP(centerPt);
AnimatePanZoomFocus(focusPointW, CoordSpace.WorldCoords, fFitDistW, duration);
if (UseCamera.IsOrthographic) {
AnimatePanZoomFocusOrtho(focusPointW, CoordSpace.WorldCoords, height, duration);
} else {
float fFitDistW = UseCamera.Manipulator().GetFitHeightCameraDistance(height);
AnimatePanZoomFocus(focusPointW, CoordSpace.WorldCoords, fFitDistW, duration);
}
}


Expand Down
1 change: 1 addition & 0 deletions platform/fCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public Vector3f Right()

public bool IsOrthographic {
get { return camera.orthographic; }
set { camera.orthographic = value; }
}

//https://docs.unity3d.com/ScriptReference/Camera-orthographicSize.html
Expand Down
25 changes: 25 additions & 0 deletions view/CameraTracking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,13 @@ public CameraTracking() {
fCamera hudCamera;
fCamera uiCamera;
fCamera cursorCamera;

bool is_orthographic_view = false;

FContext controller;



public fCamera MainCamera {
get { return mainCamera; }
}
Expand Down Expand Up @@ -145,6 +149,8 @@ public void Initialize (FContext controller) {
}
}

is_orthographic_view = mainCamera.IsOrthographic;

List<Camera> newCameras = new List<Camera>();

Vector3f mainPos = mainCamera.GetPosition();
Expand Down Expand Up @@ -262,6 +268,25 @@ public void UpdateMainCamFarDistance(float distance)
cursorCamera.FarClipPlane = distance;
}


/// <summary>
/// configure the 3D-view cameras for orthographic/perspective
/// </summary>
public void UpdateOrthographic(bool orthographic)
{
if (is_orthographic_view == orthographic)
return;
if (mainCamera != null)
mainCamera.IsOrthographic = orthographic;
if (widgetCamera != null)
widgetCamera.IsOrthographic = orthographic;
if (hudCamera != null)
hudCamera.IsOrthographic = orthographic;
if (cursorCamera != null)
cursorCamera.IsOrthographic = orthographic;
is_orthographic_view = orthographic;
}

}

}

0 comments on commit 09265c0

Please sign in to comment.