Ejemplo n.º 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");
                using (var data = new AutoGltfFileParser(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);
                }
            }
        }
Ejemplo n.º 2
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 AutoGltfFileParser(gltf.FullName).Parse();
            }
            catch (Exception ex)
            {
                Debug.LogError($"ParseError: {gltf}");
                Debug.LogException(ex);
            }

            // should unique
            using (data)
            {
                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));
            }
        }
Ejemplo n.º 3
0
        public static async Task <RuntimeGltfInstance> LoadAsync(string path, IAwaitCaller awaitCaller = null, IMaterialDescriptorGenerator materialGenerator = null)
        {
            if (!File.Exists(path))
            {
                throw new FileNotFoundException(path);
            }

            Debug.LogFormat("{0}", path);
            using (GltfData data = new AutoGltfFileParser(path).Parse())
                using (var loader = new UniGLTF.ImporterContext(data, materialGenerator: materialGenerator))
                {
                    return(await loader.LoadAsync(awaitCaller));
                }
        }
        /// <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, RenderPipelineTypes renderPipeline)
        {
#if VRM_DEVELOP
            Debug.Log("OnImportAsset to " + scriptedImporter.assetPath);
#endif

            //
            // Parse(parse glb, parser gltf json)
            //
            var data = new AutoGltfFileParser(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);

            IMaterialDescriptorGenerator materialGenerator = GetMaterialGenerator(renderPipeline);

            using (var loader = new ImporterContext(data, extractedObjects, materialGenerator: materialGenerator))
            {
                // 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);
            }
        }
Ejemplo n.º 5
0
        static void RuntimeLoadExport(FileInfo gltf, int subStrStart)
        {
            GltfData data = null;

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

            using (data)
                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);
                    }
                }
        }
Ejemplo n.º 6
0
        public static void ImportMenu()
        {
            var path = EditorUtility.OpenFilePanel("open glb", "", "gltf,glb,zip");

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

            if (Application.isPlaying)
            {
                //
                // load into scene
                //
                var data = new AutoGltfFileParser(path).Parse();
                using (var context = new ImporterContext(data))
                {
                    var loaded = context.Load();
                    loaded.ShowMeshes();
                    Selection.activeGameObject = loaded.gameObject;
                }
                return;
            }

            //
            // save as asset
            //
            if (path.StartsWithUnityAssetPath())
            {
                Debug.LogWarningFormat("disallow import from folder under the Assets");
                return;
            }

            var ext       = Path.GetExtension(path).ToLower();
            var assetPath = EditorUtility.SaveFilePanel("save prefab", "Assets", Path.GetFileNameWithoutExtension(path), ext.Substring(1));

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

            // copy
            var bytes = File.ReadAllBytes(path);

            File.WriteAllBytes(assetPath, bytes);
            if (ext == ".gltf")
            {
                // copy associated files
                var src_dir = Path.GetDirectoryName(path);
                var dst_dir = Path.GetDirectoryName(assetPath);
                var data    = new GltfFileWithResourceFilesParser(path, bytes).Parse();
                foreach (var buffer in data.GLTF.buffers)
                {
                    if (!string.IsNullOrEmpty(buffer.uri))
                    {
                        var src_path  = Path.Combine(src_dir, buffer.uri);
                        var src_bytes = File.ReadAllBytes(src_path);
                        var dst_path  = Path.Combine(dst_dir, buffer.uri);
                        File.WriteAllBytes(dst_path, src_bytes);
                        UnityPath.FromFullpath(dst_path).ImportAsset();
                    }
                }
                foreach (var buffer in data.GLTF.images)
                {
                    if (!string.IsNullOrEmpty(buffer.uri))
                    {
                        var src_path  = Path.Combine(src_dir, buffer.uri);
                        var src_bytes = File.ReadAllBytes(src_path);
                        var dst_path  = Path.Combine(dst_dir, buffer.uri);
                        File.WriteAllBytes(dst_path, src_bytes);
                        UnityPath.FromFullpath(dst_path).ImportAsset();
                    }
                }
            }

            // import as asset
            var unitypath = UnityPath.FromFullpath(assetPath);

            unitypath.ImportAsset();
            var asset = unitypath.LoadAsset <GameObject>();

            Selection.activeObject = asset;
        }