VertexBuilder.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Numerics;
  5. using SharpGLTF.Geometry.VertexTypes;
  6. namespace SharpGLTF.Geometry
  7. {
  8. public interface IVertexBuilder
  9. {
  10. IVertexGeometry GetGeometry();
  11. IVertexMaterial GetMaterial();
  12. IVertexSkinning GetSkinning();
  13. VertexBuilder<TvPP, TvMM, TvSS> ConvertTo<TvPP, TvMM, TvSS>()
  14. where TvPP : struct, IVertexGeometry
  15. where TvMM : struct, IVertexMaterial
  16. where TvSS : struct, IVertexSkinning;
  17. // void SetGeometry(IVertexGeometry);
  18. // void SetMaterial(IVertexMaterial);
  19. // void SetSkinning(IVertexSkinning);
  20. }
  21. /// <summary>
  22. /// Represents an individual vertex object.
  23. /// </summary>
  24. /// <typeparam name="TvG">
  25. /// The vertex fragment type with Position, Normal and Tangent.
  26. /// Valid types are:
  27. /// <see cref="VertexPosition"/>,
  28. /// <see cref="VertexPositionNormal"/>,
  29. /// <see cref="VertexPositionNormalTangent"/>.
  30. /// </typeparam>
  31. /// <typeparam name="TvM">
  32. /// The vertex fragment type with Colors and Texture Coordinates.
  33. /// Valid types are:
  34. /// <see cref="VertexEmpty"/>,
  35. /// <see cref="VertexColor1"/>,
  36. /// <see cref="VertexTexture1"/>,
  37. /// <see cref="VertexColor1Texture1"/>.
  38. /// <see cref="VertexColor1Texture2"/>.
  39. /// <see cref="VertexColor2Texture2"/>.
  40. /// </typeparam>
  41. /// <typeparam name="TvS">
  42. /// The vertex fragment type with Skin Joint Weights.
  43. /// Valid types are:
  44. /// <see cref="VertexEmpty"/>,
  45. /// <see cref="VertexJoints8x4"/>,
  46. /// <see cref="VertexJoints8x8"/>,
  47. /// <see cref="VertexJoints16x4"/>,
  48. /// <see cref="VertexJoints16x8"/>.
  49. /// </typeparam>
  50. [System.Diagnostics.DebuggerDisplay("Vertex 𝐏:{Position} {_GetDebugWarnings()}")]
  51. public partial struct VertexBuilder<TvG, TvM, TvS> : IVertexBuilder
  52. where TvG : struct, IVertexGeometry
  53. where TvM : struct, IVertexMaterial
  54. where TvS : struct, IVertexSkinning
  55. {
  56. #region constructors
  57. public VertexBuilder(TvG g, TvM m, TvS s)
  58. {
  59. Geometry = g;
  60. Material = m;
  61. Skinning = s;
  62. }
  63. public VertexBuilder(TvG g, TvM m, params (int, float)[] bindings)
  64. {
  65. Geometry = g;
  66. Material = m;
  67. Skinning = default;
  68. for (int i = 0; i < bindings.Length; ++i)
  69. {
  70. Skinning.SetJointBinding(i, bindings[i].Item1, bindings[i].Item2);
  71. }
  72. }
  73. public VertexBuilder(TvG g, TvM m)
  74. {
  75. Geometry = g;
  76. Material = m;
  77. Skinning = default;
  78. }
  79. public VertexBuilder(TvG g, TvS s)
  80. {
  81. Geometry = g;
  82. Material = default;
  83. Skinning = s;
  84. }
  85. public VertexBuilder(TvG g)
  86. {
  87. Geometry = g;
  88. Material = default;
  89. Skinning = default;
  90. }
  91. public VertexBuilder(TvG g, params (int, float)[] bindings)
  92. {
  93. Geometry = g;
  94. Material = default;
  95. Skinning = default;
  96. for (int i = 0; i < bindings.Length; ++i)
  97. {
  98. Skinning.SetJointBinding(i, bindings[i].Item1, bindings[i].Item2);
  99. }
  100. }
  101. public static implicit operator VertexBuilder<TvG, TvM, TvS>((TvG, TvM, TvS) tuple)
  102. {
  103. return new VertexBuilder<TvG, TvM, TvS>(tuple.Item1, tuple.Item2, tuple.Item3);
  104. }
  105. public static implicit operator VertexBuilder<TvG, TvM, TvS>((TvG, TvM) tuple)
  106. {
  107. return new VertexBuilder<TvG, TvM, TvS>(tuple.Item1, tuple.Item2);
  108. }
  109. public static implicit operator VertexBuilder<TvG, TvM, TvS>((TvG, TvS) tuple)
  110. {
  111. return new VertexBuilder<TvG, TvM, TvS>(tuple.Item1, tuple.Item2);
  112. }
  113. public static implicit operator VertexBuilder<TvG, TvM, TvS>(TvG g)
  114. {
  115. return new VertexBuilder<TvG, TvM, TvS>(g);
  116. }
  117. public static VertexBuilder<TvG, TvM, TvS> Create(Vector3 position)
  118. {
  119. var v = default(VertexBuilder<TvG, TvM, TvS>);
  120. v.Geometry.SetPosition(position);
  121. return v;
  122. }
  123. public static VertexBuilder<TvG, TvM, TvS> Create(Vector3 position,Vector3 normal)
  124. {
  125. var v = default(VertexBuilder<TvG, TvM, TvS>);
  126. v.Geometry.SetPosition(position);
  127. v.Geometry.SetNormal(normal);
  128. return v;
  129. }
  130. public static VertexBuilder<TvG, TvM, TvS> Create(Vector3 position, Vector3 normal, Vector4 tangent)
  131. {
  132. var v = default(VertexBuilder<TvG, TvM, TvS>);
  133. v.Geometry.SetPosition(position);
  134. v.Geometry.SetNormal(normal);
  135. v.Geometry.SetTangent(tangent);
  136. return v;
  137. }
  138. #endregion
  139. #region data
  140. public TvG Geometry;
  141. public TvM Material;
  142. public TvS Skinning;
  143. #endregion
  144. #region properties
  145. [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
  146. public Vector3 Position
  147. {
  148. get => Geometry.GetPosition();
  149. set => Geometry.SetPosition(value);
  150. }
  151. #endregion
  152. #region API
  153. public VertexBuilder<TvG, TvM, TvS> WithGeometry(Vector3 position)
  154. {
  155. var v = this;
  156. v.Geometry.SetPosition(position);
  157. return v;
  158. }
  159. public VertexBuilder<TvG, TvM, TvS> WithGeometry(Vector3 position, Vector3 normal)
  160. {
  161. var v = this;
  162. v.Geometry.SetPosition(position);
  163. v.Geometry.SetNormal(normal);
  164. return v;
  165. }
  166. public VertexBuilder<TvG, TvM, TvS> WithGeometry(Vector3 position, Vector3 normal, Vector4 tangent)
  167. {
  168. var v = this;
  169. v.Geometry.SetPosition(position);
  170. v.Geometry.SetNormal(normal);
  171. v.Geometry.SetTangent(tangent);
  172. return v;
  173. }
  174. public VertexBuilder<TvG, TvM, TvS> WithMaterial(params Vector2[] uvs)
  175. {
  176. var v = this;
  177. for (int i = 0; i < uvs.Length; ++i) v.Material.SetTexCoord(i, uvs[i]);
  178. return v;
  179. }
  180. public VertexBuilder<TvG, TvM, TvS> WithMaterial(Vector4 color0, params Vector2[] uvs)
  181. {
  182. var v = this;
  183. v.Material.SetColor(0, color0);
  184. for (int i = 0; i < uvs.Length; ++i) v.Material.SetTexCoord(i, uvs[i]);
  185. return v;
  186. }
  187. public VertexBuilder<TvG, TvM, TvS> WithMaterial(Vector4 color0, Vector4 color1, params Vector2[] uvs)
  188. {
  189. var v = this;
  190. v.Material.SetColor(0, color0);
  191. v.Material.SetColor(1, color1);
  192. for (int i = 0; i < uvs.Length; ++i) v.Material.SetTexCoord(i, uvs[i]);
  193. return v;
  194. }
  195. public VertexBuilder<TvG, TvM, TvS> WithSkinning(params (int, float)[] bindings)
  196. {
  197. var v = this;
  198. int i = 0;
  199. while (i < bindings.Length)
  200. {
  201. v.Skinning.SetJointBinding(i, bindings[i].Item1, bindings[i].Item2);
  202. ++i;
  203. }
  204. while (i < bindings.Length)
  205. {
  206. v.Skinning.SetJointBinding(i, 0, 0);
  207. ++i;
  208. }
  209. return v;
  210. }
  211. public void Validate()
  212. {
  213. Geometry.Validate();
  214. Material.Validate();
  215. Skinning.Validate();
  216. }
  217. public VertexBuilder<TvPP, TvMM, TvSS> ConvertTo<TvPP, TvMM, TvSS>()
  218. where TvPP : struct, IVertexGeometry
  219. where TvMM : struct, IVertexMaterial
  220. where TvSS : struct, IVertexSkinning
  221. {
  222. var p = Geometry.ConvertTo<TvPP>();
  223. var m = Material.ConvertTo<TvMM>();
  224. var s = Skinning.ConvertTo<TvSS>();
  225. return new VertexBuilder<TvPP, TvMM, TvSS>(p, m, s);
  226. }
  227. public static MeshBuilder<TMaterial, TvG, TvM, TvS> CreateCompatibleMesh<TMaterial>(string name = null)
  228. {
  229. return new MeshBuilder<TMaterial, TvG, TvM, TvS>(name);
  230. }
  231. public static MeshBuilder<TvG, TvM, TvS> CreateCompatibleMesh(string name = null)
  232. {
  233. return new MeshBuilder<TvG, TvM, TvS>(name);
  234. }
  235. private String _GetDebugWarnings()
  236. {
  237. var sb = new StringBuilder();
  238. if (Geometry.TryGetNormal(out Vector3 n))
  239. {
  240. if (!n.IsValidNormal()) sb.Append($" ❌𝚴:{n}");
  241. }
  242. if (Geometry.TryGetTangent(out Vector4 t))
  243. {
  244. if (!t.IsValidTangent()) sb.Append($" ❌𝚻:{t}");
  245. }
  246. for (int i = 0; i < Material.MaxColors; ++i)
  247. {
  248. var c = Material.GetColor(i);
  249. if (!c._IsReal() | !c.IsInRange(Vector4.Zero, Vector4.One)) sb.Append($" ❌𝐂{i}:{c}");
  250. }
  251. for (int i = 0; i < Material.MaxTextCoords; ++i)
  252. {
  253. var uv = Material.GetTexCoord(i);
  254. if (!uv._IsReal()) sb.Append($" ❌𝐔𝐕{i}:{uv}");
  255. }
  256. for (int i = 0; i < Skinning.MaxBindings; ++i)
  257. {
  258. var jw = Skinning.GetJointBinding(i);
  259. if (!jw.Weight._IsReal() || jw.Weight < 0 || jw.Joint < 0) sb.Append($" ❌𝐉𝐖{i} {jw.Joint}:{jw.Weight}");
  260. }
  261. return sb.ToString();
  262. }
  263. IVertexGeometry IVertexBuilder.GetGeometry() { return this.Geometry; }
  264. IVertexMaterial IVertexBuilder.GetMaterial() { return this.Material; }
  265. IVertexSkinning IVertexBuilder.GetSkinning() { return this.Skinning; }
  266. #endregion
  267. }
  268. }