예제 #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
    // Load the native ARTrackable structure(s) and set the UID.
    public void Load()
    {
        lock (loadLock) {
            if (this.enabled == false)
            {
                return;
            }

            //ARController.Log(LogTag + "ARTrackable.Load()");
            if (UID != NO_ID)
            {
                return;
            }

            if (pluginFunctions == null || !pluginFunctions.IsInited())
            {
                // If arwInitialiseAR() has not yet been called, we can't load the native trackable yet.
                // ARController.InitialiseAR() will trigger this again when arwInitialiseAR() has been called.
                return;
            }

            // Work out the configuration string to pass to the native side.
            string dir = Application.streamingAssetsPath;
            string cfg = "";

            switch (Type)
            {
            case TrackableType.Square:
                // Multiply width by 1000 to convert from metres to artoolkitX's millimetres.
                cfg = "single_buffer;" + PatternWidth * 1000.0f + ";buffer=" + PatternContents;
                break;

            case TrackableType.SquareBarcode:
                // Multiply width by 1000 to convert from metres to artoolkitX's millimetres.
                cfg = "single_barcode;" + BarcodeID + ";" + PatternWidth * 1000.0f;
                break;

            case TrackableType.Multimarker:
                    #if !UNITY_METRO
                if (dir.Contains("://"))
                {
                    // On Android, we need to unpack the StreamingAssets from the .jar file in which
                    // they're archived into the native file system.
                    dir = Application.temporaryCachePath;
                    if (!unpackStreamingAssetToCacheDir(MultiConfigFile))
                    {
                        dir = "";
                    }
                    else
                    {
                        //string[] unpackFiles = getPatternFiles;
                        //foreach (string patternFile in patternFiles) {
                        //if (!unpackStreamingAssetToCacheDir(patternFile)) {
                        //    dir = "";
                        //    break;
                        //}
                    }
                }
                    #endif

                if (!string.IsNullOrEmpty(dir) && !string.IsNullOrEmpty(MultiConfigFile))
                {
                    cfg = "multi;" + System.IO.Path.Combine(dir, MultiConfigFile);
                }
                break;


            case TrackableType.NFT:
                    #if !UNITY_METRO
                if (dir.Contains("://"))
                {
                    // On Android, we need to unpack the StreamingAssets from the .jar file in which
                    // they're archived into the native file system.
                    dir = Application.temporaryCachePath;
                    foreach (string ext in NFTDataExts)
                    {
                        string basename = NFTDataName + "." + ext;
                        if (!unpackStreamingAssetToCacheDir(basename))
                        {
                            dir = "";
                            break;
                        }
                    }
                }
                    #endif

                if (!string.IsNullOrEmpty(dir) && !string.IsNullOrEmpty(NFTDataName))
                {
                    cfg = "nft;" + System.IO.Path.Combine(dir, NFTDataName);
                }
                break;

            case TrackableType.TwoD:
                #if !UNITY_METRO
                if (dir.Contains("://"))
                {
                    // On Android, we need to unpack the StreamingAssets from the .jar file in which
                    // they're archived into the native file system.
                    dir = Application.temporaryCachePath;
                    if (!unpackStreamingAssetToCacheDir(TwoDImageFile))
                    {
                        dir = "";
                    }
                }
                #endif

                if (!string.IsNullOrEmpty(dir) && !string.IsNullOrEmpty(TwoDImageFile))
                {
                    cfg = "2d;" + System.IO.Path.Combine(dir, TwoDImageFile) + ";" + TwoDImageWidth;
                }
                break;

            default:
                // Unknown marker type?
                break;
            }

            // If a valid config. could be assembled, get the native side to process it, and assign the resulting ARTrackable UID.
            if (!string.IsNullOrEmpty(cfg))
            {
                uid = pluginFunctions.arwAddTrackable(cfg);
                if (UID == NO_ID)
                {
                    ARController.Log(LogTag + "Error loading marker.");
                }
                else
                {
                    // Trackable loaded. Do any additional configuration.
                    //ARController.Log("Added marker with cfg='" + cfg + "'");

                    if (Type == TrackableType.Square || Type == TrackableType.SquareBarcode)
                    {
                        UseContPoseEstimation = currentUseContPoseEstimation;
                    }
                    Filtered         = currentFiltered;
                    FilterSampleRate = currentFilterSampleRate;
                    FilterCutoffFreq = currentFilterCutoffFreq;

                    // Retrieve any required information from the configured native ARTrackable.
                    if (Type == TrackableType.NFT || Type == TrackableType.TwoD)
                    {
                        if (Type == TrackableType.NFT)
                        {
                            NFTScale = currentNFTScale;
                        }

                        int imageSizeX, imageSizeY;
                        pluginFunctions.arwGetTrackablePatternConfig(UID, 0, null, out NFTWidth, out NFTHeight, out imageSizeX, out imageSizeY);
                        NFTWidth  *= 0.001f;
                        NFTHeight *= 0.001f;
                        //ARController.Log("Got NFTWidth=" + NFTWidth + ", NFTHeight=" + NFTHeight + ".");
                    }
                    else
                    {
                        // Create array of patterns. A single marker will have array length 1.
                        int numPatterns = pluginFunctions.arwGetTrackablePatternCount(UID);
                        //ARController.Log("Trackable with UID=" + UID + " has " + numPatterns + " patterns.");
                        if (numPatterns > 0)
                        {
                            patterns = new ARPattern[numPatterns];
                            for (int i = 0; i < numPatterns; i++)
                            {
                                patterns[i] = new ARPattern(pluginFunctions, UID, i);
                            }
                        }
                    }
                }
            }
        }
    }