Beispiel #1
0
        public static void LoadVrmAsync(string path, Action <GameObject> onLoaded, Action <Exception> onError = null, bool show = true)
        {
            var context = new VRMImporterContext(UnityPath.FromFullpath(path));

            context.ParseGlb(File.ReadAllBytes(path));
            LoadVrmAsync(context, onLoaded, onError, show);
        }
Beispiel #2
0
        static void ImportMenu()
        {
            var path = EditorUtility.OpenFilePanel("open vrm", "", "vrm");

            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            if (Application.isPlaying)
            {
                ImportRuntime(path);
                return;
            }

            if (path.StartsWithUnityAssetPath())
            {
                Debug.LogWarningFormat("disallow import from folder under the Assets");
                return;
            }

            var prefabPath = EditorUtility.SaveFilePanel("save prefab", "Assets", Path.GetFileNameWithoutExtension(path), "prefab");

            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            ImportAsset(path, UnityPath.FromFullpath(prefabPath));
        }
Beispiel #3
0
        public static void OpenImportMenu()
        {
            var path = EditorUtility.OpenFilePanel("open vrm", "", "vrm");

            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            if (Application.isPlaying)
            {
                // import vrm to scene without asset creation
                ImportRuntime(path);
            }
            else
            {
                // import vrm to asset
                if (path.StartsWithUnityAssetPath())
                {
                    Debug.LogWarningFormat("disallow import from folder under the Assets");
                    return;
                }

                var prefabPath = EditorUtility.SaveFilePanel("save prefab", "Assets", Path.GetFileNameWithoutExtension(path), "prefab");
                if (string.IsNullOrEmpty(path))
                {
                    return;
                }

                vrmAssetPostprocessor.ImportVrmAndCreatePrefab(path, UnityPath.FromFullpath(prefabPath));
            }
        }
Beispiel #4
0
        public static Task <GameObject> LoadVrmAsync(string path, bool show = true)
        {
            var context = new VRMImporterContext(UnityPath.FromFullpath(path));

            context.ParseGlb(File.ReadAllBytes(path));
            return(LoadVrmAsyncInternal(context, show).ToTask());
        }
Beispiel #5
0
        public static void IntegrateSelected(GameObject go)
        {
            var meshWithMaterials = StaticMeshIntegrator.Integrate(go.transform);

            // save as asset
            var assetPath = "";

#if UNITY_2018_2_OR_NEWER
            var prefab = PrefabUtility.GetCorrespondingObjectFromSource(go);
#else
            var prefab = PrefabUtility.GetPrefabParent(go);
#endif
            if (prefab != null)
            {
                var prefabPath = AssetDatabase.GetAssetPath(prefab);
                assetPath = string.Format("{0}/{1}_{2}{3}",
                                          Path.GetDirectoryName(prefabPath),
                                          Path.GetFileNameWithoutExtension(prefabPath),
                                          go.name,
                                          ASSET_SUFFIX
                                          );
            }
            else
            {
                var path = EditorUtility.SaveFilePanel(
                    "Save mesh",
                    "Assets",
                    go.name + ".asset",
                    "asset");
                if (string.IsNullOrEmpty(path))
                {
                    return;
                }
                assetPath = UnityPath.FromFullpath(path).Value;
            }

            assetPath = AssetDatabase.GenerateUniqueAssetPath(assetPath);
            Debug.LogFormat("CreateAsset: {0}", assetPath);
            AssetDatabase.CreateAsset(meshWithMaterials.Mesh, assetPath);

            // add component
            var meshObject = new GameObject(go.name + ".static_meshes_integrated");
            if (go.transform.parent != null)
            {
                meshObject.transform.SetParent(go.transform.parent, false);
            }
            meshObject.transform.localPosition = go.transform.localPosition;
            meshObject.transform.localRotation = go.transform.localRotation;
            meshObject.transform.localScale    = go.transform.localScale;

            var filter = meshObject.AddComponent <MeshFilter>();
            filter.sharedMesh = meshWithMaterials.Mesh;
            var renderer = meshObject.AddComponent <MeshRenderer>();
            renderer.sharedMaterials = meshWithMaterials.Materials;
        }
