Ejemplo n.º 1
0
        static byte[] GetGlb(VrmLib.Model model)
        {
            // export vrm-1.0
            var exporter = new Vrm10.Vrm10Exporter();
            var option   = new VrmLib.ExportArgs
            {
                vrm = false
            };
            var glbBytes10 = exporter.Export(model, option);
            var glb10      = VrmLib.Glb.Parse(glbBytes10);

            return(glb10.ToBytes());
        }
Ejemplo n.º 2
0
        public static byte[] ToGlb(this VrmLib.Model model)
        {
            // export vrm-1.0
            var exporter10 = new Vrm10Exporter();
            var option     = new VrmLib.ExportArgs
            {
                // vrm = false
            };
            var glbBytes10 = exporter10.Export(model, option);
            var glb10      = UniGLTF.Glb.Parse(glbBytes10);

            return(glb10.ToBytes());
        }
Ejemplo n.º 3
0
    public static string GetJsonString(VrmLib.Model model)
    {
        // export vrm-1.0
        var exporter10 = new Vrm10.Vrm10Exporter();
        var option     = new VrmLib.ExportArgs
        {
            // vrm = false
        };
        var glbBytes10 = exporter10.Export(model, option);
        var glb10      = VrmLib.Glb.Parse(glbBytes10);

        return(System.Text.Encoding.UTF8.GetString(glb10.Json.Bytes.Array, glb10.Json.Bytes.Offset, glb10.Json.Bytes.Count));
    }
Ejemplo n.º 4
0
        protected override void ExportPath(string path)
        {
            m_logLabel = "";

            m_logLabel += $"export...\n";

            var root = State.ExportRoot;

            try
            {
                var converter = new UniVRM10.RuntimeVrmConverter();
                var model     = converter.ToModelFrom10(root);

                if (HumanoidValidator.HasRotationOrScale(root))
                {
                    // 正規化
                    m_logLabel += $"normalize...\n";
                    var modifier = new ModelModifier(model);
                    modifier.SkinningBake();
                }

                // 右手系に変換
                m_logLabel += $"convert to right handed coordinate...\n";
                model.ConvertCoordinate(VrmLib.Coordinates.Vrm1, ignoreVrm: false);

                // export vrm-1.0
                var exporter = new UniVRM10.Vrm10Exporter(AssetTextureUtil.IsTextureEditorAsset);
                var option   = new VrmLib.ExportArgs();
                exporter.Export(root, model, converter, option, AssetTextureUtil.GetTextureBytesWithMime, Meta ? Meta : m_tmpMeta);

                var exportedBytes = exporter.Storage.ToBytes();

                m_logLabel += $"write to {path}...\n";
                File.WriteAllBytes(path, exportedBytes);
                Debug.Log("exportedBytes: " + exportedBytes.Length);

                var assetPath = UniGLTF.UnityPath.FromFullpath(path);
                if (assetPath.IsUnderAssetsFolder)
                {
                    // asset folder 内。import を発動
                    assetPath.ImportAsset();
                }
            }
            catch (Exception ex)
            {
                m_logLabel += ex.ToString();
                // rethrow
                throw;
            }
        }
Ejemplo n.º 5
0
        public static byte[] ToGlb(this VrmLib.Model model)
        {
            // export vrm-1.0
            var exporter10 = new Vrm10Exporter(_ => false);
            var option     = new VrmLib.ExportArgs
            {
                // vrm = false
            };

            exporter10.Export(null, model, null, option);
            var glb10 = UniGLTF.Glb.Parse(exporter10.Storage.ToBytes());

            return(glb10.ToBytes());
        }
Ejemplo n.º 6
0
        protected override void ExportPath(string path)
        {
            m_logLabel = "";

            m_logLabel += $"export...\n";

            var root = State.ExportRoot;

            try
            {
                using (var arrayManager = new NativeArrayManager())
                {
                    var converter = new UniVRM10.ModelExporter();
                    var model     = converter.Export(arrayManager, root);

                    // 右手系に変換
                    m_logLabel += $"convert to right handed coordinate...\n";
                    model.ConvertCoordinate(VrmLib.Coordinates.Vrm1, ignoreVrm: false);

                    // export vrm-1.0
                    var exporter = new UniVRM10.Vrm10Exporter(new EditorTextureSerializer(), new GltfExportSettings());
                    var option   = new VrmLib.ExportArgs();
                    exporter.Export(root, model, converter, option, Vrm ? Vrm.Meta : m_tmpObject.Meta);

                    var exportedBytes = exporter.Storage.ToGlbBytes();

                    m_logLabel += $"write to {path}...\n";
                    File.WriteAllBytes(path, exportedBytes);
                    Debug.Log("exportedBytes: " + exportedBytes.Length);

                    var assetPath = UniGLTF.UnityPath.FromFullpath(path);
                    if (assetPath.IsUnderAssetsFolder)
                    {
                        // asset folder 内。import を発動
                        assetPath.ImportAsset();
                    }
                }
            }
            catch (Exception ex)
            {
                m_logLabel += ex.ToString();
                // rethrow
                throw;
            }
        }
