From 702b4f149fc73735e688cb9900e2dd3c310c5c99 Mon Sep 17 00:00:00 2001 From: Adam Mitchell Date: Fri, 3 May 2019 10:44:14 -0700 Subject: [PATCH 1/2] right mouse camera controls --- .../Examples/OrbitCameraController.cs | 66 +++++++++++-------- 1 file changed, 40 insertions(+), 26 deletions(-) diff --git a/UnityGLTF/Assets/UnityGLTF/Examples/OrbitCameraController.cs b/UnityGLTF/Assets/UnityGLTF/Examples/OrbitCameraController.cs index 071071fe7..548e2d3ec 100644 --- a/UnityGLTF/Assets/UnityGLTF/Examples/OrbitCameraController.cs +++ b/UnityGLTF/Assets/UnityGLTF/Examples/OrbitCameraController.cs @@ -3,10 +3,11 @@ // Taken from http://wiki.unity3d.com/index.php?title=MouseOrbitImproved namespace UnityGLTF.Examples { + [RequireComponent(typeof(Camera))] [AddComponentMenu("Camera-Control/Mouse Orbit with zoom")] public class OrbitCameraController : MonoBehaviour { - public Transform target; + public Vector3 targetPosition = Vector3.zero; public float distance = 5.0f; public float xSpeed = 120.0f; public float ySpeed = 120.0f; @@ -18,16 +19,20 @@ public class OrbitCameraController : MonoBehaviour public float distanceMin = .5f; public float distanceMax = 150f; + private Camera camera; + private Rigidbody cameraRigidBody; - float x = 0.0f; - float y = 0.0f; + private float x = 0.0f; + private float y = 0.0f; Quaternion rotation; // Use this for initialization void Start() { + camera = GetComponent(); + Vector3 angles = transform.eulerAngles; x = angles.y; y = angles.x; @@ -44,38 +49,47 @@ void Start() void LateUpdate() { - if (target) + var height = Display.main.renderingHeight; + var width = Display.main.renderingWidth; + + if (Input.GetMouseButton(0)) { - if (Input.GetMouseButton(0)) - { - x += Input.GetAxis("Mouse X") * xSpeed * 0.06f; - y -= Input.GetAxis("Mouse Y") * ySpeed * 0.06f; + x += Input.GetAxis("Mouse X") * xSpeed * 0.06f; + y -= Input.GetAxis("Mouse Y") * ySpeed * 0.06f; - y = ClampAngle(y, yMinLimit, yMaxLimit); + y = ClampAngle(y, yMinLimit, yMaxLimit); - rotation = Quaternion.Euler(y, x, 0); - } + rotation = Quaternion.Euler(y, x, 0); + } + else if (Input.GetMouseButton(1)) + { + var distancePerPixel = 4.0f * distance * Mathf.Tan(camera.fieldOfView / 2.0f) / height; - var height = Display.main.renderingHeight; - var width = Display.main.renderingWidth; + float deltaX = Input.GetAxis("Mouse X") * distancePerPixel; + float deltaY = Input.GetAxis("Mouse Y") * distancePerPixel; - var mouseOverRenderArea = - Input.mousePosition.x >= 0 && - Input.mousePosition.x <= width && - Input.mousePosition.y >= 0 && - Input.mousePosition.y <= height; + Vector3 deltaRight = transform.right * deltaX; + Vector3 deltaUp = transform.up * deltaY; - if (Input.GetMouseButton(0) || mouseOverRenderArea) - { - distance = Mathf.Clamp(distance * Mathf.Exp(-Input.GetAxis("Mouse ScrollWheel") * zoomSpeed), distanceMin, distanceMax); - } + targetPosition += deltaRight + deltaUp; + } - Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance); - Vector3 position = rotation * negDistance + target.position; + var mouseOverRenderArea = + Input.mousePosition.x >= 0 && + Input.mousePosition.x <= width && + Input.mousePosition.y >= 0 && + Input.mousePosition.y <= height; - transform.rotation = rotation; - transform.position = position; + if (Input.GetMouseButton(0) || mouseOverRenderArea) + { + distance = Mathf.Clamp(distance * Mathf.Exp(-Input.GetAxis("Mouse ScrollWheel") * zoomSpeed), distanceMin, distanceMax); } + + Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance); + Vector3 position = rotation * negDistance + targetPosition; + + transform.rotation = rotation; + transform.position = position; } public static float ClampAngle(float angle, float min, float max) From 4e1ff18b328e8002e4bd0316f6da40aec88abd30 Mon Sep 17 00:00:00 2001 From: Adam Mitchell Date: Fri, 3 May 2019 11:22:59 -0700 Subject: [PATCH 2/2] More accurate right click mouse motion --- .../Examples/OrbitCameraController.cs | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/UnityGLTF/Assets/UnityGLTF/Examples/OrbitCameraController.cs b/UnityGLTF/Assets/UnityGLTF/Examples/OrbitCameraController.cs index 548e2d3ec..a3d678389 100644 --- a/UnityGLTF/Assets/UnityGLTF/Examples/OrbitCameraController.cs +++ b/UnityGLTF/Assets/UnityGLTF/Examples/OrbitCameraController.cs @@ -25,6 +25,7 @@ public class OrbitCameraController : MonoBehaviour private float x = 0.0f; private float y = 0.0f; + private Vector3 prevMousePosition; Quaternion rotation; @@ -38,6 +39,8 @@ void Start() y = angles.x; rotation = Quaternion.Euler(y, x, 0); + prevMousePosition = Input.mousePosition; + cameraRigidBody = GetComponent(); // Make the rigid body not change rotation @@ -63,15 +66,10 @@ void LateUpdate() } else if (Input.GetMouseButton(1)) { - var distancePerPixel = 4.0f * distance * Mathf.Tan(camera.fieldOfView / 2.0f) / height; - - float deltaX = Input.GetAxis("Mouse X") * distancePerPixel; - float deltaY = Input.GetAxis("Mouse Y") * distancePerPixel; + var prevMouseWorldPosition = ProjectScreenPointToTargetPlane(prevMousePosition); + var mouseWorldPosition = ProjectScreenPointToTargetPlane(Input.mousePosition); - Vector3 deltaRight = transform.right * deltaX; - Vector3 deltaUp = transform.up * deltaY; - - targetPosition += deltaRight + deltaUp; + targetPosition += prevMouseWorldPosition - mouseWorldPosition; } var mouseOverRenderArea = @@ -90,6 +88,16 @@ void LateUpdate() transform.rotation = rotation; transform.position = position; + + prevMousePosition = Input.mousePosition; + } + + private Vector3 ProjectScreenPointToTargetPlane(Vector3 screenPosition) + { + var ray = camera.ScreenPointToRay(screenPosition); + var planeDistance = distance / Mathf.Cos(Mathf.Deg2Rad * Vector3.Angle(camera.transform.forward, ray.direction)); + + return camera.transform.position + (ray.direction * planeDistance); } public static float ClampAngle(float angle, float min, float max)