Example #1
0
        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);
        }
Example #2
0
        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);
                }
            }
        }
Example #3
0
        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));
        }
Example #4
0
        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);
        }
Example #5
0
        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);
        }