Program.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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(KnownChannel.BaseColor, KnownProperty.RGBA, new Vector4(1,0,0,1) );
  19. var material2 = new MaterialBuilder()
  20. .WithDoubleSide(true)
  21. .WithMetallicRoughnessShader()
  22. .WithChannelParam(KnownChannel.BaseColor, KnownProperty.RGBA, 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.AddQuadrangle(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.AddRigidMesh(mesh, Matrix4x4.Identity);
  33. // save the model in different formats
  34. var model = scene.ToGltf2();
  35. model.SaveAsWavefront("mesh.obj");
  36. model.SaveGLB("mesh.glb");
  37. model.SaveGLTF("mesh.gltf");
  38. }
  39. }
  40. }