Program.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Numerics;
  5. using SharpGLTF.Schema2;
  6. using SharpGLTF.Geometry;
  7. using SharpGLTF.Geometry.VertexTypes;
  8. namespace InfiniteSkinnedTentacle
  9. {
  10. using VERTEX = VertexBuilder<VertexPosition, VertexColor1, VertexJoints8x4>;
  11. using MESH = MeshBuilder<VertexPosition, VertexColor1, VertexJoints8x4>;
  12. class Program
  13. {
  14. // Skinning use cases and examples: https://github.com/KhronosGroup/glTF/issues/1403
  15. // hierarchy created:
  16. // Mesh1
  17. // Skin1─> Armature1
  18. // Skin2─> Armature2
  19. // Skin3─> Armature3
  20. // Scene
  21. // ├── Armature1
  22. // │ ├── Bone1
  23. // │ ├── Bone2
  24. // │ └── Bone3
  25. // ├── SkinnedMesh1─> Mesh1, Skin1
  26. // ├── Armature2
  27. // │ ├── Bone1
  28. // │ ├── Bone2
  29. // │ └── Bone3
  30. // ├── SkinnedMesh2─> Mesh1, Skin2
  31. // ├── Armature3
  32. // │ ├── Bone1
  33. // │ ├── Bone2
  34. // │ └── Bone3
  35. // └── SkinnedMesh3─> Mesh1, Skin3
  36. // ...
  37. static void Main(string[] args)
  38. {
  39. var model = ModelRoot.CreateModel();
  40. var scene = model.UseScene("default");
  41. var mesh = model
  42. .CreateMeshes(CreateMesh(10))
  43. .First();
  44. RecusiveTentacle(scene, Matrix4x4.CreateTranslation(+25, 0, +25), mesh, Quaternion.CreateFromYawPitchRoll(0f, 0.2f, 0f), 2);
  45. RecusiveTentacle(scene, Matrix4x4.CreateTranslation(-25, 0, +25), mesh, Quaternion.CreateFromYawPitchRoll(0.2f, 0f, 0f), 2);
  46. RecusiveTentacle(scene, Matrix4x4.CreateTranslation(-25, 0, -25), mesh, Quaternion.CreateFromYawPitchRoll(0f, 0f, 0.2f), 2);
  47. RecusiveTentacle(scene, Matrix4x4.CreateTranslation(+25, 0, -25), mesh, Quaternion.CreateFromYawPitchRoll(0.2f, 0f, 0f), 2);
  48. model.SaveGLB("recursive tentacles.glb");
  49. }
  50. static void RecusiveTentacle(IVisualNodeContainer parent, Matrix4x4 offset, Mesh mesh, Quaternion anim, int repeat)
  51. {
  52. parent = parent
  53. .CreateNode()
  54. .WithLocalTransform(offset);
  55. parent = AddTentacleSkeleton(parent as Node, mesh, anim);
  56. if (repeat == 0) return;
  57. var scale = Matrix4x4.CreateScale(0.2f);
  58. RecusiveTentacle(parent, Matrix4x4.CreateTranslation(+15, 0, +15) * scale, mesh, Quaternion.CreateFromYawPitchRoll(0f, 0.2f, 0f), repeat - 1);
  59. RecusiveTentacle(parent, Matrix4x4.CreateTranslation(-15, 0, +15) * scale, mesh, Quaternion.CreateFromYawPitchRoll(0.2f, 0f, 0f), repeat - 1);
  60. RecusiveTentacle(parent, Matrix4x4.CreateTranslation(-15, 0, -15) * scale, mesh, Quaternion.CreateFromYawPitchRoll(0f, 0f, 0.2f), repeat - 1);
  61. RecusiveTentacle(parent, Matrix4x4.CreateTranslation(+15, 0, -15) * scale, mesh, Quaternion.CreateFromYawPitchRoll(0.2f, 0f, 0f), repeat - 1);
  62. }
  63. static Node AddTentacleSkeleton(Node skeleton, Mesh mesh, Quaternion anim)
  64. {
  65. var bindings = new List<Node>();
  66. Node bone = null;
  67. for (int i = 0; i < 10; ++i)
  68. {
  69. if (bone == null)
  70. {
  71. bone = skeleton;
  72. }
  73. else
  74. {
  75. bone = bone.CreateNode();
  76. bone.LocalTransform = Matrix4x4.CreateTranslation(0, 10, 0);
  77. }
  78. bone.WithRotationAnimation("Track0", (0, Quaternion.Identity), (1, anim), (2, Quaternion.Identity));
  79. bindings.Add(bone);
  80. }
  81. skeleton.VisualScene.CreateNode()
  82. .WithMesh(mesh)
  83. .WithSkinBinding(bindings.ToArray());
  84. return bindings.Last();
  85. }
  86. static MESH CreateMesh(int boneCount)
  87. {
  88. var mesh = new MESH("skinned mesh");
  89. var prim = mesh.UsePrimitive(new SharpGLTF.Materials.MaterialBuilder("Default"));
  90. var a0 = default(VERTEX);
  91. var a1 = default(VERTEX);
  92. var a2 = default(VERTEX);
  93. var a3 = default(VERTEX);
  94. for (int i = 0; i < boneCount; ++i)
  95. {
  96. var b0 = new VERTEX(new Vector3(-5, i * 10, -5), Vector4.One, (i, 1));
  97. var b1 = new VERTEX(new Vector3(+5, i * 10, -5), Vector4.One, (i, 1));
  98. var b2 = new VERTEX(new Vector3(+5, i * 10, +5), Vector4.One, (i, 1));
  99. var b3 = new VERTEX(new Vector3(-5, i * 10, +5), Vector4.One, (i, 1));
  100. if (i == 0)
  101. {
  102. prim.AddPolygon(b0, b1, b2, b3);
  103. }
  104. else
  105. {
  106. prim.AddPolygon(b0, b1, a1, a0);
  107. prim.AddPolygon(b1, b2, a2, a1);
  108. prim.AddPolygon(b2, b3, a3, a2);
  109. prim.AddPolygon(b3, b0, a0, a3);
  110. }
  111. a0 = b0;
  112. a1 = b1;
  113. a2 = b2;
  114. a3 = b3;
  115. }
  116. prim.AddPolygon(a3, a2, a1, a0);
  117. return mesh;
  118. }
  119. }
  120. }