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);
                }
            }
        }
        //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);
        }
Пример #3
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);
        }
Пример #4
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;
        }