Esempio n. 1
0
        private static void ExportFromMenu()
        {
            var go   = Selection.activeObject as GameObject;
            var path = EditorUtility.SaveFilePanel(
                "Save glb",
                "",
                go.name + ".glb",
                "glb");

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

            var gltf = new glTF();

            using (var exporter = new gltfExporter(gltf))
            {
                exporter.Prepare(go);
                exporter.Export();
            }
            var bytes = gltf.ToGlbBytes();

            File.WriteAllBytes(path, bytes);

            if (path.StartsWithUnityAssetPath())
            {
                AssetDatabase.ImportAsset(path.ToUnityRelativePath());
                AssetDatabase.Refresh();
            }
        }
Esempio n. 2
0
        private static void ExportFromMenu()
        {
            var go = Selection.activeObject as GameObject;

            if (go.transform.position == Vector3.zero &&
                go.transform.rotation == Quaternion.identity &&
                go.transform.localScale == Vector3.one)
            {
                var path = EditorUtility.SaveFilePanel(
                    "Save glb", "", go.name + ".glb", "glb");
                if (string.IsNullOrEmpty(path))
                {
                    return;
                }

                var gltf = new glTF();
                using (var exporter = new gltfExporter(gltf))
                {
                    exporter.Prepare(go);
                    exporter.Export(MeshExportSettings.Default);
                }
                var bytes = gltf.ToGlbBytes();
                File.WriteAllBytes(path, bytes);

                if (path.StartsWithUnityAssetPath())
                {
                    AssetDatabase.ImportAsset(path.ToUnityRelativePath());
                    AssetDatabase.Refresh();
                }
            }
            else
            {
                EditorUtility.DisplayDialog("Error", "The Root transform should have Default translation, rotation and scale.", "ok");
            }
        }
Esempio n. 3
0
        private static void ExportFromMenu(bool isGlb, MeshExportSettings settings)
        {
            var go = Selection.activeObject as GameObject;

            var ext = isGlb ? "glb" : "gltf";

            if (go.transform.position == Vector3.zero &&
                go.transform.rotation == Quaternion.identity &&
                go.transform.localScale == Vector3.one)
            {
                var path = EditorUtility.SaveFilePanel(
                    $"Save {ext}", "", go.name + $".{ext}", $"{ext}");
                if (string.IsNullOrEmpty(path))
                {
                    return;
                }

                var gltf = new glTF();
                using (var exporter = new gltfExporter(gltf))
                {
                    exporter.Prepare(go);
                    exporter.Export(settings);
                }

                if (isGlb)
                {
                    var bytes = gltf.ToGlbBytes();
                    File.WriteAllBytes(path, bytes);
                }
                else
                {
                    var(json, buffers) = gltf.ToGltf(path);
                    // without BOM
                    var encoding = new System.Text.UTF8Encoding(false);
                    File.WriteAllText(path, json, encoding);
                    // write to local folder
                    var dir = Path.GetDirectoryName(path);
                    foreach (var b in buffers)
                    {
                        var bufferPath = Path.Combine(dir, b.uri);
                        File.WriteAllBytes(bufferPath, b.GetBytes().ToArray());
                    }
                }

                if (path.StartsWithUnityAssetPath())
                {
                    AssetDatabase.ImportAsset(path.ToUnityRelativePath());
                    AssetDatabase.Refresh();
                }
            }
            else
            {
                EditorUtility.DisplayDialog("Error", "The Root transform should have Default translation, rotation and scale.", "ok");
            }
        }
        static Byte[] Export(GameObject root)
        {
            var gltf = new glTF();

            using (var exporter = new gltfExporter(gltf))
            {
                exporter.Prepare(root);
                exporter.Export(MeshExportSettings.Default, AssetTextureUtil.IsTextureEditorAsset, AssetTextureUtil.GetTextureBytesWithMime);
                return(gltf.ToGlbBytes());
            }
        }
Esempio n. 5
0
        static Byte[] Export(GameObject root)
        {
            var gltf = new glTF();

            using (var exporter = new gltfExporter(gltf, new GltfExportSettings()))
            {
                exporter.Prepare(root);
                exporter.Export(new GltfExportSettings(), new EditorTextureSerializer());
                return(gltf.ToGlbBytes());
            }
        }
Esempio n. 6
0
        protected override void ExportPath(string path)
        {
            var ext   = Path.GetExtension(path).ToLower();
            var isGlb = false;

            switch (ext)
            {
            case ".glb": isGlb = true; break;

            case ".gltf": isGlb = false; break;

            default: throw new System.Exception();
            }

            var gltf = new glTF();

            using (var exporter = new gltfExporter(gltf, m_settings.InverseAxis))
            {
                exporter.Prepare(State.ExportRoot);
                var settings = new MeshExportSettings
                {
                    ExportOnlyBlendShapePosition    = m_settings.DropNormal,
                    UseSparseAccessorForMorphTarget = m_settings.Sparse,
                    DivideVertexBuffer = m_settings.DivideVertexBuffer,
                };
                exporter.Export(settings, new EditorTextureSerializer());
            }

            if (isGlb)
            {
                var bytes = gltf.ToGlbBytes();
                File.WriteAllBytes(path, bytes);
            }
            else
            {
                var(json, buffers) = gltf.ToGltf(path);
                // without BOM
                var encoding = new System.Text.UTF8Encoding(false);
                File.WriteAllText(path, json, encoding);
                // write to local folder
                var dir = Path.GetDirectoryName(path);
                foreach (var b in buffers)
                {
                    var bufferPath = Path.Combine(dir, b.uri);
                    File.WriteAllBytes(bufferPath, b.GetBytes().ToArray());
                }
            }

            if (path.StartsWithUnityAssetPath())
            {
                AssetDatabase.ImportAsset(path.ToUnityRelativePath());
                AssetDatabase.Refresh();
            }
        }
