Beispiel #1
0
        public static void FixTextureSampler(this Schema2.ModelRoot root)
        {
            // SharpGLTF 1.0.0-Alpha10 has an issue with TextureSamplers, it's fixed in newer versions

            foreach (var t in root.LogicalTextures)
            {
                if (t.Sampler == null)
                {
                    var sampler = root.UseTextureSampler
                                  (
                        Schema2.TextureWrapMode.REPEAT,
                        Schema2.TextureWrapMode.REPEAT,
                        Schema2.TextureMipMapFilter.DEFAULT,
                        Schema2.TextureInterpolationFilter.LINEAR
                                  );

                    t.Sampler = sampler;
                }
            }
        }
Beispiel #2
0
        public void ZipRoundtripTest()
        {
            // create a model

            var mesh = new MeshBuilder <Geometry.VertexTypes.VertexPositionNormal, Geometry.VertexTypes.VertexEmpty, Geometry.VertexTypes.VertexEmpty>("SphereMesh");

            mesh.AddSphere(Materials.MaterialBuilder.CreateDefault(), 50, System.Numerics.Matrix4x4.Identity);

            var scene = new SceneBuilder();

            scene.AddRigidMesh(mesh, System.Numerics.Matrix4x4.Identity).WithName("Sphere");

            Schema2.ModelRoot model = scene.ToGltf2();

            Assert.AreEqual("SphereMesh", model.LogicalMeshes[0].Name);
            Assert.AreEqual("Sphere", model.LogicalNodes[0].Name);

            model = _ZipRoundtrip(model);

            Assert.AreEqual("SphereMesh", model.LogicalMeshes[0].Name);
            Assert.AreEqual("Sphere", model.LogicalNodes[0].Name);
        }
Beispiel #3
0
        public void AddModel(Schema2.ModelRoot model)
        {
            foreach (var triangle in Schema2.Schema2Toolkit.Triangulate <POSITION, TEXCOORD, VEMPTY>(model.DefaultScene))
            {
                var dstMaterial = new Material();

                var srcMaterial = triangle.Item4;
                if (srcMaterial != null)
                {
                    var baseColor = srcMaterial.FindChannel("BaseColor");

                    var clr = new Vector3(baseColor.Factor.X, baseColor.Factor.Y, baseColor.Factor.Z);

                    // https://stackoverflow.com/questions/36510170/how-to-calculate-specular-contribution-in-pbr
                    dstMaterial.DiffuseColor  = clr;
                    dstMaterial.SpecularColor = new Vector3(0.2f);

                    dstMaterial.DiffuseTexture = baseColor.Image?.GetImageContent() ?? default;
                }

                this.AddTriangle(dstMaterial, triangle.Item1, triangle.Item2, triangle.Item3);
            }
        }
Beispiel #4
0
        public static MonoGameDeviceContent <MonoGameModelTemplate> CreateDeviceModel(GraphicsDevice device, Schema2.ModelRoot srcModel)
        {
            srcModel.FixTextureSampler();

            var templates = srcModel.LogicalScenes
                            .Select(item => SceneTemplate.Create(item, true))
                            .ToArray();

            var context = new LoaderContext(device);

            var meshes = templates
                         .SelectMany(item => item.LogicalMeshIds)
                         .ToDictionary(k => k, k => context.CreateMesh(srcModel.LogicalMeshes[k]));

            var mdl = new MonoGameModelTemplate(templates, srcModel.DefaultScene.LogicalIndex, meshes);

            return(new MonoGameDeviceContent <MonoGameModelTemplate>(mdl, context.Disposables.ToArray()));
        }
        public static MonoGameDeviceContent <MonoGameModelTemplate> CreateDeviceModel(GraphicsDevice device, Schema2.ModelRoot srcModel, LoaderContext context = null)
        {
            if (context == null)
            {
                context = new BasicEffectsLoaderContext(device);
            }

            context.Reset();

            var options = new Runtime.RuntimeOptions {
                IsolateMemory = true
            };

            var templates = srcModel.LogicalScenes
                            .Select(item => SceneTemplate.Create(item, options))
                            .ToArray();

            var srcMeshes = templates
                            .SelectMany(item => item.LogicalMeshIds)
                            .Distinct()
                            .Select(idx => srcModel.LogicalMeshes[idx]);

            foreach (var srcMesh in srcMeshes)
            {
                context._WriteMesh(srcMesh);
            }

            var dstMeshes = context.CreateRuntimeModels();

            var mdl = new MonoGameModelTemplate(templates, srcModel.DefaultScene.LogicalIndex, dstMeshes);

            return(new MonoGameDeviceContent <MonoGameModelTemplate>(mdl, context.Disposables.ToArray()));
        }
 public ValidationResult(Schema2.ModelRoot root, ValidationMode mode, bool instantThrow = false)
 {
     _Root         = root;
     _Mode         = mode;
     _InstantThrow = instantThrow;
 }
Beispiel #7
0
 public void SetModelError(Schema2.ModelRoot model, System.ArgumentException ex)
 {
     SetError(new ModelException(model, ex));
 }
Beispiel #8
0
 public void SetSchemaError(Schema2.ModelRoot model, System.Text.Json.JsonException ex)
 {
     SetError(new SchemaException(model, ex));
 }
Beispiel #9
0
 public void SetSchemaError(Schema2.ModelRoot model, string error)
 {
     SetError(new SchemaException(model, error));
 }