Esempio n. 1
0
//	private Guid   cacheGuid               = Guid.Empty;
//	// When indexes update, there are three likely scenarios:
//	//     1) ID has not changed.
//	//     2) ID has shifted by a few values due to sorting.
//	//     3) Content has been removed, and therefore there is no ID.
//	private int ReassociateContentID(int index, TrackableType markerType, string content) {
//		if (string.CompareOrdinal(ARToolKitAssetManager.AllMarkers[index], content) == 0) {
//			return index;
//		} else {
//			for (int i = 0; i < ARToolKitAssetManager.
//		}
//	}

    public override void OnInspectorGUI()
    {
        // Get the ARMarker that this panel will edit.
        ARTrackable arMarker = (ARTrackable)target;

        if (null == arMarker)
        {
            return;
        }
        // Attempt to load. Might not work out if e.g. for a single marker, pattern hasn't been
        // assigned yet, or for an NFT marker, dataset hasn't been specified.
        if (arMarker.UID == ARTrackable.NO_ID)
        {
            PluginFunctions.arwInitialiseAR();
            arMarker.Load();
        }

        // Check if a new image was dropped into the Project directory
        string        path = Application.streamingAssetsPath + "/" + ARToolKitAssetManager.IMAGES_DIRECTORY_NAME;
        DirectoryInfo dir  = new DirectoryInfo(path);

        FileInfo[] imageFileList = dir.GetFiles("*.jpg").Union(dir.GetFiles("*.jpeg")).ToArray();
        if (imageFileList != null && ARToolKitAssetManager.Images != null && imageFileList.Length != ARToolKitAssetManager.Images.Length)
        {
            if (imageFileList.Length < ARToolKitAssetManager.Images.Length)
            {
                //An image was deleted from the file system so we might have an empty ARTrackable now
                ARController.Log("Warning: Trackable image removed. Please check all ARTrackables and make sure that they have an image assigned.");
            }
            //We found a new trackable in the file system or a trackable was removed lets reload the trackables.
            ARToolKitAssetManager.Reload();
        }

        // Draw the drag n drop area
        DropAreaGUI();

        int selectedMarker = ArrayUtility.IndexOf(ARToolKitAssetManager.AllMarkers, arMarker.EditorMarkerName);

        arMarker.EditorMarkerIndex = EditorGUILayout.Popup("Marker", selectedMarker, ARToolKitAssetManager.AllMarkers);

        bool newSelection = false;

        if (arMarker.EditorMarkerIndex < 0)
        {
            //An image was deleted from the file system so we have an empty ARTrackable now
            ARController.Log("Warning: Trackable image removed. Please check the ARTrackable and make sure that is has an image assigned.");
            return;
        }
        else
        {
            if (string.CompareOrdinal(arMarker.EditorMarkerName, ARToolKitAssetManager.AllMarkers[arMarker.EditorMarkerIndex]) != 0)
            {
                newSelection = true;
                arMarker.EditorMarkerName = ARToolKitAssetManager.AllMarkers[arMarker.EditorMarkerIndex];
            }
        }

        ARTrackable.TrackableType markerType = DetermineTrackableType(arMarker.EditorMarkerIndex);
        if (arMarker.Type != markerType)
        {
            arMarker.ClearUnusedValues();
            arMarker.Type = markerType;
            UpdatePatternDetectionMode();
        }

        EditorGUILayout.LabelField("Type ", ARTrackable.TrackableTypeNames[arMarker.Type]);

        EditorGUILayout.LabelField("Unique ID", (arMarker.UID == ARTrackable.NO_ID ? "Not Loaded": arMarker.UID.ToString()));

        EditorGUILayout.BeginHorizontal();
        // Draw all the marker images
        if (arMarker.Patterns != null)
        {
            for (int i = 0; i < arMarker.Patterns.Length; ++i)
            {
                EditorGUILayout.Separator();
                GUILayout.Label(new GUIContent(string.Format("Pattern {0}, {1}m", i, arMarker.Patterns[i].width.ToString("n3")), arMarker.Patterns[i].texture), GUILayout.ExpandWidth(false));                 // n3 -> 3 decimal places.
            }
        }
        EditorGUILayout.EndHorizontal();

        EditorGUILayout.Separator();

        switch (arMarker.Type)
        {
        case ARTrackable.TrackableType.TwoD:
            if (newSelection)
            {
                arMarker.TwoDImageName = ARToolKitAssetManager.AllMarkers[arMarker.EditorMarkerIndex];
            }
            float twoDImageHeight = EditorGUILayout.FloatField("Image height", arMarker.TwoDImageHeight);
            if (twoDImageHeight != arMarker.TwoDImageHeight)
            {
                EditorUtility.SetDirty(arMarker);
                arMarker.TwoDImageHeight = twoDImageHeight;
            }

            float   width = 0.0f, height = 0.0f;
            int     imageWidth = 0, imageHeight = 0;
            float[] transformation = new float[16];

            if (PluginFunctions.arwGetTrackableAppearanceConfig(arMarker.UID, 0, transformation, out width, out height, out imageWidth, out imageHeight))
            {
                Color32[] imagePixels = new Color32[imageWidth * imageHeight];
                if (PluginFunctions.arwGetTrackableAppearanceImage(arMarker.UID, 0, imagePixels))
                {
                    //Set the texture with the trackable appearance.
                    Texture2D texture = new Texture2D(imageWidth, imageHeight, TextureFormat.RGBA32, true);
                    texture.SetPixels32(imagePixels);
                    texture.Apply();

                    //Display label and texture to the user
                    EditorGUILayout.BeginHorizontal();
                    GUILayout.Label("Trackable Appearance");

                    //Resize texture for viewport with max with and height
                    GUILayout.Label(ARTrackableAppearanceScale.BilinearWithMaxSize(texture, 200, 200));
                    EditorGUILayout.EndHorizontal();
                }
            }
            break;

        case ARTrackable.TrackableType.Square:
            if (newSelection)
            {
                arMarker.PatternContents = GetPatternContents(ARToolKitAssetManager.AllMarkers[arMarker.EditorMarkerIndex]);
            }
            arMarker.PatternWidth          = EditorGUILayout.FloatField("Pattern Width (m)", arMarker.PatternWidth);
            arMarker.UseContPoseEstimation = EditorGUILayout.Toggle("Contstant Pose Estimation", arMarker.UseContPoseEstimation);
            break;

        case ARTrackable.TrackableType.SquareBarcode:
            if (newSelection)
            {
                string[] idArray = ARToolKitAssetManager.AllMarkers[arMarker.EditorMarkerIndex].Split(' ');
                arMarker.BarcodeID = int.Parse(idArray[idArray.Length - 1]);
            }
            arMarker.PatternWidth          = EditorGUILayout.FloatField("Pattern Width (m)", arMarker.PatternWidth);
            arMarker.UseContPoseEstimation = EditorGUILayout.Toggle("Contstant Pose Estimation", arMarker.UseContPoseEstimation);
            break;

        case ARTrackable.TrackableType.Multimarker:
            if (newSelection)
            {
                arMarker.MultiConfigFile = ARToolKitAssetManager.AllMarkers[arMarker.EditorMarkerIndex];
            }
            break;
        }

        EditorGUILayout.Separator();

        arMarker.Filtered = EditorGUILayout.Toggle("Filter Pose", arMarker.Filtered);
        if (arMarker.Filtered)
        {
            arMarker.FilterSampleRate = EditorGUILayout.Slider("Sample Rate", arMarker.FilterSampleRate, 1.0f, 30.0f);
            arMarker.FilterCutoffFreq = EditorGUILayout.Slider("Cutoff Frequency", arMarker.FilterCutoffFreq, 1.0f, 30.0f);
        }

        if (arMarker.Type == ARTrackable.TrackableType.Square || arMarker.Type == ARTrackable.TrackableType.SquareBarcode || arMarker.Type == ARTrackable.TrackableType.Multimarker)
        {
            showGlobalSquareOptions = EditorGUILayout.Foldout(showGlobalSquareOptions, "Global Square Tracking Options");
            if (showGlobalSquareOptions)
            {
                ARController.Instance.TemplateSize = EditorGUILayout.IntSlider("Template Size (bits)", ARController.Instance.TemplateSize, 16, 64);

                int currentTemplateCountMax = ARController.Instance.TemplateCountMax;
                int newTemplateCountMax     = EditorGUILayout.IntField("Maximum Template Count", currentTemplateCountMax);
                if (newTemplateCountMax != currentTemplateCountMax && newTemplateCountMax > 0)
                {
                    ARController.Instance.TemplateCountMax = newTemplateCountMax;
                }

                bool trackInColor = EditorGUILayout.Toggle("Track Templates in Color", ARController.Instance.trackTemplatesInColor);
                if (trackInColor != ARController.Instance.trackTemplatesInColor)
                {
                    ARController.Instance.trackTemplatesInColor = trackInColor;
                    UpdatePatternDetectionMode();
                }

                ARController.Instance.BorderSize    = UnityEngine.Mathf.Clamp(EditorGUILayout.FloatField("Border Size (%)", ARController.Instance.BorderSize), 0.0f, 0.5f);
                ARController.Instance.LabelingMode  = (ARController.ARToolKitLabelingMode)EditorGUILayout.EnumPopup("Marker Border Color", ARController.Instance.LabelingMode);
                ARController.Instance.ImageProcMode = (ARController.ARToolKitImageProcMode)EditorGUILayout.EnumPopup("Image Processing Mode", ARController.Instance.ImageProcMode);
            }
        }

        var obj  = new SerializedObject(arMarker);
        var prop = obj.FindProperty("eventReceivers");

        EditorGUILayout.PropertyField(prop, new GUIContent("Event Receivers"), true);
        obj.ApplyModifiedProperties();
    }
    public override void OnInspectorGUI()
    {
        EditorGUILayout.BeginVertical();

        // Get the ARTrackable that this panel will edit.
        ARTrackable m = (ARTrackable)target;

        if (m == null)
        {
            return;
        }

        // Attempt to load. Might not work out if e.g. for a single marker, pattern hasn't been
        // assigned yet, or for an NFT marker, dataset hasn't been specified.
        if (m.UID == ARTrackable.NO_ID)
        {
            m.Load();
        }

        // Trackable tag
        m.Tag = EditorGUILayout.TextField("Trackable tag", m.Tag);
        EditorGUILayout.LabelField("UID", (m.UID == ARTrackable.NO_ID ? "Not loaded": m.UID.ToString()));

        EditorGUILayout.Separator();

        // Trackable type
        ARTrackable.TrackableType t = (ARTrackable.TrackableType)EditorGUILayout.EnumPopup("Type", m.Type);
        if (m.Type != t)   // Reload on change.
        {
            m.Unload();
            m.Type = t;
            m.Load();
        }

        // Description of the type of marker
        EditorGUILayout.LabelField("Description", ARTrackable.TrackableTypeNames[m.Type]);

        switch (m.Type)
        {
        case ARTrackable.TrackableType.Square:
        case ARTrackable.TrackableType.SquareBarcode:

            if (m.Type == ARTrackable.TrackableType.Square)
            {
                // For pattern markers, offer a popup with marker pattern file names.
                RefreshPatternFilenames();                         // Update the list of available markers from the resources dir
                if (PatternFilenames.Length > 0)
                {
                    int    patternFilenameIndex = EditorGUILayout.Popup("Pattern file", m.PatternFilenameIndex, PatternFilenames);
                    string patternFilename      = PatternAssets[patternFilenameIndex].name;
                    if (patternFilename != m.PatternFilename)
                    {
                        m.Unload();
                        m.PatternFilenameIndex = patternFilenameIndex;
                        m.PatternFilename      = patternFilename;
                        m.PatternContents      = PatternAssets[m.PatternFilenameIndex].text;
                        m.Load();
                    }
                }
                else
                {
                    m.PatternFilenameIndex = 0;
                    EditorGUILayout.LabelField("Pattern file", "No patterns available");
                    m.PatternFilename = "";
                    m.PatternContents = "";
                }
            }
            else
            {
                // For barcode markers, allow the user to specify the barcode ID.
                long BarcodeID = EditorGUILayout.LongField("Barcode ID", m.BarcodeID);
                if (BarcodeID < 0)
                {
                    BarcodeID = 0;
                }
                ARController arcontroller = Component.FindObjectOfType(typeof(ARController)) as ARController;
                if (arcontroller != null)
                {
                    long maxBarcodeID = barcodeCounts[arcontroller.MatrixCodeType] - 1;
                    if (BarcodeID > maxBarcodeID)
                    {
                        BarcodeID = maxBarcodeID;
                    }
                    EditorGUILayout.LabelField("(in range 0 to " + (barcodeCounts[arcontroller.MatrixCodeType] - 1) + ")");
                }
                if (BarcodeID != m.BarcodeID)
                {
                    m.Unload();
                    m.BarcodeID = BarcodeID;
                    m.Load();
                }
            }

            float patternWidthPrev = m.PatternWidth;
            m.PatternWidth = EditorGUILayout.FloatField("Width", m.PatternWidth);
            if (patternWidthPrev != m.PatternWidth)
            {
                m.Unload();
                m.Load();
            }
            m.UseContPoseEstimation = EditorGUILayout.Toggle("Cont. pose estimation", m.UseContPoseEstimation);

            break;

        case ARTrackable.TrackableType.Multimarker:
            string MultiConfigFile = EditorGUILayout.TextField("Multimarker config.", m.MultiConfigFile);
            if (MultiConfigFile != m.MultiConfigFile)
            {
                m.Unload();
                m.MultiConfigFile = MultiConfigFile;
                m.Load();
            }
            break;

        case ARTrackable.TrackableType.NFT:
            string NFTDataSetName = EditorGUILayout.TextField("NFT dataset name", m.NFTDataName);
            if (NFTDataSetName != m.NFTDataName)
            {
                m.Unload();
                m.NFTDataName = NFTDataSetName;
                m.Load();
            }

            float nftScalePrev = m.NFTScale;
            m.NFTScale = EditorGUILayout.FloatField("NFT marker scalefactor", m.NFTScale);
            if (nftScalePrev != m.NFTScale)
            {
                EditorUtility.SetDirty(m);
            }
            break;

        case ARTrackable.TrackableType.TwoD:
            string TwoDImageFile = EditorGUILayout.TextField("Image file", m.TwoDImageFile);
            if (TwoDImageFile != m.TwoDImageFile)
            {
                m.Unload();
                m.TwoDImageFile = TwoDImageFile;
                m.Load();
            }

            float twoDImageWidthPrev = m.TwoDImageWidth;
            m.TwoDImageWidth = EditorGUILayout.FloatField("Image width", m.TwoDImageWidth);
            if (twoDImageWidthPrev != m.TwoDImageWidth)
            {
                m.Unload();
                m.Load();
            }
            break;
        }

        EditorGUILayout.Separator();

        showFilterOptions = EditorGUILayout.Foldout(showFilterOptions, "Filter Options");
        if (showFilterOptions)
        {
            m.Filtered         = EditorGUILayout.Toggle("Filtered:", m.Filtered);
            m.FilterSampleRate = EditorGUILayout.Slider("Sample rate:", m.FilterSampleRate, 1.0f, 30.0f);
            m.FilterCutoffFreq = EditorGUILayout.Slider("Cutoff freq.:", m.FilterCutoffFreq, 1.0f, 30.0f);
        }

        EditorGUILayout.BeginHorizontal();

        // Draw all the marker images
        if (m.Patterns != null)
        {
            for (int i = 0; i < m.Patterns.Length; i++)
            {
                GUILayout.Label(new GUIContent("Pattern " + i + ", " + m.Patterns[i].width.ToString("n3") + " m", m.Patterns[i].texture), GUILayout.ExpandWidth(false)); // n3 -> 3 decimal places.
            }
        }

        EditorGUILayout.EndHorizontal();
        EditorGUILayout.EndVertical();
    }