Program.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.Numerics;
  3. using SharpGLTF.Geometry;
  4. using SharpGLTF.Geometry.VertexTypes;
  5. using SharpGLTF.Materials;
  6. using SharpGLTF.Schema2;
  7. namespace Example1
  8. {
  9. using VERTEX = SharpGLTF.Geometry.VertexTypes.VertexPosition;
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. // create two materials
  15. var material1 = new MaterialBuilder()
  16. .WithDoubleSide(true)
  17. .WithMetallicRoughnessShader()
  18. .WithChannelParam("BaseColor", new Vector4(1,0,0,1) );
  19. var material2 = new MaterialBuilder()
  20. .WithDoubleSide(true)
  21. .WithMetallicRoughnessShader()
  22. .WithChannelParam("BaseColor", new Vector4(1, 0, 1, 1));
  23. // create a mesh with two primitives, one for each material
  24. var mesh = new MeshBuilder<VERTEX>("mesh");
  25. var prim = mesh.UsePrimitive(material1);
  26. prim.AddTriangle(new VERTEX(-10, 0, 0), new VERTEX(10, 0, 0), new VERTEX(0, 10, 0));
  27. prim.AddTriangle(new VERTEX(10, 0, 0), new VERTEX(-10, 0, 0), new VERTEX(0, -10, 0));
  28. prim = mesh.UsePrimitive(material2);
  29. prim.AddConvexPolygon(new VERTEX(-5, 0, 3), new VERTEX(0, -5, 3), new VERTEX(5, 0, 3), new VERTEX(0, 5, 3));
  30. // create a new gltf model
  31. var model = ModelRoot.CreateModel();
  32. // add all meshes (just one in this case) to the model
  33. model.CreateMeshes(mesh);
  34. // create a scene, a node, and assign the first mesh
  35. model.UseScene("Default")
  36. .CreateNode()
  37. .WithMesh(model.LogicalMeshes[0]);
  38. // save the model in different formats
  39. model.SaveAsWavefront("mesh.obj");
  40. model.SaveGLB("mesh.glb");
  41. model.SaveGLTF("mesh.gltf");
  42. }
  43. }
  44. static class ToolkitUtils
  45. {
  46. public static void AddConvexPolygon<TMaterial, TvG, TvM, TvS>(this PrimitiveBuilder<TMaterial, TvG, TvM, TvS> primitive, params VertexBuilder<TvG, TvM, TvS>[] vertices)
  47. where TvG : struct, IVertexGeometry
  48. where TvM : struct, IVertexMaterial
  49. where TvS : struct, IVertexSkinning
  50. {
  51. for (int i = 2; i < vertices.Length; ++i)
  52. {
  53. var a = vertices[0];
  54. var b = vertices[i - 1];
  55. var c = vertices[i];
  56. primitive.AddTriangle(a, b, c);
  57. }
  58. }
  59. }
  60. }