//public Text test2;

        public override void OnTrackSuccess(string id, string name, Matrix4x4 poseMatrix)
        {
            Renderer[] rendererComponents = GetComponentsInChildren <Renderer>(true);
            Collider[] colliderComponents = GetComponentsInChildren <Collider>(true);

            // canvas set by me to augment UI elements
            Canvas test1 = GetComponentInChildren <Canvas>(true);

            //Enable canvas
            test1.enabled = true;


            // Enable renderers
            foreach (Renderer component in rendererComponents)
            {
                component.enabled = true;
            }

            // Enable colliders
            foreach (Collider component in colliderComponents)
            {
                component.enabled = true;
            }

            transform.position   = MatrixUtils.PositionFromMatrix(poseMatrix);
            transform.rotation   = MatrixUtils.QuaternionFromMatrix(poseMatrix);
            transform.localScale = MatrixUtils.ScaleFromMatrix(poseMatrix);
        }
        void Update()
        {
            // If world center is target then tracking pose should be set to main camera's transform
            if (worldCenterMode == WorldCenterMode.TARGET)
            {
                TrackingState  trackingState  = TrackerManager.GetInstance().GetTrackingState();
                TrackingResult trackingResult = trackingState.GetTrackingResult();
                if (trackingResult.GetCount() > 0)
                {
                    Trackable trackable  = trackingResult.GetTrackable(0);
                    Matrix4x4 targetPose = trackable.GetTargetPose().inverse;

                    if (targetPose == Matrix4x4.zero)
                    {
                        return;
                    }

                    Quaternion rotation = Quaternion.Euler(90, 0, 0);
                    Matrix4x4  m        = Matrix4x4.TRS(new Vector3(0, 0, 0), rotation, new Vector3(1, 1, 1));
                    targetPose = m * targetPose;

                    Camera.main.transform.position   = MatrixUtils.PositionFromMatrix(targetPose);
                    Camera.main.transform.rotation   = MatrixUtils.QuaternionFromMatrix(targetPose);
                    Camera.main.transform.localScale = MatrixUtils.ScaleFromMatrix(targetPose);
                }
            }
        }