Beispiel #6
0
        static void ExportJsonSchema()
        {
            var schema = JsonSchema.FromType <glTF_VRM_extensions>();
            var f      = new JsonFormatter(2);

            schema.ToJson(f);
            var json = f.ToString();

            var dir = UnityPath.FromFullpath(Application.dataPath + "/VRM/specification/0.0/schema");

            dir.EnsureFolder();

            var path = SplitAndWriteJson(JsonParser.Parse(json), dir);

            Selection.activeObject = AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(path.Value);
        }
Beispiel #7
0
        static void Import(string readPath, UnityPath prefabPath)
        {
            var bytes   = File.ReadAllBytes(readPath);
            var context = new VRMImporterContext(UnityPath.FromFullpath(readPath));

            context.ParseGlb(File.ReadAllBytes(readPath));
            context.SaveTexturesAsPng(prefabPath);

            EditorApplication.delayCall += () =>
            {
                // delay and can import png texture
                VRMImporter.LoadFromBytes(context);
                context.SaveAsAsset(prefabPath);
                context.Destroy(false);
            };
        }
Beispiel #8
0
        /// <summary>
        /// AOT向けにシリアライザを生成する
        /// </summary>
        public static void GenerateCode()
        {
            var info = new ObjectSerialization(typeof(glTF_VRM_extensions), "vrm", "Serialize_");

            Debug.Log(info);

            using (var s = File.Open(OutPath, FileMode.Create))
                using (var w = new StreamWriter(s, new UTF8Encoding(false)))
                {
                    w.Write(Begin);
                    info.GenerateSerializer(w, "Serialize");
                    w.Write(End);
                }

            Debug.LogFormat("write: {0}", OutPath);
            UnityPath.FromFullpath(OutPath).ImportAsset();
        }
