// This method updates the respective Trackable properties (e.g. size) // with data set data. public override void ApplyDataSetProperties() { // Prefabs should not be editable if (QCARUtilities.GetPrefabType(mTarget) == PrefabType.Prefab) { return; } MultiTargetBehaviour mtb = (MultiTargetBehaviour)mTarget; ConfigData.MultiTarget mtConfig; if (TrackableInDataSet(mtb.TrackableName, mtb.DataSetName)) { ConfigData dataSetData = ConfigDataManager.Instance.GetConfigData(mtb.DataSetName); dataSetData.GetMultiTarget(mtb.TrackableName, out mtConfig); } else { // If the Trackable has been removed from the data set we reset it to default. ConfigData dataSetData = ConfigDataManager.Instance.GetConfigData(QCARUtilities.GlobalVars.DEFAULT_DATA_SET_NAME); dataSetData.GetMultiTarget(QCARUtilities.GlobalVars.DEFAULT_TRACKABLE_NAME, out mtConfig); mtb.DataSetPath = QCARUtilities.GlobalVars.DEFAULT_DATA_SET_NAME; mtb.TrackableName = QCARUtilities.GlobalVars.DEFAULT_TRACKABLE_NAME; } List <ConfigData.MultiTargetPart> prtConfigs = mtConfig.parts; MultiTargetEditor.UpdateParts(mtb, prtConfigs.ToArray()); }
private void CreateMissingDataSetTrackableBehaviours(DataSet dataSet) { foreach (Trackable trackable in dataSet.GetTrackables()) { if (!mTrackableBehaviours.ContainsKey(trackable.ID)) { if (trackable is ImageTarget) { ImageTargetBehaviour itb = CreateImageTargetBehaviour((ImageTarget)trackable); // Create Virtual Buttons for this Image Target. ((IEditorImageTargetBehaviour)itb).CreateMissingVirtualButtonBehaviours(); // Add newly created Image Target to dictionary. mTrackableBehaviours[trackable.ID] = itb; } else if (trackable is MultiTarget) { MultiTargetBehaviour mtb = CreateMultiTargetBehaviour((MultiTarget)trackable); // Add newly created Multi Target to dictionary. mTrackableBehaviours[trackable.ID] = mtb; } } } }
// Updates MultiTarget parts with the values stored in the "prtConfigs" // array. Deletes all parts and recreates them. public static void UpdateParts(MultiTargetBehaviour mt, ConfigData.MultiTargetPart[] prtConfigs) { Transform childTargets = mt.transform.Find("ChildTargets"); if (childTargets != null) { Object.DestroyImmediate(childTargets.gameObject); } GameObject newObject = new GameObject(); newObject.name = "ChildTargets"; newObject.transform.parent = mt.transform; newObject.hideFlags = HideFlags.NotEditable; newObject.transform.localPosition = Vector3.zero; newObject.transform.localRotation = Quaternion.identity; newObject.transform.localScale = Vector3.one; childTargets = newObject.transform; Material maskMaterial = (Material)AssetDatabase.LoadAssetAtPath( QCARUtilities.GlobalVars.PATH_TO_MASK_MATERIAL, typeof(Material)); int numParts = prtConfigs.Length; for (int i = 0; i < numParts; ++i) { ConfigData.ImageTarget itConfig; if (!SceneManager.Instance.GetImageTarget(prtConfigs[i].name, out itConfig) && prtConfigs[i].name != QCARUtilities.GlobalVars.DEFAULT_NAME) { Debug.LogError("No image target named " + prtConfigs[i].name); return; } Vector2 size = itConfig.size; Vector3 scale = new Vector3(size.x * 0.1f, 1, size.y * 0.1f); GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane); plane.name = prtConfigs[i].name; plane.transform.parent = childTargets.transform; plane.transform.localPosition = prtConfigs[i].translation; plane.transform.localRotation = prtConfigs[i].rotation; plane.transform.localScale = scale; UpdateMaterial(plane); plane.hideFlags = HideFlags.NotEditable; MaskOutBehaviour script = (MaskOutBehaviour)plane.AddComponent(typeof(MaskOutBehaviour)); script.maskMaterial = maskMaterial; } }
private static void UpdateMaterial(MultiTargetBehaviour mt, GameObject go) { // Load reference material string referenceMaterialPath = QCARUtilities.GlobalVars.REFERENCE_MATERIAL_PATH; Material referenceMaterial = (Material)AssetDatabase.LoadAssetAtPath(referenceMaterialPath, typeof(Material)); if (referenceMaterial == null) { Debug.LogError("Could not find reference material at " + referenceMaterialPath + " please reimport Unity package."); return; } // Load texture from texture folder string pathToTexture = QCARUtilities.GlobalVars.TARGET_TEXTURES_PATH; string textureFile = pathToTexture + mt.DataSetName + "/" + go.name + "_scaled"; if (File.Exists(textureFile + ".png")) { textureFile += ".png"; } else if (File.Exists(textureFile + ".jpg")) { textureFile += ".jpg"; } Texture2D targetTexture = (Texture2D)AssetDatabase.LoadAssetAtPath(textureFile, typeof(Texture2D)); if (targetTexture == null) { // If the texture is null we simply assign a default material go.renderer.sharedMaterial = referenceMaterial; return; } // We create a new material based on the reference material Material materialForTargetTexture = new Material(referenceMaterial); materialForTargetTexture.mainTexture = targetTexture; materialForTargetTexture.name = targetTexture.name + "Material"; materialForTargetTexture.mainTextureScale = new Vector2(-1, -1); go.renderer.sharedMaterial = materialForTargetTexture; // Cleanup assets that have been created temporarily. EditorUtility.UnloadUnusedAssets(); }
public void disableMultiTargets(string name) { for (int i = 0; i < transform.childCount; i++) { GameObject obj = transform.GetChild(i).gameObject; if (obj.name != name) { MultiTargetBehaviour mtb = obj.GetComponent <MultiTargetBehaviour>(); mtb.enabled = false; } } }
public void enableMultiTargets() { for (int i = 0; i < transform.childCount; i++) { GameObject obj = transform.GetChild(i).gameObject; if (!listBoxPlaced.Contains(obj)) { MultiTargetBehaviour mtb = obj.GetComponent <MultiTargetBehaviour>(); mtb.enabled = true; } } }
private void CreateMultiTargets() { // Allocate array for all Multi Targets. int numMultiTargets = dataSetGetNumTrackableType( (int)TrackableBehaviour.TrackableType.MULTI_TARGET, mDataSetPtr); IntPtr multiTargetDataPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MultiTargetData)) * numMultiTargets); // Copy Multi Target properties from native. if (dataSetGetTrackablesOfType((int)TrackableBehaviour.TrackableType.MULTI_TARGET, multiTargetDataPtr, numMultiTargets, mDataSetPtr) == 0) { Debug.LogError("Could not create Multi Target Behaviours"); return; } // Create Multi Target Behaviours. for (int i = 0; i < numMultiTargets; ++i) { IntPtr trackablePtr = new IntPtr(multiTargetDataPtr.ToInt32() + i * Marshal.SizeOf(typeof(MultiTargetData))); MultiTargetData trackableData = (MultiTargetData) Marshal.PtrToStructure(trackablePtr, typeof(MultiTargetData)); // Do not overwrite existing Trackables. if (mTrackableBehaviourDict.ContainsKey(trackableData.id)) { continue; } // QCAR support names up to 64 characters in length, but here we allocate // a slightly larger buffer: int nameLength = 128; System.Text.StringBuilder trackableName = new System.Text.StringBuilder(nameLength); dataSetGetTrackableName(mDataSetPtr, trackableData.id, trackableName, nameLength); MultiTargetBehaviour mtb = CreateMultiTarget(trackableData.id, trackableName.ToString()); // Add newly created Multi Target to dictionary. mTrackableBehaviourDict[trackableData.id] = mtb; } Marshal.FreeHGlobal(multiTargetDataPtr); }
// Initializes the Multi Target when it is drag-dropped into the scene. public void OnEnable() { MultiTargetBehaviour mtb = (MultiTargetBehaviour)target; // We don't want to initialize if this is a prefab. if (QCARUtilities.GetPrefabType(mtb) == PrefabType.Prefab) { return; } // Make sure the scene and config.xml file are synchronized. if (!SceneManager.Instance.SceneInitialized) { SceneManager.Instance.InitScene(); } IEditorMultiTargetBehaviour editorMtb = mtb; // Only setup target if it has not been set up previously. if (!editorMtb.InitializedInEditor && !EditorApplication.isPlaying) { ConfigData.MultiTargetData mtConfig; ConfigData dataSetData = ConfigDataManager.Instance.GetConfigData(QCARUtilities.GlobalVars.DEFAULT_DATA_SET_NAME); dataSetData.GetMultiTarget(QCARUtilities.GlobalVars.DEFAULT_TRACKABLE_NAME, out mtConfig); editorMtb.SetDataSetPath(QCARUtilities.GlobalVars.DEFAULT_DATA_SET_NAME); editorMtb.SetNameForTrackable(QCARUtilities.GlobalVars.DEFAULT_TRACKABLE_NAME); List <ConfigData.MultiTargetPartData> prtConfigs = mtConfig.parts; UpdateParts(editorMtb, prtConfigs.ToArray()); editorMtb.SetInitializedInEditor(true); } else if (!EditorApplication.isPlaying) { CheckMesh(mtb); } // Cache the current scale of the target: editorMtb.SetPreviousScale(mtb.transform.localScale); }
private MultiTargetBehaviour CreateMultiTargetBehaviour(MultiTarget multiTarget) { GameObject multiTargetObject = new GameObject(); MultiTargetBehaviour newMTB = multiTargetObject.AddComponent <MultiTargetBehaviour>(); IEditorMultiTargetBehaviour newEditorMTB = newMTB; Debug.Log("Creating Multi Target with values: " + "\n ID: " + multiTarget.ID + "\n Name: " + multiTarget.Name + "\n Path: " + newEditorMTB.DataSetPath); // Set Multi Target attributes. newEditorMTB.SetNameForTrackable(multiTarget.Name); newEditorMTB.SetDataSetPath(newEditorMTB.DataSetPath); newEditorMTB.InitializeMultiTarget(multiTarget); return(newMTB); }
private MultiTargetBehaviour CreateMultiTarget(int id, string mtName) { GameObject multiTargetObject = new GameObject(); MultiTargetBehaviour newMTB = multiTargetObject.AddComponent <MultiTargetBehaviour>(); Debug.Log("Creating Multi Target with values: " + "\n ID: " + id + "\n Name: " + mtName + "\n Path: " + this.Path + "\n Storage Type: " + this.FileStorageType.ToString()); // Set Multi Target attributes. newMTB.InitializeID(id); newMTB.TrackableName = mtName; newMTB.DataSetPath = this.Path; newMTB.DataSetStorageType = this.FileStorageType; return(newMTB); }
private static void CheckMesh(MultiTargetBehaviour mtb) { // when copy-pasting targets between scenes, the mesh and materials of // the game objects get lost. This checks for them and re-creates them if they are found missing. bool updateGeometry = false; Transform childTargets = mtb.transform.Find("ChildTargets"); if (childTargets == null) { updateGeometry = true; } else { for (int i = 0; i < childTargets.childCount; i++) { Transform childTarget = childTargets.GetChild(i); MeshFilter meshFilter = childTarget.GetComponent <MeshFilter>(); MeshRenderer meshRenderer = childTarget.GetComponent <MeshRenderer>(); if (meshFilter == null || meshFilter.sharedMesh == null || meshRenderer == null || meshRenderer.sharedMaterials.Length == 0 || meshRenderer.sharedMaterials[0] == null) { updateGeometry = true; } } } if (updateGeometry) { TrackableAccessor accessor = AccessorFactory.Create(mtb); if (accessor != null) { accessor.ApplyDataSetProperties(); } } }
// Initializes the Multi Target when it is drag-dropped into the scene. public void OnEnable() { MultiTargetBehaviour mtb = (MultiTargetBehaviour)target; // We don't want to initialize if this is a prefab. if (QCARUtilities.GetPrefabType(mtb) == PrefabType.Prefab) { return; } // Make sure the scene and config.xml file are synchronized. if (!SceneManager.Instance.SceneInitialized) { SceneManager.Instance.InitScene(); } // Only setup target if it has not been set up previously. if (!mtb.mInitializedInEditor) { ConfigData.MultiTarget mtConfig; ConfigData dataSetData = ConfigDataManager.Instance.GetConfigData(QCARUtilities.GlobalVars.DEFAULT_DATA_SET_NAME); dataSetData.GetMultiTarget(QCARUtilities.GlobalVars.DEFAULT_TRACKABLE_NAME, out mtConfig); mtb.DataSetPath = QCARUtilities.GlobalVars.DEFAULT_DATA_SET_NAME; mtb.TrackableName = QCARUtilities.GlobalVars.DEFAULT_TRACKABLE_NAME; List <ConfigData.MultiTargetPart> prtConfigs = mtConfig.parts; UpdateParts(mtb, prtConfigs.ToArray()); mtb.mInitializedInEditor = true; } // Cache the current scale of the target: mtb.mPreviousScale = mtb.transform.localScale; }
private static void UpdateMaterial(MultiTargetBehaviour mt, GameObject go) { // Load reference material string referenceMaterialPath = QCARUtilities.GlobalVars.REFERENCE_MATERIAL_PATH; Material referenceMaterial = (Material)AssetDatabase.LoadAssetAtPath(referenceMaterialPath, typeof(Material)); if (referenceMaterial == null) { Debug.LogError("Could not find reference material at " + referenceMaterialPath + " please reimport Unity package."); return; } // Load texture from texture folder string pathToTexture = QCARUtilities.GlobalVars.TARGET_TEXTURES_PATH; string textureFile = pathToTexture + mt.DataSetName + "/" + go.name + "_scaled"; if (File.Exists(textureFile + ".png")) textureFile += ".png"; else if (File.Exists(textureFile + ".jpg")) textureFile += ".jpg"; Texture2D targetTexture = (Texture2D)AssetDatabase.LoadAssetAtPath(textureFile, typeof(Texture2D)); if (targetTexture == null) { // If the texture is null we simply assign a default material go.renderer.sharedMaterial = referenceMaterial; return; } // We create a new material based on the reference material Material materialForTargetTexture = new Material(referenceMaterial); materialForTargetTexture.mainTexture = targetTexture; materialForTargetTexture.name = targetTexture.name + "Material"; materialForTargetTexture.mainTextureScale = new Vector2(-1, -1); go.renderer.sharedMaterial = materialForTargetTexture; // Cleanup assets that have been created temporarily. EditorUtility.UnloadUnusedAssets(); }
// Lets the user choose a Multi Target from a drop down list. Multi Target // must be defined in the "config.xml" file. public override void OnInspectorGUI() { EditorGUIUtility.LookLikeInspector(); DrawDefaultInspector(); MultiTargetBehaviour mtb = (MultiTargetBehaviour)target; IEditorMultiTargetBehaviour editorMtb = mtb; if (QCARUtilities.GetPrefabType(mtb) == PrefabType.Prefab) { GUILayout.Label("You can't choose a target for a prefab."); } else if (ConfigDataManager.Instance.NumConfigDataObjects > 1) { // Draw list for choosing a data set. string[] dataSetList = new string[ConfigDataManager.Instance.NumConfigDataObjects]; ConfigDataManager.Instance.GetConfigDataNames(dataSetList); int currentDataSetIndex = QCARUtilities.GetIndexFromString(editorMtb.DataSetName, dataSetList); // If name is not in array we automatically choose default name; if (currentDataSetIndex < 0) { currentDataSetIndex = 0; } int newDataSetIndex = EditorGUILayout.Popup("Data Set", currentDataSetIndex, dataSetList); string chosenDataSet = dataSetList[newDataSetIndex]; ConfigData dataSetData = ConfigDataManager.Instance.GetConfigData(chosenDataSet); // Draw list for choosing a Trackable. string[] namesList = new string[dataSetData.NumMultiTargets]; dataSetData.CopyMultiTargetNames(namesList, 0); int currentTrackableIndex = QCARUtilities.GetIndexFromString(editorMtb.TrackableName, namesList); // If name is not in array we automatically choose default name; if (currentTrackableIndex < 0) { currentTrackableIndex = 0; } int newTrackableIndex = EditorGUILayout.Popup("Multi Target", currentTrackableIndex, namesList); if (namesList.Length > 0) { if (newDataSetIndex != currentDataSetIndex || newTrackableIndex != currentTrackableIndex) { editorMtb.SetDataSetPath("QCAR/" + dataSetList[newDataSetIndex] + ".xml"); editorMtb.SetNameForTrackable(namesList[newTrackableIndex]); } } } else { if (GUILayout.Button("No targets defined. Press here for target " + "creation!")) { SceneManager.Instance.GoToARPage(); } } if (GUI.changed) { EditorUtility.SetDirty(mtb); // If name has changed we apply the correct values from the config // file. TrackableAccessor accessor = AccessorFactory.Create(mtb); if (accessor != null) { accessor.ApplyDataSetProperties(); } SceneManager.Instance.SceneUpdated(); } }
/// <summary> /// Enabled/disabled Extended Tracking mode. /// </summary> /// <param name="ON"></param> public virtual void SwitchExtendedTracking(bool extTrackingEnabled) { StateManager stateManager = TrackerManager.Instance.GetStateManager(); // We iterate over all TrackableBehaviours to start or stop extended tracking for the targets they represent. bool success = true; foreach (var tb in stateManager.GetTrackableBehaviours()) { if (tb is ImageTargetBehaviour) { ImageTargetBehaviour itb = tb as ImageTargetBehaviour; if (extTrackingEnabled) { if (!itb.ImageTarget.StartExtendedTracking()) { success = false; Debug.LogError("Failed to start Extended Tracking on Target " + itb.TrackableName); } } else { itb.ImageTarget.StopExtendedTracking(); } } else if (tb is MultiTargetBehaviour) { MultiTargetBehaviour mtb = tb as MultiTargetBehaviour; if (extTrackingEnabled) { if (!mtb.MultiTarget.StartExtendedTracking()) { success = false; Debug.LogError("Failed to start Extended Tracking on Target " + mtb.TrackableName); } } else { mtb.MultiTarget.StopExtendedTracking(); } } else if (tb is CylinderTargetBehaviour) { CylinderTargetBehaviour ctb = tb as CylinderTargetBehaviour; if (extTrackingEnabled) { if (!ctb.CylinderTarget.StartExtendedTracking()) { success = false; Debug.LogError("Failed to start Extended Tracking on Target " + ctb.TrackableName); } } else { ctb.CylinderTarget.StopExtendedTracking(); } } else if (tb is ObjectTargetBehaviour) { ObjectTargetBehaviour otb = tb as ObjectTargetBehaviour; if (extTrackingEnabled) { if (!otb.ObjectTarget.StartExtendedTracking()) { success = false; Debug.LogError("Failed to start Extended Tracking on Target " + otb.TrackableName); } } else { otb.ObjectTarget.StopExtendedTracking(); } } } mExtTrackingEnabled = success && extTrackingEnabled; }
// Updates MultiTarget parts with the values stored in the "prtConfigs" // array. Deletes all parts and recreates them. public static void UpdateParts(MultiTargetBehaviour mt, ConfigData.MultiTargetPart[] prtConfigs) { Transform childTargets = mt.transform.Find("ChildTargets"); if (childTargets != null) { Object.DestroyImmediate(childTargets.gameObject); } GameObject newObject = new GameObject(); newObject.name = "ChildTargets"; newObject.transform.parent = mt.transform; newObject.hideFlags = HideFlags.NotEditable; newObject.transform.localPosition = Vector3.zero; newObject.transform.localRotation = Quaternion.identity; newObject.transform.localScale = Vector3.one; childTargets = newObject.transform; Material maskMaterial = (Material)AssetDatabase.LoadAssetAtPath( QCARUtilities.GlobalVars.PATH_TO_MASK_MATERIAL, typeof(Material)); int numParts = prtConfigs.Length; string[] imageTargetNames = SceneManager.Instance.GetImageTargetNames(); for (int i = 0; i < numParts; ++i) { bool imageTargetFound = false; // Start from 1 to ignore default targets. for (int j = 0; j < imageTargetNames.Length; ++j) { if (string.Compare(imageTargetNames[j], prtConfigs[i].name, true) == 0) { imageTargetFound = true; break; } } if (!imageTargetFound) { Debug.LogError("No image target named " + prtConfigs[i].name); return; } ConfigData.ImageTarget itConfig = SceneManager.Instance.GetImageTarget(prtConfigs[i].name); Vector2 size = itConfig.size; Vector3 scale = new Vector3(size.x * 0.1f, 1, size.y * 0.1f); GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane); plane.name = prtConfigs[i].name; plane.transform.parent = childTargets.transform; plane.transform.localPosition = prtConfigs[i].translation; plane.transform.localRotation = prtConfigs[i].rotation; plane.transform.localScale = scale; UpdateMaterial(plane); plane.hideFlags = HideFlags.NotEditable; MaskOutBehaviour script = (MaskOutBehaviour)plane.AddComponent(typeof(MaskOutBehaviour)); script.maskMaterial = maskMaterial; } }
private static void CheckMesh(MultiTargetBehaviour mtb) { // when copy-pasting targets between scenes, the mesh and materials of // the game objects get lost. This checks for them and re-creates them if they are found missing. bool updateGeometry = false; Transform childTargets = mtb.transform.Find("ChildTargets"); if (childTargets == null) { updateGeometry = true; } else { for (int i = 0; i < childTargets.childCount; i++) { Transform childTarget = childTargets.GetChild(i); MeshFilter meshFilter = childTarget.GetComponent<MeshFilter>(); MeshRenderer meshRenderer = childTarget.GetComponent<MeshRenderer>(); if (meshFilter == null || meshFilter.sharedMesh == null || meshRenderer == null || meshRenderer.sharedMaterials.Length == 0 || meshRenderer.sharedMaterials[0] == null) updateGeometry = true; } } if (updateGeometry) { TrackableAccessor accessor = AccessorFactory.Create(mtb); if (accessor != null) accessor.ApplyDataSetProperties(); } }
// The one MultiTargetBehaviour instance this accessor belongs to is set in // the constructor. public MultiTargetAccessor(MultiTargetBehaviour target) { mTarget = target; }
// The one MultiTargetBehaviour instance this accessor belongs to is set in // the constructor. public MultiTargetAccessor(MultiTargetBehaviour target) { mTarget = target; }
// Updates MultiTarget parts with the values stored in the "prtConfigs" // array. Deletes all parts and recreates them. public static void UpdateParts(MultiTargetBehaviour mt, ConfigData.MultiTargetPart[] prtConfigs) { Transform childTargets = mt.transform.Find("ChildTargets"); if (childTargets != null) { Object.DestroyImmediate(childTargets.gameObject); } GameObject newObject = new GameObject(); newObject.name = "ChildTargets"; newObject.transform.parent = mt.transform; newObject.hideFlags = HideFlags.NotEditable; newObject.transform.localPosition = Vector3.zero; newObject.transform.localRotation = Quaternion.identity; newObject.transform.localScale = Vector3.one; childTargets = newObject.transform; Material maskMaterial = (Material)AssetDatabase.LoadAssetAtPath( QCARUtilities.GlobalVars.MASK_MATERIAL_PATH, typeof(Material)); ConfigData dataSetData = ConfigDataManager.Instance.GetConfigData(mt.DataSetName); if (dataSetData == null) { Debug.LogError("Could not update Multi Target parts. A data set with the given name does not exist."); } int numParts = prtConfigs.Length; for (int i = 0; i < numParts; ++i) { if (!dataSetData.ImageTargetExists(prtConfigs[i].name) /* && * prtConfigs[i].name != QCARUtilities.GlobalVars.DEFAULT_TRACKABLE_NAME*/) { Debug.LogError("No Image Target named " + prtConfigs[i].name); return; } ConfigData.ImageTarget itConfig; dataSetData.GetImageTarget(prtConfigs[i].name, out itConfig); Vector2 size = itConfig.size; Vector3 scale = new Vector3(size.x * 0.1f, 1, size.y * 0.1f); GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane); plane.name = prtConfigs[i].name; plane.transform.parent = childTargets.transform; plane.transform.localPosition = prtConfigs[i].translation; plane.transform.localRotation = prtConfigs[i].rotation; plane.transform.localScale = scale; UpdateMaterial(mt, plane); plane.hideFlags = HideFlags.NotEditable; MaskOutBehaviour script = (MaskOutBehaviour)plane.AddComponent(typeof(MaskOutBehaviour)); script.maskMaterial = maskMaterial; } }