private CylinderTargetBehaviour CreateCylinderTargetBehaviour(CylinderTarget cylinderTarget) { GameObject cylinderTargetObject = new GameObject(); CylinderTargetBehaviour newCTB = cylinderTargetObject.AddComponent <CylinderTargetBehaviour>(); IEditorCylinderTargetBehaviour newEditorCTB = newCTB; Debug.Log("Creating Cylinder Target with values: " + "\n ID: " + cylinderTarget.ID + "\n Name: " + cylinderTarget.Name + "\n Path: " + newEditorCTB.DataSetPath + "\n Side Length: " + cylinderTarget.GetSideLength() + "\n Top Diameter: " + cylinderTarget.GetTopDiameter() + "\n Bottom Diam.: " + cylinderTarget.GetBottomDiameter()); // Set Cylinder Target attributes. newEditorCTB.SetNameForTrackable(cylinderTarget.Name); newEditorCTB.SetDataSetPath(newEditorCTB.DataSetPath); var sidelength = cylinderTarget.GetSideLength(); newEditorCTB.transform.localScale = new Vector3(sidelength, sidelength, sidelength); newEditorCTB.CorrectScale(); newEditorCTB.SetAspectRatio(cylinderTarget.GetTopDiameter() / sidelength, cylinderTarget.GetBottomDiameter() / sidelength); newEditorCTB.InitializeCylinderTarget(cylinderTarget); return(newCTB); }
private static void UpdateGeometry(IEditorCylinderTargetBehaviour ct, float sideLength, float topDiameter, float bottomDiameter, bool hasTopGeometry, bool hasBottomGeometry) { GameObject gameObject = ct.gameObject; MeshRenderer component = gameObject.GetComponent <MeshRenderer>(); if (component == null) { component = gameObject.AddComponent <MeshRenderer>(); } component.hideFlags = HideFlags.NotEditable; MeshFilter filter = gameObject.GetComponent <MeshFilter>(); if (filter == null) { filter = gameObject.AddComponent <MeshFilter>(); } filter.hideFlags = HideFlags.NotEditable; Mesh mesh = CylinderMeshFactory.CreateCylinderMesh(sideLength, topDiameter, bottomDiameter, 0x20, hasTopGeometry, hasBottomGeometry, true); filter.sharedMesh = mesh; MaskOutAbstractBehaviour behaviour = gameObject.GetComponent <MaskOutAbstractBehaviour>(); if (behaviour == null) { Material material = (Material)AssetDatabase.LoadAssetAtPath("Assets/Qualcomm Augmented Reality/Materials/DepthMask.mat", typeof(Material)); behaviour = BehaviourComponentFactory.Instance.AddMaskOutBehaviour(gameObject); behaviour.maskMaterial = material; } behaviour.hideFlags = HideFlags.NotEditable; EditorUtility.UnloadUnusedAssets(); }
// This method updates the respective Trackable appearance (e.g. // aspect ratio and texture) with data set data. public override void ApplyDataSetAppearance() { // Prefabs should not be editable if (QCARUtilities.GetPrefabType(mTarget) == PrefabType.Prefab) { return; } IEditorCylinderTargetBehaviour ctb = (CylinderTargetBehaviour)mTarget; ConfigData.CylinderTargetData ctConfig; if (TrackableInDataSet(ctb.TrackableName, ctb.DataSetName)) { ConfigData dataSetData = ConfigDataManager.Instance.GetConfigData(ctb.DataSetName); dataSetData.GetCylinderTarget(ctb.TrackableName, out ctConfig); } 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.GetCylinderTarget(QCARUtilities.GlobalVars.DEFAULT_TRACKABLE_NAME, out ctConfig); ctb.SetDataSetPath(QCARUtilities.GlobalVars.DEFAULT_DATA_SET_NAME); ctb.SetNameForTrackable(QCARUtilities.GlobalVars.DEFAULT_TRACKABLE_NAME); } //update geometry based on properties from dataset CylinderTargetEditor.UpdateAspectRatio(ctb, ctConfig); }
public void OnEnable() { CylinderTargetAbstractBehaviour target = (CylinderTargetAbstractBehaviour)base.target; if (QCARUtilities.GetPrefabType(target) != PrefabType.Prefab) { if (!SceneManager.Instance.SceneInitialized) { SceneManager.Instance.InitScene(); } IEditorCylinderTargetBehaviour ct = target; if (!ct.InitializedInEditor && !EditorApplication.isPlaying) { ConfigData.CylinderTargetData data; ConfigDataManager.Instance.GetConfigData("--- EMPTY ---").GetCylinderTarget("--- EMPTY ---", out data); ct.SetDataSetPath("--- EMPTY ---"); ct.SetNameForTrackable("--- EMPTY ---"); UpdateAspectRatio(ct, data); UpdateScale(ct, data.sideLength); ct.SetInitializedInEditor(true); } else if (!EditorApplication.isPlaying) { CheckMesh(ct); } ct.SetPreviousScale(target.transform.localScale); } }
public static void UpdateAspectRatio(IEditorCylinderTargetBehaviour ct, ConfigData.CylinderTargetData ctConfig) { float topRatio = ctConfig.topDiameter / ctConfig.sideLength; float bottomRatio = ctConfig.bottomDiameter / ctConfig.sideLength; ct.SetAspectRatio(topRatio, bottomRatio); UpdateGeometry(ct, 1f, topRatio, bottomRatio, ctConfig.hasTopGeometry, ctConfig.hasBottomGeometry); UpdateMaterials(ct, ctConfig.hasBottomGeometry, ctConfig.hasTopGeometry, true); }
/// <summary> /// Define the ratio between sidelength, top diameter, and bottom diameter. /// Geometry and materials are updated according to the new parameters. /// </summary> public static void UpdateAspectRatio(IEditorCylinderTargetBehaviour ct, ConfigData.CylinderTargetData ctConfig) { var topRatio = ctConfig.topDiameter / ctConfig.sideLength; var bottomRatio = ctConfig.bottomDiameter / ctConfig.sideLength; ct.SetAspectRatio(topRatio, bottomRatio); UpdateGeometry(ct, 1.0f, topRatio, bottomRatio, ctConfig.hasTopGeometry, ctConfig.hasBottomGeometry); //assign materials UpdateMaterials(ct, ctConfig.hasBottomGeometry, ctConfig.hasTopGeometry, INSIDE_MATERIAL); }
/// <summary> /// Define the ratio between sidelength, top diameter, and bottom diameter. /// Geometry and materials are updated according to the new parameters. /// </summary> public static void UpdateAspectRatio(IEditorCylinderTargetBehaviour ct, ConfigData.CylinderTargetData ctConfig) { var topRatio = ctConfig.topDiameter/ctConfig.sideLength; var bottomRatio = ctConfig.bottomDiameter/ctConfig.sideLength; ct.SetAspectRatio(topRatio, bottomRatio); UpdateGeometry(ct, 1.0f, topRatio, bottomRatio, ctConfig.hasTopGeometry, ctConfig.hasBottomGeometry); //assign materials UpdateMaterials(ct, ctConfig.hasBottomGeometry, ctConfig.hasTopGeometry, INSIDE_MATERIAL); }
private static void CheckMesh(IEditorCylinderTargetBehaviour editorCtb) { GameObject gameObject = editorCtb.gameObject; MeshFilter component = gameObject.GetComponent <MeshFilter>(); MeshRenderer renderer = gameObject.GetComponent <MeshRenderer>(); if (((component == null) || (component.sharedMesh == null)) || (((renderer == null) || (renderer.sharedMaterials.Length == 0)) || (renderer.sharedMaterials[0] == null))) { ConfigData.CylinderTargetData data2; ConfigDataManager.Instance.GetConfigData(editorCtb.DataSetName).GetCylinderTarget(editorCtb.TrackableName, out data2); UpdateAspectRatio(editorCtb, data2); } }
/// <summary> /// Updates CylinderTarget. Deletes all parts and recreates them. /// Creates a mesh with vertices, normals, and texture coordinates. /// Top and bottom geometry are represented as separate submeshes, /// i.e. resulting mesh contains 1, 2, or 3 submeshes. /// </summary> private static void UpdateGeometry(IEditorCylinderTargetBehaviour ct, float sideLength, float topDiameter, float bottomDiameter, bool hasTopGeometry, bool hasBottomGeometry) { GameObject gameObject = ct.gameObject; MeshRenderer meshRenderer = gameObject.GetComponent <MeshRenderer>(); if (!meshRenderer) { meshRenderer = gameObject.AddComponent <MeshRenderer>(); } //disable the editing functionality of meshRenderer in inspector UI meshRenderer.hideFlags = HideFlags.NotEditable; MeshFilter meshFilter = gameObject.GetComponent <MeshFilter>(); if (!meshFilter) { meshFilter = gameObject.AddComponent <MeshFilter>(); } //disable the editing functionality of meshFilter in inspector UI meshFilter.hideFlags = HideFlags.NotEditable; Mesh cylinderMesh = CylinderMeshFactory.CreateCylinderMesh(sideLength, topDiameter, bottomDiameter, NUM_PERIMETER_VERTICES, hasTopGeometry, hasBottomGeometry, INSIDE_MATERIAL); meshFilter.sharedMesh = cylinderMesh; //create and attach mask-out material (if not existing yet) MaskOutBehaviour script = gameObject.GetComponent <MaskOutBehaviour>(); if (!script) { Material maskMaterial = (Material)AssetDatabase.LoadAssetAtPath( QCARUtilities.GlobalVars.MASK_MATERIAL_PATH, typeof(Material)); script = gameObject.AddComponent <MaskOutBehaviour>(); script.maskMaterial = maskMaterial; } //disable the editing functionality of the script in inspector UI script.hideFlags = HideFlags.NotEditable; // Cleanup assets that have been created temporarily. EditorUtility.UnloadUnusedAssets(); }
public static void UpdateScale(IEditorCylinderTargetBehaviour ct, float scale) { float num = ct.transform.localScale.x / scale; ct.transform.localScale = new Vector3(scale, scale, scale); if (ct.PreserveChildSize) { foreach (Transform transform in ct.transform) { transform.localPosition = new Vector3(transform.localPosition.x * num, transform.localPosition.y * num, transform.localPosition.z * num); transform.localScale = new Vector3(transform.localScale.x * num, transform.localScale.y * num, transform.localScale.z * num); } } }
private CylinderTargetAbstractBehaviour CreateCylinderTargetBehaviour(CylinderTarget cylinderTarget) { GameObject gameObject = new GameObject(); CylinderTargetAbstractBehaviour behaviour = BehaviourComponentFactory.Instance.AddCylinderTargetBehaviour(gameObject); IEditorCylinderTargetBehaviour behaviour2 = behaviour; Debug.Log(string.Concat(new object[] { "Creating Cylinder Target with values: \n ID: ", cylinderTarget.ID, "\n Name: ", cylinderTarget.Name, "\n Path: ", behaviour2.DataSetPath, "\n Side Length: ", cylinderTarget.GetSideLength(), "\n Top Diameter: ", cylinderTarget.GetTopDiameter(), "\n Bottom Diam.: ", cylinderTarget.GetBottomDiameter() })); behaviour2.SetNameForTrackable(cylinderTarget.Name); behaviour2.SetDataSetPath(behaviour2.DataSetPath); float sideLength = cylinderTarget.GetSideLength(); behaviour2.transform.localScale = new Vector3(sideLength, sideLength, sideLength); behaviour2.CorrectScale(); behaviour2.SetAspectRatio(cylinderTarget.GetTopDiameter() / sideLength, cylinderTarget.GetBottomDiameter() / sideLength); behaviour2.InitializeCylinderTarget(cylinderTarget); return(behaviour); }
public override void ApplyDataSetProperties() { if (QCARUtilities.GetPrefabType(base.mTarget) != PrefabType.Prefab) { ConfigData.CylinderTargetData data; IEditorCylinderTargetBehaviour mTarget = (CylinderTargetAbstractBehaviour)base.mTarget; if (this.TrackableInDataSet(mTarget.TrackableName, mTarget.DataSetName)) { ConfigDataManager.Instance.GetConfigData(mTarget.DataSetName).GetCylinderTarget(mTarget.TrackableName, out data); } else { ConfigDataManager.Instance.GetConfigData("--- EMPTY ---").GetCylinderTarget("--- EMPTY ---", out data); mTarget.SetDataSetPath("--- EMPTY ---"); mTarget.SetNameForTrackable("--- EMPTY ---"); } CylinderTargetEditor.UpdateScale(mTarget, data.sideLength); } }
// Initializes the Cylinder Target when it is drag-dropped into the scene. public void OnEnable() { var ctb = (CylinderTargetBehaviour)target; // We don't want to initialize if this is a prefab. if (QCARUtilities.GetPrefabType(ctb) == PrefabType.Prefab) { return; } // Make sure the scene and config.xml file are synchronized. if (!SceneManager.Instance.SceneInitialized) { SceneManager.Instance.InitScene(); } IEditorCylinderTargetBehaviour editorCtb = ctb; // Only setup target if it has not been set up previously. if (!editorCtb.InitializedInEditor && !EditorApplication.isPlaying) { ConfigData.CylinderTargetData ctConfig; ConfigData dataSetData = ConfigDataManager.Instance.GetConfigData(QCARUtilities.GlobalVars.DEFAULT_DATA_SET_NAME); dataSetData.GetCylinderTarget(QCARUtilities.GlobalVars.DEFAULT_TRACKABLE_NAME, out ctConfig); editorCtb.SetDataSetPath(QCARUtilities.GlobalVars.DEFAULT_DATA_SET_NAME); editorCtb.SetNameForTrackable(QCARUtilities.GlobalVars.DEFAULT_TRACKABLE_NAME); UpdateAspectRatio(editorCtb, ctConfig); UpdateScale(editorCtb, ctConfig.sideLength); editorCtb.SetInitializedInEditor(true); } else if (!EditorApplication.isPlaying) { CheckMesh(editorCtb); } // Cache the current scale of the target: editorCtb.SetPreviousScale(ctb.transform.localScale); }
private static void CheckMesh(IEditorCylinderTargetBehaviour editorCtb) { // 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. GameObject itObject = editorCtb.gameObject; MeshFilter meshFilter = itObject.GetComponent <MeshFilter>(); MeshRenderer meshRenderer = itObject.GetComponent <MeshRenderer>(); if (meshFilter == null || meshFilter.sharedMesh == null || meshRenderer == null || meshRenderer.sharedMaterials.Length == 0 || meshRenderer.sharedMaterials[0] == null) { ConfigData dataSetData = ConfigDataManager.Instance.GetConfigData(editorCtb.DataSetName); ConfigData.CylinderTargetData ctConfig; dataSetData.GetCylinderTarget(editorCtb.TrackableName, out ctConfig); //geometry has to be updated when a new target has been selected UpdateAspectRatio(editorCtb, ctConfig); } }
/// <summary> /// Define a new scale for the cylinder target, which corresponds to the sidelength. /// If PreserveChildSize is true, all child objects will be scaled inversely in order to keep their original size. /// </summary> /// <param name="ct">Local scale of this cylinder target will be updated</param> /// <param name="scale">Uniform scale of the target, corresponds to the sidelength</param> public static void UpdateScale(IEditorCylinderTargetBehaviour ct, float scale) { var childScaleFactor = ct.transform.localScale.x / scale; ct.transform.localScale = new Vector3(scale, scale, scale); // Check if 3D content should keep its size or if it should be scaled // with the target. if (ct.PreserveChildSize) { foreach (Transform child in ct.transform) { child.localPosition = new Vector3(child.localPosition.x * childScaleFactor, child.localPosition.y * childScaleFactor, child.localPosition.z * childScaleFactor); child.localScale = new Vector3(child.localScale.x * childScaleFactor, child.localScale.y * childScaleFactor, child.localScale.z * childScaleFactor); } } }
/// <summary> /// Define a new scale for the cylinder target, which corresponds to the sidelength. /// If PreserveChildSize is true, all child objects will be scaled inversely in order to keep their original size. /// </summary> /// <param name="ct">Local scale of this cylinder target will be updated</param> /// <param name="scale">Uniform scale of the target, corresponds to the sidelength</param> public static void UpdateScale(IEditorCylinderTargetBehaviour ct, float scale) { var childScaleFactor = ct.transform.localScale.x / scale; ct.transform.localScale = new Vector3(scale, scale, scale); // Check if 3D content should keep its size or if it should be scaled // with the target. if (ct.PreserveChildSize) { foreach (Transform child in ct.transform) { child.localPosition = new Vector3(child.localPosition.x*childScaleFactor, child.localPosition.y*childScaleFactor, child.localPosition.z*childScaleFactor); child.localScale = new Vector3(child.localScale.x*childScaleFactor, child.localScale.y*childScaleFactor, child.localScale.z*childScaleFactor); } } }
private static void CheckMesh(IEditorCylinderTargetBehaviour editorCtb) { // 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. GameObject itObject = editorCtb.gameObject; MeshFilter meshFilter = itObject.GetComponent<MeshFilter>(); MeshRenderer meshRenderer = itObject.GetComponent<MeshRenderer>(); if (meshFilter == null || meshFilter.sharedMesh == null || meshRenderer == null || meshRenderer.sharedMaterials.Length == 0 || meshRenderer.sharedMaterials[0] == null) { ConfigData dataSetData = ConfigDataManager.Instance.GetConfigData(editorCtb.DataSetName); ConfigData.CylinderTargetData ctConfig; dataSetData.GetCylinderTarget(editorCtb.TrackableName, out ctConfig); //geometry has to be updated when a new target has been selected UpdateAspectRatio(editorCtb, ctConfig); } }
// Lets the user choose a Cylinder Target from a drop down list. Cylinder Target // must be defined in the "config.xml" file. public override void OnInspectorGUI() { EditorGUIUtility.LookLikeInspector(); DrawDefaultInspector(); CylinderTargetBehaviour ctb = (CylinderTargetBehaviour)target; IEditorCylinderTargetBehaviour editorCtb = ctb; if (QCARUtilities.GetPrefabType(ctb) == 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(editorCtb.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.NumCylinderTargets]; dataSetData.CopyCylinderTargetNames(namesList, 0); int currentTrackableIndex = QCARUtilities.GetIndexFromString(editorCtb.TrackableName, namesList); // If name is not in array we automatically choose default name; if (currentTrackableIndex < 0) { currentTrackableIndex = 0; } int newTrackableIndex = EditorGUILayout.Popup("Cylinder Target", currentTrackableIndex, namesList); if (namesList.Length > 0) { if (newDataSetIndex != currentDataSetIndex || newTrackableIndex != currentTrackableIndex) { editorCtb.SetDataSetPath("QCAR/" + dataSetList[newDataSetIndex] + ".xml"); editorCtb.SetNameForTrackable(namesList[newTrackableIndex]); ConfigData.CylinderTargetData ctConfig; dataSetData.GetCylinderTarget(ctb.TrackableName, out ctConfig); //geometry has to be updated when a new target has been selected UpdateAspectRatio(editorCtb, ctConfig); UpdateScale(editorCtb, ctConfig.sideLength); } } //Expose editors for setting sideLength and diameterss //these values are not really stored, but instead the scale-factor is updated var sideLength = EditorGUILayout.FloatField("Side Length", ctb.SideLength); if (sideLength != ctb.SideLength) { ctb.SetSideLength(sideLength); } float topDiameter = EditorGUILayout.FloatField("Top Diameter ", ctb.TopDiameter); if (topDiameter != ctb.TopDiameter) { ctb.SetTopDiameter(topDiameter); } float bottomDiameter = EditorGUILayout.FloatField("Bottom Diameter", ctb.BottomDiameter); if (bottomDiameter != ctb.BottomDiameter) { ctb.SetBottomDiameter(bottomDiameter); } // Draw check box to de-/activate "preserve child size" mode. editorCtb.SetPreserveChildSize(EditorGUILayout.Toggle("Preserve child size", editorCtb.PreserveChildSize)); } else { if (GUILayout.Button("No targets defined. Press here for target " + "creation!")) { SceneManager.Instance.GoToTargetManagerPage(); } } if (GUI.changed) { EditorUtility.SetDirty(ctb); SceneManager.Instance.SceneUpdated(); } }
internal void AssociateTrackableBehavioursForDataSet(DataSet dataSet) { DataSetTrackableBehaviour[] behaviourArray = (DataSetTrackableBehaviour[])UnityEngine.Object.FindObjectsOfType(typeof(DataSetTrackableBehaviour)); foreach (DataSetTrackableBehaviour behaviour in behaviourArray) { if (!this.mBehavioursMarkedForDeletion.Contains(behaviour)) { IEditorDataSetTrackableBehaviour behaviour2 = behaviour; if (behaviour2.TrackableName == null) { Debug.LogError("Found Trackable without name."); } else if (behaviour2.DataSetPath.Equals(dataSet.Path)) { bool flag = false; foreach (Trackable trackable in dataSet.GetTrackables()) { if (trackable.Name.Equals(behaviour2.TrackableName)) { if (this.mTrackableBehaviours.ContainsKey(trackable.ID)) { if (!this.mAutomaticallyCreatedBehaviours.Contains(trackable.ID) && !this.mBehavioursMarkedForDeletion.Contains(this.mTrackableBehaviours[trackable.ID])) { flag = true; continue; } UnityEngine.Object.Destroy(this.mTrackableBehaviours[trackable.ID].gameObject); this.mTrackableBehaviours.Remove(trackable.ID); this.mAutomaticallyCreatedBehaviours.Remove(trackable.ID); } if ((behaviour is ImageTargetAbstractBehaviour) && (trackable is ImageTarget)) { IEditorImageTargetBehaviour behaviour3 = (ImageTargetAbstractBehaviour)behaviour; flag = true; behaviour3.InitializeImageTarget((ImageTarget)trackable); this.mTrackableBehaviours[trackable.ID] = behaviour; Debug.Log(string.Concat(new object[] { "Found Trackable named ", behaviour.Trackable.Name, " with id ", behaviour.Trackable.ID })); } else if ((behaviour is MultiTargetAbstractBehaviour) && (trackable is MultiTarget)) { flag = true; IEditorMultiTargetBehaviour behaviour4 = (MultiTargetAbstractBehaviour)behaviour; behaviour4.InitializeMultiTarget((MultiTarget)trackable); this.mTrackableBehaviours[trackable.ID] = behaviour; Debug.Log(string.Concat(new object[] { "Found Trackable named ", behaviour.Trackable.Name, " with id ", behaviour.Trackable.ID })); } else if ((behaviour is CylinderTargetAbstractBehaviour) && (trackable is CylinderTarget)) { flag = true; IEditorCylinderTargetBehaviour behaviour5 = (CylinderTargetAbstractBehaviour)behaviour; behaviour5.InitializeCylinderTarget((CylinderTarget)trackable); this.mTrackableBehaviours[trackable.ID] = behaviour; Debug.Log(string.Concat(new object[] { "Found Trackable named ", behaviour.Trackable.Name, " with id ", behaviour.Trackable.ID })); } else if ((behaviour is IEditorRigidBodyTargetBehaviour) && (trackable is InternalRigidBodyTarget)) { flag = true; ((IEditorRigidBodyTargetBehaviour)behaviour).InitializeRigidBodyTarget((InternalRigidBodyTarget)trackable); this.mTrackableBehaviours[trackable.ID] = behaviour; Debug.Log(string.Concat(new object[] { "Found Trackable named ", behaviour.Trackable.Name, " with id ", behaviour.Trackable.ID })); } } } if (!flag) { Debug.LogError("Could not associate DataSetTrackableBehaviour '" + behaviour2.TrackableName + "' - no matching Trackable found in DataSet!"); } } } } VirtualButtonAbstractBehaviour[] vbBehaviours = (VirtualButtonAbstractBehaviour[])UnityEngine.Object.FindObjectsOfType(typeof(VirtualButtonAbstractBehaviour)); this.AssociateVirtualButtonBehaviours(vbBehaviours, dataSet); this.CreateMissingDataSetTrackableBehaviours(dataSet); }
private static void UpdateMaterials(IEditorCylinderTargetBehaviour ct, bool hasBottomGeometry, bool hasTopGeometry, bool insideMaterial) { string assetPath = "Assets/Qualcomm Augmented Reality/Materials/DefaultTarget.mat"; Material source = (Material)AssetDatabase.LoadAssetAtPath(assetPath, typeof(Material)); if (source == null) { Debug.LogError("Could not find reference material at " + assetPath + " please reimport Unity package."); } else { string str2 = "Assets/Editor/QCAR/CylinderTargetTextures/" + ct.DataSetName + "/" + ct.TrackableName; List <string> list = new List <string> { str2 + ".Body_scaled" }; if (hasBottomGeometry) { list.Add(str2 + ".Bottom_scaled"); } if (hasTopGeometry) { list.Add(str2 + ".Top_scaled"); } int count = list.Count; Material[] materialArray = new Material[insideMaterial ? (count * 2) : count]; for (int i = 0; i < list.Count; i++) { string str3 = list[i]; if (File.Exists(str3 + ".png")) { str3 = str3 + ".png"; } else if (File.Exists(str3 + ".jpg")) { str3 = str3 + ".jpg"; } else if (File.Exists(str3 + ".jpeg")) { str3 = str3 + ".jpeg"; } else { materialArray[i] = source; if (insideMaterial) { materialArray[i + count] = source; } continue; } Texture2D textured = (Texture2D)AssetDatabase.LoadAssetAtPath(str3, typeof(Texture2D)); Material material2 = new Material(source); if (textured != null) { material2.mainTexture = textured; material2.name = textured.name + "Material"; material2.mainTextureScale = new Vector2(-1f, -1f); } materialArray[i] = material2; if (insideMaterial) { Material material3 = new Material(material2) { shader = Shader.Find("Custom/BrightTexture") }; materialArray[i + count] = material3; } } ct.renderer.sharedMaterials = materialArray; Resources.UnloadUnusedAssets(); } }
/// <summary> /// Create and assign materials for cylinder targets. The newly created materials /// are based on the default material. /// </summary> private static void UpdateMaterials(IEditorCylinderTargetBehaviour ct, bool hasBottomGeometry, bool hasTopGeometry, bool insideMaterial) { // Load reference material. var referenceMaterialPath = QCARUtilities.GlobalVars.REFERENCE_MATERIAL_PATH; var 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. Textures have per convention the // same name as Image Targets + "_scaled" as postfix. var pathToTextures = QCARUtilities.GlobalVars.CYLINDER_TARGET_TEXTURES_PATH + ct.DataSetName + "/" + ct.TrackableName; var textureFiles = new List <string> { pathToTextures + ".Body_scaled" }; if (hasBottomGeometry) { textureFiles.Add(pathToTextures + ".Bottom_scaled"); } if (hasTopGeometry) { textureFiles.Add(pathToTextures + ".Top_scaled"); } var numSubMeshes = textureFiles.Count; var materials = new Material[insideMaterial ? numSubMeshes * 2 : numSubMeshes]; for (var i = 0; i < textureFiles.Count; i++) { var textureFile = textureFiles[i]; if (File.Exists(textureFile + ".png")) { textureFile += ".png"; } else if (File.Exists(textureFile + ".jpg")) { textureFile += ".jpg"; } else if (File.Exists(textureFile + ".jpeg")) { textureFile += ".jpeg"; } else { materials[i] = referenceMaterial; if (insideMaterial) { materials[i + numSubMeshes] = referenceMaterial; } continue; } var targetTexture = (Texture2D)AssetDatabase.LoadAssetAtPath(textureFile, typeof(Texture2D)); // We create a new material that is based on the reference material but // also contains a texture. var materialForTargetTexture = new Material(referenceMaterial); if (targetTexture != null) { materialForTargetTexture.mainTexture = targetTexture; materialForTargetTexture.name = targetTexture.name + "Material"; materialForTargetTexture.mainTextureScale = new Vector2(-1, -1); } materials[i] = materialForTargetTexture; if (insideMaterial) { var brightMaterial = new Material(materialForTargetTexture); brightMaterial.shader = Shader.Find("Custom/BrightTexture"); materials[i + numSubMeshes] = brightMaterial; } } ct.renderer.sharedMaterials = materials; Resources.UnloadUnusedAssets(); }
public override void OnInspectorGUI() { EditorGUIUtility.LookLikeInspector(); base.DrawDefaultInspector(); CylinderTargetAbstractBehaviour target = (CylinderTargetAbstractBehaviour)base.target; IEditorCylinderTargetBehaviour ct = target; if (QCARUtilities.GetPrefabType(target) == PrefabType.Prefab) { GUILayout.Label("You can't choose a target for a prefab.", new GUILayoutOption[0]); } else if (ConfigDataManager.Instance.NumConfigDataObjects > 1) { string[] configDataNames = new string[ConfigDataManager.Instance.NumConfigDataObjects]; ConfigDataManager.Instance.GetConfigDataNames(configDataNames); int indexFromString = QCARUtilities.GetIndexFromString(ct.DataSetName, configDataNames); if (indexFromString < 0) { indexFromString = 0; } int index = EditorGUILayout.Popup("Data Set", indexFromString, configDataNames, new GUILayoutOption[0]); string dataSetName = configDataNames[index]; ConfigData configData = ConfigDataManager.Instance.GetConfigData(dataSetName); string[] arrayToFill = new string[configData.NumCylinderTargets]; configData.CopyCylinderTargetNames(arrayToFill, 0); int selectedIndex = QCARUtilities.GetIndexFromString(ct.TrackableName, arrayToFill); if (selectedIndex < 0) { selectedIndex = 0; } int num4 = EditorGUILayout.Popup("Cylinder Target", selectedIndex, arrayToFill, new GUILayoutOption[0]); if ((arrayToFill.Length > 0) && ((index != indexFromString) || (num4 != selectedIndex))) { ConfigData.CylinderTargetData data2; ct.SetDataSetPath("QCAR/" + configDataNames[index] + ".xml"); ct.SetNameForTrackable(arrayToFill[num4]); configData.GetCylinderTarget(target.TrackableName, out data2); UpdateAspectRatio(ct, data2); UpdateScale(ct, data2.sideLength); } float num5 = EditorGUILayout.FloatField("Side Length", target.SideLength, new GUILayoutOption[0]); if (num5 != target.SideLength) { target.SetSideLength(num5); } float num6 = EditorGUILayout.FloatField("Top Diameter ", target.TopDiameter, new GUILayoutOption[0]); if (num6 != target.TopDiameter) { target.SetTopDiameter(num6); } float num7 = EditorGUILayout.FloatField("Bottom Diameter", target.BottomDiameter, new GUILayoutOption[0]); if (num7 != target.BottomDiameter) { target.SetBottomDiameter(num7); } ct.SetExtendedTracking(EditorGUILayout.Toggle("Extended tracking", ct.ExtendedTracking, new GUILayoutOption[0])); ct.SetPreserveChildSize(EditorGUILayout.Toggle("Preserve child size", ct.PreserveChildSize, new GUILayoutOption[0])); } else if (GUILayout.Button("No targets defined. Press here for target creation!", new GUILayoutOption[0])) { SceneManager.Instance.GoToTargetManagerPage(); } if (GUI.changed) { EditorUtility.SetDirty(target); SceneManager.Instance.SceneUpdated(); } }
/// <summary> /// Finds DataSetTrackableBehaviours for this dataset and associates them with the Trackables in the DataSet. /// VirtualButtonBehaviours created in the scene are associated with the VirtualButtons in the DataSet or created there. /// /// If there is a Trackable in the DataSet where no TrackableBehaviour exists yet, this Behaviour is created, together with its VirtualButtons. /// </summary> public void AssociateTrackableBehavioursForDataSet(DataSet dataSet) { // Step: Add all TrackableBehaviours that belong to this data set and // are already instantiated in the scene to the dictionary. DataSetTrackableBehaviour[] trackableBehaviours = (DataSetTrackableBehaviour[]) Object.FindObjectsOfType(typeof(DataSetTrackableBehaviour)); // Initialize all Image Targets foreach (DataSetTrackableBehaviour trackableBehaviour in trackableBehaviours) { // trackable has been destroyed and shouldn't be associated if (mBehavioursMarkedForDeletion.Contains(trackableBehaviour)) { continue; } IEditorDataSetTrackableBehaviour editorTrackableBehaviour = trackableBehaviour; if (editorTrackableBehaviour.TrackableName == null) { Debug.LogError("Found Trackable without name."); continue; } // check if the TrackableBehaviour references this DataSet if (editorTrackableBehaviour.DataSetPath.Equals(dataSet.Path)) { bool matchFound = false; // find the trackable to be associated with this TrackableBehaviour: foreach (Trackable trackable in dataSet.GetTrackables()) { if (trackable.Name.Equals(editorTrackableBehaviour.TrackableName)) { if (mTrackableBehaviours.ContainsKey(trackable.ID)) { // don't replace existing behaviour if it has been created manually if (!mAutomaticallyCreatedBehaviours.Contains(trackable.ID) && !mBehavioursMarkedForDeletion.Contains(mTrackableBehaviours[trackable.ID])) { matchFound = true; continue; } // destroy automatically created behaviour - will be replaced by new one Object.Destroy(mTrackableBehaviours[trackable.ID].gameObject); mTrackableBehaviours.Remove(trackable.ID); mAutomaticallyCreatedBehaviours.Remove(trackable.ID); } if (trackableBehaviour is ImageTargetBehaviour && trackable is ImageTarget) { IEditorImageTargetBehaviour editorImageTargetBehaviour = (ImageTargetBehaviour)trackableBehaviour; matchFound = true; editorImageTargetBehaviour.InitializeImageTarget((ImageTarget)trackable); mTrackableBehaviours[trackable.ID] = trackableBehaviour; Debug.Log("Found Trackable named " + trackableBehaviour.Trackable.Name + " with id " + trackableBehaviour.Trackable.ID); } else if (trackableBehaviour is MultiTargetBehaviour && trackable is MultiTarget) { matchFound = true; IEditorMultiTargetBehaviour editorMultiTargetBehaviour = (MultiTargetBehaviour)trackableBehaviour; editorMultiTargetBehaviour.InitializeMultiTarget((MultiTarget)trackable); mTrackableBehaviours[trackable.ID] = trackableBehaviour; Debug.Log("Found Trackable named " + trackableBehaviour.Trackable.Name + " with id " + trackableBehaviour.Trackable.ID); } else if (trackableBehaviour is CylinderTargetBehaviour && trackable is CylinderTarget) { matchFound = true; IEditorCylinderTargetBehaviour editorCylinderTargetBehaviour = (CylinderTargetBehaviour)trackableBehaviour; editorCylinderTargetBehaviour.InitializeCylinderTarget((CylinderTarget)trackable); mTrackableBehaviours[trackable.ID] = trackableBehaviour; Debug.Log("Found Trackable named " + trackableBehaviour.Trackable.Name + " with id " + trackableBehaviour.Trackable.ID); } } } if (!matchFound) { Debug.LogError("Could not associate DataSetTrackableBehaviour '" + editorTrackableBehaviour.TrackableName + "' - no matching Trackable found in DataSet!"); } } } // Step 2: Add all VirtualButtonBehaviours that belong to this data set // and are already instantiated in the scene to the dictionary. VirtualButtonBehaviour[] vbBehaviours = (VirtualButtonBehaviour[]) Object.FindObjectsOfType(typeof(VirtualButtonBehaviour)); AssociateVirtualButtonBehaviours(vbBehaviours, dataSet); // Step 3: Create TrackableBehaviours that are not existing in the scene. CreateMissingDataSetTrackableBehaviours(dataSet); }
/// <summary> /// Updates CylinderTarget. Deletes all parts and recreates them. /// Creates a mesh with vertices, normals, and texture coordinates. /// Top and bottom geometry are represented as separate submeshes, /// i.e. resulting mesh contains 1, 2, or 3 submeshes. /// </summary> private static void UpdateGeometry(IEditorCylinderTargetBehaviour ct, float sideLength, float topDiameter, float bottomDiameter, bool hasTopGeometry, bool hasBottomGeometry) { GameObject gameObject = ct.gameObject; MeshRenderer meshRenderer = gameObject.GetComponent<MeshRenderer>(); if (!meshRenderer) meshRenderer = gameObject.AddComponent<MeshRenderer>(); //disable the editing functionality of meshRenderer in inspector UI meshRenderer.hideFlags = HideFlags.NotEditable; MeshFilter meshFilter = gameObject.GetComponent<MeshFilter>(); if (!meshFilter) meshFilter = gameObject.AddComponent<MeshFilter>(); //disable the editing functionality of meshFilter in inspector UI meshFilter.hideFlags = HideFlags.NotEditable; Mesh cylinderMesh = CylinderMeshFactory.CreateCylinderMesh(sideLength, topDiameter, bottomDiameter, NUM_PERIMETER_VERTICES, hasTopGeometry, hasBottomGeometry, INSIDE_MATERIAL); meshFilter.sharedMesh = cylinderMesh; //create and attach mask-out material (if not existing yet) MaskOutBehaviour script = gameObject.GetComponent<MaskOutBehaviour>(); if (!script) { Material maskMaterial = (Material) AssetDatabase.LoadAssetAtPath( QCARUtilities.GlobalVars.MASK_MATERIAL_PATH, typeof (Material)); script = gameObject.AddComponent<MaskOutBehaviour>(); script.maskMaterial = maskMaterial; } //disable the editing functionality of the script in inspector UI script.hideFlags = HideFlags.NotEditable; // Cleanup assets that have been created temporarily. EditorUtility.UnloadUnusedAssets(); }
/// <summary> /// Create and assign materials for cylinder targets. The newly created materials /// are based on the default material. /// </summary> private static void UpdateMaterials(IEditorCylinderTargetBehaviour ct, bool hasBottomGeometry, bool hasTopGeometry, bool insideMaterial) { // Load reference material. var referenceMaterialPath = QCARUtilities.GlobalVars.REFERENCE_MATERIAL_PATH; var 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. Textures have per convention the // same name as Image Targets + "_scaled" as postfix. var pathToTextures = QCARUtilities.GlobalVars.CYLINDER_TARGET_TEXTURES_PATH + ct.DataSetName + "/" + ct.TrackableName; var textureFiles = new List<string> { pathToTextures + ".Body_scaled" }; if (hasBottomGeometry) textureFiles.Add(pathToTextures + ".Bottom_scaled"); if (hasTopGeometry) textureFiles.Add(pathToTextures + ".Top_scaled"); var numSubMeshes = textureFiles.Count; var materials = new Material[insideMaterial ? numSubMeshes * 2 : numSubMeshes]; for(var i = 0; i < textureFiles.Count; i++) { var textureFile = textureFiles[i]; if (File.Exists(textureFile + ".png")) textureFile += ".png"; else if (File.Exists(textureFile + ".jpg")) textureFile += ".jpg"; else if (File.Exists(textureFile + ".jpeg")) textureFile += ".jpeg"; else { materials[i] = referenceMaterial; if (insideMaterial) materials[i + numSubMeshes] = referenceMaterial; continue; } var targetTexture = (Texture2D)AssetDatabase.LoadAssetAtPath(textureFile, typeof(Texture2D)); // We create a new material that is based on the reference material but // also contains a texture. var materialForTargetTexture = new Material(referenceMaterial); if (targetTexture != null) { materialForTargetTexture.mainTexture = targetTexture; materialForTargetTexture.name = targetTexture.name + "Material"; materialForTargetTexture.mainTextureScale = new Vector2(-1, -1); } materials[i] = materialForTargetTexture; if (insideMaterial) { var brightMaterial = new Material(materialForTargetTexture); brightMaterial.shader = Shader.Find("Custom/BrightTexture"); materials[i + numSubMeshes] = brightMaterial; } } ct.renderer.sharedMaterials = materials; Resources.UnloadUnusedAssets(); }