Program.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 scene
  31. var scene = new SharpGLTF.Scenes.SceneBuilder();
  32. scene.AddMesh(mesh, Matrix4x4.Identity);
  33. // save the model in different formats
  34. var model = scene.ToSchema2();
  35. model.SaveAsWavefront("mesh.obj");
  36. model.SaveGLB("mesh.glb");
  37. model.SaveGLTF("mesh.gltf");
  38. }
  39. }
  40. static class ToolkitUtils
  41. {
  42. public static void AddConvexPolygon<TMaterial, TvG, TvM, TvS>(this PrimitiveBuilder<TMaterial, TvG, TvM, TvS> primitive, params VertexBuilder<TvG, TvM, TvS>[] vertices)
  43. where TvG : struct, IVertexGeometry
  44. where TvM : struct, IVertexMaterial
  45. where TvS : struct, IVertexSkinning
  46. {
  47. for (int i = 2; i < vertices.Length; ++i)
  48. {
  49. var a = vertices[0];
  50. var b = vertices[i - 1];
  51. var c = vertices[i];
  52. primitive.AddTriangle(a, b, c);
  53. }
  54. }
  55. }
  56. }