Exemple #1
0
    public bool SetupCamera(float nearClipPlane, float farClipPlane, Matrix4x4 projectionMatrix, ref bool opticalOut)
    {
        Camera c = this.gameObject.GetComponent <Camera>();

        // A perspective projection matrix from the tracker
        c.orthographic = false;

        // Shouldn't really need to set these, because they are part of the custom
        // projection matrix, but it seems that in the editor, the preview camera view
        // isn't using the custom projection matrix.
        c.nearClipPlane = nearClipPlane;
        c.farClipPlane  = farClipPlane;

        if (Optical)
        {
            float   fovy;
            float   aspect;
            float[] m = new float[16];
            float[] p = new float[16];
            opticalSetupOK = PluginFunctions.arwLoadOpticalParams(null, OpticalParamsFileContents, OpticalParamsFileContents.Length, out fovy, out aspect, m, p);
            if (!opticalSetupOK)
            {
                ARController.Log(LogTag + "Error loading optical parameters.");
                return(false);
            }
            m[12] *= 0.001f;
            m[13] *= 0.001f;
            m[14] *= 0.001f;
            ARController.Log(LogTag + "Optical parameters: fovy=" + fovy + ", aspect=" + aspect + ", camera position (m)={" + m[12].ToString("F3") + ", " + m[13].ToString("F3") + ", " + m[14].ToString("F3") + "}");

            c.projectionMatrix = ARUtilityFunctions.MatrixFromFloatArray(p);

            opticalViewMatrix = ARUtilityFunctions.MatrixFromFloatArray(m);
            if (OpticalEyeLateralOffsetRight != 0.0f)
            {
                opticalViewMatrix = Matrix4x4.TRS(new Vector3(-OpticalEyeLateralOffsetRight, 0.0f, 0.0f), Quaternion.identity, Vector3.one) * opticalViewMatrix;
            }
            // Convert to left-hand matrix.
            opticalViewMatrix = ARUtilityFunctions.LHMatrixFromRHMatrix(opticalViewMatrix);

            opticalOut = true;
        }
        else
        {
            c.projectionMatrix = projectionMatrix;
        }

        // Don't clear anything or else we interfere with other foreground cameras
        c.clearFlags = CameraClearFlags.Nothing;

        // Renders after the clear and background cameras
        c.depth = 2;

        c.transform.position   = new Vector3(0.0f, 0.0f, 0.0f);
        c.transform.rotation   = new Quaternion(0.0f, 0.0f, 0.0f, 1.0f);
        c.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);


        return(true);
    }
Exemple #2
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);
            }
        }
    }
Exemple #3
0
    public ARPattern(IPluginFunctions pluginFunctions, int trackableID, int patternID)
    {
        float[] matrixRawArray = new float[16];
        float   widthRaw       = 0.0f;
        float   heightRaw      = 0.0f;

        // Get the pattern local transformation and size.

        if (!pluginFunctions.arwGetTrackablePatternConfig(trackableID, patternID, matrixRawArray, out widthRaw, out heightRaw, out imageSizeX, out imageSizeY))
        {
            throw new ArgumentException("Invalid argument", "markerID,patternID");
        }
        width  = widthRaw * 0.001f;
        height = heightRaw * 0.001f;

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

        Matrix4x4 matrixRaw = ARUtilityFunctions.MatrixFromFloatArray(matrixRawArray);

        //ARController.Log("arwGetMarkerPatternConfig(" + markerID + ", " + patternID + ", ...) got matrix: [" + Environment.NewLine + matrixRaw.ToString("F3").Trim() + "]");

        // artoolkitX 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.
        matrix = ARUtilityFunctions.LHMatrixFromRHMatrix(matrixRaw);

        // Handle pattern image.
        if (imageSizeX > 0 && imageSizeY > 0)
        {
            // Allocate a new texture for the pattern image
            texture            = new Texture2D(imageSizeX, imageSizeY, TextureFormat.RGBA32, false);
            texture.filterMode = FilterMode.Point;
            texture.wrapMode   = TextureWrapMode.Clamp;
            texture.anisoLevel = 0;

            // Get the pattern image data and load it into the texture
            Color[] colors = new Color[imageSizeX * imageSizeY];
            if (pluginFunctions.arwGetTrackablePatternImage(trackableID, patternID, colors))
            {
                texture.SetPixels(colors);
                texture.Apply();
            }
        }
    }
Exemple #4
0
    public ARPattern(int markerID, int patternID)
    {
        float[] matrixRawArray = new float[16];
        float   widthRaw       = 0.0f;
        float   heightRaw      = 0.0f;

        // Get the pattern local transformation and size.
        if (!PluginFunctions.arwGetTrackableAppearanceConfig(markerID, patternID, matrixRawArray, out widthRaw, out heightRaw, out imageSizeX, out imageSizeY))
        {
            throw new System.ArgumentException("Invalid argument", "markerID,patternID");
        }

        width  = widthRaw * ARTrackable.UNITY_TO_ARTOOLKIT;
        height = heightRaw * ARTrackable.UNITY_TO_ARTOOLKIT;
        // Scale the position from ARToolKit units (mm) into Unity units (m).
        matrixRawArray[12] *= ARTrackable.UNITY_TO_ARTOOLKIT;
        matrixRawArray[13] *= ARTrackable.UNITY_TO_ARTOOLKIT;
        matrixRawArray[14] *= ARTrackable.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.
        matrix = ARUtilityFunctions.LHMatrixFromRHMatrix(matrixRaw);

        // Handle pattern image.
        if (imageSizeX > 0 && imageSizeY > 0)
        {
            // Allocate a new texture for the pattern image
            texture            = new Texture2D(imageSizeX, imageSizeY, TextureFormat.RGBA32, false);
            texture.filterMode = FilterMode.Point;
            texture.wrapMode   = TextureWrapMode.Clamp;
            texture.anisoLevel = 0;

            // Get the pattern image data and load it into the texture
            Color32[] colors32 = new Color32[imageSizeX * imageSizeY];
            if (PluginFunctions.arwGetTrackableAppearanceImage(markerID, patternID, colors32))
            {
                texture.SetPixels32(colors32);
                texture.Apply();
            }
        }
    }
    // 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()
    {
        // Only query visibility if we are running in the Player.
        if (!Application.isPlaying)
        {
            return;
        }

        lock (loadLock)
        {
            //ARController.Log(LogTag + "ARTrackable.Update()");
            if (UID == NO_ID)
            {
                visible = false;
                return;
            }

            if (pluginFunctions == null || !pluginFunctions.IsInited())
            {
                visible = false;
                return;
            }

            float[] matrixRawArray = new float[16];
            visible = pluginFunctions.arwQueryTrackableVisibilityAndTransformation(UID, matrixRawArray);
            //ARController.Log(LogTag + "ARTrackable.Update() UID=" + UID + ", visible=" + visible);

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

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

                // artoolkitX 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);
            }
        }
    }
Exemple #6
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);
            }
        }
    }
Exemple #7
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));
                }
            }
        }
    }