Beispiel #1
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);
            }
        }