Esempio n. 1
0
        public static void Load(ImporterContext ctx)
        {
            // textures
            if (ctx.GLTF.textures != null)
            {
                for (int i = 0; i < ctx.GLTF.textures.Count; ++i)
                {
                    var item = new TextureItem(ctx.GLTF, i, ctx.TextureBaseDir);
                    ctx.AddTexture(item);
                }
            }
            foreach (var x in ctx.GetTextures())
            {
                x.Process(ctx.GLTF, ctx.Storage);
            }

            // materials
            if (ctx.MaterialImporter == null)
            {
                ctx.MaterialImporter = new MaterialImporter(new ShaderStore(ctx), ctx);
            }

            if (ctx.GLTF.materials == null || !ctx.GLTF.materials.Any())
            {
                // no material
                ctx.AddMaterial(ctx.MaterialImporter.CreateMaterial(0, null));
            }
            else
            {
                for (int i = 0; i < ctx.GLTF.materials.Count; ++i)
                {
                    var index    = i;
                    var material = ctx.MaterialImporter.CreateMaterial(index, ctx.GLTF.materials[i]);
                    ctx.AddMaterial(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");
            }

            var meshImporter = new MeshImporter();

            for (int i = 0; i < ctx.GLTF.meshes.Count; ++i)
            {
                var meshContext       = meshImporter.ReadMesh(ctx, i);
                var meshWithMaterials = BuildMesh(ctx, meshContext);

                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);
        }