示例#3
0
        /// <summary></summary>
        /// <returns>tracking pose matrix</returns>
        public Matrix4x4 GetPose()
        {
            NativeAPI.maxst_Trackable_getPose(cPtr, glPoseMatrix);

            if (AbstractARManager.Instance.WorldCenterModeSetting ==
                AbstractARManager.WorldCenterMode.TARGET)
            {
                return(Matrix4x4.identity);
            }
            else
            {
                Matrix4x4  tempPose = MatrixUtils.GetUnityPoseMatrix(glPoseMatrix);
                Quaternion rotation = Quaternion.Euler(90, 0, 0);
                Matrix4x4  m        = Matrix4x4.TRS(AbstractARManager.Instance.GetARCamera().transform.position, rotation, new Vector3(1, 1, 1));

                // First. Change world matrix
                tempPose = m * tempPose;

                rotation = Quaternion.Euler(-90, 0, 0);
                m        = Matrix4x4.TRS(Vector3.zero, rotation, new Vector3(1, 1, 1));

                // Second. Change local matrix
                tempPose = tempPose * m;

                return(tempPose);
            }
        }
        /// <summary>
        /// Get projection matrix
        /// </summary>
        /// <returns>Matrix4x4 projection matrix</returns>
        public Matrix4x4 GetProjectionMatrix()
        {
            float[] glProjection = new float[16];

            NativeAPI.CameraDevice_getProjectionMatrix(glProjection);

            return(MatrixUtils.ConvertGLProjectionToUnityProjection(glProjection));
        }
        /// <summary>
        /// Create sterescopic camera
        /// </summary>
        /// <param name="cameraTransform"></param>
        public void CreateWearableEye(Transform cameraTransform)
        {
            if (eyeLeft == null)
            {
                eyeLeft = new GameObject("EyeLeft");
                eyeLeft.AddComponent <Camera>();
            }

            if (eyeRight == null)
            {
                eyeRight = new GameObject("EyeRight");
                eyeRight.AddComponent <Camera>();
            }

            eyeLeft.transform.parent  = cameraTransform;
            eyeRight.transform.parent = cameraTransform;

            eyeLeft.transform.localPosition = Vector3.zero;
            eyeLeft.transform.localRotation = Quaternion.identity;
            eyeLeft.transform.localScale    = Vector3.one;

            eyeRight.transform.localPosition = Vector3.zero;
            eyeRight.transform.localRotation = Quaternion.identity;
            eyeRight.transform.localScale    = Vector3.one;

            Camera mainCamera  = cameraTransform.GetComponent <Camera>();
            Camera leftCamera  = eyeLeft.GetComponent <Camera>();
            Camera rightCamera = eyeRight.GetComponent <Camera>();

            CameraClearFlags clearFlags      = mainCamera.clearFlags;
            Color            backgroundColor = mainCamera.backgroundColor;
            float            depth           = mainCamera.depth + 1;
            float            nearClipPlane   = mainCamera.nearClipPlane;
            float            farClipPlane    = mainCamera.farClipPlane;

            float[] projectionMatrixEyeLeftPtr  = GetProjectionMatrix(WearableCalibration.EyeType.EYE_LEFT);
            float[] projectionMatrixEyeRightPtr = GetProjectionMatrix(WearableCalibration.EyeType.EYE_RIGHT);

            Matrix4x4 projectionEyeLeft  = MatrixUtils.ConvertGLProjectionToUnityProjection(projectionMatrixEyeLeftPtr);
            Matrix4x4 projectionEyeRight = MatrixUtils.ConvertGLProjectionToUnityProjection(projectionMatrixEyeRightPtr);

            leftCamera.clearFlags       = clearFlags;
            leftCamera.backgroundColor  = backgroundColor;
            leftCamera.depth            = depth;
            leftCamera.nearClipPlane    = nearClipPlane;
            leftCamera.farClipPlane     = farClipPlane;
            leftCamera.rect             = new Rect(0.0f, 0.0f, 0.5f, 1.0f);
            leftCamera.projectionMatrix = projectionEyeLeft;

            rightCamera.clearFlags       = clearFlags;
            rightCamera.backgroundColor  = backgroundColor;
            rightCamera.depth            = depth;
            rightCamera.nearClipPlane    = nearClipPlane;
            rightCamera.farClipPlane     = farClipPlane;
            rightCamera.rect             = new Rect(0.5f, 0.0f, 0.5f, 1.0f);
            rightCamera.projectionMatrix = projectionEyeRight;
        }
