コード例 #1
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();
            }
        }
    }
コード例 #2
0
    private Matrix4x4 opticalViewMatrix;                     // This transform expresses the position and orientation of the physical camera in eye coordinates.

    public bool SetupCamera(IPluginFunctions pluginFunctions, float nearClipPlane, float farClipPlane, Matrix4x4 projectionMatrix, out bool opticalOut)
    {
        Camera c = this.gameObject.GetComponent <Camera>();

        opticalOut = false;

        // 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];

            if (OpticalCalibrationMode0 == OpticalCalibrationMode.ARXOpticalParametersFile)
            {
                opticalSetupOK = pluginFunctions.arwLoadOpticalParams(null, OpticalParamsFileContents, OpticalParamsFileContents.Length, nearClipPlane, farClipPlane, out fovy, out aspect, m, p);
                if (!opticalSetupOK)
                {
                    ARController.Log(LogTag + "Error loading ARX optical parameters.");
                    return(false);
                }

                // Convert millimetres to metres.
                m[12] *= 0.001f;
                m[13] *= 0.001f;
                m[14] *= 0.001f;
                c.projectionMatrix = ARUtilityFunctions.MatrixFromFloatArray(p);
                ARController.Log(LogTag + "Optical parameters: fovy=" + fovy + ", aspect=" + aspect + ", camera position (m)={" + m[12].ToString("F3") + ", " + m[13].ToString("F3") + ", " + m[14].ToString("F3") + "}");
            }
            else
            {
                m[0] = m[5] = m[10] = m[15] = 1.0f;
                m[1] = m[2] = m[3] = m[4] = m[6] = m[7] = m[8] = m[9] = m[11] = m[12] = m[13] = m[14] = 0.0f;
            }

            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);
    }