public void CreateSceneWithMeshBuilder() { TestContext.CurrentContext.AttachShowDirLink(); TestContext.CurrentContext.AttachGltfValidatorLinks(); // create a material var material1 = new MaterialBuilder("material1") .WithChannelParam(KnownChannel.BaseColor, Vector4.One); // create model var meshBuilder = new MeshBuilder <VPOSNRM>("mesh1"); meshBuilder.VertexPreprocessor.SetDebugPreprocessors(); // define 4 vertices var v1 = new VPOSNRM(-10, 10, 0, 0, 0, 1); var v2 = new VPOSNRM(10, 10, 0, 0, 0, 1); var v3 = new VPOSNRM(10, -10, 0, 0, 0, 1); var v4 = new VPOSNRM(-10, -10, 0, 0, 0, 1); // add a polygon to the primitive that uses material1 as key. meshBuilder.UsePrimitive(material1).AddQuadrangle(v1, v2, v3, v4); // create a scene var scene = new SceneBuilder(); scene.AddRigidMesh(meshBuilder, Matrix4x4.Identity); scene.AttachToCurrentTest("result.glb"); scene.AttachToCurrentTest("result.gltf"); }
public void CreateSceneWithInterleavedMeshBuilder() { TestContext.CurrentContext.AttachShowDirLink(); TestContext.CurrentContext.AttachGltfValidatorLinks(); // define 4 vertices var v1 = new VPOSNRM(-10, 10, 0, 0, 0, 1); var v2 = new VPOSNRM(10, 10, 0, 0, 0, 1); var v3 = new VPOSNRM(10, -10, 0, 0, 0, 1); var v4 = new VPOSNRM(-10, -10, 0, 0, 0, 1); // create a material var material1 = new MaterialBuilder("material1").WithChannelParam(KnownChannel.BaseColor, Vector4.One); // create model var meshBuilder = new MeshBuilder <VPOSNRM>("mesh1"); meshBuilder.VertexPreprocessor.SetDebugPreprocessors(); // add a polygon to the primitive that uses material1 as key. meshBuilder.UsePrimitive(material1).AddQuadrangle(v1, v2, v3, v4); // create a gltf scene var model = ModelRoot.CreateModel(); model.UseScene("Default") .CreateNode("RootNode") .WithMesh(model.CreateMesh(meshBuilder)); model.AttachToCurrentTest("result.glb"); model.AttachToCurrentTest("result.gltf"); }
public void CreateSceneWithInterleavedQuadMesh() { TestContext.CurrentContext.AttachShowDirLink(); TestContext.CurrentContext.AttachGltfValidatorLinks(); var vertices = new[] { VPOSNRM.Create(new Vector3(-10, 10, 0), Vector3.UnitZ), VPOSNRM.Create(new Vector3(10, 10, 0), Vector3.UnitZ), VPOSNRM.Create(new Vector3(10, -10, 0), Vector3.UnitZ), VPOSNRM.Create(new Vector3(-10, -10, 0), Vector3.UnitZ) }; var model = ModelRoot.CreateModel(); var mesh = model.CreateMesh("mesh1"); mesh.CreatePrimitive() .WithMaterial(model.CreateMaterial("Default").WithDefault(Vector4.One).WithDoubleSide(true)) .WithVertexAccessors(vertices) .WithIndicesAccessor(PrimitiveType.TRIANGLES, new int[] { 0, 1, 2, 0, 2, 3 }); var scene = model.UseScene("Default"); var rnode = scene.CreateNode("RootNode").WithMesh(mesh); model.AttachToCurrentTest("result.glb"); model.AttachToCurrentTest("result.gltf"); }
public static byte[] ToGlb(this FeatureCollection fc, string classifyField, double longitude, double latitude, Color fromColor, Color toColor, int numberOfClasses, int Maximum) { var colors = ColorRange.GetGradients(fromColor, toColor, numberOfClasses).ToList(); var center_coordinate = new GeoCoordinate(latitude, longitude);; var mesh = VPOSNRM.CreateCompatibleMesh("shape"); foreach (var f in fc.Features) { var z = Convert.ToDouble(f.Properties["z"]); var amount = Convert.ToInt32(f.Properties[classifyField]); var point = (GeoJSON.Net.Geometry.Point)f.Geometry; var coords = point.Coordinates; var distance_latitude = new GeoCoordinate(coords.Latitude, longitude).GetDistanceTo(center_coordinate); distance_latitude = coords.Latitude < center_coordinate.Latitude ? distance_latitude * -1 : distance_latitude; var distance_longitude = new GeoCoordinate(latitude, coords.Longitude).GetDistanceTo(center_coordinate); distance_longitude = coords.Longitude < center_coordinate.Longitude ? distance_longitude * -1 : distance_longitude; var color_index = (int)Math.Round((double)(amount * colors.Count / Maximum)); var rgb = colors[color_index]; var material = MaterialCreator.CreateMaterial(rgb.R, rgb.G, rgb.B); var translate = Matrix4x4.CreateTranslation((float)distance_longitude, (float)z * 2, (float)distance_latitude); mesh.AddCube(material, translate); } var scene = new SceneBuilder(); scene.AddMesh(mesh, Matrix4x4.Identity); var glb = scene.ToSchema2().WriteGLB(); var bytes = glb.ToArray(); return(bytes); }
private static void _AddSphereTriangle <TMaterial>(this MeshBuilder <TMaterial, VPOSNRM, VEMPTY, VEMPTY> meshBuilder, TMaterial material, Matrix4x4 xform, Vector3 a, Vector3 b, Vector3 c, int iterations = 0) { if (iterations <= 0) { var aa = new VPOSNRM(Vector3.Transform(a, xform), Vector3.Normalize(Vector3.TransformNormal(a, xform))); var bb = new VPOSNRM(Vector3.Transform(b, xform), Vector3.Normalize(Vector3.TransformNormal(b, xform))); var cc = new VPOSNRM(Vector3.Transform(c, xform), Vector3.Normalize(Vector3.TransformNormal(c, xform))); meshBuilder.UsePrimitive(material).AddTriangle(aa, bb, cc); return; } --iterations; var ab = Vector3.Normalize(a + b) * a.Length(); var bc = Vector3.Normalize(b + c) * b.Length(); var ca = Vector3.Normalize(c + a) * c.Length(); // central triangle _AddSphereTriangle(meshBuilder, material, xform, ab, bc, ca, iterations); // vertex triangles _AddSphereTriangle(meshBuilder, material, xform, a, ab, ca, iterations); _AddSphereTriangle(meshBuilder, material, xform, b, bc, ab, iterations); _AddSphereTriangle(meshBuilder, material, xform, c, ca, bc, iterations); }
public void CreateSceneWithRandomShapes() { TestContext.CurrentContext.AttachShowDirLink(); TestContext.CurrentContext.AttachGltfValidatorLinks(); var rnd = new Random(177); // create materials var materials = Enumerable .Range(0, 10) .Select(idx => new Materials.MaterialBuilder() .WithChannelParam("BaseColor", new Vector4(rnd.NextVector3(), 1))) .ToList(); // create scene var scene = new SceneBuilder(); for (int i = 0; i < 100; ++i) { // create mesh var mat = materials[rnd.Next(0, 10)]; var mesh = VPOSNRM.CreateCompatibleMesh("shape"); #if DEBUG mesh.VertexPreprocessor.SetValidationPreprocessors(); #else mesh.VertexPreprocessor.SetSanitizerPreprocessors(); #endif if ((i & 1) == 0) { mesh.AddCube(mat, Matrix4x4.Identity); } else { mesh.AddSphere(mat, 0.5f, Matrix4x4.Identity); } mesh.Validate(); // create random transform var r = rnd.NextVector3() * 5; var xform = Matrix4x4.CreateFromYawPitchRoll(r.X, r.Y, r.Z) * Matrix4x4.CreateTranslation(rnd.NextVector3() * 25); scene.AddRigidMesh(mesh, xform); } // save the model as GLB scene.AttachToCurrentTest("shapes.glb"); scene.AttachToCurrentTest("shapes.plotly"); }
public void CreateAllAnimationTypesScene() { // 3D View 7.1908.9012.0 has an issue displaying off-center meshes with animated morph targets. TestContext.CurrentContext.AttachShowDirLink(); TestContext.CurrentContext.AttachGltfValidatorLinks(); // create two materials var pink = new MaterialBuilder("material1") .WithChannelParam(KnownChannel.BaseColor, new Vector4(1, 0, 1, 1)); var yellow = new MaterialBuilder("material2") .WithChannelParam(KnownChannel.BaseColor, new Vector4(1, 1, 0, 1)); var scene = new SceneBuilder(); var mesh1 = VPOSNRM.CreateCompatibleMesh("shape1"); mesh1.AddCube(MaterialBuilder.CreateDefault(), Matrix4x4.Identity); var inst1 = scene.AddRigidMesh(mesh1, Matrix4x4.Identity); var mesh2 = VPOSNRM.CreateCompatibleMesh("shape2"); mesh2.AddCube(pink, Matrix4x4.Identity); var inst2 = scene.AddRigidMesh(mesh2, Matrix4x4.CreateTranslation(2, 0, 0)); scene.AttachToCurrentTest("static.glb"); scene.AttachToCurrentTest("static.gltf"); var morphBuilder = mesh2.UseMorphTarget(0); morphBuilder.SetVertexDelta(morphBuilder.Positions.ElementAt(0), (Vector3.UnitY, Vector3.Zero)); morphBuilder.SetVertexDelta(morphBuilder.Positions.ElementAt(1), (Vector3.UnitY, Vector3.Zero)); morphBuilder.SetVertexDelta(morphBuilder.Positions.ElementAt(2), (Vector3.UnitY, Vector3.Zero)); morphBuilder.SetVertexDelta(morphBuilder.Positions.ElementAt(3), (Vector3.UnitY, Vector3.Zero)); inst2.Content.UseMorphing().Value = Transforms.SparseWeight8.Create(1); var curve = inst2.Content.UseMorphing().UseTrackBuilder("Default"); curve.SetPoint(0, Transforms.SparseWeight8.Create(0)); curve.SetPoint(1, Transforms.SparseWeight8.Create(1)); curve.SetPoint(2, Transforms.SparseWeight8.Create(0)); var gltf = scene.ToGltf2(); // Assert.AreEqual(1, gltf.LogicalMeshes[1].MorphWeights[0]); scene.AttachToCurrentTest("mopth.glb"); scene.AttachToCurrentTest("mopth.gltf"); }
public void TestCreateEmptyMesh() { // create a scenebuilder with an empty mesh var sb = new SceneBuilder(); sb.AddRigidMesh(VPOSNRM.CreateCompatibleMesh("Empty"), Matrix4x4.Identity); var schema = sb.ToGltf2(); Assert.AreEqual(0, schema.LogicalMeshes.Count, "SceneBuilder should detect empty meshes and remove them."); schema.CreateMesh("Empty2"); var fileName = AttachmentInfo.From("empty.glb").File.FullName; Assert.Throws <SharpGLTF.Validation.SchemaException>(() => schema.SaveGLB(fileName)); }
public void CreateSceneWithEmptyMeshes() { // Schema2 does NOT allow meshes to be empty, or meshes with empty MeshPrimitives. // but MeshBuilder and SceneBuilder should be able to handle them. TestContext.CurrentContext.AttachGltfValidatorLinks(); var rnd = new Random(177); // create materials var materials = Enumerable .Range(0, 10) .Select(idx => new MaterialBuilder($"material{idx}") .WithChannelParam(KnownChannel.BaseColor, KnownProperty.RGBA, new Vector4(rnd.NextVector3(), 1))) .ToList(); // create meshes var mesh1 = VPOSNRM.CreateCompatibleMesh("mesh1"); mesh1.VertexPreprocessor.SetSanitizerPreprocessors(); mesh1.AddCube(materials[0], Matrix4x4.Identity); mesh1.UsePrimitive(materials[1]).AddTriangle(default, default, default); // add degenerated triangle to produce an empty primitive
public void CreateAllAnimationTypesScene() { // 3D View 7.1908.9012.0 has an issue displaying off-center meshes with animated morph targets. TestContext.CurrentContext.AttachGltfValidatorLinks(); // create two materials var pink = new MaterialBuilder("material1") .WithChannelParam(KnownChannel.BaseColor, KnownProperty.RGBA, new Vector4(1, 0, 1, 1)); var yellow = new MaterialBuilder("material2") .WithChannelParam(KnownChannel.BaseColor, KnownProperty.RGBA, new Vector4(1, 1, 0, 1)); var mesh1 = VPOSNRM.CreateCompatibleMesh("shape1"); mesh1.AddCube(pink, Matrix4x4.Identity); var mesh2 = VPOSNRM.CreateCompatibleMesh("shape2"); mesh2.AddCube(yellow, Matrix4x4.Identity); var scene = new SceneBuilder(); var inst1 = scene.AddRigidMesh(mesh1, Matrix4x4.Identity); // meshes intended to support animation must be created using an armature var armature = new NodeBuilder(); armature.LocalTransform = Matrix4x4.CreateTranslation(2, 0, 0); var inst2 = scene.AddRigidMesh(mesh2, armature); scene.AttachToCurrentTest("static.glb"); scene.AttachToCurrentTest("static.gltf"); // up to this point, the scene has two plain unanimated cubes. var morphBuilder = mesh2.UseMorphTarget(0); morphBuilder.SetVertexDelta(morphBuilder.Positions.ElementAt(0), (Vector3.UnitY, Vector3.Zero)); morphBuilder.SetVertexDelta(morphBuilder.Positions.ElementAt(1), (Vector3.UnitY, Vector3.Zero)); morphBuilder.SetVertexDelta(morphBuilder.Positions.ElementAt(2), (Vector3.UnitY, Vector3.Zero)); morphBuilder.SetVertexDelta(morphBuilder.Positions.ElementAt(3), (Vector3.UnitY, Vector3.Zero)); // set default value. inst2.Content.UseMorphing().SetValue(1); // ser animation curve. var curve = inst2.Content.UseMorphing().UseTrackBuilder("Default"); curve.SetPoint(0, true, 0); curve.SetPoint(1, true, 1); curve.SetPoint(2, true, 0); var gltf = scene.ToGltf2(); TestContext.WriteLine(gltf.GetJsonPreview()); var meshIdx = 1; Assert.AreEqual(1, gltf.LogicalMeshes[meshIdx].Primitives[0].MorphTargetsCount); Assert.AreEqual(1, gltf.LogicalMeshes[meshIdx].MorphWeights[0]); Assert.AreEqual(1, gltf.LogicalAnimations.Count); scene.AttachToCurrentTest("mopth.glb"); scene.AttachToCurrentTest("mopth.gltf"); }
public void CreateSceneWithRandomShapes(bool useGpuInstancing) { TestContext.CurrentContext.AttachGltfValidatorLinks(); var rnd = new Random(177); // create materials var materials = Enumerable .Range(0, 10) .Select(idx => new MaterialBuilder() .WithChannelParam(KnownChannel.BaseColor, KnownProperty.RGBA, new Vector4(rnd.NextVector3(), 1))) .ToList(); // create meshes var sphereMeshes = Enumerable .Range(0, 10) .Select(idx => materials[idx]) .Select(mat => { var mesh = VPOSNRM.CreateCompatibleMesh("shape"); #if DEBUG mesh.VertexPreprocessor.SetValidationPreprocessors(); #else mesh.VertexPreprocessor.SetSanitizerPreprocessors(); #endif mesh.AddSphere(mat, 0.5f, Matrix4x4.Identity); mesh.Validate(); return(mesh); }); var cubeMeshes = Enumerable .Range(0, 10) .Select(idx => materials[idx]) .Select(mat => { var mesh = VPOSNRM.CreateCompatibleMesh("shape"); #if DEBUG mesh.VertexPreprocessor.SetValidationPreprocessors(); #else mesh.VertexPreprocessor.SetSanitizerPreprocessors(); #endif mesh.AddCube(mat, Matrix4x4.Identity); mesh.Validate(); return(mesh); }); var meshes = sphereMeshes.Concat(cubeMeshes).ToArray(); // create scene var scene = new SceneBuilder(); for (int i = 0; i < 100; ++i) { var mesh = meshes[rnd.Next(0, 20)]; // create random transform var r = rnd.NextQuaternion(); var t = rnd.NextVector3() * 25; scene.AddRigidMesh(mesh, (r, t)); } // collapse to glTF var gltf = scene.ToGltf2(useGpuInstancing ? SceneBuilderSchema2Settings.WithGpuInstancing : SceneBuilderSchema2Settings.Default); var bounds = Runtime.MeshDecoder.EvaluateBoundingBox(gltf.DefaultScene); // Assert.AreEqual(defaultBounds,instancedBounds); // save the model as GLB gltf.AttachToCurrentTest("shapes.glb"); scene.AttachToCurrentTest("shapes.plotly"); }