示例#6
0
        public override void OnTrackSuccess(string id, string name, Matrix4x4 poseMatrix)
        {
            Renderer[] rendererComponents = GetComponentsInChildren <Renderer>(true);
            Collider[] colliderComponents = GetComponentsInChildren <Collider>(true);

            // Enable renderers
            foreach (Renderer component in rendererComponents)
            {
                component.enabled = true;
            }

            // Enable colliders
            foreach (Collider component in colliderComponents)
            {
                component.enabled = true;
            }

            transform.position = MatrixUtils.PositionFromMatrix(poseMatrix);
            transform.rotation = MatrixUtils.QuaternionFromMatrix(poseMatrix);
        }
        private bool ReadMap(string fileName)
        {
            MapViewer.GetInstance().Deinitialize();
            if (!MapViewer.GetInstance().Initialize(fileName))
            {
                return(false);
            }

            IntPtr jsonPtr = MapViewer.GetInstance().GetJson();
            string json    = Marshal.PtrToStringAnsi(jsonPtr);

            if (json.Length < 1)
            {
                Debug.Log("Map is not opened");
                return(false);
            }

            Map3D map3D = JsonReader.Deserialize <Map3D>(json);

            int width  = map3D.width;
            int height = map3D.height;

            maxKeyframeCount = map3D.imageCount;

            vertices = new Vector3[map3D.vertexCount];
            for (int i = 0; i < map3D.vertexCount; i++)
            {
                vertices[i] = new Vector3(map3D.vertices[i].x, -map3D.vertices[i].y, map3D.vertices[i].z);
            }

            cameraMatrices = new Matrix4x4[maxKeyframeCount];
            for (int i = 0; i < maxKeyframeCount; i++)
            {
                for (int j = 0; j < 16; j++)
                {
                    cameraMatrices[i][j] = map3D.poseMatrices[i][j];
                }
                cameraMatrices[i] = cameraMatrices[i].inverse;
            }

            cameraRotations = new Quaternion[maxKeyframeCount];
            for (int i = 0; i < maxKeyframeCount; i++)
            {
                cameraRotations[i] = MatrixUtils.InvQuaternionFromMatrix(cameraMatrices[i]);
                Vector3 tempR = cameraRotations[i].eulerAngles;
                tempR.x            = -tempR.x;
                tempR.y            = -tempR.y;
                cameraRotations[i] = Quaternion.Euler(tempR);
            }

            Shader color = Shader.Find("Unlit/Texture");

            materials = new Material[maxKeyframeCount];
            for (int i = 0; i < maxKeyframeCount; i++)
            {
                materials[i]             = new Material(color);
                materials[i].mainTexture = GetCameraTexture(i, width, height);
            }

            return(true);
        }
        private bool ReadMap(string fileName)
        {
            MapViewer.GetInstance().Deinitialize();
            int length = MapViewer.GetInstance().Initialize(fileName);

            if (length == -1)
            {
                return(false);
            }

            byte[] jsonBuffer = new byte[length];
            MapViewer.GetInstance().GetJson(jsonBuffer, length);

            string json = Encoding.UTF8.GetString(jsonBuffer);

            if (json.Length < 1)
            {
                Debug.Log("Map is not opened");
                return(false);
            }

            Map3D map3D = JsonReader.Deserialize <Map3D>(json);

            int width  = map3D.width;
            int height = map3D.height;

            maxKeyframeCount = map3D.imageCount;

            vertices = new Vector3[map3D.vertexCount];
            for (int i = 0; i < map3D.vertexCount; i++)
            {
                vertices[i] = new Vector3(map3D.vertices[i].x, -map3D.vertices[i].y, map3D.vertices[i].z);
            }

            cameraMatrices = new Matrix4x4[maxKeyframeCount];
            for (int i = 0; i < maxKeyframeCount; i++)
            {
                for (int j = 0; j < 16; j++)
                {
                    cameraMatrices[i][j] = map3D.poseMatrices[i][j];
                }
                cameraMatrices[i] = cameraMatrices[i].inverse;
            }

            cameraRotations = new Quaternion[maxKeyframeCount];
            for (int i = 0; i < maxKeyframeCount; i++)
            {
                cameraRotations[i] = MatrixUtils.InvQuaternionFromMatrix(cameraMatrices[i]);
                Vector3 tempR = cameraRotations[i].eulerAngles;
                tempR.x            = -tempR.x;
                tempR.y            = -tempR.y;
                cameraRotations[i] = Quaternion.Euler(tempR);
            }

            Shader color = Shader.Find("Unlit/Texture");

            materials = new Material[maxKeyframeCount];
            for (int i = 0; i < maxKeyframeCount; i++)
            {
                materials[i]             = new Material(color);
                materials[i].mainTexture = GetCameraTexture(i, width, height);
            }

            if (map3D.tagAnchors != null)
            {
                TagAnchor[] anchors = map3D.tagAnchors;
                this.tagAnchors = anchors;
            }
            else
            {
                tagAnchors = null;
                Debug.Log("No Anchors");
            }
            return(true);
        }
