コード例 #1
0
        /*
         * static void LoadWorld()
         * {
         * string packagePath = "/Users/akzeac/Shared/aws-robomaker-hospital-world";
         * string localPath = "/worlds/hospital.world";
         *
         * string xmlData = File.ReadAllText(packagePath + localPath);
         * Sdf.SdfFile sdf = Sdf.SdfFile.Create(xmlData);
         *
         * var modelPaths = Sdf.SdfFile.CreateModelPaths(packagePath);
         * Sdf.SdfFile newSdf = sdf.ResolveIncludes(modelPaths);
         *
         * CreateWorld(newSdf.Worlds[0]);
         * }
         */


        static async Task CreateRobotsAsync([NotNull] ExternalResourceManager manager)
        {
            string unityDirectory = "Resources/Package/iviz/robots";
            string absolutePath   = $"{UnityEngine.Application.dataPath}/{unityDirectory}";

            Directory.CreateDirectory(absolutePath);

            //Dictionary<string, string> resourceFile = new Dictionary<string, string>();

            /*
             * foreach (string robotName in manager.GetRobotNames())
             * {
             *  string filename = ExternalResourceManager.SanitizeForFilename(robotName).Replace(".", "_");
             *  resourceFile[robotName] = filename;
             * }
             */

            Debug.LogWarning("SavedAssetLoader: Not writing robot resource files.");

            foreach (string robotName in manager.GetRobotNames())
            {
                var(_, robotDescription) = await manager.TryGetRobotAsync(robotName);

                string filename = ExternalResourceManager.SanitizeForFilename(robotName).Replace(".", "_");
                File.WriteAllText(absolutePath + "/" + filename + ".txt", robotDescription);
            }
        }
コード例 #2
0
        public static async void CreateAllAssets()
        {
            Resource.ClearResources();
            ExternalResourceManager manager      = Resource.External;
            IReadOnlyList <string>  resourceUris = manager.GetListOfModels();

            Debug.Log("SavedAssetLoader: Transferring " + resourceUris.Count + " assets...");
            foreach (string uri in resourceUris)
            {
                await CreateAssetAsync(new Uri(uri), manager);
            }

            await CreateRobotsAsync(manager);

            AssetDatabase.Refresh();

            Debug.Log("SavedAssetLoader: Done!");

            // ugly workaround
            GameObject managerNode = GameObject.Find("External Resources");

            if (managerNode != null)
            {
                DestroyImmediate(managerNode);
            }

            Resource.ClearResources();
        }
コード例 #3
0
ファイル: Resource.cs プロジェクト: KIT-ISAS/iviz
 public static void ClearResources()
 {
     internals         = null;
     externals         = null;
     texturedMaterials = null;
 }
