Program.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Numerics;
  3. using SharpGLTF.Geometry;
  4. using SharpGLTF.Materials;
  5. using SharpGLTF.Schema2;
  6. namespace Example1
  7. {
  8. using VERTEX = SharpGLTF.Geometry.VertexTypes.VertexPosition;
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. // create two materials
  14. var material1 = new MaterialBuilder()
  15. .WithDoubleSide(true)
  16. .WithMetallicRoughnessShader()
  17. .WithChannelParam("BaseColor", new Vector4(1,0,0,1) );
  18. var material2 = new MaterialBuilder()
  19. .WithDoubleSide(true)
  20. .WithMetallicRoughnessShader()
  21. .WithChannelParam("BaseColor", new Vector4(1, 0, 1, 1));
  22. // create a mesh with two primitives, one for each material
  23. var mesh = new MeshBuilder<VERTEX>("mesh");
  24. var prim = mesh.UsePrimitive(material1);
  25. prim.AddTriangle(new VERTEX(-10, 0, 0), new VERTEX(10, 0, 0), new VERTEX(0, 10, 0));
  26. prim.AddTriangle(new VERTEX(10, 0, 0), new VERTEX(-10, 0, 0), new VERTEX(0, -10, 0));
  27. prim = mesh.UsePrimitive(material2);
  28. prim.AddPolygon(new VERTEX(-5, 0, 3), new VERTEX(0, -5, 3), new VERTEX(5, 0, 3), new VERTEX(0, 5, 3));
  29. // create a new gltf model
  30. var model = ModelRoot.CreateModel();
  31. // add all meshes (just one in this case) to the model
  32. model.CreateMeshes(mesh);
  33. // create a scene, a node, and assign the first mesh
  34. model.UseScene("Default")
  35. .CreateNode()
  36. .WithMesh(model.LogicalMeshes[0]);
  37. // save the model in different formats
  38. model.SaveAsWavefront("mesh.obj");
  39. model.SaveGLB("mesh.glb");
  40. model.SaveGLTF("mesh.gltf");
  41. }
  42. }
  43. }