Exemplo n.º 1
0
        public static void Load(ImporterContext ctx)
        {
            // textures
            if (ctx.GLTF.textures != null)
            {
                ctx.Textures.AddRange(ctx.GLTF.textures.Select((x, i) => new TextureItem(ctx.GLTF, i, ctx.TextureBaseDir)));
            }
            foreach (var x in ctx.Textures)
            {
                x.Process(ctx.GLTF, ctx.Storage);
            }

            // materials
            if (ctx.CreateMaterial == null)
            {
                ctx.CreateMaterial = MaterialIO.CreateMaterialFuncFromShader(new ShaderStore());
            }

            if (ctx.GLTF.materials == null || !ctx.GLTF.materials.Any())
            {
                // no material
                ctx.Materials.Add(ctx.CreateMaterial(ctx, 0));
            }
            else
            {
                for (int i = 0; i < ctx.GLTF.materials.Count; ++i)
                {
                    var index    = i;
                    var material = ctx.CreateMaterial(ctx, index);

                    var originalName = material.name;
                    for (int j = 1; ctx.Materials.Any(x => x.name == material.name); ++j)
                    {
                        material.name = string.Format("{0}({1})", originalName, j);
                    }
                    ctx.Materials.Add(material);
                }
            }

            // meshes
            if (ctx.GLTF.meshes
                .SelectMany(x => x.primitives)
                .Any(x => x.extensions.KHR_draco_mesh_compression != null))
            {
                throw new UniGLTFNotSupportedException("draco is not supported");
            }

            for (int i = 0; i < ctx.GLTF.meshes.Count; ++i)
            {
                var meshWithMaterials = ImportMesh(ctx, i);

                var mesh = meshWithMaterials.Mesh;

                // mesh name
                if (string.IsNullOrEmpty(mesh.name))
                {
                    mesh.name = string.Format("UniGLTF import#{0}", i);
                }
                var originalName = mesh.name;
                for (int j = 1; ctx.Meshes.Any(x => x.Mesh.name == mesh.name); ++j)
                {
                    mesh.name = string.Format("{0}({1})", originalName, j);
                }

                ctx.Meshes.Add(meshWithMaterials);
            }

            // nodes
            ctx.Nodes.AddRange(ctx.GLTF.nodes.Select(x => ImportNode(x).transform));

            var nodes = ctx.Nodes.Select((x, i) => BuildHierarchy(ctx, i)).ToList();

            gltfImporter.FixCoordinate(ctx, nodes);

            // skinning
            for (int i = 0; i < nodes.Count; ++i)
            {
                gltfImporter.SetupSkinning(ctx, nodes, i);
            }

            // connect root
            ctx.Root = new GameObject("_root_");
            foreach (var x in ctx.GLTF.rootnodes)
            {
                var t = nodes[x].Transform;
                t.SetParent(ctx.Root.transform, false);
            }

            ImportAnimation(ctx);

            //Debug.LogFormat("Import {0}", ctx.Path);
        }
Exemplo n.º 2
0
        public static void Import <T>(ImporterContext ctx, ArraySegment <Byte> glbBinChunk) where T : glTF
        {
            // exclude not gltf-2.0
            var parsed = ctx.Json.ParseAsJson();

            try
            {
                if (parsed["asset"]["version"].GetString() != "2.0")
                {
                    throw new UniGLTFException("is not gltf-2.0: {0}", ctx.Path);
                }
            }
            catch (Exception)
            {
                throw new UniGLTFException("{0}: fail to parse json", ctx.Path);
            }

            // parse json
            try
            {
                ctx.GLTF = JsonUtility.FromJson <T>(ctx.Json);
            }
            catch (Exception)
            {
                throw new UniGLTFException("{0}: fail to parse json", ctx.Path);
            }
            if (ctx.GLTF == null)
            {
                throw new UniGLTFException("{0}: fail to parse json", ctx.Path);
            }

            if (ctx.GLTF.asset.version != "2.0")
            {
                throw new UniGLTFException("unknown gltf version {0}", ctx.GLTF.asset.version);
            }

            // parepare byte buffer
            ctx.GLTF.baseDir = Path.GetDirectoryName(ctx.Path);
            foreach (var buffer in ctx.GLTF.buffers)
            {
                buffer.OpenStorage(ctx.GLTF.baseDir, glbBinChunk);
            }

            // textures
            ctx.Textures.AddRange(ImportTextures(ctx.GLTF)
                                  .Select(x =>
            {
                var samplerIndex = ctx.GLTF.textures[x.TextureIndex].sampler;
                var sampler      = ctx.GLTF.samplers[samplerIndex];

                if (x.Texture == null)
                {
                    Debug.LogWarningFormat("May be import order, not yet texture is not imported. Later, manualy reimport {0}", ctx.Path);
                }
                else
                {
                    SetSampler(x.Texture, sampler);
                }
                return(x);
            }));

            // materials
            if (ctx.CreateMaterial == null)
            {
                ctx.CreateMaterial = CreateMaterialFuncFromShader(Shader.Find("Standard"));
            }
            if (ctx.GLTF.materials == null || !ctx.GLTF.materials.Any())
            {
                ctx.Materials.Add(ctx.CreateMaterial(ctx, 0));
            }
            else
            {
                for (int i = 0; i < ctx.GLTF.materials.Count; ++i)
                {
                    ctx.Materials.Add(ctx.CreateMaterial(ctx, i));
                }
            }

            // meshes
            if (ctx.GLTF.meshes
                .SelectMany(x => x.primitives)
                .Any(x => x.extensions.KHR_draco_mesh_compression != null))
            {
                throw new UniGLTFException("draco is not supported");
            }

            ctx.Meshes.AddRange(ctx.GLTF.meshes.Select((x, i) =>
            {
                var meshWithMaterials = ImportMesh(ctx, i);
                var mesh = meshWithMaterials.Mesh;
                if (string.IsNullOrEmpty(mesh.name))
                {
                    mesh.name = string.Format("UniGLTF import#{0}", i);
                }
                return(meshWithMaterials);
            }));

            // nodes
            ctx.Nodes.AddRange(ctx.GLTF.nodes.Select(x => ImportNode(x).transform));

            var nodes = ctx.Nodes.Select((x, i) => BuildHierarchy(ctx, i)).ToList();

            gltfImporter.FixCoordinate(ctx, nodes);

            // skinning
            for (int i = 0; i < nodes.Count; ++i)
            {
                gltfImporter.SetupSkinning(ctx, nodes, i);
            }

            // connect root
            ctx.Root = new GameObject("_root_");
            foreach (var x in ctx.GLTF.rootnodes)
            {
                var t = nodes[x].Transform;
                t.SetParent(ctx.Root.transform, false);
            }

            ImportAnimation(ctx);

            //Debug.LogFormat("Import {0}", ctx.Path);
        }