//================================================================================================================================ // // Copyright (c) 2015-2019 VisionStar Information Technology (Shanghai) Co., Ltd. All Rights Reserved. // EasyAR is the registered trademark or trademark of VisionStar Information Technology (Shanghai) Co., Ltd in China // and other countries for the augmented reality technology developed by VisionStar Information Technology (Shanghai) Co., Ltd. // //================================================================================================================================ using System; using System.Collections.Generic; using UnityEngine; namespace easyar { public class Display : IDisposable { private Dictionary rotations = new Dictionary(); #if UNITY_ANDROID && !UNITY_EDITOR private static AndroidJavaObject defaultDisplay; #endif public Display() { if (Application.platform == RuntimePlatform.Android) { InitializeAndroid(); } else if (Application.platform == RuntimePlatform.IPhonePlayer) { InitializeIOS(); } } ~Display() { DeleteAndroidJavaObjects(); } public int Rotation { get { if (Application.platform == RuntimePlatform.Android) { #if UNITY_ANDROID && !UNITY_EDITOR var rotation = defaultDisplay.Call("getRotation"); return rotations[rotation]; #endif } else if (Application.platform == RuntimePlatform.IPhonePlayer) { return rotations[(int)Screen.orientation]; } return 0; } } public void Dispose() { DeleteAndroidJavaObjects(); GC.SuppressFinalize(this); } public Matrix4x4 GetCompensation(CameraParameters camParams) { var screenRotation = Rotation; var imageRotation = camParams.imageOrientation(screenRotation) / 180f * Mathf.PI; Matrix4x4 rotationMatrix = Matrix4x4.identity; rotationMatrix.m00 = Mathf.Cos(-imageRotation); rotationMatrix.m01 = -Mathf.Sin(-imageRotation); rotationMatrix.m10 = Mathf.Sin(-imageRotation); rotationMatrix.m11 = Mathf.Cos(-imageRotation); return rotationMatrix; } // pointInView should be normalized to [0, 1] public Vector2 ImageCoordinatesFromScreenCoordinates(Vector2 pointInView, CameraParameters cameraParameters, Camera camera) { return cameraParameters.imageCoordinatesFromScreenCoordinates( camera.aspect, Rotation, true, false, new Vec2F(pointInView.x, 1 - pointInView.y)).ToUnityVector(); } private void InitializeIOS() { rotations[(int)ScreenOrientation.Portrait] = 0; rotations[(int)ScreenOrientation.LandscapeLeft] = 90; rotations[(int)ScreenOrientation.PortraitUpsideDown] = 180; rotations[(int)ScreenOrientation.LandscapeRight] = 270; } private void InitializeAndroid() { #if UNITY_ANDROID && !UNITY_EDITOR using (var surfaceClass = new AndroidJavaClass("android.view.Surface")) using (var contextClass = new AndroidJavaClass("android.content.Context")) using (var windowService = contextClass.GetStatic("WINDOW_SERVICE")) using (var unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) using (var currentActivity = unityPlayerClass.GetStatic("currentActivity")) using (var systemService = currentActivity.Call("getSystemService", windowService)) { defaultDisplay = systemService.Call("getDefaultDisplay"); rotations[surfaceClass.GetStatic("ROTATION_0")] = 0; rotations[surfaceClass.GetStatic("ROTATION_90")] = 90; rotations[surfaceClass.GetStatic("ROTATION_180")] = 180; rotations[surfaceClass.GetStatic("ROTATION_270")] = 270; } #endif } private void DeleteAndroidJavaObjects() { #if UNITY_ANDROID && !UNITY_EDITOR if (defaultDisplay != null) { defaultDisplay.Dispose(); } #endif } } }