コード例 #4
0
        static async Task CreateAssetAsync([NotNull] Uri assetUri, ExternalResourceManager manager)
        {
            const string basePath = "Assets/Resources/Package/";
            string       uriPath  = assetUri.Host + Uri.UnescapeDataString(assetUri.AbsolutePath);

            string relativePath = Path.GetDirectoryName(uriPath);

            if (!string.IsNullOrEmpty(relativePath) && Path.DirectorySeparatorChar == '\\')
            {
                relativePath = relativePath.Replace('\\', '/');
            }

            string filename = Path.GetFileNameWithoutExtension(uriPath);
            string filenameWithExtension = Path.GetFileName(uriPath);

            string topPath        = basePath + relativePath;
            string innerPath      = topPath + "/" + filename;
            string unityDirectory = "Resources/Package/" + relativePath + "/" + filename;

            string absolutePath = UnityEngine.Application.dataPath + "/" + unityDirectory;

            if (Directory.Exists(absolutePath))
            {
                Debug.Log("SavedAssetLoader: Skipping " + assetUri + "...");
                return;
            }

            Directory.CreateDirectory(absolutePath);

            var resourceInfo = await manager.TryGetGameObjectAsync(assetUri.ToString(), null, default);

            if (resourceInfo == null)
            {
                throw new FileNotFoundException(assetUri.ToString());
            }

            GameObject obj = resourceInfo.Object;

            MeshTrianglesResource[] resources     = obj.GetComponentsInChildren <MeshTrianglesResource>();
            HashSet <Mesh>          writtenMeshes = new HashSet <Mesh>();

            int meshId = 0;

            foreach (var resource in resources)
            {
                MeshFilter filter = resource.GetComponent <MeshFilter>();
                if (writtenMeshes.Contains(filter.sharedMesh))
                {
                    continue;
                }

                if (filter.sharedMesh.GetIndexCount(0) != 0)
                {
                    Unwrapping.GenerateSecondaryUVSet(filter.sharedMesh);
                }

                writtenMeshes.Add(filter.sharedMesh);

                //string meshPath = AssetDatabase.GenerateUniqueAssetPath(innerPath + "/mesh.mesh");
                //AssetDatabase.CreateAsset(filter.sharedMesh, meshPath);


                StringBuilder sb = new StringBuilder();
                Mesh          m  = filter.sharedMesh;
                foreach (Vector3 v in m.vertices)
                {
                    sb.AppendFormat(BuiltIns.Culture, "v {0} {1} {2}\n", -v.x, v.y, v.z);
                }

                foreach (Vector3 v in m.normals)
                {
                    sb.AppendFormat(BuiltIns.Culture, "vn {0} {1} {2}\n", -v.x, v.y, v.z);
                }

                sb.AppendLine();
                foreach (Vector2 v in m.uv)
                {
                    sb.AppendFormat(BuiltIns.Culture, "vt {0} {1}\n", v.x, v.y);
                }

                for (int subMesh = 0; subMesh < m.subMeshCount; subMesh++)
                {
                    sb.AppendLine();
                    int[] triangles = m.GetTriangles(subMesh);
                    for (int i = 0; i < triangles.Length; i += 3)
                    {
                        sb.AppendFormat(BuiltIns.Culture, "f {0}/{0}/{0} {1}/{1}/{1} {2}/{2}/{2}\n",
                                        triangles[i] + 1, triangles[i + 2] + 1, triangles[i + 1] + 1);
                    }
                }

                File.WriteAllText($"{absolutePath}/mesh-{meshId}.obj", sb.ToString());
                AssetDatabase.Refresh();

                string meshPath = $"{innerPath}/mesh-{meshId}.obj";
                Mesh   newMesh  = AssetDatabase.LoadAssetAtPath <Mesh>(meshPath);
                filter.sharedMesh = newMesh;

                meshId++;
            }

            Material baseMaterial = UnityEngine.Resources.Load <Material>("Materials/Standard");

            Dictionary <(Color, Color, Texture2D), Material> writtenColors =
                new Dictionary <(Color, Color, Texture2D), Material>();

            int textureId = 0;

            foreach (var resource in resources)
            {
                MeshRenderer renderer = resource.GetComponent <MeshRenderer>();
                Color        color    = resource.Color;
                Color        emissive = resource.EmissiveColor;
                Texture2D    texture  = resource.DiffuseTexture;

                if (writtenColors.TryGetValue((color, emissive, texture), out Material material))
                {
                    resource.SetMaterialValuesDirect((Texture2D)material.mainTexture, emissive, color, Color.white);
                    renderer.sharedMaterial = material;
                    continue;
                }

                material                = Instantiate(baseMaterial);
                material.color          = color;
                material.mainTexture    = texture;
                renderer.sharedMaterial = material;

                writtenColors.Add((color, emissive, texture), material);
                string materialPath = AssetDatabase.GenerateUniqueAssetPath(innerPath + "/material.mat");
                AssetDatabase.CreateAsset(material, materialPath);

                if (texture == null)
                {
                    resource.SetMaterialValuesDirect(null, emissive, color, Color.white);
                    continue;
                }

                byte[] bytes = texture.EncodeToPNG();
                File.WriteAllBytes($"{absolutePath}/texture-{textureId}.png", bytes);
                AssetDatabase.Refresh();

                string    texturePath = $"{innerPath}/texture-{textureId}.png";
                Texture2D newTexture  = AssetDatabase.LoadAssetAtPath <Texture2D>(texturePath);
                material.mainTexture = newTexture;

                textureId++;

                resource.SetMaterialValuesDirect(newTexture, emissive, color, Color.white);
            }


            foreach (var resource in resources)
            {
                BoxCollider collider = resource.GetComponent <BoxCollider>();
                collider.center  = resource.LocalBounds.center;
                collider.size    = resource.LocalBounds.size;
                collider.enabled = false;

                //DestroyImmediate(resource);
                resource.enabled = false;
            }

            foreach (var marker in obj.GetComponentsInChildren <AggregatedMeshMarkerResource>())
            {
                //DestroyImmediate(marker);
                marker.enabled = false;
            }

            Debug.Log("Writing to " + topPath + "/" + filenameWithExtension + ".prefab");
            PrefabUtility.SaveAsPrefabAssetAndConnect(obj, topPath + "/" + filenameWithExtension + ".prefab",
                                                      InteractionMode.UserAction);

            //DestroyImmediate(obj);
        }