Beispiel #9
0
        static void GenerateSerializerTypes(System.Type type)
        {
            var name = string.Format("{0}_Deserialize", type.Name);
            var info = new UniGLTF.ObjectSerialization(type, "vci", name);

            Debug.Log(info);

            using (var s = File.Open(GetPath(name), FileMode.Create))
                using (var w = new StreamWriter(s, Encoding.UTF8))
                {
                    w.NewLine = "\n";
                    w.Write(@"
using System;
using System.Collections.Generic;
using UniJSON;
using UnityEngine;
using UniGLTF;
using VCI;
using Object = System.Object;

namespace VCI {

public static class ");
                    w.Write(@"
" + type.Name + "_Deserializer");

                    w.Write(@"
{

");

                    info.GenerateDeserializer(w, "Deserialize");

                    // footer
                    w.Write(@"
} // VciDeserializer
} // VCI
");
                }

            // CRLF を LF に変換して再保存
            File.WriteAllText(GetPath(name), File.ReadAllText(GetPath(name), Encoding.UTF8).Replace("\r\n", "\n"), Encoding.UTF8);

            Debug.LogFormat("write: {0}", GetPath(name));
            UnityPath.FromFullpath(GetPath(name)).ImportAsset();
        }
Beispiel #10
0
        /// <summary>
        /// Import GLB.
        /// </summary>
        /// <returns></returns>
        public static void ImportGlb()
        {
            string path = EditorUtility.OpenFilePanel(title: "Open File Dialog", directory: "", extension: "glb");

            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            if (Application.isPlaying)
            {
                //
                // load into scene
                //
                var context = new ImporterContext();

                context.Load(path);

                context.ShowMeshes();

                Selection.activeGameObject = context.Root;
            }
            else
            {
                //
                // save as asset
                //
                if (path.StartsWithUnityAssetPath())
                {
                    Debug.LogWarningFormat("disallow import from folder under the Assets");
                    return;
                }

                string assetPath = EditorUtility.SaveFilePanel(title: "Save prefab", directory: "Assets", defaultName: Path.GetFileNameWithoutExtension(path), extension: "prefab");

                if (string.IsNullOrEmpty(path))
                {
                    return;
                }

                // import as asset
                GlbAssetPostprocessor.ImportAsset(src: path, prefabPath: UnityPath.FromFullpath(assetPath));
            }
        }
        static void ExportJsonSchema()
        {
            var dir = UnityPath.FromFullpath(Application.dataPath + "/VRM/specification/0.0/schema");

            dir.EnsureFolder();

            var path = dir.Child("vrm.schema.json");

            Debug.LogFormat("write SsonSchema: {0}", path.FullPath);
            var schema = JsonSchema.FromType <glTF_VRM_extensions>();
            var f      = new JsonFormatter(2);

            schema.ToJson(f);
            var json = f.ToString();

            File.WriteAllText(path.FullPath, json, Encoding.UTF8);

            Selection.activeObject = AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(path.Value);
        }
Beispiel #12
0
        /// <summary>
        /// Import VGO.
        /// </summary>
        /// <returns></returns>
        public static async Task ImportVgo()
        {
            string path = EditorUtility.OpenFilePanel(title: "Open File Dialog", directory: "", extension: "vgo");

            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            if (Application.isPlaying)
            {
                //
                // load into scene
                //
                var context = new VgoImporter();

                await context.LoadAsync(path, showMeshes : true, enableUpdateWhenOffscreen : true);

                Selection.activeGameObject = context.Root;
            }
            else
            {
                //
                // save as asset
                //
                if (path.StartsWithUnityAssetPath())
                {
                    Debug.LogWarningFormat("disallow import from folder under the Assets");
                    return;
                }

                string assetPath = EditorUtility.SaveFilePanel(title: "Save prefab", directory: "Assets", defaultName: Path.GetFileNameWithoutExtension(path), extension: "prefab");

                if (string.IsNullOrEmpty(path))
                {
                    return;
                }

                // import as asset
                VgoAssetPostprocessor.ImportAsset(path, UnityPath.FromFullpath(assetPath));
            }
        }
Beispiel #13
0
        static void ImportMenu()
        {
            var path = EditorUtility.OpenFilePanel("open vrm", "", "vrm");

            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            if (Application.isPlaying)
            {
                // load into scene
                var parser = new GltfParser();
                parser.ParsePath(path);

                using (var context = new VRMImporterContext(parser))
                {
                    context.Load();
                    context.EnableUpdateWhenOffscreen();
                    context.ShowMeshes();
                    context.DisposeOnGameObjectDestroyed();
                    Selection.activeGameObject = context.Root;
                }
            }
            else
            {
                if (path.StartsWithUnityAssetPath())
                {
                    Debug.LogWarningFormat("disallow import from folder under the Assets");
                    return;
                }

                var assetPath = EditorUtility.SaveFilePanel("save prefab", "Assets", Path.GetFileNameWithoutExtension(path), "prefab");
                if (string.IsNullOrEmpty(path))
                {
                    return;
                }

                if (!assetPath.StartsWithUnityAssetPath())
                {
                    Debug.LogWarningFormat("out of asset path: {0}", assetPath);
                    return;
                }

                // import as asset
                var prefabPath = UnityPath.FromUnityPath(assetPath);
                var parser     = new GltfParser();
                parser.ParseGlb(File.ReadAllBytes(path));

                Action <IEnumerable <string> > onCompleted = _ =>
                {
                    //
                    // after textures imported
                    //
                    using (var context = new VRMImporterContext(parser))
                    {
                        var editor = new VRMEditorImporterContext(context, prefabPath);
                        context.Load();
                        editor.SaveAsAsset();
                    }
                };

                using (var context = new VRMImporterContext(parser))
                {
                    var editor = new VRMEditorImporterContext(context, prefabPath);
                    editor.ConvertAndExtractImages(UnityPath.FromFullpath(path), onCompleted);
                }
            }
        }