Esempio n. 7
0
        private static void Export(GameObject go, string path, MeshExportSettings settings, Axises inverseAxis)
        {
            var ext   = Path.GetExtension(path).ToLower();
            var isGlb = false;

            switch (ext)
            {
            case ".glb": isGlb = true; break;

            case ".gltf": isGlb = false; break;

            default: throw new System.Exception();
            }

            var gltf = new glTF();

            using (var exporter = new gltfExporter(gltf, inverseAxis))
            {
                exporter.Prepare(go);
                exporter.Export(settings, AssetTextureUtil.IsTextureEditorAsset);
            }


            if (isGlb)
            {
                var bytes = gltf.ToGlbBytes();
                File.WriteAllBytes(path, bytes);
            }
            else
            {
                var(json, buffers) = gltf.ToGltf(path);
                // without BOM
                var encoding = new System.Text.UTF8Encoding(false);
                File.WriteAllText(path, json, encoding);
                // write to local folder
                var dir = Path.GetDirectoryName(path);
                foreach (var b in buffers)
                {
                    var bufferPath = Path.Combine(dir, b.uri);
                    File.WriteAllBytes(bufferPath, b.GetBytes().ToArray());
                }
            }

            if (path.StartsWithUnityAssetPath())
            {
                AssetDatabase.ImportAsset(path.ToUnityRelativePath());
                AssetDatabase.Refresh();
            }
        }
Esempio n. 8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="path"></param>
        /// <param name="settings"></param>
        /// <param name="destroy">作業が終わったらDestoryするべき一時オブジェクト</param>
        static void Export(string path, GameObject exportRoot, VRMMetaObject meta,
                           VRMExportSettings settings, IReadOnlyList <MeshUtility.MeshExportInfo> info,
                           List <GameObject> destroy)
        {
            var target = exportRoot;

            // 常にコピーする。シーンを変化させない
            target = GameObject.Instantiate(target);
            destroy.Add(target);

            var metaBehaviour = target.GetComponent <VRMMeta>();

            if (metaBehaviour == null)
            {
                metaBehaviour      = target.AddComponent <VRMMeta>();
                metaBehaviour.Meta = meta;
            }
            if (metaBehaviour.Meta == null)
            {
                // 来ないはず
                throw new Exception("meta required");
            }

            {
                // copy元
                var animator         = exportRoot.GetComponent <Animator>();
                var beforeTransforms = exportRoot.GetComponentsInChildren <Transform>();
                // copy先
                var afterTransforms = target.GetComponentsInChildren <Transform>();
                // copy先のhumanoidBoneのリストを得る
                var bones           = (HumanBodyBones[])Enum.GetValues(typeof(HumanBodyBones));
                var humanTransforms = bones
                                      .Where(x => x != HumanBodyBones.LastBone)
                                      .Select(x => animator.GetBoneTransform(x))
                                      .Where(x => x != null)
                                      .Select(x => afterTransforms[Array.IndexOf(beforeTransforms, x)]) // copy 先を得る
                                      .ToArray();

                var nameCount = target.GetComponentsInChildren <Transform>()
                                .GroupBy(x => x.name)
                                .ToDictionary(x => x.Key, x => x.Count());
                foreach (var t in target.GetComponentsInChildren <Transform>())
                {
                    if (humanTransforms.Contains(t))
                    {
                        // keep original name
                        continue;
                    }

                    if (nameCount[t.name] > 1)
                    {
                        // 重複するボーン名をリネームする
                        ForceUniqueName(t, nameCount);
                    }
                }
            }

            // 正規化
            if (settings.PoseFreeze)
            {
                // BoneNormalizer.Execute は Copy を作って正規化する。UNDO無用
                target = VRMBoneNormalizer.Execute(target, settings.ForceTPose, false);
                destroy.Add(target);
            }

            // 元のBlendShapeClipに変更を加えないように複製
            var proxy = target.GetComponent <VRMBlendShapeProxy>();

            if (proxy != null)
            {
                var copyBlendShapeAvatar = CopyBlendShapeAvatar(proxy.BlendShapeAvatar, settings.ReduceBlendshapeClip);
                proxy.BlendShapeAvatar = copyBlendShapeAvatar;

                // BlendShape削減
                if (settings.ReduceBlendshape)
                {
                    foreach (SkinnedMeshRenderer smr in target.GetComponentsInChildren <SkinnedMeshRenderer>())
                    {
                        // 未使用のBlendShapeを間引く
                        ReplaceMesh(target, smr, copyBlendShapeAvatar);
                    }
                }
            }

            // 出力
            var sw   = System.Diagnostics.Stopwatch.StartNew();
            var gltf = new UniGLTF.glTF();

            using (var exporter = new VRMExporter(gltf))
            {
                exporter.Prepare(target);
                exporter.Export(settings.MeshExportSettings);
            }
            var bytes = gltf.ToGlbBytes();

            File.WriteAllBytes(path, bytes);
            Debug.LogFormat("Export elapsed {0}", sw.Elapsed);

            if (path.StartsWithUnityAssetPath())
            {
                // 出力ファイルのインポートを発動
                AssetDatabase.ImportAsset(path.ToUnityRelativePath());
            }
        }