public static OrthographicCameraComponent Clone(OrthographicCameraComponent com) { var copied = new OrthographicCameraComponent(com.Width, com.prevScreenHeight); copied.Copy(com); return(copied); }
public bool TryChangeCameraDistance(ref float delta, ref Vector3 zoomAround, out OrthographicCameraComponent changed) { changed = null; // Handle the 'zoomAround' point var target = Position + LookDirection; var relativeTarget = zoomAround - target; var relativePosition = zoomAround - Position; if (relativePosition.Length() < 1e-4) { if (delta > 0) //If Zoom out from very close distance, increase the initial relativePosition { relativePosition.Normalize(); relativePosition /= 10; } else //If Zoom in too close, stop it. { return(false); } } var f = Math.Pow(2.5, delta); var newRelativePosition = relativePosition * (float)f; var newRelativeTarget = relativeTarget * (float)f; var newTarget = zoomAround - newRelativeTarget; var newPosition = zoomAround - newRelativePosition; var newDistance = (newPosition - zoomAround).Length(); var oldDistance = (Position - zoomAround).Length(); if (newDistance > FarPlaneDistance && (oldDistance < FarPlaneDistance || newDistance > oldDistance)) { var ratio = (newDistance - FarPlaneDistance) / newDistance; f *= 1 - ratio; newRelativePosition = relativePosition * (float)f; newRelativeTarget = relativeTarget * (float)f; newTarget = zoomAround - newRelativeTarget; newPosition = zoomAround - newRelativePosition; delta = (float)(Math.Log(f) / Math.Log(2.5)); } if (newDistance < NearPlaneDistance && (oldDistance > NearPlaneDistance || newDistance < oldDistance)) { var ratio = (NearPlaneDistance - newDistance) / newDistance; f *= (1 + ratio); newRelativePosition = relativePosition * (float)f; newRelativeTarget = relativeTarget * (float)f; newTarget = zoomAround - newRelativeTarget; newPosition = zoomAround - newRelativePosition; delta = (float)(Math.Log(f) / Math.Log(2.5)); } var newLookDirection = newTarget - newPosition; changed = new OrthographicCameraComponent(Width, prevScreenHeight); changed.Copy(this); //new data changed.LookDirection = newLookDirection.Normalized(); changed.Position = newPosition; return(true); }