示例#1
0
        VrmLib.Model ToModel(UnityEngine.GameObject target)
        {
            var exporter = new UniVRM10.RuntimeVrmConverter();
            var model    = exporter.ToModelFrom10(target);

            return(model);
        }
示例#2
0
        private Model ToVrmModel(GameObject root)
        {
            var exporter = new UniVRM10.RuntimeVrmConverter();
            var model    = exporter.ToModelFrom10(root, root.GetComponent <VRMMeta>().Meta);

            model.ConvertCoordinate(VrmLib.Coordinates.Gltf, ignoreVrm: false);
            return(model);
        }
示例#3
0
        public void UnityMaterialTest(string materialName, Type vrmLibMaterialType, bool sameShader = true)
        {
            // asset (cerate copy for avoid modify asset)
            var src = new Material(Resources.Load <Material>(materialName));

            // asset => vrmlib
            var converter      = new UniVRM10.RuntimeVrmConverter();
            var vrmLibMaterial = converter.Export10(src, (a, b, c, d) => null);

            Assert.AreEqual(vrmLibMaterialType, vrmLibMaterial.GetType());

            // vrmlib => gltf
            var textures = new List <VrmLib.Texture>();

            var(gltfMaterial, hasKhrUnlit) = ToProtobufMaterial(vrmLibMaterial, textures);
            if (gltfMaterial.extensions != null)
            {
                gltfMaterial.extensions = gltfMaterial.extensions.Deserialize();
            }
            Assert.AreEqual(hasKhrUnlit, glTF_KHR_materials_unlit.IsEnable(gltfMaterial));

            // gltf => json
            var jsonMaterial = Serialize(gltfMaterial, UniGLTF.GltfSerializer.Serialize_gltf_materials_ITEM);

            // gltf <= json
            var deserialized = UniGLTF.GltfDeserializer.Deserialize_gltf_materials_LIST(jsonMaterial.ParseAsJson());

            // vrmlib <= gltf
            var loaded  = deserialized.FromGltf(textures);
            var context = ModelDiffContext.Create();

            ModelDiffExtensions.MaterialEquals(context, vrmLibMaterial, loaded);
            var diff = context.List
                       .Where(x => !s_ignoreKeys.Contains(x.Context))
                       .ToArray();

            if (diff.Length > 0)
            {
                Debug.LogWarning(string.Join("\n", diff.Select(x => $"{x.Context}: {x.Message}")));
            }
            Assert.AreEqual(0, diff.Length);

            // <= vrmlib
            var map = new Dictionary <VrmLib.Texture, Texture2D>();
            var dst = UniVRM10.RuntimeUnityMaterialBuilder.CreateMaterialAsset(loaded, hasVertexColor: false, map);

            dst.name = src.name;

            if (sameShader)
            {
                CompareUnityMaterial(src, dst);
            }
        }
        public void UnityMaterialTest(string materialName, Type vrmLibMaterialType, bool sameShader = true)
        {
            // cerate copy avoid modify asset
            var src = new Material(Resources.Load <Material>(materialName));

            // => vrmlib
            var converter      = new UniVRM10.RuntimeVrmConverter();
            var vrmLibMaterial = converter.Export10(src, (a, b, c, d) => null);

            Assert.AreEqual(vrmLibMaterialType, vrmLibMaterial.GetType());

            // => protobuf
            var textures = new List <VrmLib.Texture>();

            var(protobufMaterial, hasKhrUnlit) = ToProtobufMaterial(vrmLibMaterial, textures);
            Assert.AreEqual(hasKhrUnlit, protobufMaterial.Extensions?.KHRMaterialsUnlit != null);

            // => json
            var settings     = Google.Protobuf.JsonFormatter.Settings.Default.WithPreserveProtoFieldNames(true);
            var jsonMaterial = new Google.Protobuf.JsonFormatter(settings).Format(protobufMaterial);

            // <= json
            var parserSettings = Google.Protobuf.JsonParser.Settings.Default;
            var deserialized   = new Google.Protobuf.JsonParser(parserSettings).Parse <VrmProtobuf.Material>(jsonMaterial);

            // <= protobuf
            var loaded  = deserialized.FromGltf(textures);
            var context = ModelDiffContext.Create();

            ModelDiffExtensions.MaterialEquals(context, vrmLibMaterial, loaded);
            var diff = context.List
                       .Where(x => !s_ignoreKeys.Contains(x.Context))
                       .ToArray();

            if (diff.Length > 0)
            {
                Debug.LogWarning(string.Join("\n", diff.Select(x => $"{x.Context}: {x.Message}")));
            }
            Assert.AreEqual(0, diff.Length);

            // <= vrmlib
            var map = new Dictionary <VrmLib.Texture, Texture2D>();
            var dst = UniVRM10.RuntimeUnityMaterialBuilder.CreateMaterialAsset(loaded, hasVertexColor: false, map);

            dst.name = src.name;

            if (sameShader)
            {
                CompareUnityMaterial(src, dst);
            }
        }
示例#5
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;
            }
        }
        private void OnExportClicked()
        {
            var rootObject = m_gameObjectField.value as GameObject;

            // save dialog
            var path = EditorUtility.SaveFilePanel(
                "Save glb",
                null,
                rootObject.name + EXTENSION,
                EXTENSION.Substring(1));

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

            try
            {
                var exporter = new UniVRM10.RuntimeVrmConverter();
                var model    = exporter.ToGlbModel(rootObject);

                // 右手系に変換
                model.ConvertCoordinate(VrmLib.Coordinates.Gltf, ignoreVrm: false);
                var exportedBytes = GetGlb(model);

                File.WriteAllBytes(path, exportedBytes);
                Debug.Log("exportedBytes: " + exportedBytes.Length);

                var assetPath = ToAssetPath(path);
                if (!string.IsNullOrEmpty(assetPath))
                {
                    AssetDatabase.ImportAsset(assetPath);
                }
            }
            catch (Exception ex)
            {
                // rethrow
                throw;
            }
        }
        void OnExportClicked()
        {
            m_logLabel.text = "";
            var rootObject = m_gameObjectField.value as GameObject;

            if (!ValidateRoot(rootObject))
            {
                m_logLabel.text += "export aborted";
                return;
            }

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

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

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

            try
            {
                var exporter = new UniVRM10.RuntimeVrmConverter();
                var meta     = m_meta.GetVRMMetaObject();
                var model    = exporter.ToModelFrom10(rootObject, meta);

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

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

                m_logLabel.text += $"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.text += ex.ToString();
                // rethrow
                throw;
            }
        }
示例#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 exporter = new UniVRM10.RuntimeVrmConverter();
                var model    = exporter.ToModelFrom10(root, Meta ? Meta : m_tmpMeta);

                // 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.Gltf, ignoreVrm: false);

                var exportedBytes = GetGlb(model);
                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;
            }
        }