Skip to content

Instantly share code, notes, and snippets.

@maxattack
Created January 9, 2018 17:41
Show Gist options
  • Save maxattack/a7d0909b6c11744af29031a81c8873e2 to your computer and use it in GitHub Desktop.
Save maxattack/a7d0909b6c11744af29031a81c8873e2 to your computer and use it in GitHub Desktop.
Orbit Camera Degrees-of-Freedom
/*
Copyright (c) 2018 Max Kaufmann
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using UnityEngine;
namespace LittlePolygon {
public struct OrbitCameraDOF {
public Vector3 trackingPosition;
public Quaternion rotation;
public float distance;
public Vector2 framing;
}
public static class OrbitCameraExtensions {
public static void SetOrbit(this Camera cam, OrbitCameraDOF dof) {
var tanFOVY = Mathf.Tan(0.5f * Mathf.Deg2Rad * cam.fieldOfView);
var tanFOVX = tanFOVY * cam.aspect;
var screenToWorld = Mathf.Abs(dof.distance) * new Vector2(tanFOVX, tanFOVY);
var parallax = new Vector2(screenToWorld.x * dof.framing.x, screenToWorld.y * dof.framing.y);
var localOff = new Vector3(-parallax.x, -parallax.y, -dof.distance);
cam.transform.position = dof.trackingPosition + dof.rotation * localOff;
cam.transform.rotation = dof.rotation;
}
public static void GetOrbit(this Camera cam, Vector3 trackingPosition, out OrbitCameraDOF result) {
// easy params
result.trackingPosition = trackingPosition;
result.rotation = cam.transform.rotation;
// distance
var pos = cam.transform.position;
var toTrackingOff = trackingPosition - pos;
var fwd = result.rotation * Vector3.forward;
result.distance = Vector3.Dot(toTrackingOff, fwd);
// framing
var toCameraOff = pos - trackingPosition;
var parallax = Quaternion.Inverse(result.rotation) * toCameraOff;
var tanFOVY = Mathf.Tan(0.5f * Mathf.Deg2Rad * cam.fieldOfView);
var tanFOVX = tanFOVY * cam.aspect;
var screenToWorld = result.distance * new Vector2(tanFOVX, tanFOVY);
result.framing = new Vector2(-parallax.x / screenToWorld.x, -parallax.y / screenToWorld.y);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment