// Updates arVisible, arPosition, arRotation based on linked marker state.
    private void UpdateTracking()
    {
        // Note the current time
        timeLastUpdate = Time.realtimeSinceStartup;

        // First, ensure we have a base marker. If none, then no markers are currently in view.
        ARMarker marker = GetMarker();

        if (marker == null)
        {
            markerInsight = false;
            if (arVisible)
            {
                // Marker was visible but now is hidden.
                timeTrackingLost = timeLastUpdate;
                arVisible        = false;
            }
        }
        else
        {
            markerInsight = true;

            if (marker.Visible)
            {
                Matrix4x4 pose;
                if (Optical && opticalSetupOK)
                {
                    pose = (opticalViewMatrix * marker.TransformationMatrix).inverse;
                }
                else
                {
                    pose = marker.TransformationMatrix.inverse;
                }

                arPosition = ARUtilityFunctions.PositionFromMatrix(pose);
                // Camera orientation: In ARToolKit, zero rotation of the camera corresponds to looking vertically down on a marker
                // lying flat on the ground. In Unity however, if we still treat markers as being flat on the ground, we clash with Unity's
                // camera "rotation", because an unrotated Unity camera is looking horizontally.
                // So we choose to treat an unrotated marker as standing vertically, and apply a transform to the scene to
                // to get it to lie flat on the ground.
                arRotation = ARUtilityFunctions.QuaternionFromMatrix(pose);

                if (!arVisible)
                {
                    // Marker was hidden but now is visible.
                    arVisible = true;
                }
            }
            else
            {
                if (arVisible)
                {
                    // Marker was visible but now is hidden.
                    timeTrackingLost = timeLastUpdate;
                    arVisible        = false;
                }
            }
        }
    }
    public override void Start()
    {
        base.Start();

        Matrix4x4 targetInWorldFrame  = targetObject.transform.localToWorldMatrix;
        Matrix4x4 targetInCameraFrame = this.gameObject.GetComponent <Camera>().transform.parent.worldToLocalMatrix *targetInWorldFrame;

        vrTargetPosition = ARUtilityFunctions.PositionFromMatrix(targetInCameraFrame);
        vrTargetRotation = ARUtilityFunctions.QuaternionFromMatrix(targetInCameraFrame);

        vrObserverAzimuth = vrObserverElevation = 0.0f;         // VR mode starts pointing in direction specified by the axes of the target.
        vrObserverOffset  = Vector3.zero;
    }
Exemple #3
0
    // We use Update() here, but be aware that unless ARController has been configured to
    // execute first (Unity Editor->Edit->Project Settings->Script Execution Order) then
    // state produced by this update may lag by one frame.
    void Update()
    {
        float[] matrixRawArray = new float[16];

        ARController.Log(LogTag + "ARMarker.Update()");
        if (UID == NO_ID || !PluginFunctions.inited)
        {
            visible = false;
            return;
        }

        // Query visibility if we are running in the Player.
        if (Application.isPlaying)
        {
            visible = PluginFunctions.arwQueryMarkerTransformation(UID, matrixRawArray);
            ARController.Log(LogTag + "ARMarker.Update() UID=" + UID + ", visible=" + visible);

            if (visible)
            {
                matrixRawArray[12] *= 0.001f;                 // Scale the position from ARToolKit units (mm) into Unity units (m).
                matrixRawArray[13] *= 0.001f;
                matrixRawArray[14] *= 0.001f;

                Matrix4x4 matrixRaw = ARUtilityFunctions.MatrixFromFloatArray(matrixRawArray);
                //ARController.Log("arwQueryMarkerTransformation(" + UID + ") got matrix: [" + Environment.NewLine + matrixRaw.ToString("F3").Trim() + "]");

                // ARToolKit uses right-hand coordinate system where the marker lies in x-y plane with right in direction of +x,
                // up in direction of +y, and forward (towards viewer) in direction of +z.
                // Need to convert to Unity's left-hand coordinate system where marker lies in x-y plane with right in direction of +x,
                // up in direction of +y, and forward (towards viewer) in direction of -z.
                transformationMatrix = ARUtilityFunctions.LHMatrixFromRHMatrix(matrixRaw);

                // Output current position: Added by Kazu on Apr 2 2016
                Vector3 position = ARUtilityFunctions.PositionFromMatrix(transformationMatrix);
                print("Position of Barcode ID #" + BarcodeID + ": (" + position.x * 1000 + ", " + position.y * 1000 + ", " + position.z * 1000 + ")");
//				print ("position.x [mm]: "+ position.x * 1000);
//				print ("position.y [mm]: "+ position.y * 1000);
//				print ("position.z [mm]: "+ position.z * 1000);
//				If you need quaternion, you can use the followings.
//				Quaternion orientation = ARUtilityFunctions.QuaternionFromMatrix(transformationMatrix);
            }
        }
    }
    List <Vector4> getMeanPos()
    {
        List <Vector4> p    = new List <Vector4>();
        int            ct   = 0;
        Vector3        tpos = new Vector3(0.0f, 0.0f, 0.0f);
        Vector4        trot = new Vector4(0.0f, 0.0f, 0.0f, 0.0f);
        int            i    = 0;

        for (i = 0; i < markers.Length; i++)
        {
            if (!markers[i].Visible)
            {
                continue;
            }
            ct++;
            Matrix4x4  pose        = markers[i].TransformationMatrix;
            Vector3    position    = ARUtilityFunctions.PositionFromMatrix(pose);
            Quaternion orientation = ARUtilityFunctions.QuaternionFromMatrix(pose);
            Vector4    ori         = new Vector4(orientation [0], orientation [1], orientation [2], orientation[3]);

            tpos += position;
            trot += ori;
        }
        if (ct == 0)
        {
            return(p);
        }

        Debug.Log(ct);
        tpos /= ct;
        trot /= ct;

        p.Add(new Vector4(tpos[0], tpos[1], tpos[2], 0.0f));
        p.Add(trot);
        return(p);
    }
Exemple #5
0
    // Use LateUpdate to be sure the ARMarker has updated before we try and use the transformation.
    void LateUpdate()
    {
        // Local scale is always 1 for now
        transform.localScale = Vector3.one;

        // Update tracking if we are running in the Player.
        if (Application.isPlaying)
        {
            // Sanity check, make sure we have an AROrigin in parent hierachy.
            AROrigin origin = GetOrigin();
            if (origin == null)
            {
                ARController.Log(LogTag + "No Origin");
                //visible = visibleOrRemain = false;
            }
            else
            {
                // Sanity check, make sure we have an ARMarker assigned.
                ARMarker marker = GetMarker();
                if (marker == null)
                {
                    ARController.Log(LogTag + "No ARMarker");
                    //visible = visibleOrRemain = false;
                }
                else
                {
                    // Note the current time
                    float timeNow = Time.realtimeSinceStartup;

                    ARMarker baseMarker = origin.GetBaseMarker();
                    if (baseMarker != null && marker.Visible)
                    {
                        if (!visible)
                        {
                            // Marker was hidden but now is visible.
                            ARController.Log(LogTag + "Marker was hidden but now is visible.");
                            visible = visibleOrRemain = true;
                            if (eventReceiver != null)
                            {
                                eventReceiver.BroadcastMessage("OnMarkerFound", marker, SendMessageOptions.DontRequireReceiver);
                            }

                            for (int i = 0; i < this.transform.childCount; i++)
                            {
                                this.transform.GetChild(i).gameObject.SetActive(true);
                            }
                        }
                        else
                        {
//							ARController.Log (LogTag + "Marker stayed visible");
                        }

                        Matrix4x4 pose;
                        if (marker == baseMarker)
                        {
                            // If this marker is the base, no need to take base inverse etc.
                            pose = origin.transform.localToWorldMatrix;
                        }
                        else
                        {
                            pose = (origin.transform.localToWorldMatrix * baseMarker.TransformationMatrix.inverse * marker.TransformationMatrix);
                        }
                        transform.position = ARUtilityFunctions.PositionFromMatrix(pose);
                        transform.rotation = ARUtilityFunctions.QuaternionFromMatrix(pose);

                        if (eventReceiver != null)
                        {
                            eventReceiver.BroadcastMessage("OnMarkerTracked", marker, SendMessageOptions.DontRequireReceiver);
                        }
                    }
                    else
                    {
                        if (visible)
                        {
                            // Marker was visible but now is hidden.
                            ARController.Log(LogTag + "Marker was visible but now is hidden. (after " + secondsToRemainVisible + "s)");
                            visible          = false;
                            timeTrackingLost = timeNow;
                        }
                        else
                        {
//							ARControllertroller.Log (LogTag + "Marker stayed hidden.");
                        }

                        if (visibleOrRemain && (timeNow - timeTrackingLost >= secondsToRemainVisible))
                        {
                            visibleOrRemain = false;
                            if (eventReceiver != null)
                            {
                                eventReceiver.BroadcastMessage("OnMarkerLost", marker, SendMessageOptions.DontRequireReceiver);
                            }
                            for (int i = 0; i < this.transform.childCount; i++)
                            {
                                this.transform.GetChild(i).gameObject.SetActive(false);
                            }
                        }
                    }
                }  // marker
            }      // origin
        }          // Application.isPlaying
        else
        {
            ARController.Log(LogTag + "Applicaiton Not Playing");
        }
    }
    // Use LateUpdate to be sure the ARTrackable has updated before we try and use the transformation.
    void LateUpdate()
    {
        // Local scale is always 1 for now
        transform.localScale = Vector3.one;

        // Update tracking if we are running in the Player.
        if (Application.isPlaying)
        {
            // Sanity check, make sure we have an AROrigin in parent hierachy.
            AROrigin origin = GetOrigin();
            if (origin == null)
            {
                //visible = visibleOrRemain = false;
            }
            else
            {
                // Sanity check, make sure we have an ARTrackable assigned.
                ARTrackable trackable = GetTrackable();
                if (trackable == null)
                {
                    //visible = visibleOrRemain = false;
                }
                else
                {
                    // Note the current time
                    float timeNow = Time.realtimeSinceStartup;

                    ARTrackable baseTrackable = origin.GetBaseTrackable();
                    if (baseTrackable != null && trackable.Visible)
                    {
                        if (!visible)
                        {
                            // Trackable was hidden but now is visible.
                            visible = visibleOrRemain = true;
                            if (eventReceiver != null)
                            {
                                eventReceiver.BroadcastMessage("OnTrackableFound", trackable, SendMessageOptions.DontRequireReceiver);
                            }

                            for (int i = 0; i < this.transform.childCount; i++)
                            {
                                this.transform.GetChild(i).gameObject.SetActive(true);
                            }
                        }

                        Matrix4x4 pose;
                        if (trackable == baseTrackable)
                        {
                            // If this marker is the base, no need to take base inverse etc.
                            pose = origin.transform.localToWorldMatrix;
                        }
                        else
                        {
                            pose = (origin.transform.localToWorldMatrix * baseTrackable.TransformationMatrix.inverse * trackable.TransformationMatrix);
                        }
                        transform.position = ARUtilityFunctions.PositionFromMatrix(pose);
                        transform.rotation = ARUtilityFunctions.QuaternionFromMatrix(pose);

                        if (eventReceiver != null)
                        {
                            eventReceiver.BroadcastMessage("OnTrackableTracked", trackable, SendMessageOptions.DontRequireReceiver);
                        }
                    }
                    else
                    {
                        if (visible)
                        {
                            // Trackable was visible but now is hidden.
                            visible          = false;
                            timeTrackingLost = timeNow;
                        }

                        if (visibleOrRemain && (timeNow - timeTrackingLost >= secondsToRemainVisible))
                        {
                            visibleOrRemain = false;
                            if (eventReceiver != null)
                            {
                                eventReceiver.BroadcastMessage("OnTrackableLost", trackable, SendMessageOptions.DontRequireReceiver);
                            }
                            for (int i = 0; i < this.transform.childCount; i++)
                            {
                                this.transform.GetChild(i).gameObject.SetActive(false);
                            }
                        }
                    }
                } // marker
            }     // origin
        }         // Application.isPlaying
    }
Exemple #7
0
 // Display the rotaion and position part to scene
 void showText(Matrix4x4 curPose) // Newly added
 {
     Rotation.text       = "Rotation: " + ARUtilityFunctions.QuaternionFromMatrix(curPose).ToString();
     Position.text       = "Position: " + ARUtilityFunctions.PositionFromMatrix(curPose).ToString();
     Transformation.text = curPose.ToString();
 }
Exemple #8
0
    // Updates arVisible, arPosition, arRotation based on linked marker state.
    private void UpdateTracking(Boolean write)
    {
        // Note the current time
        timeLastUpdate = Time.realtimeSinceStartup;

        // First, ensure we have a base marker. If none, then no markers are currently in view.
        ARMarker marker = GetMarker();

        if (marker == null)
        {
            if (arVisible)
            {
                // Marker was visible but now is hidden.
                timeTrackingLost = timeLastUpdate;
                arVisible        = false;
            }
        }
        else
        {
            if (marker.Visible)
            {
                Matrix4x4 pose;
                //Matrix4x4 myPose;
                if (Optical && opticalSetupOK)
                {
                    pose = (opticalViewMatrix * marker.TransformationMatrix).inverse;
                    // myPose is the transformation from marker to camera
                    myPose = (opticalViewMatrix * marker.TransformationMatrix);
                }
                else
                {
                    pose = marker.TransformationMatrix.inverse;
                    // myPose is the transformation from marker to camera
                    myPose = marker.TransformationMatrix;
                }

                // choose to treat an unrotated marker as standing vertically, and apply a transform to the scene to
                // to get it to lie flat on the ground.
                arPosition = ARUtilityFunctions.PositionFromMatrix(pose);
                arRotation = ARUtilityFunctions.QuaternionFromMatrix(pose);

                // Show the pose to Text
                showText(myPose);

                // Write the pose to txt
                if (write)
                {
                    writeText(myPose);
                }

                // read pivot calibration from txt file
                string  path = "C:/Users/zhaoz/Desktop/JohnsHopkins/Spring2017/CISII/F200/Pivot Calibration/pointTipNeedle.txt";
                Vector4 tipMarker;
                tipMarker = readPivotCalibration(path);

                // transform the tip position from wrt marker to wrt camera
                tipCamera = myPose * tipMarker;

                // convert Vector4 tipMarker to 3D
                renderEndMarker.x = tipMarker.x;
                renderEndMarker.y = tipMarker.y;
                renderEndMarker.z = tipMarker.z;

                tipPositionCamera.text = "Tip Position: " + tipCamera.ToString();

                if (!arVisible)
                {
                    // Marker was hidden but still show the augmentation as it was there
                    arVisible = true;
                }
            }
            else
            {
                if (arVisible)
                {
                    // Marker was hidden but still show the augmentation as it was there
                    timeTrackingLost = timeLastUpdate;
                    arVisible        = false;
                }
            }
        }
    }
    // Use LateUpdate to be sure the ARMarker has updated before we try and use the transformation.
    void LateUpdate()
    {
        // Local scale is always 1 for now
        transform.localScale = Vector3.one;

        // Update tracking if we are running in the Player.
        if (Application.isPlaying)
        {
            // Sanity check, make sure we have an AROrigin in parent hierachy.
            AROrigin origin = GetOrigin();
            if (origin == null)
            {
                //visible = visibleOrRemain = false;
                return;
            }
            // Sanity check, make sure we have an ARMarker assigned.
            ARMarker marker = GetMarker();
            if (marker == null)
            {
                //visible = visibleOrRemain = false;
                return;
            }

            // Note the current time
            timeNowSeconds = Time.realtimeSinceStartup;

            ARMarker baseMarker = origin.GetBaseMarker();
            if (baseMarker != null && marker.Visible)
            {
                if (!visible)
                {
                    // Marker was hidden but now is visible.
                    visible = visibleOrRemain = true;
                    if (eventReceiver != null)
                    {
                        eventReceiver.BroadcastMessage("OnMarkerFound", marker, SendMessageOptions.DontRequireReceiver);
                    }
                    for (int i = 0; i < this.transform.childCount; i++)
                    {
                        this.transform.GetChild(i).gameObject.SetActive(true);
                    }
                }

                Matrix4x4 pose;
                if (marker == baseMarker)
                {
                    // If this marker is the base, no need to take base inverse etc.
                    pose = origin.transform.localToWorldMatrix;
                }
                else
                {
                    pose = (origin.transform.localToWorldMatrix * baseMarker.TransformationMatrix.inverse * marker.TransformationMatrix);
                }
                transform.position = ARUtilityFunctions.PositionFromMatrix(pose);
                transform.rotation = ARUtilityFunctions.QuaternionFromMatrix(pose);

                if (eventReceiver != null)
                {
                    eventReceiver.BroadcastMessage("OnMarkerTracked", marker, SendMessageOptions.DontRequireReceiver);
                }
                OnMarkerMadeVisible(marker);
            }
            else
            {
                OnMarkerLost(marker);
            }
        }         // Application.isPlaying
    }
Exemple #10
0
    void OnMarkerTracked(ARMarker marker)
    {
        Vector3 positionTarget = ARUtilityFunctions.PositionFromMatrix(marker.TransformationMatrix);
        Vector3 normalPosition = new Vector3(positionTarget.x / (0.5f * positionTarget.z), positionTarget.y / (0.5f * positionTarget.z), markersFX[0].transform.localPosition.z);

        int         lowCutoffvalue  = (int)(((normalPosition.x + 0.9) / 1.8) * 4900) + 100;
        int         highCutoffvalue = (int)(((normalPosition.x + 0.9) / 1.8) * 1495) + 5;
        AudioSource theAudio        = null;

        if (marker.Tag == markersTags[0])
        {
            theAudio = audio1;
            markersFX[0].SetActive(true);
            markersFX[0].transform.localPosition = normalPosition;
        }
        else if (marker.Tag == markersTags[1])
        {
            audio1.GetComponent <AudioEchoFilter> ().delay      = 30;
            audio1.GetComponent <AudioEchoFilter> ().decayRatio = 0.7f;
            //theAudio = audio2;
            //markersFX[1].SetActive (true);
            //markersFX[1].transform.localPosition = normalPosition;
        }
        else if (marker.Tag == markersTags[2])
        {
            audio1.GetComponent <SEF_lowpass> ().cutoffFrequency = lowCutoffvalue;
            audio2.GetComponent <SEF_lowpass> ().cutoffFrequency = lowCutoffvalue;
        }
        else if (marker.Tag == markersTags[3])
        {
            audio1.GetComponent <SEF_highpass> ().cutoffFrequency = highCutoffvalue;
            audio2.GetComponent <SEF_highpass> ().cutoffFrequency = highCutoffvalue;
        }



        if (theAudio != null)
        {
            float pitch = 0;
            if (normalPosition.x > -0.3f && normalPosition.x < 0.3f)
            {
                pitch = 1f;
                if (audio1.GetComponent <AudioEchoFilter> ().enabled)
                {
                    pitch = 0.9f;
                }
                theAudio.pitch = pitch;
            }
            else if (normalPosition.x > -0.9f && normalPosition.x < -0.3f)
            {
                pitch          = 0.7f;
                theAudio.pitch = pitch;
            }
            else if (normalPosition.x > 0.3f && normalPosition.x < 0.9f)
            {
                pitch          = 1.4f;
                theAudio.pitch = pitch;
            }

            if (normalPosition.y > -0.2f && normalPosition.y < 0.2f)
            {
                int a = 0;
            }
            else if (normalPosition.y > -0.6f && normalPosition.y < -0.2f)
            {
                int a = 0;
            }
            else if (normalPosition.y > 0.2f && normalPosition.y < 0.6f)
            {
                int a = 0;
            }
        }
    }
    // Update is called once per frame
    void Update()
    {
        foreach (var item in possiblePieces)
        {
//            Debug.Log("x: " + (Origin.transform.position.x - item.Item1.transform.position.x) +
//" y: " + (Origin.transform.position.y - item.Item1.transform.position.y) +
//" z: " + (Origin.transform.position.z - item.Item1.transform.position.z));
            if (item.Item1.activeInHierarchy)
            {
                ARMarker  myMarker = item.Item1.transform.parent.GetComponent <ARTrackedObject>().GetMarker();
                Matrix4x4 pose     = myMarker.TransformationMatrix;
                Vector3   position = ARUtilityFunctions.PositionFromMatrix(pose);
                //Quaternion orientation = ARUtilityFunctions.QuaternionFromMatrix(pose);
                //float x = position.x - distanceToOrigin[5].x;
                //float y = position.y - distanceToOrigin[5].y;
                //float z = position.z - distanceToOrigin[5].z;
                //Debug.Log("x: " + x +
                //" y: " + y +
                //" z: " + z);

                var minDistance = float.MaxValue;
                int minIndex    = -1;
                for (int i = 0; i < NUMBER_OF_SQUARES; i++)
                {
                    var distance = Vector3.Distance(position, piecePositionsFromCamera[i]);
                    if (distance < minDistance)
                    {
                        minDistance = distance;
                        minIndex    = i;
                    }
                }

                if (minDistance < MaxSlackDistance)
                {
                    if (positions[minIndex].pieceColor != item.Item2)
                    {
                        if (minDistance < positions[minIndex].distance)
                        {
                            UpdatePosition(item.Item2, minDistance, minIndex);
                        }
                    }
                    else
                    {
                        positions[minIndex].dateTime = DateTime.UtcNow;
                    }
                }
            }
        }
        for (int i = 0; i < positions.Count; i++)
        {
            if (positions[i].pieceColor != PieceColor.None)
            {
                if ((DateTime.UtcNow - positions[i].dateTime).TotalMilliseconds > TimeToLoseAPieceMS)
                {
                    UpdatePosition(PieceColor.None, float.MaxValue, i);
                }
            }
        }

        //for (int i = 0; i < NUMBER_OF_SQUARES; i++)
        //{
        //    if (pieces[i].Item2.MyColor == PieceColor.None)
        //    {
        //        bool hintExists = false;
        //        foreach (var item in neighbours[i])
        //        {

        //            if (pieces[item].Item2.MyColor != PieceColor.None)
        //            {
        //                hintExists = true;
        //                hintImages[i].enabled = true;
        //            }
        //        }
        //        if (!hintExists)
        //        {
        //            hintImages[i].enabled = false;
        //        }
        //    } else if(hintImages[i].enabled == true)
        //    {
        //        hintImages[i].enabled = false;
        //    }
        //}
    }
    public void CalibratePiecePositions()
    {
        //The piece with the greater x and lower y is going to be the one on the bottom left corner

        List <Tuple <GameObject, Vector3> > piecesPositions = new List <Tuple <GameObject, Vector3> >();

        foreach (var item in possiblePieces)
        {
            if (item.Item1.activeInHierarchy)
            {
                ARMarker  myMarker = item.Item1.transform.parent.GetComponent <ARTrackedObject>().GetMarker();
                Matrix4x4 pose     = myMarker.TransformationMatrix;
                Vector3   position = ARUtilityFunctions.PositionFromMatrix(pose);
                piecesPositions.Add(new Tuple <GameObject, Vector3>(item.Item1, position));
            }
        }
        if (piecesPositions.Count == 6)
        {
            //Sort the positions by x
            piecesPositions.Sort((x, y) => y.Item2.x.CompareTo(x.Item2.x));
            Vector3 bottomLeft, bottomRight, topLeft, topRight, middleLeft, middleRight;
            if (piecesPositions[0].Item2.y < piecesPositions[1].Item2.y)
            {
                bottomLeft  = piecesPositions[0].Item2;
                bottomRight = piecesPositions[1].Item2;
            }
            else
            {
                bottomLeft  = piecesPositions[1].Item2;
                bottomRight = piecesPositions[0].Item2;
            }
            if (piecesPositions[4].Item2.y < piecesPositions[5].Item2.y)
            {
                topLeft  = piecesPositions[4].Item2;
                topRight = piecesPositions[5].Item2;
            }
            else
            {
                topLeft  = piecesPositions[5].Item2;
                topRight = piecesPositions[4].Item2;
            }
            if (piecesPositions[2].Item2.y < piecesPositions[3].Item2.y)
            {
                middleLeft  = piecesPositions[2].Item2;
                middleRight = piecesPositions[3].Item2;
            }
            else
            {
                middleLeft  = piecesPositions[3].Item2;
                middleRight = piecesPositions[2].Item2;
            }
            piecePositionsFromCamera.Clear();
            piecePositionsFromCamera.Add(bottomLeft);
            piecePositionsFromCamera.Add(new Vector3(middleLeft.x, bottomLeft.y, (bottomLeft.z + middleLeft.z) / 2));
            piecePositionsFromCamera.Add(new Vector3(middleRight.x, topLeft.y, (topLeft.z + middleRight.z) / 2));
            piecePositionsFromCamera.Add(topLeft);
            piecePositionsFromCamera.Add(new Vector3(bottomLeft.x, middleLeft.y, (middleLeft.z + bottomLeft.z) / 2));
            piecePositionsFromCamera.Add(middleLeft);
            piecePositionsFromCamera.Add(new Vector3(middleRight.x, middleLeft.y, (middleLeft.z + middleRight.z) / 2));
            piecePositionsFromCamera.Add(new Vector3(topLeft.x, middleLeft.y, (middleLeft.z + topLeft.z) / 2));
            piecePositionsFromCamera.Add(new Vector3(bottomRight.x, middleRight.y, (middleRight.z + bottomRight.z) / 2));
            piecePositionsFromCamera.Add(new Vector3(middleLeft.x, middleRight.y, (middleRight.z + middleLeft.z) / 2));
            piecePositionsFromCamera.Add(middleRight);
            piecePositionsFromCamera.Add(new Vector3(topRight.x, middleRight.y, (middleRight.z + topRight.z) / 2));
            piecePositionsFromCamera.Add(bottomRight);
            piecePositionsFromCamera.Add(new Vector3(middleLeft.x, bottomRight.y, (bottomRight.z + middleLeft.z) / 2));
            piecePositionsFromCamera.Add(new Vector3(middleRight.x, topRight.y, (topRight.z + middleRight.z) / 2));
            piecePositionsFromCamera.Add(topRight);

            SaveCalibration();
        }
        else
        {
            Debug.Log("Did not find 6 pieces to calibrate!");
        }
    }
Exemple #13
0
    // 1 - Query for visibility.
    // 2 - Determine if visibility state is new.
    // 3 - If visible, calculate marker pose.
    // 4 - If visible, set marker pose.
    // 5 - If visibility state is new, notify event receivers via "OnMarkerFound" or "OnMarkerLost".
    // 6 - If visibility state is new, set appropriate active state for marker children.
    // 7 - If visible, notify event receivers that the marker's pose has been updated via "OnMarkerTracked".
    protected virtual void LateUpdate()
    {
        if (!Application.isPlaying)
        {
            return;
        }

        float[] matrixRawArray = new float[16];
        lock (loadLock) {
            if (UID == NO_ID || !PluginFunctions.inited)
            {
                visible = false;
                return;
            }

            Vector3 storedScale = transform.localScale;
            transform.localScale = Vector3.one;

            // 1 - Query for visibility.
            bool nowVisible = PluginFunctions.arwQueryMarkerTransformation(UID, matrixRawArray);

            // 2 - Determine if visibility state is new.
            bool notify = (nowVisible != visible);
            visible = nowVisible;

            // 3 - If visible, calculate marker pose.
            if (visible)
            {
                // Scale the position from ARToolKit units (mm) into Unity units (m).
                matrixRawArray[12] *= UNITY_TO_ARTOOLKIT;
                matrixRawArray[13] *= UNITY_TO_ARTOOLKIT;
                matrixRawArray[14] *= UNITY_TO_ARTOOLKIT;

                Matrix4x4 matrixRaw = ARUtilityFunctions.MatrixFromFloatArray(matrixRawArray);
                // ARToolKit uses right-hand coordinate system where the marker lies in x-y plane with right in direction of +x,
                // up in direction of +y, and forward (towards viewer) in direction of +z.
                // Need to convert to Unity's left-hand coordinate system where marker lies in x-y plane with right in direction of +x,
                // up in direction of +y, and forward (towards viewer) in direction of -z.
                transformationMatrix = ARUtilityFunctions.LHMatrixFromRHMatrix(matrixRaw);

                // 4 - If visible, set marker pose.
                Matrix4x4 pose = ARStaticCamera.Instance.transform.localToWorldMatrix * transformationMatrix;
                transform.position   = ARUtilityFunctions.PositionFromMatrix(pose);
                transform.rotation   = ARUtilityFunctions.RotationFromMatrix(pose);
                transform.localScale = storedScale;
            }

            // 5 - If visibility state is new, notify event receivers via "OnMarkerFound" or "OnMarkerLost".
            if (notify)
            {
                if (null != eventReceivers && eventReceivers.Count > 0)
                {
                    if (visible)
                    {
                        eventReceivers.ForEach(x => x.OnMarkerFound(this));
                    }
                    else
                    {
                        eventReceivers.ForEach(x => x.OnMarkerLost(this));
                    }
                }
                // 6 - If visibility state is new, set appropriate active state for marker children.
                for (int i = 0; i < transform.childCount; ++i)
                {
                    transform.GetChild(i).gameObject.SetActive(visible);
                }
            }

            if (visible)
            {
                // 7 - If visible, notify event receivers that the marker's pose has been updated via "OnMarkerTracked".
                if (null != eventReceivers && eventReceivers.Count > 0)
                {
                    eventReceivers.ForEach(x => x.OnMarkerTracked(this));
                }
            }
        }
    }