示例#9
0
        internal void Create(Vector3[] vertices, Matrix4x4[] cameraMatrices, Material[] materials)
        {
            int materialsLength = materials.Length;

            if (materialsLength == 0)
            {
                return;
            }

            transform.localRotation = Camera.main.transform.localRotation;

            textureWidth  = materials[0].mainTexture.width;
            textureHeight = materials[0].mainTexture.height;

            GameObject mapViewerCameraObject = Resources.Load <GameObject>("MaxstAR/Contents/MapViewerCamera");

            groupObjects  = new GameObject[materialsLength];
            cameraObjects = new GameObject[materialsLength];
            imageObjects  = new GameObject[materialsLength];
            meshObjects   = new GameObject[materialsLength];

            for (int i = 0; i < materialsLength; i++)
            {
                groupObjects[i]  = new GameObject();
                cameraObjects[i] = Instantiate(mapViewerCameraObject);
                meshObjects[i]   = new GameObject();
                imageObjects[i]  = GameObject.CreatePrimitive(PrimitiveType.Quad);

                groupObjects[i].name  = "Keyframe" + i;
                cameraObjects[i].name = "Camera";
                meshObjects[i].name   = "Mesh";
                imageObjects[i].name  = "Image";

                groupObjects[i].transform.parent  = transform;
                cameraObjects[i].transform.parent = groupObjects[i].transform;
                meshObjects[i].transform.parent   = groupObjects[i].transform;
                imageObjects[i].transform.parent  = groupObjects[i].transform;

                cameraObjects[i].layer = LayerMask.NameToLayer("Ignore Raycast");
                meshObjects[i].AddComponent(typeof(MeshFilter));
                meshObjects[i].AddComponent(typeof(MeshRenderer));
                meshObjects[i].GetComponent <MeshFilter>().mesh    = CreateMapViewerMesh(i, vertices);
                meshObjects[i].GetComponent <Renderer>().material  = materials[i];
                imageObjects[i].GetComponent <Renderer>().material = materials[i];

                groupObjects[i].transform.localPosition = Vector3.zero;
                groupObjects[i].transform.localRotation = Quaternion.identity;
                groupObjects[i].transform.localScale    = new Vector3(1.0f, 1.0f, 1.0f);

                Matrix4x4 m = MatrixUtils.GetUnityPoseMatrix(cameraMatrices[i]);
                cameraObjects[i].transform.localRotation = MatrixUtils.QuaternionFromMatrix(m);
                cameraObjects[i].transform.localPosition = MatrixUtils.PositionFromMatrix(m);
                cameraObjects[i].transform.localScale    = new Vector3(0.01f, 0.01f, 0.01f);

                meshObjects[i].transform.localPosition = Vector3.zero;
                meshObjects[i].transform.localRotation = Quaternion.identity;
                meshObjects[i].transform.localScale    = new Vector3(scaleFactor, scaleFactor, scaleFactor);

                //imageObjects[i].transform.localPosition = new Vector3(0.0f, 0.0f, 10.0f);
                //imageObjects[i].transform.localRotation = Quaternion.identity;
                //imageObjects[i].transform.localScale = new Vector3(10.0f, 10.0f * -textureHeight / textureWidth, 10.0f);


                Vector4 p = m * (new Vector4(0.0f, 0.0f, 10.0f, 1.0f));
                imageObjects[i].transform.localRotation = MatrixUtils.QuaternionFromMatrix(m);
                imageObjects[i].transform.localPosition = new Vector3(p.x, p.y, p.z);
                imageObjects[i].transform.localScale    = new Vector3(10.0f, 10.0f * -textureHeight / textureWidth, 10.0f);
            }

            gameViewSize                      = GetGameViewSize();
            viewCameraObject                  = new GameObject();
            viewCameraObject.name             = "ViewCamera";
            viewCameraObject.transform.parent = transform;
            Camera camera = viewCameraObject.AddComponent <Camera>();

            camera.ResetProjectionMatrix();
            camera.nearClipPlane   = 0.03f;
            camera.farClipPlane    = 10000.0f;
            camera.aspect          = gameViewSize.x / gameViewSize.y;
            camera.clearFlags      = CameraClearFlags.SolidColor;
            camera.backgroundColor = Color.black;
            camera.cullingMask    &= ~(1 << LayerMask.NameToLayer("Ignore Raycast"));

            ///////////////////////// TODO: check /////////////////////////
            if (gameViewSize.x > gameViewSize.y * textureWidth / textureHeight)
            {
                camera.fieldOfView = 2.0f * Mathf.Atan2(vy, vz) * Mathf.Rad2Deg;
            }
            else
            {
                camera.fieldOfView = 2.0f * Mathf.Atan2(vx * gameViewSize.y / gameViewSize.x, vz) * Mathf.Rad2Deg;
            }

            viewCameraObject.transform.localPosition = cameraObjects[0].transform.localPosition;
            viewCameraObject.transform.localRotation = cameraObjects[0].transform.localRotation;
        }
示例#10
0
        // Get target pose for camera transform (When world center mode is TARGET)
        internal Matrix4x4 GetTargetPose()
        {
            NativeAPI.maxst_Trackable_getPose(cPtr, glPoseMatrix);

            return(MatrixUtils.GetUnityPoseMatrix(glPoseMatrix));
        }