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
        }
        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);
        }
Beispiel #3
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);
                }
            }
        }
Beispiel #4
0
        private static GameObject CreateStlParent(string stlFile)
        {
            Mesh[] meshes = StlImporter.ImportMesh(stlFile);
            if (meshes == null)
            {
                return(null);
            }

            GameObject parent   = new GameObject(Path.GetFileNameWithoutExtension(stlFile));
            Material   material = GetDefaultDiffuseMaterial();

            for (int i = 0; i < meshes.Length; i++)
            {
                string meshAssetPath = GetMeshAssetPath(stlFile, i);
                RuntimeUrdf.AssetDatabase_CreateAsset(meshes[i], meshAssetPath);
                GameObject gameObject = CreateStlGameObject(meshAssetPath, material);
                gameObject.transform.SetParent(parent.transform, false);
            }
            return(parent);
        }
Beispiel #5
0
        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);
        }
Beispiel #6
0
 public void AssetDatabase_CreateAsset_GameObject()
 {
     RuntimeUrdf.runtimeModeEnabled = false;
     RuntimeUrdf.AssetDatabase_CreateAsset(new TextAsset("TextAsset content"), $"{createAssetPath}/TestAsset3", true);
     Assert.IsNotNull(AssetDatabase.FindAssets("TestAsset3", new string[] { createAssetPath }));
 }