Ejemplo n.º 7
0
        public static byte[] Export(this IVrmExporter exporter, Model m, ExportArgs option)
        {
            exporter.ExportAsset(m);

            ///
            /// 必要な容量を先に確保
            /// (sparseは考慮してないので大きめ)
            ///
            {
                var reserveBytes = 0;
                // image
                foreach (var image in m.Images)
                {
                    reserveBytes += image.Bytes.Count;
                }
                // mesh
                foreach (var g in m.MeshGroups)
                {
                    foreach (var mesh in g.Meshes)
                    {
                        // 頂点バッファ
                        reserveBytes += mesh.IndexBuffer.ByteLength;
                        foreach (var kv in mesh.VertexBuffer)
                        {
                            reserveBytes += kv.Value.ByteLength;
                        }
                        // morph
                        foreach (var morph in mesh.MorphTargets)
                        {
                            foreach (var kv in morph.VertexBuffer)
                            {
                                reserveBytes += kv.Value.ByteLength;
                            }
                        }
                    }
                }
                exporter.Reserve(reserveBytes);
            }

            exporter.ExportImageAndTextures(m.Images, m.Textures);

            // material
            foreach (var src in m.Materials)
            {
                if (src is MToonMaterial mtoon)
                {
                    exporter.ExportMaterialMToon(src, mtoon, m.Textures);
                }
                else if (src is UnlitMaterial unlit)
                {
                    exporter.ExportMaterialUnlit(src, unlit, m.Textures);
                }
                else if (src is PBRMaterial pbr)
                {
                    exporter.ExportMaterialPBR(src, pbr, m.Textures);
                }
                else
                {
                    throw new NotImplementedException();
                }
            }

            // mesh
            exporter.ExportMeshes(m.MeshGroups, m.Materials, option);

            // node
            exporter.ExportNodes(m.Root, m.Nodes, m.MeshGroups, option);

            // animation
            exporter.ExportAnimations(m.Animations, m.Nodes, option);

            if (option.vrm)
            {
                ExportVrm(exporter, m);
            }

            return(exporter.ToBytes());
        }
Ejemplo n.º 8
0
        void OnExportClicked(GameObject root)
        {
            m_logLabel = "";

            string directory;

            if (string.IsNullOrEmpty(m_lastExportDir))
            {
                directory = Directory.GetParent(Application.dataPath).ToString();
            }
            else
            {
                directory = m_lastExportDir;
            }

            // save dialog
            var path = EditorUtility.SaveFilePanel(
                "Save vrm",
                directory,
                root.name + EXTENSION,
                EXTENSION.Substring(1));

            if (string.IsNullOrEmpty(path))
            {
                return;
            }
            m_lastExportDir = Path.GetDirectoryName(path).Replace("\\", "/");

            m_logLabel += $"export...\n";

            try
            {
                var converter = new UniVRM10.RuntimeVrmConverter();
                var model     = converter.ToModelFrom10(root);

                // if (MeshUtility.Validators.HumanoidValidator.HasRotationOrScale(root))
                // {
                //     // 正規化
                //     m_logLabel += $"normalize...\n";
                //     var modifier = new ModelModifier(model);
                //     modifier.SkinningBake();
                // }

                // 右手系に変換
                m_logLabel += $"convert to right handed coordinate...\n";
                model.ConvertCoordinate(VrmLib.Coordinates.Vrm1, ignoreVrm: false);

                // export vrm-1.0
                var exporter = new UniVRM10.Vrm10Exporter(AssetTextureUtil.IsTextureEditorAsset);
                var option   = new VrmLib.ExportArgs();
                exporter.Export(root, model, converter, option, Meta ? Meta : m_tmpMeta);

                var exportedBytes = exporter.Storage.ToBytes();

                m_logLabel += $"write to {path}...\n";
                File.WriteAllBytes(path, exportedBytes);
                Debug.Log("exportedBytes: " + exportedBytes.Length);

                var assetPath = ToAssetPath(path);
                if (!string.IsNullOrEmpty(assetPath))
                {
                    AssetDatabase.ImportAsset(assetPath);
                }
            }
            catch (Exception ex)
            {
                m_logLabel += ex.ToString();
                // rethrow
                throw;
            }
        }