SolidMeshUtils.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Numerics;
  4. using System.Text;
  5. using SharpGLTF.Geometry;
  6. using SharpGLTF.Geometry.VertexTypes;
  7. namespace SharpGLTF.Geometry.Parametric
  8. {
  9. using VERTEX = VertexBuilder<VertexPositionNormal, VertexColor1Texture1, VertexEmpty>;
  10. interface IParametricShape<TMaterial>
  11. {
  12. void AddTo(IMeshBuilder<TMaterial> meshBuilder, Matrix4x4 xform);
  13. }
  14. class Cube<TMaterial> : IParametricShape<TMaterial>
  15. {
  16. #region lifecycle
  17. public Cube(TMaterial material)
  18. {
  19. _Front = _Back = _Left = _Right = _Top = _Bottom = material;
  20. }
  21. public Cube(TMaterial material, float width, float height, float length)
  22. {
  23. _Front = _Back = _Left = _Right = _Top = _Bottom = material;
  24. _Size = new Vector3(width, height, length);
  25. }
  26. #endregion
  27. #region data
  28. private Vector3 _Size = Vector3.One;
  29. private TMaterial _Front;
  30. private TMaterial _Back;
  31. private TMaterial _Left;
  32. private TMaterial _Right;
  33. private TMaterial _Top;
  34. private TMaterial _Bottom;
  35. #endregion
  36. #region API
  37. public void AddTo(IMeshBuilder<TMaterial> meshBuilder, Matrix4x4 xform)
  38. {
  39. var x = Vector3.UnitX * _Size.X * 0.5f;
  40. var y = Vector3.UnitY * _Size.Y * 0.5f;
  41. var z = Vector3.UnitZ * _Size.Z * 0.5f;
  42. _AddCubeFace(meshBuilder.UsePrimitive(_Right), x, y, z, xform);
  43. _AddCubeFace(meshBuilder.UsePrimitive(_Left), -x, z, y, xform);
  44. _AddCubeFace(meshBuilder.UsePrimitive(_Top), y, z, x, xform);
  45. _AddCubeFace(meshBuilder.UsePrimitive(_Bottom), -y, x, z, xform);
  46. _AddCubeFace(meshBuilder.UsePrimitive(_Front), z, x, y, xform);
  47. _AddCubeFace(meshBuilder.UsePrimitive(_Back), -z, y, x, xform);
  48. }
  49. private static void _AddCubeFace(IPrimitiveBuilder primitiveBuilder, Vector3 origin, Vector3 axisX, Vector3 axisY, Matrix4x4 xform)
  50. {
  51. var p1 = Vector3.Transform(origin - axisX - axisY, xform);
  52. var p2 = Vector3.Transform(origin + axisX - axisY, xform);
  53. var p3 = Vector3.Transform(origin + axisX + axisY, xform);
  54. var p4 = Vector3.Transform(origin - axisX + axisY, xform);
  55. var n = Vector3.Normalize(Vector3.TransformNormal(origin, xform));
  56. primitiveBuilder.AddConvexPolygon
  57. (
  58. new VERTEX( (p1, n), (Vector4.One, Vector2.Zero) ),
  59. new VERTEX( (p2, n), (Vector4.One, Vector2.UnitX) ),
  60. new VERTEX( (p3, n), (Vector4.One, Vector2.One) ),
  61. new VERTEX( (p4, n), (Vector4.One, Vector2.UnitY) )
  62. );
  63. }
  64. public MeshBuilder<TMaterial, VertexPositionNormal, VertexColor1Texture1, VertexEmpty> ToMesh(Matrix4x4 xform)
  65. {
  66. var mesh = new MeshBuilder<TMaterial, VertexPositionNormal, VertexColor1Texture1, VertexEmpty>();
  67. AddTo(mesh, xform);
  68. return mesh;
  69. }
  70. #endregion
  71. }
  72. class IcoSphere<TMaterial> : IParametricShape<TMaterial>
  73. {
  74. #region lifecycle
  75. public IcoSphere(TMaterial material, float radius = 0.5f)
  76. {
  77. _Material = material;
  78. _Radius = radius;
  79. }
  80. #endregion
  81. #region data
  82. private float _Radius = 0.5f;
  83. private TMaterial _Material;
  84. private int _Subdivision = 3;
  85. #endregion
  86. #region API
  87. public void AddTo(IMeshBuilder<TMaterial> meshBuilder, Matrix4x4 xform)
  88. {
  89. // http://blog.andreaskahler.com/2009/06/creating-icosphere-mesh-in-code.html
  90. var t = 1 + (float)(Math.Sqrt(5.0) / 2);
  91. var v0 = new Vector3(-1, t, 0) * _Radius;
  92. var v1 = new Vector3(1, t, 0) * _Radius;
  93. var v2 = new Vector3(-1, -t, 0) * _Radius;
  94. var v3 = new Vector3(1, -t, 0) * _Radius;
  95. var v4 = new Vector3(0, -1, t) * _Radius;
  96. var v5 = new Vector3(0, 1, t) * _Radius;
  97. var v6 = new Vector3(0, -1, -t) * _Radius;
  98. var v7 = new Vector3(0, 1, -t) * _Radius;
  99. var v8 = new Vector3(t, 0, -1) * _Radius;
  100. var v9 = new Vector3(t, 0, 1) * _Radius;
  101. var v10 = new Vector3(-t, 0, -1) * _Radius;
  102. var v11 = new Vector3(-t, 0, 1) * _Radius;
  103. var prim = meshBuilder.UsePrimitive(_Material);
  104. // 5 faces around point 0
  105. _AddSphereFace(prim, xform, v0, v11, v5, _Subdivision);
  106. _AddSphereFace(prim, xform, v0, v5, v1, _Subdivision);
  107. _AddSphereFace(prim, xform, v0, v1, v7, _Subdivision);
  108. _AddSphereFace(prim, xform, v0, v7, v10, _Subdivision);
  109. _AddSphereFace(prim, xform, v0, v10, v11, _Subdivision);
  110. // 5 adjacent faces
  111. _AddSphereFace(prim, xform, v1, v5, v9, _Subdivision);
  112. _AddSphereFace(prim, xform, v5, v11, v4, _Subdivision);
  113. _AddSphereFace(prim, xform, v11, v10, v2, _Subdivision);
  114. _AddSphereFace(prim, xform, v10, v7, v6, _Subdivision);
  115. _AddSphereFace(prim, xform, v7, v1, v8, _Subdivision);
  116. // 5 faces around point 3
  117. _AddSphereFace(prim, xform, v3, v9, v4, _Subdivision);
  118. _AddSphereFace(prim, xform, v3, v4, v2, _Subdivision);
  119. _AddSphereFace(prim, xform, v3, v2, v6, _Subdivision);
  120. _AddSphereFace(prim, xform, v3, v6, v8, _Subdivision);
  121. _AddSphereFace(prim, xform, v3, v8, v9, _Subdivision);
  122. // 5 adjacent faces
  123. _AddSphereFace(prim, xform, v4, v9, v5, _Subdivision);
  124. _AddSphereFace(prim, xform, v2, v4, v11, _Subdivision);
  125. _AddSphereFace(prim, xform, v6, v2, v10, _Subdivision);
  126. _AddSphereFace(prim, xform, v8, v6, v7, _Subdivision);
  127. _AddSphereFace(prim, xform, v9, v8, v1, _Subdivision);
  128. }
  129. private static void _AddSphereFace(IPrimitiveBuilder primitiveBuilder, Matrix4x4 xform, Vector3 a, Vector3 b, Vector3 c, int iterations = 0)
  130. {
  131. if (iterations <= 0)
  132. {
  133. var tt = (a + b + c) / 3.0f;
  134. var aa = _CreateVertex(a, xform);
  135. var bb = _CreateVertex(b, xform);
  136. var cc = _CreateVertex(c, xform);
  137. primitiveBuilder.AddTriangle(aa, bb, cc);
  138. return;
  139. }
  140. --iterations;
  141. var ab = Vector3.Normalize(a + b) * a.Length();
  142. var bc = Vector3.Normalize(b + c) * b.Length();
  143. var ca = Vector3.Normalize(c + a) * c.Length();
  144. // central triangle
  145. _AddSphereFace(primitiveBuilder, xform, ab, bc, ca, iterations);
  146. // vertex triangles
  147. _AddSphereFace(primitiveBuilder, xform, a, ab, ca, iterations);
  148. _AddSphereFace(primitiveBuilder, xform, b, bc, ab, iterations);
  149. _AddSphereFace(primitiveBuilder, xform, c, ca, bc, iterations);
  150. }
  151. private static VERTEX _CreateVertex(Vector3 position, Matrix4x4 xform)
  152. {
  153. var v = new VERTEX();
  154. v.Geometry.Position = Vector3.Transform(position, xform);
  155. v.Geometry.Normal = Vector3.Normalize(Vector3.TransformNormal(position, xform));
  156. v.Material.Color = Vector4.One;
  157. v.Material.TexCoord = Vector2.Zero;
  158. return v;
  159. }
  160. #endregion
  161. }
  162. static class SolidMeshUtils
  163. {
  164. public static void AddCube<TMaterial>(this IMeshBuilder<TMaterial> meshBuilder, TMaterial material, Matrix4x4 xform)
  165. {
  166. var cube = new Cube<TMaterial>(material);
  167. cube.AddTo(meshBuilder, xform);
  168. }
  169. public static void AddSphere<TMaterial>(this IMeshBuilder<TMaterial> meshBuilder, TMaterial material, Single radius, Matrix4x4 xform)
  170. {
  171. var sphere = new IcoSphere<TMaterial>(material, radius);
  172. sphere.AddTo(meshBuilder, xform);
  173. }
  174. public static MeshBuilder<VertexPosition, VertexTexture1> CreateTerrainMesh(int width, int length, Func<int,int,float> heightFunction, string terrainColorImagePath)
  175. {
  176. // we create a new material to use with the terrain mesh
  177. var material = new Materials.MaterialBuilder("TerrainMaterial")
  178. .WithChannelImage(Materials.KnownChannels.BaseColor, terrainColorImagePath);
  179. // we create a MeshBuilder
  180. var terrainMesh = new MeshBuilder<VertexPosition, VertexTexture1>("terrain");
  181. var texScale = new Vector2(width, length);
  182. // fill the MeshBuilder with quads using the heightFunction.
  183. for (int y = 1; y < length; ++y)
  184. {
  185. for (int x = 1; x < width; ++x)
  186. {
  187. // quad vertex positions
  188. var a = new Vector3(x - 1, heightFunction(x - 1, y + 0), y + 0);
  189. var b = new Vector3(x + 0, heightFunction(x + 0, y + 0), y + 0);
  190. var c = new Vector3(x + 0, heightFunction(x + 0, y - 1), y - 1);
  191. var d = new Vector3(x - 1, heightFunction(x - 1, y - 1), y - 1);
  192. // quad UV coordinates
  193. var at = new Vector2(a.X, a.Z) / texScale;
  194. var bt = new Vector2(b.X, b.Z) / texScale;
  195. var ct = new Vector2(c.X, c.Z) / texScale;
  196. var dt = new Vector2(d.X, d.Z) / texScale;
  197. terrainMesh
  198. .UsePrimitive(material)
  199. .AddConvexPolygon
  200. (
  201. (a, at),
  202. (b, bt),
  203. (c, ct),
  204. (d, dt)
  205. );
  206. }
  207. }
  208. terrainMesh.Validate();
  209. return terrainMesh;
  210. }
  211. }
  212. }