/// <summary>
        /// Converts this <see cref="SceneBuilder"/> instance into a <see cref="ModelRoot"/> instance.
        /// </summary>
        /// <param name="useStridedBuffers">True to generate strided vertex buffers whenever possible.</param>
        /// <returns>A new <see cref="ModelRoot"/> instance.</returns>
        public ModelRoot ToGltf2(SceneBuilderSchema2Settings settings)
        {
            var context = new Schema2SceneBuilder();

            var dstModel = ModelRoot.CreateModel();

            context.AddGeometryResources(dstModel, new[] { this }, settings);

            var dstScene = dstModel.UseScene(0);

            dstScene.Name = this.Name;

            context.AddScene(dstScene, this);

            dstModel.DefaultScene = dstScene;

            return(dstModel);
        }
        /// <summary>
        /// Convertes a collection of <see cref="SceneBuilder"/> instances to a single <see cref="ModelRoot"/> instance.
        /// </summary>
        /// <param name="srcScenes">A collection of scenes</param>
        /// <param name="useStridedBuffers">True to generate strided vertex buffers whenever possible.</param>
        /// <returns>A new <see cref="ModelRoot"/> instance.</returns>
        public static ModelRoot ToSchema2(IEnumerable <SceneBuilder> srcScenes, SceneBuilderSchema2Settings settings)
        {
            Guard.NotNull(srcScenes, nameof(srcScenes));

            var context = new Schema2SceneBuilder();

            var dstModel = ModelRoot.CreateModel();

            context.AddGeometryResources(dstModel, srcScenes, settings);

            foreach (var srcScene in srcScenes)
            {
                var dstScene = dstModel.UseScene(dstModel.LogicalScenes.Count);

                dstScene.Name = srcScene.Name;

                context.AddScene(dstScene, srcScene);
            }

            return(dstModel);
        }
Пример #3
0
        /// <summary>
        /// Converts a collection of <see cref="SceneBuilder"/> instances to a single <see cref="ModelRoot"/> instance.
        /// </summary>
        /// <param name="srcScenes">A collection of scenes</param>
        /// <param name="settings">Conversion settings.</param>
        /// <returns>A new <see cref="ModelRoot"/> instance.</returns>
        public static ModelRoot ToGltf2(IEnumerable <SceneBuilder> srcScenes, SceneBuilderSchema2Settings settings)
        {
            Guard.NotNull(srcScenes, nameof(srcScenes));

            var context = new Schema2SceneBuilder();

            var dstModel = ModelRoot.CreateModel();

            context.AddGeometryResources(dstModel, srcScenes, settings);

            foreach (var srcScene in srcScenes)
            {
                var dstScene = dstModel.UseScene(dstModel.LogicalScenes.Count);
                srcScene.TryCopyNameAndExtrasTo(dstScene);

                context.AddScene(dstScene, srcScene);
            }

            dstModel.DefaultScene = dstModel.LogicalScenes[0];

            return(dstModel);
        }
Пример #4
0
        public void AddGeometryResources(ModelRoot root, IEnumerable <SceneBuilder> srcScenes, SceneBuilderSchema2Settings settings)
        {
            // gather all unique MeshBuilders

            var srcMeshes = srcScenes
                            .SelectMany(item => item.Instances)
                            .Select(item => item.Content?.GetGeometryAsset())
                            .Where(item => item != null)
                            .Distinct()
                            .ToArray();

            // gather all unique MaterialBuilders

            var materialGroups = srcMeshes
                                 .SelectMany(item => item.Primitives)
                                 .Where(item => !Geometry.MeshBuilderToolkit.IsEmpty(item))
                                 .Select(item => item.Material)
                                 .Distinct()
                                 .ToList()
                                 // group by equal content, to reduce material splitting whenever possible.
                                 .GroupBy(item => item, Materials.MaterialBuilder.ContentComparer);

            // create a Schema2.Material for every MaterialBuilder.

            foreach (var mg in materialGroups)
            {
                var val = root.CreateMaterial(mg.Key);
                foreach (var key in mg)
                {
                    _Materials[key] = val;
                }
            }

            // create a Schema2.Mesh for every MeshBuilder.

            var dstMeshes = root.CreateMeshes(mat => _Materials[mat], settings, srcMeshes);

            for (int i = 0; i < srcMeshes.Length; ++i)
            {
                _Meshes[srcMeshes[i]] = dstMeshes[i];
            }

            // TODO: here we could check that every dstMesh has been correctly created.
        }
Пример #5
0
 /// <summary>
 /// Converts this <see cref="SceneBuilder"/> instance into a <see cref="ModelRoot"/> instance.
 /// </summary>
 /// <param name="settings">Conversion settings.</param>
 /// <returns>A new <see cref="ModelRoot"/> instance.</returns>
 public ModelRoot ToGltf2(SceneBuilderSchema2Settings settings)
 {
     return(ToGltf2(new[] { this }, settings));
 }