Example #1
0
        public void GltfSampleModelsTest_DamagedHelmet()
        {
            var env = System.Environment.GetEnvironmentVariable("GLTF_SAMPLE_MODELS");

            if (string.IsNullOrEmpty(env))
            {
                return;
            }
            var root = new DirectoryInfo($"{env}/2.0");

            if (!root.Exists)
            {
                return;
            }

            {
                var path = Path.Combine(root.FullName, "DamagedHelmet/glTF-Binary/DamagedHelmet.glb");
                var data = new AmbiguousGltfFileParser(path).Parse();

                var matDesc = new GltfMaterialDescriptorGenerator().Get(data, 0);
                Assert.AreEqual("Standard", matDesc.ShaderName);
                Assert.AreEqual(5, matDesc.TextureSlots.Count);
                var(key, value) = matDesc.EnumerateSubAssetKeyValue().First();
                Assert.AreEqual(new SubAssetKey(typeof(Texture2D), "texture_0"), key);
            }
        }
        /// <summary>
        /// glb をパースして、UnityObject化、さらにAsset化する
        /// </summary>
        /// <param name="scriptedImporter"></param>
        /// <param name="context"></param>
        /// <param name="reverseAxis"></param>
        protected static void Import(ScriptedImporter scriptedImporter, AssetImportContext context, Axes reverseAxis)
        {
#if VRM_DEVELOP
            Debug.Log("OnImportAsset to " + scriptedImporter.assetPath);
#endif

            //
            // Parse(parse glb, parser gltf json)
            //
            var data = new AmbiguousGltfFileParser(scriptedImporter.assetPath).Parse();


            //
            // Import(create unity objects)
            //

            // 2 回目以降の Asset Import において、 Importer の設定で Extract した UnityEngine.Object が入る
            var extractedObjects = scriptedImporter.GetExternalObjectMap()
                                   .Where(x => x.Value != null)
                                   .ToDictionary(kv => new SubAssetKey(kv.Value.GetType(), kv.Key.name), kv => kv.Value);

            using (var loader = new ImporterContext(data, extractedObjects))
            {
                // Configure TextureImporter to Extracted Textures.
                foreach (var textureInfo in loader.TextureDescriptorGenerator.Get().GetEnumerable())
                {
                    TextureImporterConfigurator.Configure(textureInfo, loader.TextureFactory.ExternalTextures);
                }

                loader.InvertAxis = reverseAxis;
                var loaded = loader.Load();
                loaded.ShowMeshes();

                loaded.TransferOwnership((k, o) =>
                {
                    context.AddObjectToAsset(k.Name, o);
                });
                var root = loaded.Root;
                GameObject.DestroyImmediate(loaded);

                context.AddObjectToAsset(root.name, root);
                context.SetMainObject(root);
            }
        }
Example #3
0
        static void RuntimeLoadExport(FileInfo gltf, int subStrStart)
        {
            GltfData data = null;

            try
            {
                data = new AmbiguousGltfFileParser(gltf.FullName).Parse();
            }
            catch (Exception ex)
            {
                Debug.LogError($"ParseError: {gltf}");
                Debug.LogException(ex);
            }

            using (var loader = new ImporterContext(data))
            {
                try
                {
                    var loaded = loader.Load();
                    if (loaded == null)
                    {
                        Debug.LogWarning($"root is null: ${gltf}");
                        return;
                    }

                    if (Skip.Contains(gltf.Directory.Parent.Name))
                    {
                        // Export issue:
                        // skip
                        return;
                    }

                    Export(loaded.gameObject);
                }
                catch (Exception ex)
                {
                    Message(gltf.FullName.Substring(subStrStart), ex);
                }
            }
        }
Example #4
0
        /// <summary>
        /// Extract をテスト
        /// </summary>
        /// <param name="gltf"></param>
        /// <param name="root"></param>
        static void EditorLoad(FileInfo gltf, int subStrStart)
        {
            GltfData data = null;

            try
            {
                data = new AmbiguousGltfFileParser(gltf.FullName).Parse();
            }
            catch (Exception ex)
            {
                Debug.LogError($"ParseError: {gltf}");
                Debug.LogException(ex);
            }

            // should unique
            var gltfTextures = new GltfTextureDescriptorGenerator(data).Get().GetEnumerable()
                               .Select(x => x.SubAssetKey)
                               .ToArray();
            var distinct = gltfTextures.Distinct().ToArray();

            Assert.True(gltfTextures.Length == distinct.Length);
            Assert.True(gltfTextures.SequenceEqual(distinct));
        }