static Material defaultMaterial = null; // used RuntimeURDF private static void CreateDefaultMaterial() { Material material = defaultMaterial; #if UNITY_EDITOR if (!RuntimeUrdf.IsRuntimeMode()) { material = RuntimeUrdf.AssetDatabase_LoadAssetAtPath <Material>(UrdfAssetPathHandler.GetMaterialAssetPath(DefaultMaterialName)); } #endif if (material != null) { return; } material = MaterialExtensions.CreateBasicMaterial(); MaterialExtensions.SetMaterialColor(material, new Color(0.33f, 0.33f, 0.33f, 0.0f)); // just keep it in memory while the app is running. defaultMaterial = material; #if UNITY_EDITOR if (!RuntimeUrdf.IsRuntimeMode()) { // create the material to be reused RuntimeUrdf.AssetDatabase_CreateAsset(material, UrdfAssetPathHandler.GetMaterialAssetPath(DefaultMaterialName)); } #endif }
// Initializes import pipeline and reads the urdf file. private static ImportPipelineData ImportPipelineInit(string filename, ImportSettings settings, bool loadStatus, bool forceRuntimeMode) { ImportPipelineData im = new ImportPipelineData(); im.settings = settings; im.loadStatus = loadStatus; im.wasRuntimeMode = RuntimeUrdf.IsRuntimeMode(); im.forceRuntimeMode = forceRuntimeMode; if (forceRuntimeMode) { RuntimeUrdf.SetRuntimeMode(true); } im.robot = new Robot(filename); if (!UrdfAssetPathHandler.IsValidAssetPath(im.robot.filename)) { Debug.LogError("URDF file and ressources must be placed in Assets Folder:\n" + Application.dataPath); if (forceRuntimeMode) { // set runtime mode back to what it was RuntimeUrdf.SetRuntimeMode(im.wasRuntimeMode); } return(null); } return(im); }
private static void CopyDaeTextureToExportDestination(string prefabPath, string newFolderLocation) { //Get material from Collada prefab Material material = RuntimeUrdf.AssetDatabase_LoadAssetAtPath <Material>(prefabPath); if (material == null || material.mainTexture == null) { return; } //Get relative subfolder where texture is, compared to the DAE file. string commonFolder = Path.GetDirectoryName(prefabPath).SetSeparatorChar(); string texturePath = RuntimeUrdf.AssetDatabase_GetAssetPath(material.mainTexture).SetSeparatorChar(); string relativeLocation = ""; if (texturePath.Contains(commonFolder)) { relativeLocation = texturePath.Substring(commonFolder.Length + 1); } string newTexturePath = Path.Combine(newFolderLocation, relativeLocation); Directory.CreateDirectory(Path.GetDirectoryName(newTexturePath)); CopyFileToNewLocation(UrdfAssetPathHandler.GetFullAssetPath(texturePath), newTexturePath); }
private static Material CreateMaterial(this Link.Visual.Material urdfMaterial) { if (urdfMaterial.name == "") { urdfMaterial.name = GenerateMaterialName(urdfMaterial); } var material = RuntimeUrdf.AssetDatabase_LoadAssetAtPath <Material>(UrdfAssetPathHandler.GetMaterialAssetPath(urdfMaterial.name)); if (material != null) { //material already exists return(material); } material = MaterialExtensions.CreateBasicMaterial(); if (urdfMaterial.color != null) { MaterialExtensions.SetMaterialColor(material, CreateColor(urdfMaterial.color)); } else if (urdfMaterial.texture != null) { material.mainTexture = LoadTexture(urdfMaterial.texture.filename); } RuntimeUrdf.AssetDatabase_CreateAsset(material, UrdfAssetPathHandler.GetMaterialAssetPath(urdfMaterial.name)); return(material); }
private static string LocateAssetFile(string invalidPath) { string fileExtension = Path.GetExtension(invalidPath)?.Replace(".", ""); string newPath = RuntimeUrdf.EditorUtility_OpenFilePanel( "Couldn't find asset at " + invalidPath + ". Select correct file.", UrdfAssetPathHandler.GetPackageRoot(), fileExtension); return(UrdfAssetPathHandler.GetRelativeAssetPath(newPath)); }
public static T FindUrdfAsset <T>(string urdfFileName) where T : UnityEngine.Object { string fileAssetPath = UrdfAssetPathHandler.GetRelativeAssetPathFromUrdfPath(urdfFileName); // check if it is an asset tha requires post processing (AIRO-908) var originalUrdfPath = UrdfAssetPathHandler.GetRelativeAssetPathFromUrdfPath(urdfFileName, false); if (originalUrdfPath.ToLower().EndsWith(".stl")) { // it is an asset that requires post processing if (UrdfRobotExtensions.importsettings.OverwriteExistingPrefabs || !RuntimeUrdf.AssetExists(fileAssetPath, true)) { // post process again to (re)create prefabs StlAssetPostProcessor.PostprocessStlFile(originalUrdfPath); } } T assetObject = RuntimeUrdf.AssetDatabase_LoadAssetAtPath <T>(fileAssetPath); if (assetObject) { return(assetObject); } //If asset was not found, let user choose whether to search for //or ignore the missing asset. string invalidPath = fileAssetPath ?? urdfFileName; int option = RuntimeUrdf.EditorUtility_DisplayDialogComplex("Urdf Importer: Asset Not Found", "Current root folder: " + UrdfAssetPathHandler.GetPackageRoot() + "\n\nExpected asset path: " + invalidPath, "Locate Asset", "Ignore Missing Asset", "Locate Root Folder"); switch (option) { case 0: fileAssetPath = LocateAssetFile(invalidPath); break; case 1: break; case 2: fileAssetPath = LocateRootAssetFolder <T>(urdfFileName); break; } assetObject = (T)RuntimeUrdf.AssetDatabase_LoadAssetAtPath(fileAssetPath, typeof(T)); if (assetObject != null) { return(assetObject); } ChooseFailureOption(urdfFileName); return(null); }
private static void ConvertMeshToColliders(GameObject gameObject, string location = null, bool setConvex = true) { MeshFilter[] meshFilters = gameObject.GetComponentsInChildren <MeshFilter>(); if (UrdfRobotExtensions.importsettings.convexMethod == ImportSettings.convexDecomposer.unity) { foreach (MeshFilter meshFilter in meshFilters) { GameObject child = meshFilter.gameObject; MeshCollider meshCollider = child.AddComponent <MeshCollider>(); meshCollider.sharedMesh = meshFilter.sharedMesh; meshCollider.convex = setConvex; Object.DestroyImmediate(child.GetComponent <MeshRenderer>()); Object.DestroyImmediate(meshFilter); } } else { string templateFileName = ""; string filePath = ""; int meshIndex = 0; if (!RuntimeUrdf.IsRuntimeMode() && location != null) { string meshFilePath = UrdfAssetPathHandler.GetRelativeAssetPathFromUrdfPath(location, false); templateFileName = Path.GetFileNameWithoutExtension(meshFilePath); filePath = Path.GetDirectoryName(meshFilePath); } foreach (MeshFilter meshFilter in meshFilters) { GameObject child = meshFilter.gameObject; VHACD decomposer = child.AddComponent <VHACD>(); List <Mesh> colliderMeshes = decomposer.GenerateConvexMeshes(meshFilter.sharedMesh); foreach (Mesh collider in colliderMeshes) { if (!RuntimeUrdf.IsRuntimeMode()) { meshIndex++; string name = $"{filePath}/{templateFileName}_{meshIndex}.asset"; Debug.Log($"Creating new mesh file: {name}"); RuntimeUrdf.AssetDatabase_CreateAsset(collider, name); RuntimeUrdf.AssetDatabase_SaveAssets(); } MeshCollider current = child.AddComponent <MeshCollider>(); current.sharedMesh = collider; current.convex = setConvex; } Component.DestroyImmediate(child.GetComponent <VHACD>()); Object.DestroyImmediate(child.GetComponent <MeshRenderer>()); Object.DestroyImmediate(meshFilter); } } }
private static void MoveMaterialsToNewLocation(string oldPackageRoot) { if (RuntimeUrdf.AssetDatabase_IsValidFolder(Path.Combine(oldPackageRoot, MaterialFolderName))) { RuntimeUrdf.AssetDatabase_MoveAsset( Path.Combine(oldPackageRoot, MaterialFolderName), Path.Combine(UrdfAssetPathHandler.GetPackageRoot(), MaterialFolderName)); } else { RuntimeUrdf.AssetDatabase_CreateFolder(UrdfAssetPathHandler.GetPackageRoot(), MaterialFolderName); } }
private static Link.Visual.Material.Texture ExportTextureData(Texture texture) { string oldTexturePath = UrdfAssetPathHandler.GetFullAssetPath(RuntimeUrdf.AssetDatabase_GetAssetPath(texture)); string newTexturePath = UrdfExportPathHandler.GetNewResourcePath(Path.GetFileName(oldTexturePath)); if (oldTexturePath != newTexturePath) { File.Copy(oldTexturePath, newTexturePath, true); } string packagePath = UrdfExportPathHandler.GetPackagePathForResource(newTexturePath); return(new Link.Visual.Material.Texture(packagePath)); }
private static string CopyMeshToExportDestination(string prefabPath) { string newPrefabPath = UrdfExportPathHandler.GetNewMeshPath(Path.GetFileName(prefabPath)); if (Path.GetExtension(prefabPath)?.ToLower() == ".dae") { CopyDaeTextureToExportDestination(prefabPath, Path.GetDirectoryName(newPrefabPath)); } prefabPath = UrdfAssetPathHandler.GetFullAssetPath(prefabPath); CopyFileToNewLocation(prefabPath, newPrefabPath); return(newPrefabPath); }
private static string LocateRootAssetFolder <T>(string urdfFileName) where T : UnityEngine.Object { string newAssetPath = RuntimeUrdf.EditorUtility_OpenFolderPanel( "Locate package root folder", Path.Combine(Path.GetDirectoryName(Application.dataPath), "Assets"), ""); if (UrdfAssetPathHandler.IsValidAssetPath(newAssetPath)) { UrdfAssetPathHandler.SetPackageRoot(newAssetPath, true); } else { Debug.LogWarning("Selected package root " + newAssetPath + " is not within the Assets folder."); } return(UrdfAssetPathHandler.GetRelativeAssetPathFromUrdfPath(urdfFileName)); }
private static GameObject CreateMeshColliderRuntime(Link.Geometry.Mesh mesh) { string meshFilePath = UrdfAssetPathHandler.GetRelativeAssetPathFromUrdfPath(mesh.filename, false); GameObject meshObject = null; if (meshFilePath.ToLower().EndsWith(".stl")) { meshObject = StlAssetPostProcessor.CreateStlGameObjectRuntime(meshFilePath); } else { Debug.LogError("Unable to create mesh collider for the mesh: " + mesh.filename); } if (meshObject != null) { ConvertMeshToColliders(meshObject); } return(meshObject); }
private static GameObject CreateMeshVisualRuntime(Link.Geometry.Mesh mesh) { GameObject meshObject = null; if (!string.IsNullOrEmpty(mesh.filename)) { try { string meshFilePath = UrdfAssetPathHandler.GetRelativeAssetPathFromUrdfPath(mesh.filename, false); if (meshFilePath.ToLower().EndsWith(".stl")) { meshObject = StlAssetPostProcessor.CreateStlGameObjectRuntime(meshFilePath); } else if (meshFilePath.ToLower().EndsWith(".dae")) { float globalScale = ColladaAssetPostProcessor.ReadGlobalScale(meshFilePath); meshObject = MeshImporter.Load(meshFilePath, globalScale, globalScale, globalScale); if (meshObject != null) { ColladaAssetPostProcessor.ApplyColladaOrientation(meshObject, meshFilePath); } } else if (meshFilePath.ToLower().EndsWith(".obj")) { meshObject = MeshImporter.Load(meshFilePath); } } catch (Exception ex) { Debug.LogAssertion(ex); } if (meshObject == null) { Debug.LogError("Unable to load visual mesh: " + mesh.filename); } } return(meshObject); }
private static void ConvertCylinderToCollider(MeshFilter filter) { GameObject go = filter.gameObject; var collider = filter.sharedMesh; // Only create an asset if not runtime import if (!RuntimeUrdf.IsRuntimeMode()) { var packageRoot = UrdfAssetPathHandler.GetPackageRoot(); var filePath = RuntimeUrdf.AssetDatabase_GUIDToAssetPath(RuntimeUrdf.AssetDatabase_CreateFolder($"{packageRoot}", "meshes")); var name = $"{filePath}/Cylinder.asset"; Debug.Log($"Creating new cylinder file: {name}"); RuntimeUrdf.AssetDatabase_CreateAsset(collider, name, uniquePath: true); RuntimeUrdf.AssetDatabase_SaveAssets(); } MeshCollider current = go.AddComponent <MeshCollider>(); current.sharedMesh = collider; current.convex = true; Object.DestroyImmediate(go.GetComponent <MeshRenderer>()); Object.DestroyImmediate(filter); }
// Creates the robot game object. private static void ImportPipelineCreateObject(ImportPipelineData im) { im.robotGameObject = new GameObject(im.robot.name); importsettings = im.settings; im.settings.totalLinks = im.robot.links.Count; CreateTag(); SetTag(im.robotGameObject); im.robotGameObject.AddComponent <UrdfRobot>(); im.robotGameObject.AddComponent <Unity.Robotics.UrdfImporter.Control.Controller>(); if (RuntimeUrdf.IsRuntimeMode()) {// In runtime mode, we have to disable controller while robot is being constructed. im.robotGameObject.GetComponent <Unity.Robotics.UrdfImporter.Control.Controller>().enabled = false; } im.robotGameObject.GetComponent <UrdfRobot>().SetAxis(im.settings.choosenAxis); UrdfAssetPathHandler.SetPackageRoot(Path.GetDirectoryName(im.robot.filename)); UrdfMaterial.InitializeRobotMaterials(im.robot); UrdfPlugins.Create(im.robotGameObject.transform, im.robot.plugins); }
public static void SetUrdfMaterial(GameObject gameObject, Link.Visual.Material urdfMaterial) { if (urdfMaterial != null) { var material = CreateMaterial(urdfMaterial); SetMaterial(gameObject, material); } else { //If the URDF material is not defined, and the renderer is missing //a material, assign the default material. Renderer renderer = gameObject.GetComponentInChildren <Renderer>(); if (renderer != null && renderer.sharedMaterial == null) { Material material = defaultMaterial; #if UNITY_EDITOR if (!RuntimeUrdf.IsRuntimeMode()) { material = RuntimeUrdf.AssetDatabase_LoadAssetAtPath <Material>(UrdfAssetPathHandler.GetMaterialAssetPath(DefaultMaterialName)); } #endif SetMaterial(gameObject, material); } } }