在上文中,我们把Ogre里的网格分解成点线面后,我们要完成一个新的功能,在点上突出显示.

  得到顶点位置后,这个功能也就是一个很简单的事,最开始是每个顶点添加一个子节点,节点上添加一个圆点.

  1. foreach (var vect in this.Points)
  2. {
  3. var cEntity = EntityBuilder.Instance.SphereX1.Clone(Guid.NewGuid().ToString());
  4. cEntity.UserData = vect;
  5. var cNode = this.PointNode.CreateChildSceneNode(vect.VertexInfo.Position);
  6. cNode.Scale = Vector3.UnitScale * scale;
  7. cNode.AttachObject(cEntity);
  8. }

more entity

  这个是最简单的,但是添加后,FPS直接下降到100,虽然100现在是够了,但是后面肯定还有新的内容,所以这种方案是放弃的.

  前面这个Mesh有约300个顶点,这种方法添加三百个Entity,只有一个SubEntity的这种,但是也会造成三百个批次,FPS肯定下降的厉害,我们要做的就是减少批次,于是我们采用StaticGeometry,如下:

  1. StaticGeometry sg = Root.Instance.SceneManager.CreateStaticGeometry("point_sg", );
  2. foreach (var vect in this.Points)
  3. {
  4. var scale = mesh.BoundingBox.Size.Length / ;
  5. sg.AddEntity(cEntity, vect.VertexInfo.Position, Quaternion.Identity, Vector3.UnitScale * scale);
  6. }
  7. sg.Build();

StaticGeometry

  确实,我们能看到效果了,FPS几乎不变,只有少数下降,但是staticGeometry有个缺点,他是独立的,与原Node上实体没有关系了,就是如果我移动了Node的位置,点的位置没有变化.

  本来在就我想直接设置point_size就好,不过有个问题,这个参数和硬件有关,不同的硬件可能大小不一样,还有就是这个点上只能是球形,颜色也要求一样,这几样都达不到要求,我考虑把所有球放入一个Mesh里,一个Mesh对应一个SubMesh,在我原来认为,Ogre会自动把同材质的SubMesh做一个批次渲染,但是FPS告诉我们,一个SubMesh对应一个批次,同材质也是.  

  1. ManualObject mo = new ManualObject(this.MeshName + "point/Show");
  2. foreach (var vect in this.Points)
  3. {
  4. var scale = mesh.BoundingBox.Size.Length / ;
  5. var sphereX1 = new Sphere3D();
  6. sphereX1.Center = vect.VertexInfo.Position;
  7. sphereX1.ThetaDiv = ;
  8. sphereX1.Radius = 0.1 * scale;
  9. sphereX1.ToManualObjectSection(mo, "MeshViewer/AxisX");
  10. }

one ManualObject more submesh

  我们知道ManualObject一个begin与end对就Mesh里的一个SubMesh,Ogre没给我们合并,所以还是三百多个批次,FPS又下降下二位数了.我查了下,公告板集合这个东东,下面是公告板集合的代码与效果:

  1. BillboardSet set = Root.Instance.SceneManager.CreateBillboardSet();
  2. set.BillboardType = BillboardType.Point;
  3. var scale = mesh.BoundingBox.Size.Length * 0.1;
  4. set.SetDefaultDimensions(scale, scale);
  5. foreach (var vect in this.Points)
  6. {
  7. set.CreateBillboard(vect.VertexInfo.Position, ColorEx.DarkRed);
  8. }
  9. this.PointNode.AttachObject(set);

BillboardSet

  公告板给我们生成始终朝向我们的四边形,这里没用上面的模型,用公告板也能达到FPS不下降的效果,但是他顶点固定显示成四边形,也不能显示成别的模型现在看来还是达不到我们的需求,需要注意的是,默认的射线相交查询不会得到公告板集,需要我们设置query.QueryTypeMask |= (uint)SceneQueryTypeMask.Fx;因为默认的场景创建的CreateRayQuery并不包含公告板集的查询mask,我们需要加上公告板集的mask(SceneQueryTypeMask.Fx).

  其实现在这里,差不多就清楚了,减少渲染批次,StaticGeometry与InstancedGeometry就有这个效果,我们查看相应源码GeometryBucket.cs里的类GeometryBucket与他的方法Assign,Build就很清楚了,思路有点像前文用EdgeListBuilder分析模型重组得到所有顶点,三角形这个过程.Build就是把多个顶点缓冲与索引缓冲组合成一个顶点缓冲与一个索引缓冲的情况,当然根据MaterialBucket这个类来看,肯定是只能组合同材质的,并且我们不需要考虑staticGeometry里模型的LOD,材质等情况,我们完全能对应我们这里的需求,写个简单方便易用的类.

  1. public class MergerBatch
  2. {
  3. private List<Vector3> positions;
  4. private List<uint> colors;
  5. private List<int> indexs;
  6.  
  7. private int startIndex = ;
  8. private AxisAlignedBox box = null;
  9.  
  10. private bool HaveColor
  11. {
  12. get
  13. {
  14. return this.colors != null && this.colors.Count > ;
  15. }
  16. }
  17.  
  18. public MergerBatch()
  19. {
  20. positions = new List<Vector3>();
  21. colors = new List<uint>();
  22. indexs = new List<int>();
  23. startIndex = ;
  24. box = new AxisAlignedBox();
  25. }
  26.  
  27. public void AddBatch(IList<Vector3> pos, IList<int> index, ColorEx color)
  28. {
  29. foreach (var p in pos)
  30. {
  31. this.positions.Add(p);
  32. this.colors.Add((uint)color.ToRGBA());
  33. box.Merge(p);
  34. }
  35. foreach (var i in index)
  36. {
  37. this.indexs.Add(i + startIndex);
  38. }
  39. startIndex = this.positions.Count;
  40. }
  41.  
  42. public Mesh ToMesh(string meshName, bool bRecreate = false)
  43. {
  44. var mesh = MeshManager.Instance.GetByName(meshName) as Mesh;
  45. if (mesh != null)
  46. {
  47. if (bRecreate)
  48. {
  49. MeshManager.Instance.Remove(meshName);
  50. }
  51. else
  52. {
  53. return mesh;
  54. }
  55. }
  56. mesh = MeshManager.Instance.CreateManual(meshName, "General", null);
  57.  
  58. mesh.SharedVertexData = new VertexData();
  59. mesh.SharedVertexData.vertexCount = this.positions.Count;
  60.  
  61. //顶点位置数据写入顶点缓冲
  62. VertexDeclaration decl = mesh.SharedVertexData.vertexDeclaration;
  63. decl.AddElement(, , VertexElementType.Float3, VertexElementSemantic.Position);
  64. HardwareVertexBuffer vbuf =
  65. HardwareBufferManager.Instance.CreateVertexBuffer(
  66. decl.Clone(), mesh.SharedVertexData.vertexCount, BufferUsage.StaticWriteOnly);
  67. vbuf.WriteData(, vbuf.Size, this.positions.ToArray(), true);
  68. //写入颜色
  69. decl.AddElement(, , VertexElementType.Color, VertexElementSemantic.Diffuse);
  70. HardwareVertexBuffer cbuf =
  71. HardwareBufferManager.Instance.CreateVertexBuffer(
  72. decl.Clone(), mesh.SharedVertexData.vertexCount, BufferUsage.StaticWriteOnly);
  73. cbuf.WriteData(, cbuf.Size, this.colors.ToArray(), true);
  74.  
  75. VertexBufferBinding bind = mesh.SharedVertexData.vertexBufferBinding;
  76. bind.SetBinding(, vbuf);
  77. bind.SetBinding(, cbuf);
  78.  
  79. //建立一个subMesh
  80.  
  81. HardwareIndexBuffer ibuf = HardwareBufferManager.Instance.CreateIndexBuffer(IndexType.Size32, this.indexs.Count, BufferUsage.StaticWriteOnly);
  82. ibuf.WriteData(, ibuf.Size, this.indexs.ToArray(), true);
  83.  
  84. SubMesh sub = mesh.CreateSubMesh();
  85. sub.useSharedVertices = true;
  86. sub.indexData = new IndexData();
  87. sub.indexData.indexBuffer = ibuf;
  88. sub.indexData.indexCount = this.indexs.Count;
  89. sub.indexData.indexStart = ;
  90. //sub.MaterialName = "Voxel/Default";
  91.  
  92. mesh.BoundingBox = box;
  93. mesh.Load();
  94. return mesh;
  95. }
  96.  
  97. public void Clear()
  98. {
  99. this.positions.Clear();
  100. this.colors.Clear();
  101. this.indexs.Clear();
  102. startIndex = ;
  103. }
  104. }

MergerBatch

  这个类把多次顶点与索引组合成一个Mesh,我们需要一个方便生成一些简单几何模型的类,下面是从开源项目HeilxToolkit里相关代码移植过来的.

  1. public class MeshBuilder
  2. {
  3. /// <summary>
  4. /// 'All curves should have the same number of Vector2s' exception message.
  5. /// </summary>
  6. private const string AllCurvesShouldHaveTheSameNumberOfVector2s =
  7. "All curves should have the same number of Vector2s";
  8.  
  9. /// <summary>
  10. /// 'Source mesh normal vectors should not be null' exception message.
  11. /// </summary>
  12. private const string SourceMeshNormalsShouldNotBeNull = "Source mesh normal vectors should not be null.";
  13.  
  14. /// <summary>
  15. /// 'Source mesh texture coordinates should not be null' exception message.
  16. /// </summary>
  17. private const string SourceMeshTextureCoordinatesShouldNotBeNull =
  18. "Source mesh texture coordinates should not be null.";
  19.  
  20. /// <summary>
  21. /// 'Wrong number of diameters' exception message.
  22. /// </summary>
  23. private const string WrongNumberOfDiameters = "Wrong number of diameters.";
  24.  
  25. /// <summary>
  26. /// 'Wrong number of angles' exception message.
  27. /// </summary>
  28. private const string WrongNumberOfAngles = "Wrong number of angles.";
  29.  
  30. /// <summary>
  31. /// 'Wrong number of positions' exception message.
  32. /// </summary>
  33. private const string WrongNumberOfPositions = "Wrong number of positions.";
  34.  
  35. /// <summary>
  36. /// 'Wrong number of normal vectors' exception message.
  37. /// </summary>
  38. private const string WrongNumberOfNormals = "Wrong number of normal vectors.";
  39.  
  40. /// <summary>
  41. /// 'Wrong number of texture coordinates' exception message.
  42. /// </summary>
  43. private const string WrongNumberOfTextureCoordinates = "Wrong number of texture coordinates.";
  44.  
  45. /// <summary>
  46. /// The circle cache.
  47. /// </summary>
  48. private static readonly ThreadLocal<Dictionary<int, IList<Vector2>>> CircleCache = new ThreadLocal<Dictionary<int, IList<Vector2>>>(() => new Dictionary<int, IList<Vector2>>());
  49.  
  50. /// <summary>
  51. /// The unit sphere cache.
  52. /// </summary>
  53. private static readonly ThreadLocal<Dictionary<int, MeshGeometry3D>> UnitSphereCache = new ThreadLocal<Dictionary<int, MeshGeometry3D>>(() => new Dictionary<int, MeshGeometry3D>());
  54.  
  55. /// <summary>
  56. /// The normal vectors.
  57. /// </summary>
  58. private IList<Vector3> normals;
  59.  
  60. /// <summary>
  61. /// The positions.
  62. /// </summary>
  63. private IList<Vector3> positions;
  64.  
  65. /// <summary>
  66. /// The texture coordinates.
  67. /// </summary>
  68. private IList<Vector2> textureCoordinates;
  69.  
  70. /// <summary>
  71. /// The triangle indices.
  72. /// </summary>
  73. private IList<int> triangleIndices;
  74.  
  75. /// <summary>
  76. /// Initializes a new instance of the <see cref="MeshBuilder"/> class.
  77. /// </summary>
  78. /// <remarks>
  79. /// Normal and texture coordinate generation are included.
  80. /// </remarks>
  81. public MeshBuilder()
  82. : this(true, true)
  83. {
  84. }
  85.  
  86. /// <summary>
  87. /// Initializes a new instance of the <see cref="MeshBuilder"/> class.
  88. /// </summary>
  89. /// <param name="generateNormals">
  90. /// Generate normal vectors.
  91. /// </param>
  92. /// <param name="generateTextureCoordinates">
  93. /// Generate texture coordinates.
  94. /// </param>
  95. public MeshBuilder(bool generateNormals, bool generateTextureCoordinates)
  96. {
  97. this.positions = new List<Vector3>();
  98. this.triangleIndices = new List<int>();
  99.  
  100. if (generateNormals)
  101. {
  102. this.normals = new List<Vector3>();
  103. }
  104.  
  105. if (generateTextureCoordinates)
  106. {
  107. this.textureCoordinates = new List<Vector2>();
  108. }
  109. }
  110.  
  111. /// <summary>
  112. /// Box face enumeration.
  113. /// </summary>
  114. [Flags]
  115. public enum BoxFaces
  116. {
  117. /// <summary>
  118. /// The top.
  119. /// </summary>
  120. Top = 0x1,
  121.  
  122. /// <summary>
  123. /// The bottom.
  124. /// </summary>
  125. Bottom = 0x2,
  126.  
  127. /// <summary>
  128. /// The left side.
  129. /// </summary>
  130. Left = 0x4,
  131.  
  132. /// <summary>
  133. /// The right side.
  134. /// </summary>
  135. Right = 0x8,
  136.  
  137. /// <summary>
  138. /// The front side.
  139. /// </summary>
  140. Front = 0x10,
  141.  
  142. /// <summary>
  143. /// The back side.
  144. /// </summary>
  145. Back = 0x20,
  146.  
  147. /// <summary>
  148. /// All sides.
  149. /// </summary>
  150. All = Top | Bottom | Left | Right | Front | Back
  151. }
  152.  
  153. /// <summary>
  154. /// Gets the normal vectors of the mesh.
  155. /// </summary>
  156. /// <value>The normal vectors.</value>
  157. public IList<Vector3> Normals
  158. {
  159. get
  160. {
  161. return this.normals;
  162. }
  163. }
  164.  
  165. /// <summary>
  166. /// Gets the positions collection of the mesh.
  167. /// </summary>
  168. /// <value> The positions. </value>
  169. public IList<Vector3> Positions
  170. {
  171. get
  172. {
  173. return this.positions;
  174. }
  175. }
  176.  
  177. /// <summary>
  178. /// Gets the texture coordinates of the mesh.
  179. /// </summary>
  180. /// <value>The texture coordinates.</value>
  181. public IList<Vector2> TextureCoordinates
  182. {
  183. get
  184. {
  185. return this.textureCoordinates;
  186. }
  187. }
  188.  
  189. /// <summary>
  190. /// Gets the triangle indices.
  191. /// </summary>
  192. /// <value>The triangle indices.</value>
  193. public IList<int> TriangleIndices
  194. {
  195. get
  196. {
  197. return this.triangleIndices;
  198. }
  199. }
  200.  
  201. /// <summary>
  202. /// Gets or sets a value indicating whether to create normal vectors.
  203. /// </summary>
  204. /// <value>
  205. /// <c>true</c> if normal vectors should be created; otherwise, <c>false</c>.
  206. /// </value>
  207. public bool CreateNormals
  208. {
  209. get
  210. {
  211. return this.normals != null;
  212. }
  213.  
  214. set
  215. {
  216. if (value && this.normals == null)
  217. {
  218. this.normals = new List<Vector3>();
  219. }
  220.  
  221. if (!value)
  222. {
  223. this.normals = null;
  224. }
  225. }
  226. }
  227.  
  228. /// <summary>
  229. /// Gets or sets a value indicating whether to create texture coordinates.
  230. /// </summary>
  231. /// <value>
  232. /// <c>true</c> if texture coordinates should be created; otherwise, <c>false</c>.
  233. /// </value>
  234. public bool CreateTextureCoordinates
  235. {
  236. get
  237. {
  238. return this.textureCoordinates != null;
  239. }
  240.  
  241. set
  242. {
  243. if (value && this.textureCoordinates == null)
  244. {
  245. this.textureCoordinates = new List<Vector2>();
  246. }
  247.  
  248. if (!value)
  249. {
  250. this.textureCoordinates = null;
  251. }
  252. }
  253. }
  254.  
  255. /// <summary>
  256. /// Gets a circle section (cached).
  257. /// </summary>
  258. /// <param name="thetaDiv">
  259. /// The number of division.
  260. /// </param>
  261. /// <returns>
  262. /// A circle.
  263. /// </returns>
  264. public static IList<Vector2> GetCircle(int thetaDiv)
  265. {
  266. IList<Vector2> circle;
  267. if (!CircleCache.Value.TryGetValue(thetaDiv, out circle))
  268. {
  269. circle = new List<Vector2>();
  270. CircleCache.Value.Add(thetaDiv, circle);
  271. for (int i = ; i < thetaDiv; i++)
  272. {
  273. double theta = Math.PI * * ((double)i / (thetaDiv - ));
  274. circle.Add(new Vector2(Math.Cos(theta), -Math.Sin(theta)));
  275. }
  276. }
  277. return circle;
  278. }
  279.  
  280. /// <summary>
  281. /// Adds an arrow to the mesh.
  282. /// </summary>
  283. /// <param name="Vector21">
  284. /// The start Vector2.
  285. /// </param>
  286. /// <param name="Vector22">
  287. /// The end Vector2.
  288. /// </param>
  289. /// <param name="diameter">
  290. /// The diameter of the arrow cylinder.
  291. /// </param>
  292. /// <param name="headLength">
  293. /// Length of the head (relative to diameter).
  294. /// </param>
  295. /// <param name="thetaDiv">
  296. /// The number of divisions around the arrow.
  297. /// </param>
  298. public void AddArrow(Vector3 Vector21, Vector3 Vector22, double diameter, double headLength = , int thetaDiv = )
  299. {
  300. var dir = Vector22 - Vector21;
  301. double length = dir.Length;
  302. double r = diameter / ;
  303.  
  304. var pc = new List<Vector2>
  305. {
  306. new Vector2(, ),
  307. new Vector2(, r),
  308. new Vector2(length - (diameter * headLength), r),
  309. new Vector2(length - (diameter * headLength), r * ),
  310. new Vector2(length, )
  311. };
  312.  
  313. this.AddRevolvedGeometry(pc, null, Vector21, dir, thetaDiv);
  314. }
  315.  
  316. /// <summary>
  317. /// Adds the edges of a bounding box as cylinders.
  318. /// </summary>
  319. /// <param name="boundingBox">
  320. /// The bounding box.
  321. /// </param>
  322. /// <param name="diameter">
  323. /// The diameter of the cylinders.
  324. /// </param>
  325. public void AddBoundingBox(Rect boundingBox, double diameter)
  326. {
  327. var p0 = new Vector3(boundingBox.X, boundingBox.Y, boundingBox.Z);
  328. var p1 = new Vector3(boundingBox.X, boundingBox.Y + boundingBox.SizeY, boundingBox.Z);
  329. var p2 = new Vector3(boundingBox.X + boundingBox.SizeX, boundingBox.Y + boundingBox.SizeY, boundingBox.Z);
  330. var p3 = new Vector3(boundingBox.X + boundingBox.SizeX, boundingBox.Y, boundingBox.Z);
  331. var p4 = new Vector3(boundingBox.X, boundingBox.Y, boundingBox.Z + boundingBox.SizeZ);
  332. var p5 = new Vector3(boundingBox.X, boundingBox.Y + boundingBox.SizeY, boundingBox.Z + boundingBox.SizeZ);
  333. var p6 = new Vector3(boundingBox.X + boundingBox.SizeX, boundingBox.Y + boundingBox.SizeY, boundingBox.Z + boundingBox.SizeZ);
  334. var p7 = new Vector3(boundingBox.X + boundingBox.SizeX, boundingBox.Y, boundingBox.Z + boundingBox.SizeZ);
  335.  
  336. Action<Vector3, Vector3> addEdge = (c1, c2) => this.AddCylinder(c1, c2, diameter, );
  337.  
  338. addEdge(p0, p1);
  339. addEdge(p1, p2);
  340. addEdge(p2, p3);
  341. addEdge(p3, p0);
  342.  
  343. addEdge(p4, p5);
  344. addEdge(p5, p6);
  345. addEdge(p6, p7);
  346. addEdge(p7, p4);
  347.  
  348. addEdge(p0, p4);
  349. addEdge(p1, p5);
  350. addEdge(p2, p6);
  351. addEdge(p3, p7);
  352. }
  353.  
  354. /// <summary>
  355. /// Adds a box aligned with the X, Y and Z axes.
  356. /// </summary>
  357. /// <param name="center">
  358. /// The center Vector2 of the box.
  359. /// </param>
  360. /// <param name="xlength">
  361. /// The length of the box along the X axis.
  362. /// </param>
  363. /// <param name="ylength">
  364. /// The length of the box along the Y axis.
  365. /// </param>
  366. /// <param name="zlength">
  367. /// The length of the box along the Z axis.
  368. /// </param>
  369. public void AddBox(Vector3 center, double xlength, double ylength, double zlength)
  370. {
  371. this.AddBox(center, xlength, ylength, zlength, BoxFaces.All);
  372. }
  373.  
  374. /// <summary>
  375. /// Adds a box aligned with the X, Y and Z axes.
  376. /// </summary>
  377. /// <param name="rectangle">
  378. /// The 3-D "rectangle".
  379. /// </param>
  380. public void AddBox(Rect rectangle)
  381. {
  382. this.AddBox(
  383. new Vector3(rectangle.X + (rectangle.SizeX * 0.5), rectangle.Y + (rectangle.SizeY * 0.5), rectangle.Z + (rectangle.SizeZ * 0.5)),
  384. rectangle.SizeX,
  385. rectangle.SizeY,
  386. rectangle.SizeZ,
  387. BoxFaces.All);
  388. }
  389.  
  390. /// <summary>
  391. /// Adds a box with the specified faces, aligned with the X, Y and Z axes.
  392. /// </summary>
  393. /// <param name="center">
  394. /// The center Vector2 of the box.
  395. /// </param>
  396. /// <param name="xlength">
  397. /// The length of the box along the X axis.
  398. /// </param>
  399. /// <param name="ylength">
  400. /// The length of the box along the Y axis.
  401. /// </param>
  402. /// <param name="zlength">
  403. /// The length of the box along the Z axis.
  404. /// </param>
  405. /// <param name="faces">
  406. /// The faces to include.
  407. /// </param>
  408. public void AddBox(Vector3 center, double xlength, double ylength, double zlength, BoxFaces faces)
  409. {
  410. this.AddBox(center, new Vector3(, , ), new Vector3(, , ), xlength, ylength, zlength, faces);
  411. }
  412.  
  413. /// <summary>
  414. /// Adds a box with the specified faces, aligned with the specified axes.
  415. /// </summary>
  416. /// <param name="center">The center Vector2 of the box.</param>
  417. /// <param name="x">The x axis.</param>
  418. /// <param name="y">The y axis.</param>
  419. /// <param name="xlength">The length of the box along the X axis.</param>
  420. /// <param name="ylength">The length of the box along the Y axis.</param>
  421. /// <param name="zlength">The length of the box along the Z axis.</param>
  422. /// <param name="faces">The faces to include.</param>
  423. public void AddBox(Vector3 center, Vector3 x, Vector3 y, double xlength, double ylength, double zlength, BoxFaces faces = BoxFaces.All)
  424. {
  425. var z = x.Cross(y);
  426. if ((faces & BoxFaces.Front) == BoxFaces.Front)
  427. {
  428. this.AddCubeFace(center, x, z, xlength, ylength, zlength);
  429. }
  430.  
  431. if ((faces & BoxFaces.Back) == BoxFaces.Back)
  432. {
  433. this.AddCubeFace(center, -x, z, xlength, ylength, zlength);
  434. }
  435.  
  436. if ((faces & BoxFaces.Left) == BoxFaces.Left)
  437. {
  438. this.AddCubeFace(center, -y, z, ylength, xlength, zlength);
  439. }
  440.  
  441. if ((faces & BoxFaces.Right) == BoxFaces.Right)
  442. {
  443. this.AddCubeFace(center, y, z, ylength, xlength, zlength);
  444. }
  445.  
  446. if ((faces & BoxFaces.Top) == BoxFaces.Top)
  447. {
  448. this.AddCubeFace(center, z, y, zlength, xlength, ylength);
  449. }
  450.  
  451. if ((faces & BoxFaces.Bottom) == BoxFaces.Bottom)
  452. {
  453. this.AddCubeFace(center, -z, y, zlength, xlength, ylength);
  454. }
  455. }
  456.  
  457. /// <summary>
  458. /// Adds a (possibly truncated) cone.
  459. /// </summary>
  460. /// <param name="origin">
  461. /// The origin.
  462. /// </param>
  463. /// <param name="direction">
  464. /// The direction (normalization not required).
  465. /// </param>
  466. /// <param name="baseRadius">
  467. /// The base radius.
  468. /// </param>
  469. /// <param name="topRadius">
  470. /// The top radius.
  471. /// </param>
  472. /// <param name="height">
  473. /// The height.
  474. /// </param>
  475. /// <param name="baseCap">
  476. /// Include a base cap if set to <c>true</c> .
  477. /// </param>
  478. /// <param name="topCap">
  479. /// Include the top cap if set to <c>true</c> .
  480. /// </param>
  481. /// <param name="thetaDiv">
  482. /// The number of divisions around the cone.
  483. /// </param>
  484. /// <remarks>
  485. /// See http://en.wikipedia.org/wiki/Cone_(geometry).
  486. /// </remarks>
  487. public void AddCone(
  488. Vector3 origin,
  489. Vector3 direction,
  490. double baseRadius,
  491. double topRadius,
  492. double height,
  493. bool baseCap,
  494. bool topCap,
  495. int thetaDiv)
  496. {
  497. var pc = new List<Vector2>();
  498. var tc = new List<double>();
  499. if (baseCap)
  500. {
  501. pc.Add(new Vector2(, ));
  502. tc.Add();
  503. }
  504.  
  505. pc.Add(new Vector2(, baseRadius));
  506. tc.Add();
  507. pc.Add(new Vector2(height, topRadius));
  508. tc.Add();
  509. if (topCap)
  510. {
  511. pc.Add(new Vector2(height, ));
  512. tc.Add();
  513. }
  514.  
  515. this.AddRevolvedGeometry(pc, tc, origin, direction, thetaDiv);
  516. }
  517.  
  518. /// <summary>
  519. /// Adds a cone.
  520. /// </summary>
  521. /// <param name="origin">The origin Vector2.</param>
  522. /// <param name="apex">The apex Vector2.</param>
  523. /// <param name="baseRadius">The base radius.</param>
  524. /// <param name="baseCap">
  525. /// Include a base cap if set to <c>true</c> .
  526. /// </param>
  527. /// <param name="thetaDiv">The theta div.</param>
  528. public void AddCone(Vector3 origin, Vector3 apex, double baseRadius, bool baseCap, int thetaDiv)
  529. {
  530. var dir = apex - origin;
  531. this.AddCone(origin, dir, baseRadius, , dir.Length, baseCap, false, thetaDiv);
  532. }
  533.  
  534. /// <summary>
  535. /// Adds a cube face.
  536. /// </summary>
  537. /// <param name="center">
  538. /// The center of the cube.
  539. /// </param>
  540. /// <param name="normal">
  541. /// The normal vector for the face.
  542. /// </param>
  543. /// <param name="up">
  544. /// The up vector for the face.
  545. /// </param>
  546. /// <param name="dist">
  547. /// The distance from the center of the cube to the face.
  548. /// </param>
  549. /// <param name="width">
  550. /// The width of the face.
  551. /// </param>
  552. /// <param name="height">
  553. /// The height of the face.
  554. /// </param>
  555. public void AddCubeFace(Vector3 center, Vector3 normal, Vector3 up, double dist, double width, double height)
  556. {
  557. var right = normal.Cross(up);
  558. var n = normal * dist / ;
  559. up *= height / ;
  560. right *= width / ;
  561. var p1 = center + n - up - right;
  562. var p2 = center + n - up + right;
  563. var p3 = center + n + up + right;
  564. var p4 = center + n + up - right;
  565.  
  566. int i0 = this.positions.Count;
  567. this.positions.Add(p1);
  568. this.positions.Add(p2);
  569. this.positions.Add(p3);
  570. this.positions.Add(p4);
  571. if (this.normals != null)
  572. {
  573. this.normals.Add(normal);
  574. this.normals.Add(normal);
  575. this.normals.Add(normal);
  576. this.normals.Add(normal);
  577. }
  578.  
  579. if (this.textureCoordinates != null)
  580. {
  581. this.textureCoordinates.Add(new Vector2(, ));
  582. this.textureCoordinates.Add(new Vector2(, ));
  583. this.textureCoordinates.Add(new Vector2(, ));
  584. this.textureCoordinates.Add(new Vector2(, ));
  585. }
  586.  
  587. this.triangleIndices.Add(i0 + );
  588. this.triangleIndices.Add(i0 + );
  589. this.triangleIndices.Add(i0 + );
  590. this.triangleIndices.Add(i0 + );
  591. this.triangleIndices.Add(i0 + );
  592. this.triangleIndices.Add(i0 + );
  593. }
  594.  
  595. /// <summary>
  596. /// Adds a cylinder to the mesh.
  597. /// </summary>
  598. /// <param name="p1">
  599. /// The first Vector2.
  600. /// </param>
  601. /// <param name="p2">
  602. /// The second Vector2.
  603. /// </param>
  604. /// <param name="diameter">
  605. /// The diameters.
  606. /// </param>
  607. /// <param name="thetaDiv">
  608. /// The number of divisions around the cylinder.
  609. /// </param>
  610. /// <remarks>
  611. /// See http://en.wikipedia.org/wiki/Cylinder_(geometry).
  612. /// </remarks>
  613. public void AddCylinder(Vector3 p1, Vector3 p2, double diameter, int thetaDiv)
  614. {
  615. Vector3 n = p2 - p1;
  616. double l = n.Length;
  617. n.Normalize();
  618. this.AddCone(p1, n, diameter / , diameter / , l, false, false, thetaDiv);
  619. }
  620.  
  621. /// <summary>
  622. /// Adds a collection of edges as cylinders.
  623. /// </summary>
  624. /// <param name="Vector2s">
  625. /// The Vector2s.
  626. /// </param>
  627. /// <param name="edges">
  628. /// The edge indices.
  629. /// </param>
  630. /// <param name="diameter">
  631. /// The diameter of the cylinders.
  632. /// </param>
  633. /// <param name="thetaDiv">
  634. /// The number of divisions around the cylinders.
  635. /// </param>
  636. public void AddEdges(IList<Vector3> Vector2s, IList<int> edges, double diameter, int thetaDiv)
  637. {
  638. for (int i = ; i < edges.Count - ; i += )
  639. {
  640. this.AddCylinder(Vector2s[edges[i]], Vector2s[edges[i + ]], diameter, thetaDiv);
  641. }
  642. }
  643.  
  644. /// <summary>
  645. /// Adds an extruded surface of the specified curve.
  646. /// </summary>
  647. /// <param name="Vector2s">
  648. /// The 2D Vector2s describing the curve to extrude.
  649. /// </param>
  650. /// <param name="xaxis">
  651. /// The x-axis.
  652. /// </param>
  653. /// <param name="p0">
  654. /// The start origin of the extruded surface.
  655. /// </param>
  656. /// <param name="p1">
  657. /// The end origin of the extruded surface.
  658. /// </param>
  659. /// <remarks>
  660. /// The y-axis is determined by the cross product between the specified x-axis and the p1-origin vector.
  661. /// </remarks>
  662. public void AddExtrudedGeometry(IList<Vector2> Vector2s, Vector3 xaxis, Vector3 p0, Vector3 p1)
  663. {
  664. var ydirection = xaxis.Cross(p1 - p0);
  665. ydirection.Normalize();
  666. xaxis.Normalize();
  667.  
  668. int index0 = this.positions.Count;
  669. int np = * Vector2s.Count;
  670. foreach (var p in Vector2s)
  671. {
  672. var v = (xaxis * p.x) + (ydirection * p.y);
  673. this.positions.Add(p0 + v);
  674. this.positions.Add(p1 + v);
  675. v.Normalize();
  676. if (this.normals != null)
  677. {
  678. this.normals.Add(v);
  679. this.normals.Add(v);
  680. }
  681.  
  682. if (this.textureCoordinates != null)
  683. {
  684. this.textureCoordinates.Add(new Vector2(, ));
  685. this.textureCoordinates.Add(new Vector2(, ));
  686. }
  687.  
  688. int i1 = index0 + ;
  689. int i2 = (index0 + ) % np;
  690. int i3 = ((index0 + ) % np) + ;
  691.  
  692. this.triangleIndices.Add(i1);
  693. this.triangleIndices.Add(i2);
  694. this.triangleIndices.Add(index0);
  695.  
  696. this.triangleIndices.Add(i1);
  697. this.triangleIndices.Add(i3);
  698. this.triangleIndices.Add(i2);
  699. }
  700. }
  701.  
  702. /// <summary>
  703. /// Adds a polygon.
  704. /// </summary>
  705. /// <param name="Vector2s">The 2D Vector2s defining the polygon.</param>
  706. /// <param name="axisX">The x axis.</param>
  707. /// <param name="axisY">The y axis.</param>
  708. /// <param name="origin">The origin.</param>
  709. public void AddPolygon(IList<Vector2> Vector2s, Vector3 axisX, Vector3 axisY, Vector3 origin)
  710. {
  711. var indices = CuttingEarsTriangulator.Triangulate(Vector2s);
  712. var index0 = this.positions.Count;
  713. foreach (var p in Vector2s)
  714. {
  715. this.positions.Add(origin + (axisX * p.x) + (axisY * p.y));
  716. }
  717.  
  718. foreach (var i in indices)
  719. {
  720. this.triangleIndices.Add(index0 + i);
  721. }
  722. }
  723.  
  724. /// <summary>
  725. /// Adds an extruded surface of the specified line segments.
  726. /// </summary>
  727. /// <param name="Vector2s">The 2D Vector2s describing the line segments to extrude. The number of Vector2s must be even.</param>
  728. /// <param name="axisX">The x-axis.</param>
  729. /// <param name="p0">The start origin of the extruded surface.</param>
  730. /// <param name="p1">The end origin of the extruded surface.</param>
  731. /// <remarks>The y-axis is determined by the cross product between the specified x-axis and the p1-origin vector.</remarks>
  732. public void AddExtrudedSegments(IList<Vector2> Vector2s, Vector3 axisX, Vector3 p0, Vector3 p1)
  733. {
  734. if (Vector2s.Count % != )
  735. {
  736. throw new InvalidOperationException("The number of Vector2s should be even.");
  737. }
  738.  
  739. var axisY = axisX.Cross(p1 - p0);
  740. axisY.Normalize();
  741. axisX.Normalize();
  742. int index0 = this.positions.Count;
  743.  
  744. for (int i = ; i < Vector2s.Count; i++)
  745. {
  746. var p = Vector2s[i];
  747. var d = (axisX * p.x) + (axisY * p.y);
  748. this.positions.Add(p0 + d);
  749. this.positions.Add(p1 + d);
  750.  
  751. if (this.normals != null)
  752. {
  753. d.Normalize();
  754. this.normals.Add(d);
  755. this.normals.Add(d);
  756. }
  757.  
  758. if (this.textureCoordinates != null)
  759. {
  760. var v = i / (Vector2s.Count - 1d);
  761. this.textureCoordinates.Add(new Vector2(, v));
  762. this.textureCoordinates.Add(new Vector2(, v));
  763. }
  764. }
  765.  
  766. int n = Vector2s.Count - ;
  767. for (int i = ; i < n; i++)
  768. {
  769. int i0 = index0 + (i * );
  770. int i1 = i0 + ;
  771. int i2 = i0 + ;
  772. int i3 = i0 + ;
  773.  
  774. this.triangleIndices.Add(i0);
  775. this.triangleIndices.Add(i1);
  776. this.triangleIndices.Add(i2);
  777.  
  778. this.triangleIndices.Add(i2);
  779. this.triangleIndices.Add(i3);
  780. this.triangleIndices.Add(i0);
  781. }
  782. }
  783.  
  784. /// <summary>
  785. /// Adds a lofted surface.
  786. /// </summary>
  787. /// <param name="positionsList">
  788. /// List of lofting sections.
  789. /// </param>
  790. /// <param name="normalList">
  791. /// The normal list.
  792. /// </param>
  793. /// <param name="textureCoordinateList">
  794. /// The texture coordinate list.
  795. /// </param>
  796. /// <remarks>
  797. /// See http://en.wikipedia.org/wiki/Loft_(3D).
  798. /// </remarks>
  799. public void AddLoftedGeometry(
  800. IList<IList<Vector3>> positionsList,
  801. IList<IList<Vector3>> normalList,
  802. IList<IList<Vector2>> textureCoordinateList)
  803. {
  804. int index0 = this.positions.Count;
  805. int n = -;
  806. for (int i = ; i < positionsList.Count; i++)
  807. {
  808. var pc = positionsList[i];
  809.  
  810. // check that all curves have same number of Vector2s
  811. if (n == -)
  812. {
  813. n = pc.Count;
  814. }
  815.  
  816. if (pc.Count != n)
  817. {
  818. throw new InvalidOperationException(AllCurvesShouldHaveTheSameNumberOfVector2s);
  819. }
  820.  
  821. // add the Vector2s
  822. foreach (var p in pc)
  823. {
  824. this.positions.Add(p);
  825. }
  826.  
  827. // add normals
  828. if (this.normals != null && normalList != null)
  829. {
  830. var nc = normalList[i];
  831. foreach (var normal in nc)
  832. {
  833. this.normals.Add(normal);
  834. }
  835. }
  836.  
  837. // add texcoords
  838. if (this.textureCoordinates != null && textureCoordinateList != null)
  839. {
  840. var tc = textureCoordinateList[i];
  841. foreach (var t in tc)
  842. {
  843. this.textureCoordinates.Add(t);
  844. }
  845. }
  846. }
  847.  
  848. for (int i = ; i + < positionsList.Count; i++)
  849. {
  850. for (int j = ; j + < n; j++)
  851. {
  852. int i0 = index0 + (i * n) + j;
  853. int i1 = i0 + n;
  854. int i2 = i1 + ;
  855. int i3 = i0 + ;
  856. this.triangleIndices.Add(i0);
  857. this.triangleIndices.Add(i1);
  858. this.triangleIndices.Add(i2);
  859.  
  860. this.triangleIndices.Add(i2);
  861. this.triangleIndices.Add(i3);
  862. this.triangleIndices.Add(i0);
  863. }
  864. }
  865. }
  866.  
  867. /// <summary>
  868. /// Adds a single node.
  869. /// </summary>
  870. /// <param name="position">
  871. /// The position.
  872. /// </param>
  873. /// <param name="normal">
  874. /// The normal.
  875. /// </param>
  876. /// <param name="textureCoordinate">
  877. /// The texture coordinate.
  878. /// </param>
  879. public void AddNode(Vector3 position, Vector3 normal, Vector2 textureCoordinate)
  880. {
  881. this.positions.Add(position);
  882.  
  883. if (this.normals != null)
  884. {
  885. this.normals.Add(normal);
  886. }
  887.  
  888. if (this.textureCoordinates != null)
  889. {
  890. this.textureCoordinates.Add(textureCoordinate);
  891. }
  892. }
  893.  
  894. /// <summary>
  895. /// Adds a (possibly hollow) pipe.
  896. /// </summary>
  897. /// <param name="Vector21">
  898. /// The start Vector2.
  899. /// </param>
  900. /// <param name="Vector22">
  901. /// The end Vector2.
  902. /// </param>
  903. /// <param name="innerDiameter">
  904. /// The inner diameter.
  905. /// </param>
  906. /// <param name="diameter">
  907. /// The outer diameter.
  908. /// </param>
  909. /// <param name="thetaDiv">
  910. /// The number of divisions around the pipe.
  911. /// </param>
  912. public void AddPipe(Vector3 Vector21, Vector3 Vector22, double innerDiameter, double diameter, int thetaDiv)
  913. {
  914. var dir = Vector22 - Vector21;
  915.  
  916. double height = dir.Length;
  917. dir.Normalize();
  918.  
  919. var pc = new List<Vector2>
  920. {
  921. new Vector2(, innerDiameter / ),
  922. new Vector2(, diameter / ),
  923. new Vector2(height, diameter / ),
  924. new Vector2(height, innerDiameter / )
  925. };
  926.  
  927. var tc = new List<double> { , , , };
  928.  
  929. if (innerDiameter > )
  930. {
  931. // Add the inner surface
  932. pc.Add(new Vector2(, innerDiameter / ));
  933. tc.Add();
  934. }
  935.  
  936. this.AddRevolvedGeometry(pc, tc, Vector21, dir, thetaDiv);
  937. }
  938.  
  939. /// <summary>
  940. /// Adds a polygon.
  941. /// </summary>
  942. /// <param name="Vector2s">
  943. /// The Vector2s of the polygon.
  944. /// </param>
  945. /// <remarks>
  946. /// If the number of Vector2s is greater than 4, a triangle fan is used.
  947. /// </remarks>
  948. public void AddPolygon(IList<Vector3> Vector2s)
  949. {
  950. switch (Vector2s.Count)
  951. {
  952. case :
  953. this.AddTriangle(Vector2s[], Vector2s[], Vector2s[]);
  954. break;
  955. case :
  956. this.AddQuad(Vector2s[], Vector2s[], Vector2s[], Vector2s[]);
  957. break;
  958. default:
  959. this.AddTriangleFan(Vector2s);
  960. break;
  961. }
  962. }
  963.  
  964. /// <summary>
  965. /// Adds a pyramid.
  966. /// </summary>
  967. /// <param name="center">
  968. /// The center.
  969. /// </param>
  970. /// <param name="sideLength">
  971. /// Length of the sides of the pyramid.
  972. /// </param>
  973. /// <param name="height">
  974. /// The height of the pyramid.
  975. /// </param>
  976. /// <remarks>
  977. /// See http://en.wikipedia.org/wiki/Pyramid_(geometry).
  978. /// </remarks>
  979. public void AddPyramid(Vector3 center, double sideLength, double height)
  980. {
  981. this.AddPyramid(center, new Vector3(, , ), new Vector3(, , ), sideLength, height);
  982. }
  983.  
  984. /// <summary>
  985. /// Adds a pyramid.
  986. /// </summary>
  987. /// <param name="center">The center.</param>
  988. /// <param name="normal">The normal vector (normalized).</param>
  989. /// <param name="up">The 'up' vector (normalized).</param>
  990. /// <param name="sideLength">Length of the sides of the pyramid.</param>
  991. /// <param name="height">The height of the pyramid.</param>
  992. public void AddPyramid(Vector3 center, Vector3 normal, Vector3 up, double sideLength, double height)
  993. {
  994. var right = normal.Cross(up);
  995. var n = normal * sideLength / ;
  996. up *= height;
  997. right *= sideLength / ;
  998.  
  999. var p1 = center - n - right;
  1000. var p2 = center - n + right;
  1001. var p3 = center + n + right;
  1002. var p4 = center + n - right;
  1003. var p5 = center + up;
  1004.  
  1005. this.AddTriangle(p1, p2, p5);
  1006. this.AddTriangle(p2, p3, p5);
  1007. this.AddTriangle(p3, p4, p5);
  1008. this.AddTriangle(p4, p1, p5);
  1009. }
  1010.  
  1011. /// <summary>
  1012. /// Adds an octahedron.
  1013. /// </summary>
  1014. /// <param name="center">The center.</param>
  1015. /// <param name="normal">The normal vector.</param>
  1016. /// <param name="up">The up vector.</param>
  1017. /// <param name="sideLength">Length of the side.</param>
  1018. /// <param name="height">The half height of the octahedron.</param>
  1019. /// <remarks>See <a href="http://en.wikipedia.org/wiki/Octahedron">Octahedron</a>.</remarks>
  1020. public void AddOctahedron(Vector3 center, Vector3 normal, Vector3 up, double sideLength, double height)
  1021. {
  1022. var right = normal.Cross(up);
  1023. var n = normal * sideLength / ;
  1024. up *= height / ;
  1025. right *= sideLength / ;
  1026.  
  1027. var p1 = center - n - up - right;
  1028. var p2 = center - n - up + right;
  1029. var p3 = center + n - up + right;
  1030. var p4 = center + n - up - right;
  1031. var p5 = center + up;
  1032. var p6 = center - up;
  1033.  
  1034. this.AddTriangle(p1, p2, p5);
  1035. this.AddTriangle(p2, p3, p5);
  1036. this.AddTriangle(p3, p4, p5);
  1037. this.AddTriangle(p4, p1, p5);
  1038.  
  1039. this.AddTriangle(p2, p1, p6);
  1040. this.AddTriangle(p3, p2, p6);
  1041. this.AddTriangle(p4, p3, p6);
  1042. this.AddTriangle(p1, p4, p6);
  1043. }
  1044.  
  1045. /// <summary>
  1046. /// Adds a quadrilateral polygon.
  1047. /// </summary>
  1048. /// <param name="p0">
  1049. /// The first Vector2.
  1050. /// </param>
  1051. /// <param name="p1">
  1052. /// The second Vector2.
  1053. /// </param>
  1054. /// <param name="p2">
  1055. /// The third Vector2.
  1056. /// </param>
  1057. /// <param name="p3">
  1058. /// The fourth Vector2.
  1059. /// </param>
  1060. /// <remarks>
  1061. /// See http://en.wikipedia.org/wiki/Quadrilateral.
  1062. /// </remarks>
  1063. public void AddQuad(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3)
  1064. {
  1065. //// The nodes are arranged in counter-clockwise order
  1066. //// p3 p2
  1067. //// +---------------+
  1068. //// | |
  1069. //// | |
  1070. //// +---------------+
  1071. //// origin p1
  1072. var uv0 = new Vector2(, );
  1073. var uv1 = new Vector2(, );
  1074. var uv2 = new Vector2(, );
  1075. var uv3 = new Vector2(, );
  1076. this.AddQuad(p0, p1, p2, p3, uv0, uv1, uv2, uv3);
  1077. }
  1078.  
  1079. /// <summary>
  1080. /// Adds a quadrilateral polygon.
  1081. /// </summary>
  1082. /// <param name="p0">
  1083. /// The first Vector2.
  1084. /// </param>
  1085. /// <param name="p1">
  1086. /// The second Vector2.
  1087. /// </param>
  1088. /// <param name="p2">
  1089. /// The third Vector2.
  1090. /// </param>
  1091. /// <param name="p3">
  1092. /// The fourth Vector2.
  1093. /// </param>
  1094. /// <param name="uv0">
  1095. /// The first texture coordinate.
  1096. /// </param>
  1097. /// <param name="uv1">
  1098. /// The second texture coordinate.
  1099. /// </param>
  1100. /// <param name="uv2">
  1101. /// The third texture coordinate.
  1102. /// </param>
  1103. /// <param name="uv3">
  1104. /// The fourth texture coordinate.
  1105. /// </param>
  1106. /// <remarks>
  1107. /// See http://en.wikipedia.org/wiki/Quadrilateral.
  1108. /// </remarks>
  1109. public void AddQuad(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, Vector2 uv0, Vector2 uv1, Vector2 uv2, Vector2 uv3)
  1110. {
  1111. //// The nodes are arranged in counter-clockwise order
  1112. //// p3 p2
  1113. //// +---------------+
  1114. //// | |
  1115. //// | |
  1116. //// +---------------+
  1117. //// origin p1
  1118. int i0 = this.positions.Count;
  1119.  
  1120. this.positions.Add(p0);
  1121. this.positions.Add(p1);
  1122. this.positions.Add(p2);
  1123. this.positions.Add(p3);
  1124.  
  1125. if (this.textureCoordinates != null)
  1126. {
  1127. this.textureCoordinates.Add(uv0);
  1128. this.textureCoordinates.Add(uv1);
  1129. this.textureCoordinates.Add(uv2);
  1130. this.textureCoordinates.Add(uv3);
  1131. }
  1132.  
  1133. if (this.normals != null)
  1134. {
  1135. var w = (p3 - p0).Cross(p1 - p0);
  1136. w.Normalize();
  1137. this.normals.Add(w);
  1138. this.normals.Add(w);
  1139. this.normals.Add(w);
  1140. this.normals.Add(w);
  1141. }
  1142.  
  1143. this.triangleIndices.Add(i0 + );
  1144. this.triangleIndices.Add(i0 + );
  1145. this.triangleIndices.Add(i0 + );
  1146.  
  1147. this.triangleIndices.Add(i0 + );
  1148. this.triangleIndices.Add(i0 + );
  1149. this.triangleIndices.Add(i0 + );
  1150. }
  1151.  
  1152. /// <summary>
  1153. /// Adds a list of quadrilateral polygons.
  1154. /// </summary>
  1155. /// <param name="quadPositions">
  1156. /// The Vector2s.
  1157. /// </param>
  1158. /// <param name="quadNormals">
  1159. /// The normal vectors.
  1160. /// </param>
  1161. /// <param name="quadTextureCoordinates">
  1162. /// The texture coordinates.
  1163. /// </param>
  1164. public void AddQuads(
  1165. IList<Vector3> quadPositions, IList<Vector3> quadNormals, IList<Vector2> quadTextureCoordinates)
  1166. {
  1167. if (quadPositions == null)
  1168. {
  1169. throw new ArgumentNullException("quadPositions");
  1170. }
  1171.  
  1172. if (this.normals != null && quadNormals == null)
  1173. {
  1174. throw new ArgumentNullException("quadNormals");
  1175. }
  1176.  
  1177. if (this.textureCoordinates != null && quadTextureCoordinates == null)
  1178. {
  1179. throw new ArgumentNullException("quadTextureCoordinates");
  1180. }
  1181.  
  1182. if (quadNormals != null && quadNormals.Count != quadPositions.Count)
  1183. {
  1184. throw new InvalidOperationException(WrongNumberOfNormals);
  1185. }
  1186.  
  1187. if (quadTextureCoordinates != null && quadTextureCoordinates.Count != quadPositions.Count)
  1188. {
  1189. throw new InvalidOperationException(WrongNumberOfTextureCoordinates);
  1190. }
  1191.  
  1192. Debug.Assert(quadPositions.Count > && quadPositions.Count % == , "Wrong number of positions.");
  1193.  
  1194. int index0 = this.positions.Count;
  1195. foreach (var p in quadPositions)
  1196. {
  1197. this.positions.Add(p);
  1198. }
  1199.  
  1200. if (this.textureCoordinates != null && quadTextureCoordinates != null)
  1201. {
  1202. foreach (var tc in quadTextureCoordinates)
  1203. {
  1204. this.textureCoordinates.Add(tc);
  1205. }
  1206. }
  1207.  
  1208. if (this.normals != null && quadNormals != null)
  1209. {
  1210. foreach (var n in quadNormals)
  1211. {
  1212. this.normals.Add(n);
  1213. }
  1214. }
  1215.  
  1216. int indexEnd = this.positions.Count;
  1217. for (int i = index0; i + < indexEnd; i++)
  1218. {
  1219. this.triangleIndices.Add(i);
  1220. this.triangleIndices.Add(i + );
  1221. this.triangleIndices.Add(i + );
  1222.  
  1223. this.triangleIndices.Add(i + );
  1224. this.triangleIndices.Add(i + );
  1225. this.triangleIndices.Add(i);
  1226. }
  1227. }
  1228.  
  1229. /// <summary>
  1230. /// Adds a rectangular mesh (m x n Vector2s).
  1231. /// </summary>
  1232. /// <param name="Vector2s">
  1233. /// The one-dimensional array of Vector2s. The Vector2s are stored row-by-row.
  1234. /// </param>
  1235. /// <param name="columns">
  1236. /// The number of columns in the rectangular mesh.
  1237. /// </param>
  1238. public void AddRectangularMesh(IList<Vector3> Vector2s, int columns)
  1239. {
  1240. if (Vector2s == null)
  1241. {
  1242. throw new ArgumentNullException("Vector2s");
  1243. }
  1244.  
  1245. int index0 = this.Positions.Count;
  1246.  
  1247. foreach (var pt in Vector2s)
  1248. {
  1249. this.positions.Add(pt);
  1250. }
  1251.  
  1252. int rows = Vector2s.Count / columns;
  1253.  
  1254. this.AddRectangularMeshTriangleIndices(index0, rows, columns);
  1255. if (this.normals != null)
  1256. {
  1257. this.AddRectangularMeshNormals(index0, rows, columns);
  1258. }
  1259.  
  1260. if (this.textureCoordinates != null)
  1261. {
  1262. this.AddRectangularMeshTextureCoordinates(rows, columns);
  1263. }
  1264. }
  1265.  
  1266. /// <summary>
  1267. /// Adds a rectangular mesh defined by a two-dimensional array of Vector2s.
  1268. /// </summary>
  1269. /// <param name="Vector2s">
  1270. /// The Vector2s.
  1271. /// </param>
  1272. /// <param name="texCoords">
  1273. /// The texture coordinates (optional).
  1274. /// </param>
  1275. /// <param name="closed0">
  1276. /// set to <c>true</c> if the mesh is closed in the first dimension.
  1277. /// </param>
  1278. /// <param name="closed1">
  1279. /// set to <c>true</c> if the mesh is closed in the second dimension.
  1280. /// </param>
  1281. public void AddRectangularMesh(
  1282. Vector3[,] Vector2s, Vector2[,] texCoords = null, bool closed0 = false, bool closed1 = false)
  1283. {
  1284. if (Vector2s == null)
  1285. {
  1286. throw new ArgumentNullException("Vector2s");
  1287. }
  1288.  
  1289. int rows = Vector2s.GetUpperBound() + ;
  1290. int columns = Vector2s.GetUpperBound() + ;
  1291. int index0 = this.positions.Count;
  1292. for (int i = ; i < rows; i++)
  1293. {
  1294. for (int j = ; j < columns; j++)
  1295. {
  1296. this.positions.Add(Vector2s[i, j]);
  1297. }
  1298. }
  1299.  
  1300. this.AddRectangularMeshTriangleIndices(index0, rows, columns, closed0, closed1);
  1301.  
  1302. if (this.normals != null)
  1303. {
  1304. this.AddRectangularMeshNormals(index0, rows, columns);
  1305. }
  1306.  
  1307. if (this.textureCoordinates != null)
  1308. {
  1309. if (texCoords != null)
  1310. {
  1311. for (int i = ; i < rows; i++)
  1312. {
  1313. for (int j = ; j < columns; j++)
  1314. {
  1315. this.textureCoordinates.Add(texCoords[i, j]);
  1316. }
  1317. }
  1318. }
  1319. else
  1320. {
  1321. this.AddRectangularMeshTextureCoordinates(rows, columns);
  1322. }
  1323. }
  1324. }
  1325.  
  1326. /// <summary>
  1327. /// Adds a regular icosahedron.
  1328. /// </summary>
  1329. /// <param name="center">
  1330. /// The center.
  1331. /// </param>
  1332. /// <param name="radius">
  1333. /// The radius.
  1334. /// </param>
  1335. /// <param name="shareVertices">
  1336. /// Share vertices if set to <c>true</c> .
  1337. /// </param>
  1338. /// <remarks>
  1339. /// See <a href="http://en.wikipedia.org/wiki/Icosahedron">Wikipedia</a> and <a href="http://www.gamedev.net/community/forums/topic.asp?topic_id=283350">link</a>.
  1340. /// </remarks>
  1341. public void AddRegularIcosahedron(Vector3 center, double radius, bool shareVertices)
  1342. {
  1343. double a = Math.Sqrt(2.0 / (5.0 + Math.Sqrt(5.0)));
  1344.  
  1345. double b = Math.Sqrt(2.0 / (5.0 - Math.Sqrt(5.0)));
  1346.  
  1347. var icosahedronIndices = new[]
  1348. {
  1349. , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
  1350. , , , , , , , , , , , , , , , , , , , , , , , , , ,
  1351. };
  1352.  
  1353. var icosahedronVertices = new[]
  1354. {
  1355. new Vector3(-a, , b), new Vector3(a, , b), new Vector3(-a, , -b), new Vector3(a, , -b),
  1356. new Vector3(, b, a), new Vector3(, b, -a), new Vector3(, -b, a), new Vector3(, -b, -a),
  1357. new Vector3(b, a, ), new Vector3(-b, a, ), new Vector3(b, -a, ), new Vector3(-b, -a, )
  1358. };
  1359.  
  1360. if (shareVertices)
  1361. {
  1362. int index0 = this.positions.Count;
  1363. foreach (var v in icosahedronVertices)
  1364. {
  1365. this.positions.Add(center + (v * radius));
  1366. }
  1367.  
  1368. foreach (int i in icosahedronIndices)
  1369. {
  1370. this.triangleIndices.Add(index0 + i);
  1371. }
  1372. }
  1373. else
  1374. {
  1375. for (int i = ; i + < icosahedronIndices.Length; i += )
  1376. {
  1377. this.AddTriangle(
  1378. center + (icosahedronVertices[icosahedronIndices[i]] * radius),
  1379. center + (icosahedronVertices[icosahedronIndices[i + ]] * radius),
  1380. center + (icosahedronVertices[icosahedronIndices[i + ]] * radius));
  1381. }
  1382. }
  1383. }
  1384.  
  1385. /// <summary>
  1386. /// Adds a surface of revolution.
  1387. /// </summary>
  1388. /// <param name="origin">The origin.</param>
  1389. /// <param name="axis">The axis.</param>
  1390. /// <param name="section">The Vector2s defining the curve to revolve.</param>
  1391. /// <param name="sectionIndices">The indices of the line segments of the section.</param>
  1392. /// <param name="thetaDiv">The number of divisions.</param>
  1393. /// <param name="textureValues">The texture values.</param>
  1394. public void AddSurfaceOfRevolution(
  1395. Vector3 origin,
  1396. Vector3 axis,
  1397. IList<Vector2> section,
  1398. IList<int> sectionIndices,
  1399. int thetaDiv = ,
  1400. IList<double> textureValues = null)
  1401. {
  1402. if (this.textureCoordinates != null && textureValues == null)
  1403. {
  1404. throw new ArgumentNullException("textureValues");
  1405. }
  1406.  
  1407. if (textureValues != null && textureValues.Count != section.Count)
  1408. {
  1409. throw new InvalidOperationException(WrongNumberOfTextureCoordinates);
  1410. }
  1411.  
  1412. axis.Normalize();
  1413.  
  1414. // Find two unit vectors orthogonal to the specified direction
  1415. var u = axis.FindAnyPerpendicular();
  1416. var v = axis.Cross(u);
  1417. var circle = GetCircle(thetaDiv);
  1418. int n = section.Count;
  1419. int index0 = this.positions.Count;
  1420. for (int i = ; i < thetaDiv; i++)
  1421. {
  1422. var w = (v * circle[i].x) + (u * circle[i].y);
  1423. for (int j = ; j < n; j++)
  1424. {
  1425. var q1 = origin + (axis * section[j].y) + (w * section[j].x);
  1426. this.positions.Add(q1);
  1427. if (this.normals != null)
  1428. {
  1429. double tx = section[j + ].x - section[j].x;
  1430. double ty = section[j + ].y - section[j].y;
  1431. var normal = (-axis * ty) + (w * tx);
  1432. normal.Normalize();
  1433. this.normals.Add(normal);
  1434. }
  1435.  
  1436. if (this.textureCoordinates != null)
  1437. {
  1438. this.textureCoordinates.Add(new Vector2((double)i / (thetaDiv - ), textureValues == null ? (double)j / (n - ) : textureValues[j]));
  1439. }
  1440. }
  1441. }
  1442.  
  1443. for (int i = ; i < thetaDiv; i++)
  1444. {
  1445. var ii = (i + ) % thetaDiv;
  1446. for (int j = ; j + < sectionIndices.Count; j += )
  1447. {
  1448. var j0 = sectionIndices[j];
  1449. var j1 = sectionIndices[j + ];
  1450.  
  1451. int i0 = index0 + (i * n) + j0;
  1452. int i1 = index0 + (ii * n) + j0;
  1453. int i2 = index0 + (i * n) + j1;
  1454. int i3 = index0 + (ii * n) + j1;
  1455.  
  1456. this.triangleIndices.Add(i0);
  1457. this.triangleIndices.Add(i1);
  1458. this.triangleIndices.Add(i3);
  1459.  
  1460. this.triangleIndices.Add(i3);
  1461. this.triangleIndices.Add(i2);
  1462. this.triangleIndices.Add(i0);
  1463. }
  1464. }
  1465. }
  1466.  
  1467. /// <summary>
  1468. /// Adds a surface of revolution.
  1469. /// </summary>
  1470. /// <param name="Vector2s">The Vector2s (x coordinates are distance from the origin along the axis of revolution, y coordinates are radius, )</param>
  1471. /// <param name="textureValues">The v texture coordinates, one for each Vector2 in the <paramref name="Vector2s" /> list.</param>
  1472. /// <param name="origin">The origin of the revolution axis.</param>
  1473. /// <param name="direction">The direction of the revolution axis.</param>
  1474. /// <param name="thetaDiv">The number of divisions around the mesh.</param>
  1475. /// <remarks>
  1476. /// See http://en.wikipedia.org/wiki/Surface_of_revolution.
  1477. /// </remarks>
  1478. public void AddRevolvedGeometry(IList<Vector2> Vector2s, IList<double> textureValues, Vector3 origin, Vector3 direction, int thetaDiv)
  1479. {
  1480. direction.Normalize();
  1481.  
  1482. // Find two unit vectors orthogonal to the specified direction
  1483. var u = direction.FindAnyPerpendicular();
  1484. var v = direction.Cross(u);
  1485.  
  1486. u.Normalize();
  1487. v.Normalize();
  1488.  
  1489. var circle = GetCircle(thetaDiv);
  1490.  
  1491. int index0 = this.positions.Count;
  1492. int n = Vector2s.Count;
  1493.  
  1494. int totalNodes = (Vector2s.Count - ) * * thetaDiv;
  1495. int rowNodes = (Vector2s.Count - ) * ;
  1496.  
  1497. for (int i = ; i < thetaDiv; i++)
  1498. {
  1499. var w = (v * circle[i].x) + (u * circle[i].y);
  1500.  
  1501. for (int j = ; j + < n; j++)
  1502. {
  1503. // Add segment
  1504. var q1 = origin + (direction * Vector2s[j].x) + (w * Vector2s[j].y);
  1505. var q2 = origin + (direction * Vector2s[j + ].x) + (w * Vector2s[j + ].y);
  1506.  
  1507. // TODO: should not add segment if q1==q2 (corner Vector2)
  1508. // const double eps = 1e-6;
  1509. // if (Vector3.Subtract(q1, q2).LengthSquared < eps)
  1510. // continue;
  1511. this.positions.Add(q1);
  1512. this.positions.Add(q2);
  1513.  
  1514. if (this.normals != null)
  1515. {
  1516. double tx = Vector2s[j + ].x - Vector2s[j].x;
  1517. double ty = Vector2s[j + ].y - Vector2s[j].y;
  1518. var normal = (-direction * ty) + (w * tx);
  1519. normal.Normalize();
  1520.  
  1521. this.normals.Add(normal);
  1522. this.normals.Add(normal);
  1523. }
  1524.  
  1525. if (this.textureCoordinates != null)
  1526. {
  1527. this.textureCoordinates.Add(new Vector2((double)i / (thetaDiv - ), textureValues == null ? (double)j / (n - ) : textureValues[j]));
  1528. this.textureCoordinates.Add(new Vector2((double)i / (thetaDiv - ), textureValues == null ? (double)(j + ) / (n - ) : textureValues[j + ]));
  1529. }
  1530.  
  1531. int i0 = index0 + (i * rowNodes) + (j * );
  1532. int i1 = i0 + ;
  1533. int i2 = index0 + ((((i + ) * rowNodes) + (j * )) % totalNodes);
  1534. int i3 = i2 + ;
  1535.  
  1536. this.triangleIndices.Add(i1);
  1537. this.triangleIndices.Add(i0);
  1538. this.triangleIndices.Add(i2);
  1539.  
  1540. this.triangleIndices.Add(i1);
  1541. this.triangleIndices.Add(i2);
  1542. this.triangleIndices.Add(i3);
  1543. }
  1544. }
  1545. }
  1546.  
  1547. /// <summary>
  1548. /// Adds a sphere (by subdividing a regular icosahedron).
  1549. /// </summary>
  1550. /// <param name="center">
  1551. /// The center of the sphere.
  1552. /// </param>
  1553. /// <param name="radius">
  1554. /// The radius of the sphere.
  1555. /// </param>
  1556. /// <param name="subdivisions">
  1557. /// The number of triangular subdivisions of the original icosahedron.
  1558. /// </param>
  1559. /// <remarks>
  1560. /// See <a href="http://www.fho-emden.de/~hoffmann/ikos27042002.pdf">link</a>.
  1561. /// </remarks>
  1562. public void AddSubdivisionSphere(Vector3 center, double radius, int subdivisions)
  1563. {
  1564. int p0 = this.positions.Count;
  1565. this.Append(GetUnitSphere(subdivisions));
  1566. int p1 = this.positions.Count;
  1567. for (int i = p0; i < p1; i++)
  1568. {
  1569. this.positions[i] = center + (radius * this.positions[i]);
  1570. }
  1571. }
  1572.  
  1573. /// <summary>
  1574. /// Adds a sphere.
  1575. /// </summary>
  1576. /// <param name="center">
  1577. /// The center of the sphere.
  1578. /// </param>
  1579. /// <param name="radius">
  1580. /// The radius of the sphere.
  1581. /// </param>
  1582. /// <param name="thetaDiv">
  1583. /// The number of divisions around the sphere.
  1584. /// </param>
  1585. /// <param name="phiDiv">
  1586. /// The number of divisions from top to bottom of the sphere.
  1587. /// </param>
  1588. public void AddSphere(Vector3 center, double radius, int thetaDiv = , int phiDiv = )
  1589. {
  1590. this.AddEllipsoid(center, radius, radius, radius, thetaDiv, phiDiv);
  1591. }
  1592.  
  1593. /// <summary>
  1594. /// Adds an ellipsoid.
  1595. /// </summary>
  1596. /// <param name="center">
  1597. /// The center of the ellipsoid.
  1598. /// </param>
  1599. /// <param name="radiusx">
  1600. /// The x radius of the ellipsoid.
  1601. /// </param>
  1602. /// <param name="radiusy">
  1603. /// The y radius of the ellipsoid.
  1604. /// </param>
  1605. /// <param name="radiusz">
  1606. /// The z radius of the ellipsoid.
  1607. /// </param>
  1608. /// <param name="thetaDiv">
  1609. /// The number of divisions around the ellipsoid.
  1610. /// </param>
  1611. /// <param name="phiDiv">
  1612. /// The number of divisions from top to bottom of the ellipsoid.
  1613. /// </param>
  1614. public void AddEllipsoid(Vector3 center, double radiusx, double radiusy, double radiusz, int thetaDiv = , int phiDiv = )
  1615. {
  1616. int index0 = this.Positions.Count;
  1617. double dt = * Math.PI / thetaDiv;
  1618. double dp = Math.PI / phiDiv;
  1619.  
  1620. for (int pi = ; pi <= phiDiv; pi++)
  1621. {
  1622. double phi = pi * dp;
  1623.  
  1624. for (int ti = ; ti <= thetaDiv; ti++)
  1625. {
  1626. // we want to start the mesh on the x axis
  1627. double theta = ti * dt;
  1628.  
  1629. // Spherical coordinates
  1630. // http://mathworld.wolfram.com/SphericalCoordinates.html
  1631. double x = Math.Cos(theta) * Math.Sin(phi);
  1632. double y = Math.Sin(theta) * Math.Sin(phi);
  1633. double z = Math.Cos(phi);
  1634.  
  1635. var p = new Vector3(center.x + (radiusx * x), center.y + (radiusy * y), center.z + (radiusz * z));
  1636. this.positions.Add(p);
  1637.  
  1638. if (this.normals != null)
  1639. {
  1640. var n = new Vector3(x, y, z);
  1641. this.normals.Add(n);
  1642. }
  1643.  
  1644. if (this.textureCoordinates != null)
  1645. {
  1646. var uv = new Vector2(theta / ( * Math.PI), phi / Math.PI);
  1647. this.textureCoordinates.Add(uv);
  1648. }
  1649. }
  1650. }
  1651.  
  1652. this.AddRectangularMeshTriangleIndices(index0, phiDiv + , thetaDiv + , true);
  1653. }
  1654.  
  1655. /// <summary>
  1656. /// Adds a triangle.
  1657. /// </summary>
  1658. /// <param name="p0">
  1659. /// The first Vector2.
  1660. /// </param>
  1661. /// <param name="p1">
  1662. /// The second Vector2.
  1663. /// </param>
  1664. /// <param name="p2">
  1665. /// The third Vector2.
  1666. /// </param>
  1667. public void AddTriangle(Vector3 p0, Vector3 p1, Vector3 p2)
  1668. {
  1669. var uv0 = new Vector2(, );
  1670. var uv1 = new Vector2(, );
  1671. var uv2 = new Vector2(, );
  1672. this.AddTriangle(p0, p1, p2, uv0, uv1, uv2);
  1673. }
  1674.  
  1675. /// <summary>
  1676. /// Adds a triangle.
  1677. /// </summary>
  1678. /// <param name="p0">
  1679. /// The first Vector2.
  1680. /// </param>
  1681. /// <param name="p1">
  1682. /// The second Vector2.
  1683. /// </param>
  1684. /// <param name="p2">
  1685. /// The third Vector2.
  1686. /// </param>
  1687. /// <param name="uv0">
  1688. /// The first texture coordinate.
  1689. /// </param>
  1690. /// <param name="uv1">
  1691. /// The second texture coordinate.
  1692. /// </param>
  1693. /// <param name="uv2">
  1694. /// The third texture coordinate.
  1695. /// </param>
  1696. public void AddTriangle(Vector3 p0, Vector3 p1, Vector3 p2, Vector2 uv0, Vector2 uv1, Vector2 uv2)
  1697. {
  1698. int i0 = this.positions.Count;
  1699.  
  1700. this.positions.Add(p0);
  1701. this.positions.Add(p1);
  1702. this.positions.Add(p2);
  1703.  
  1704. if (this.textureCoordinates != null)
  1705. {
  1706. this.textureCoordinates.Add(uv0);
  1707. this.textureCoordinates.Add(uv1);
  1708. this.textureCoordinates.Add(uv2);
  1709. }
  1710.  
  1711. if (this.normals != null)
  1712. {
  1713. var w = (p1 - p0).Cross(p2 - p0);
  1714. w.Normalize();
  1715. this.normals.Add(w);
  1716. this.normals.Add(w);
  1717. this.normals.Add(w);
  1718. }
  1719.  
  1720. this.triangleIndices.Add(i0 + );
  1721. this.triangleIndices.Add(i0 + );
  1722. this.triangleIndices.Add(i0 + );
  1723. }
  1724.  
  1725. /// <summary>
  1726. /// Adds a triangle fan.
  1727. /// </summary>
  1728. /// <param name="vertices">
  1729. /// The vertex indices of the triangle fan.
  1730. /// </param>
  1731. public void AddTriangleFan(IList<int> vertices)
  1732. {
  1733. for (int i = ; i + < vertices.Count; i++)
  1734. {
  1735. this.triangleIndices.Add(vertices[]);
  1736. this.triangleIndices.Add(vertices[i + ]);
  1737. this.triangleIndices.Add(vertices[i + ]);
  1738. }
  1739. }
  1740.  
  1741. /// <summary>
  1742. /// Adds a triangle fan to the mesh
  1743. /// </summary>
  1744. /// <param name="fanPositions">
  1745. /// The Vector2s of the triangle fan.
  1746. /// </param>
  1747. /// <param name="fanNormals">
  1748. /// The normal vectors of the triangle fan.
  1749. /// </param>
  1750. /// <param name="fanTextureCoordinates">
  1751. /// The texture coordinates of the triangle fan.
  1752. /// </param>
  1753. public void AddTriangleFan(
  1754. IList<Vector3> fanPositions, IList<Vector3> fanNormals = null, IList<Vector2> fanTextureCoordinates = null)
  1755. {
  1756. if (this.positions == null)
  1757. {
  1758. throw new ArgumentNullException("fanPositions");
  1759. }
  1760.  
  1761. if (this.normals != null && this.normals == null)
  1762. {
  1763. throw new ArgumentNullException("fanNormals");
  1764. }
  1765.  
  1766. if (this.textureCoordinates != null && this.textureCoordinates == null)
  1767. {
  1768. throw new ArgumentNullException("fanTextureCoordinates");
  1769. }
  1770.  
  1771. int index0 = this.positions.Count;
  1772. foreach (var p in fanPositions)
  1773. {
  1774. this.positions.Add(p);
  1775. }
  1776.  
  1777. if (this.textureCoordinates != null && fanTextureCoordinates != null)
  1778. {
  1779. foreach (var tc in fanTextureCoordinates)
  1780. {
  1781. this.textureCoordinates.Add(tc);
  1782. }
  1783. }
  1784.  
  1785. if (this.normals != null && fanNormals != null)
  1786. {
  1787. foreach (var n in fanNormals)
  1788. {
  1789. this.normals.Add(n);
  1790. }
  1791. }
  1792.  
  1793. int indexEnd = this.positions.Count;
  1794. for (int i = index0; i + < indexEnd; i++)
  1795. {
  1796. this.triangleIndices.Add(index0);
  1797. this.triangleIndices.Add(i + );
  1798. this.triangleIndices.Add(i + );
  1799. }
  1800. }
  1801.  
  1802. /// <summary>
  1803. /// Adds a triangle strip to the mesh.
  1804. /// </summary>
  1805. /// <param name="stripPositions">
  1806. /// The Vector2s of the triangle strip.
  1807. /// </param>
  1808. /// <param name="stripNormals">
  1809. /// The normal vectors of the triangle strip.
  1810. /// </param>
  1811. /// <param name="stripTextureCoordinates">
  1812. /// The texture coordinates of the triangle strip.
  1813. /// </param>
  1814. /// <remarks>
  1815. /// See http://en.wikipedia.org/wiki/Triangle_strip.
  1816. /// </remarks>
  1817. public void AddTriangleStrip(
  1818. IList<Vector3> stripPositions,
  1819. IList<Vector3> stripNormals = null,
  1820. IList<Vector2> stripTextureCoordinates = null)
  1821. {
  1822. if (stripPositions == null)
  1823. {
  1824. throw new ArgumentNullException("stripPositions");
  1825. }
  1826.  
  1827. if (this.normals != null && stripNormals == null)
  1828. {
  1829. throw new ArgumentNullException("stripNormals");
  1830. }
  1831.  
  1832. if (this.textureCoordinates != null && stripTextureCoordinates == null)
  1833. {
  1834. throw new ArgumentNullException("stripTextureCoordinates");
  1835. }
  1836.  
  1837. if (stripNormals != null && stripNormals.Count != stripPositions.Count)
  1838. {
  1839. throw new InvalidOperationException(WrongNumberOfNormals);
  1840. }
  1841.  
  1842. if (stripTextureCoordinates != null && stripTextureCoordinates.Count != stripPositions.Count)
  1843. {
  1844. throw new InvalidOperationException(WrongNumberOfTextureCoordinates);
  1845. }
  1846.  
  1847. int index0 = this.positions.Count;
  1848. for (int i = ; i < stripPositions.Count; i++)
  1849. {
  1850. this.positions.Add(stripPositions[i]);
  1851. if (this.normals != null && stripNormals != null)
  1852. {
  1853. this.normals.Add(stripNormals[i]);
  1854. }
  1855.  
  1856. if (this.textureCoordinates != null && stripTextureCoordinates != null)
  1857. {
  1858. this.textureCoordinates.Add(stripTextureCoordinates[i]);
  1859. }
  1860. }
  1861.  
  1862. int indexEnd = this.positions.Count;
  1863. for (int i = index0; i + < indexEnd; i += )
  1864. {
  1865. this.triangleIndices.Add(i);
  1866. this.triangleIndices.Add(i + );
  1867. this.triangleIndices.Add(i + );
  1868.  
  1869. if (i + < indexEnd)
  1870. {
  1871. this.triangleIndices.Add(i + );
  1872. this.triangleIndices.Add(i + );
  1873. this.triangleIndices.Add(i + );
  1874. }
  1875. }
  1876. }
  1877.  
  1878. /// <summary>
  1879. /// Adds a polygon specified by vertex index (uses a triangle fan).
  1880. /// </summary>
  1881. /// <param name="vertexIndices">The vertex indices.</param>
  1882. public void AddPolygon(IList<int> vertexIndices)
  1883. {
  1884. int n = vertexIndices.Count;
  1885. for (int i = ; i + < n; i++)
  1886. {
  1887. this.triangleIndices.Add(vertexIndices[]);
  1888. this.triangleIndices.Add(vertexIndices[i + ]);
  1889. this.triangleIndices.Add(vertexIndices[i + ]);
  1890. }
  1891. }
  1892.  
  1893. /// <summary>
  1894. /// Adds a polygon defined by vertex indices (uses the cutting ears algorithm).
  1895. /// </summary>
  1896. /// <param name="vertexIndices">The vertex indices.</param>
  1897. public void AddPolygonByCuttingEars(IList<int> vertexIndices)
  1898. {
  1899. var Vector2s = vertexIndices.Select(vi => this.positions[vi]).ToList();
  1900.  
  1901. var poly3D = new Polygon3D(Vector2s);
  1902.  
  1903. // Transform the polygon to 2D
  1904. var poly2D = poly3D.Flatten();
  1905.  
  1906. // Triangulate
  1907. var triangulatedIndices = poly2D.Triangulate();
  1908. if (triangulatedIndices != null)
  1909. {
  1910. foreach (var i in triangulatedIndices)
  1911. {
  1912. this.triangleIndices.Add(vertexIndices[i]);
  1913. }
  1914. }
  1915. }
  1916.  
  1917. /// <summary>
  1918. /// Adds a list of triangles.
  1919. /// </summary>
  1920. /// <param name="trianglePositions">
  1921. /// The Vector2s (the number of Vector2s must be a multiple of 3).
  1922. /// </param>
  1923. /// <param name="triangleNormals">
  1924. /// The normal vectors (corresponding to the Vector2s).
  1925. /// </param>
  1926. /// <param name="triangleTextureCoordinates">
  1927. /// The texture coordinates (corresponding to the Vector2s).
  1928. /// </param>
  1929. public void AddTriangles(
  1930. IList<Vector3> trianglePositions,
  1931. IList<Vector3> triangleNormals = null,
  1932. IList<Vector2> triangleTextureCoordinates = null)
  1933. {
  1934. if (trianglePositions == null)
  1935. {
  1936. throw new ArgumentNullException("trianglePositions");
  1937. }
  1938.  
  1939. if (this.normals != null && triangleNormals == null)
  1940. {
  1941. throw new ArgumentNullException("triangleNormals");
  1942. }
  1943.  
  1944. if (this.textureCoordinates != null && triangleTextureCoordinates == null)
  1945. {
  1946. throw new ArgumentNullException("triangleTextureCoordinates");
  1947. }
  1948.  
  1949. if (trianglePositions.Count % != )
  1950. {
  1951. throw new InvalidOperationException(WrongNumberOfPositions);
  1952. }
  1953.  
  1954. if (triangleNormals != null && triangleNormals.Count != trianglePositions.Count)
  1955. {
  1956. throw new InvalidOperationException(WrongNumberOfNormals);
  1957. }
  1958.  
  1959. if (triangleTextureCoordinates != null && triangleTextureCoordinates.Count != trianglePositions.Count)
  1960. {
  1961. throw new InvalidOperationException(WrongNumberOfTextureCoordinates);
  1962. }
  1963.  
  1964. int index0 = this.positions.Count;
  1965. foreach (var p in trianglePositions)
  1966. {
  1967. this.positions.Add(p);
  1968. }
  1969.  
  1970. if (this.textureCoordinates != null && triangleTextureCoordinates != null)
  1971. {
  1972. foreach (var tc in triangleTextureCoordinates)
  1973. {
  1974. this.textureCoordinates.Add(tc);
  1975. }
  1976. }
  1977.  
  1978. if (this.normals != null && triangleNormals != null)
  1979. {
  1980. foreach (var n in triangleNormals)
  1981. {
  1982. this.normals.Add(n);
  1983. }
  1984. }
  1985.  
  1986. int indexEnd = this.positions.Count;
  1987. for (int i = index0; i < indexEnd; i++)
  1988. {
  1989. this.triangleIndices.Add(i);
  1990. }
  1991. }
  1992.  
  1993. /// <summary>
  1994. /// Adds a tube.
  1995. /// </summary>
  1996. /// <param name="path">
  1997. /// A list of Vector2s defining the centers of the tube.
  1998. /// </param>
  1999. /// <param name="values">
  2000. /// The texture coordinate X-values (optional).
  2001. /// </param>
  2002. /// <param name="diameters">
  2003. /// The diameters (optional).
  2004. /// </param>
  2005. /// <param name="thetaDiv">
  2006. /// The number of divisions around the tube.
  2007. /// </param>
  2008. /// <param name="isTubeClosed">
  2009. /// Set to true if the tube path is closed.
  2010. /// </param>
  2011. public void AddTube(IList<Vector3> path, double[] values, double[] diameters, int thetaDiv, bool isTubeClosed)
  2012. {
  2013. var circle = GetCircle(thetaDiv);
  2014. this.AddTube(path, values, diameters, circle, isTubeClosed, true);
  2015. }
  2016.  
  2017. /// <summary>
  2018. /// Adds a tube.
  2019. /// </summary>
  2020. /// <param name="path">
  2021. /// A list of Vector2s defining the centers of the tube.
  2022. /// </param>
  2023. /// <param name="diameter">
  2024. /// The diameter of the tube.
  2025. /// </param>
  2026. /// <param name="thetaDiv">
  2027. /// The number of divisions around the tube.
  2028. /// </param>
  2029. /// <param name="isTubeClosed">
  2030. /// Set to true if the tube path is closed.
  2031. /// </param>
  2032. public void AddTube(IList<Vector3> path, double diameter, int thetaDiv, bool isTubeClosed)
  2033. {
  2034. this.AddTube(path, null, new[] { diameter }, thetaDiv, isTubeClosed);
  2035. }
  2036.  
  2037. /// <summary>
  2038. /// Adds a tube with a custom section.
  2039. /// </summary>
  2040. /// <param name="path">
  2041. /// A list of Vector2s defining the centers of the tube.
  2042. /// </param>
  2043. /// <param name="values">
  2044. /// The texture coordinate X values (optional).
  2045. /// </param>
  2046. /// <param name="diameters">
  2047. /// The diameters (optional).
  2048. /// </param>
  2049. /// <param name="section">
  2050. /// The section to extrude along the tube path.
  2051. /// </param>
  2052. /// <param name="isTubeClosed">
  2053. /// If the tube is closed set to <c>true</c> .
  2054. /// </param>
  2055. /// <param name="isSectionClosed">
  2056. /// if set to <c>true</c> [is section closed].
  2057. /// </param>
  2058. public void AddTube(
  2059. IList<Vector3> path,
  2060. IList<double> values,
  2061. IList<double> diameters,
  2062. IList<Vector2> section,
  2063. bool isTubeClosed,
  2064. bool isSectionClosed)
  2065. {
  2066. if (values != null && values.Count == )
  2067. {
  2068. throw new InvalidOperationException(WrongNumberOfTextureCoordinates);
  2069. }
  2070.  
  2071. if (diameters != null && diameters.Count == )
  2072. {
  2073. throw new InvalidOperationException(WrongNumberOfDiameters);
  2074. }
  2075.  
  2076. int index0 = this.positions.Count;
  2077. int pathLength = path.Count;
  2078. int sectionLength = section.Count;
  2079. if (pathLength < || sectionLength < )
  2080. {
  2081. return;
  2082. }
  2083.  
  2084. var up = (path[] - path[]).FindAnyPerpendicular();
  2085.  
  2086. int diametersCount = diameters != null ? diameters.Count : ;
  2087. int valuesCount = values != null ? values.Count : ;
  2088.  
  2089. //*******************************
  2090. //*** PROPOSED SOLUTION *********
  2091. var lastUp = new Vector3();
  2092. var lastForward = new Vector3();
  2093. //*** PROPOSED SOLUTION *********
  2094. //*******************************
  2095.  
  2096. for (int i = ; i < pathLength; i++)
  2097. {
  2098. double r = diameters != null ? diameters[i % diametersCount] / : ;
  2099. int i0 = i > ? i - : i;
  2100. int i1 = i + < pathLength ? i + : i;
  2101. var forward = path[i1] - path[i0];
  2102. var right = up.Cross(forward);
  2103.  
  2104. up = forward.Cross(right);
  2105. up.Normalize();
  2106. right.Normalize();
  2107. var u = right;
  2108. var v = up;
  2109.  
  2110. //*******************************
  2111. //*** PROPOSED SOLUTION *********
  2112. // ** I think this will work because if path[n-1] is same Vector2,
  2113. // ** it is always a reflection of the current move
  2114. // ** so reversing the last move vector should work?
  2115. //*******************************
  2116. if (u.IsInvalid || v.IsInvalid)
  2117. {
  2118. forward = lastForward;
  2119. forward = Vector3.Negate(forward);
  2120. up = lastUp;
  2121. //** Please verify that negation of "up" is correct here
  2122. up = Vector3.Negate(up);
  2123. right = up.Cross(forward);
  2124. up.Normalize();
  2125. right.Normalize();
  2126. u = right;
  2127. v = up;
  2128. }
  2129. lastForward = forward;
  2130. lastUp = up;
  2131. //*** PROPOSED SOLUTION *********
  2132. //*******************************
  2133.  
  2134. for (int j = ; j < sectionLength; j++)
  2135. {
  2136. var w = (section[j].x * u * r) + (section[j].y * v * r);
  2137. var q = path[i] + w;
  2138. this.positions.Add(q);
  2139. if (this.normals != null)
  2140. {
  2141. w.Normalize();
  2142. this.normals.Add(w);
  2143. }
  2144.  
  2145. if (this.textureCoordinates != null)
  2146. {
  2147. this.textureCoordinates.Add(
  2148. values != null
  2149. ? new Vector2(values[i % valuesCount], (double)j / (sectionLength - ))
  2150. : new Vector2());
  2151. }
  2152. }
  2153. }
  2154.  
  2155. this.AddRectangularMeshTriangleIndices(index0, pathLength, sectionLength, isSectionClosed, isTubeClosed);
  2156. }
  2157.  
  2158. /// <summary>
  2159. /// Adds a tube with a custom section.
  2160. /// </summary>
  2161. /// <param name="path">A list of Vector2s defining the centers of the tube.</param>
  2162. /// <param name="angles">The rotation of the section as it moves along the path</param>
  2163. /// <param name="values">The texture coordinate X values (optional).</param>
  2164. /// <param name="diameters">The diameters (optional).</param>
  2165. /// <param name="section">The section to extrude along the tube path.</param>
  2166. /// <param name="sectionXAxis">The initial alignment of the x-axis of the section into the
  2167. /// 3D viewport</param>
  2168. /// <param name="isTubeClosed">If the tube is closed set to <c>true</c> .</param>
  2169. /// <param name="isSectionClosed">if set to <c>true</c> [is section closed].</param>
  2170. public void AddTube(
  2171. IList<Vector3> path,
  2172. IList<double> angles,
  2173. IList<double> values,
  2174. IList<double> diameters,
  2175. IList<Vector2> section,
  2176. Vector3 sectionXAxis,
  2177. bool isTubeClosed,
  2178. bool isSectionClosed)
  2179. {
  2180. if (values != null && values.Count == )
  2181. {
  2182. throw new InvalidOperationException(WrongNumberOfTextureCoordinates);
  2183. }
  2184.  
  2185. if (diameters != null && diameters.Count == )
  2186. {
  2187. throw new InvalidOperationException(WrongNumberOfDiameters);
  2188. }
  2189.  
  2190. if (angles != null && angles.Count == )
  2191. {
  2192. throw new InvalidOperationException(WrongNumberOfAngles);
  2193. }
  2194.  
  2195. int index0 = this.positions.Count;
  2196. int pathLength = path.Count;
  2197. int sectionLength = section.Count;
  2198. if (pathLength < || sectionLength < )
  2199. {
  2200. return;
  2201. }
  2202.  
  2203. var forward = path[] - path[];
  2204. var right = sectionXAxis;
  2205. var up = forward.Cross(right);
  2206. up.Normalize();
  2207. right.Normalize();
  2208.  
  2209. int diametersCount = diameters != null ? diameters.Count : ;
  2210. int valuesCount = values != null ? values.Count : ;
  2211. int anglesCount = angles != null ? angles.Count : ;
  2212.  
  2213. for (int i = ; i < pathLength; i++)
  2214. {
  2215. double radius = diameters != null ? diameters[i % diametersCount] / : ;
  2216. double theta = angles != null ? angles[i % anglesCount] : 0.0;
  2217.  
  2218. double ct = Math.Cos(theta);
  2219. double st = Math.Sin(theta);
  2220.  
  2221. int i0 = i > ? i - : i;
  2222. int i1 = i + < pathLength ? i + : i;
  2223.  
  2224. forward = path[i1] - path[i0];
  2225. right = up.Cross(forward);
  2226. if (right.LengthSquared > 1e-)
  2227. {
  2228. up = forward.Cross(right);
  2229. }
  2230.  
  2231. up.Normalize();
  2232. right.Normalize();
  2233. for (int j = ; j < sectionLength; j++)
  2234. {
  2235. var x = (section[j].x * ct) - (section[j].y * st);
  2236. var y = (section[j].x * st) + (section[j].y * ct);
  2237.  
  2238. var w = (x * right * radius) + (y * up * radius);
  2239. var q = path[i] + w;
  2240. this.positions.Add(q);
  2241. if (this.normals != null)
  2242. {
  2243. w.Normalize();
  2244. this.normals.Add(w);
  2245. }
  2246.  
  2247. if (this.textureCoordinates != null)
  2248. {
  2249. this.textureCoordinates.Add(
  2250. values != null
  2251. ? new Vector2(values[i % valuesCount], (double)j / (sectionLength - ))
  2252. : new Vector2());
  2253. }
  2254. }
  2255. }
  2256.  
  2257. this.AddRectangularMeshTriangleIndices(index0, pathLength, sectionLength, isSectionClosed, isTubeClosed);
  2258. }
  2259.  
  2260. /// <summary>
  2261. /// Appends the specified mesh.
  2262. /// </summary>
  2263. /// <param name="mesh">
  2264. /// The mesh.
  2265. /// </param>
  2266. public void Append(MeshBuilder mesh)
  2267. {
  2268. if (mesh == null)
  2269. {
  2270. throw new ArgumentNullException("mesh");
  2271. }
  2272.  
  2273. this.Append(mesh.positions, mesh.triangleIndices, mesh.normals, mesh.textureCoordinates);
  2274. }
  2275.  
  2276. /// <summary>
  2277. /// Appends the specified mesh.
  2278. /// </summary>
  2279. /// <param name="mesh">
  2280. /// The mesh.
  2281. /// </param>
  2282. public void Append(MeshGeometry3D mesh)
  2283. {
  2284. if (mesh == null)
  2285. {
  2286. throw new ArgumentNullException("mesh");
  2287. }
  2288.  
  2289. this.Append(mesh.Positions, mesh.TriangleIndices, this.normals != null ? mesh.Normals : null, this.textureCoordinates != null ? mesh.TextureCoordinates : null);
  2290. }
  2291.  
  2292. /// <summary>
  2293. /// Appends the specified Vector2s and triangles.
  2294. /// </summary>
  2295. /// <param name="positionsToAppend">
  2296. /// The Vector2s to append.
  2297. /// </param>
  2298. /// <param name="triangleIndicesToAppend">
  2299. /// The triangle indices to append.
  2300. /// </param>
  2301. /// <param name="normalsToAppend">
  2302. /// The normal vectors to append.
  2303. /// </param>
  2304. /// <param name="textureCoordinatesToAppend">
  2305. /// The texture coordinates to append.
  2306. /// </param>
  2307. public void Append(
  2308. IList<Vector3> positionsToAppend,
  2309. IList<int> triangleIndicesToAppend,
  2310. IList<Vector3> normalsToAppend = null,
  2311. IList<Vector2> textureCoordinatesToAppend = null)
  2312. {
  2313. if (positionsToAppend == null)
  2314. {
  2315. throw new ArgumentNullException("positionsToAppend");
  2316. }
  2317.  
  2318. if (this.normals != null && normalsToAppend == null)
  2319. {
  2320. throw new InvalidOperationException(SourceMeshNormalsShouldNotBeNull);
  2321. }
  2322.  
  2323. if (this.textureCoordinates != null && textureCoordinatesToAppend == null)
  2324. {
  2325. throw new InvalidOperationException(SourceMeshTextureCoordinatesShouldNotBeNull);
  2326. }
  2327.  
  2328. if (normalsToAppend != null && normalsToAppend.Count != positionsToAppend.Count)
  2329. {
  2330. throw new InvalidOperationException(WrongNumberOfNormals);
  2331. }
  2332.  
  2333. if (textureCoordinatesToAppend != null && textureCoordinatesToAppend.Count != positionsToAppend.Count)
  2334. {
  2335. throw new InvalidOperationException(WrongNumberOfTextureCoordinates);
  2336. }
  2337.  
  2338. int index0 = this.positions.Count;
  2339. foreach (var p in positionsToAppend)
  2340. {
  2341. this.positions.Add(p);
  2342. }
  2343.  
  2344. if (this.normals != null && normalsToAppend != null)
  2345. {
  2346. foreach (var n in normalsToAppend)
  2347. {
  2348. this.normals.Add(n);
  2349. }
  2350. }
  2351.  
  2352. if (this.textureCoordinates != null && textureCoordinatesToAppend != null)
  2353. {
  2354. foreach (var t in textureCoordinatesToAppend)
  2355. {
  2356. this.textureCoordinates.Add(t);
  2357. }
  2358. }
  2359.  
  2360. foreach (int i in triangleIndicesToAppend)
  2361. {
  2362. this.triangleIndices.Add(index0 + i);
  2363. }
  2364. }
  2365.  
  2366. /// <summary>
  2367. /// Chamfers the specified corner (experimental code).
  2368. /// </summary>
  2369. /// <param name="p">
  2370. /// The corner Vector2.
  2371. /// </param>
  2372. /// <param name="d">
  2373. /// The chamfer distance.
  2374. /// </param>
  2375. /// <param name="eps">
  2376. /// The corner search limit distance.
  2377. /// </param>
  2378. /// <param name="chamferVector2s">
  2379. /// If this parameter is provided, the collection will be filled with the generated chamfer Vector2s.
  2380. /// </param>
  2381. public void ChamferCorner(Vector3 p, double d, double eps = 1e-, IList<Vector3> chamferVector2s = null)
  2382. {
  2383. this.NoSharedVertices();
  2384.  
  2385. this.normals = null;
  2386. this.textureCoordinates = null;
  2387.  
  2388. var cornerNormal = this.FindCornerNormal(p, eps);
  2389.  
  2390. var newCornerVector2 = p - (cornerNormal * d);
  2391. int index0 = this.positions.Count;
  2392. this.positions.Add(newCornerVector2);
  2393.  
  2394. var plane = new Plane3D(newCornerVector2, cornerNormal);
  2395.  
  2396. int ntri = this.triangleIndices.Count;
  2397.  
  2398. for (int i = ; i < ntri; i += )
  2399. {
  2400. int i0 = i;
  2401. int i1 = i + ;
  2402. int i2 = i + ;
  2403. var p0 = this.positions[this.triangleIndices[i0]];
  2404. var p1 = this.positions[this.triangleIndices[i1]];
  2405. var p2 = this.positions[this.triangleIndices[i2]];
  2406. double d0 = (p - p0).LengthSquared;
  2407. double d1 = (p - p1).LengthSquared;
  2408. double d2 = (p - p2).LengthSquared;
  2409. double mind = Math.Min(d0, Math.Min(d1, d2));
  2410. if (mind > eps)
  2411. {
  2412. continue;
  2413. }
  2414.  
  2415. if (d1 < eps)
  2416. {
  2417. i0 = i + ;
  2418. i1 = i + ;
  2419. i2 = i;
  2420. }
  2421.  
  2422. if (d2 < eps)
  2423. {
  2424. i0 = i + ;
  2425. i1 = i;
  2426. i2 = i + ;
  2427. }
  2428.  
  2429. p0 = this.positions[this.triangleIndices[i0]];
  2430. p1 = this.positions[this.triangleIndices[i1]];
  2431. p2 = this.positions[this.triangleIndices[i2]];
  2432.  
  2433. // origin is the corner vertex (at index i0)
  2434. // find the intersections between the chamfer plane and the two edges connected to the corner
  2435. var p01 = plane.LineIntersection(p0, p1);
  2436. var p02 = plane.LineIntersection(p0, p2);
  2437.  
  2438. if (p01 == null)
  2439. {
  2440. continue;
  2441. }
  2442.  
  2443. if (p02 == null)
  2444. {
  2445. continue;
  2446. }
  2447.  
  2448. if (chamferVector2s != null)
  2449. {
  2450. // add the chamfered Vector2s
  2451. if (!chamferVector2s.Contains(p01.Value))
  2452. {
  2453. chamferVector2s.Add(p01.Value);
  2454. }
  2455.  
  2456. if (!chamferVector2s.Contains(p02.Value))
  2457. {
  2458. chamferVector2s.Add(p02.Value);
  2459. }
  2460. }
  2461.  
  2462. int i01 = i0;
  2463.  
  2464. // change the original triangle to use the first chamfer Vector2
  2465. this.positions[this.triangleIndices[i01]] = p01.Value;
  2466.  
  2467. int i02 = this.positions.Count;
  2468. this.positions.Add(p02.Value);
  2469.  
  2470. // add a new triangle for the other chamfer Vector2
  2471. this.triangleIndices.Add(i01);
  2472. this.triangleIndices.Add(i2);
  2473. this.triangleIndices.Add(i02);
  2474.  
  2475. // add a triangle connecting the chamfer Vector2s and the new corner Vector2
  2476. this.triangleIndices.Add(index0);
  2477. this.triangleIndices.Add(i01);
  2478. this.triangleIndices.Add(i02);
  2479. }
  2480.  
  2481. this.NoSharedVertices();
  2482. }
  2483.  
  2484. /// <summary>
  2485. /// Checks the performance limits.
  2486. /// </summary>
  2487. /// <remarks>
  2488. /// See <a href="http://msdn.microsoft.com/en-us/library/bb613553.aspx">MSDN</a>.
  2489. /// Try to keep mesh sizes under these limits:
  2490. /// Positions : 20,001 Vector2 instances
  2491. /// TriangleIndices : 60,003 integer instances
  2492. /// </remarks>
  2493. public void CheckPerformanceLimits()
  2494. {
  2495. if (this.positions.Count > )
  2496. {
  2497. LogManager.Instance.Write(string.Format("Too many positions ({0}).", this.positions.Count));
  2498. }
  2499.  
  2500. if (this.triangleIndices.Count > )
  2501. {
  2502. LogManager.Instance.Write(string.Format("Too many triangle indices ({0}).", this.triangleIndices.Count));
  2503. }
  2504. }
  2505.  
  2506. /// <summary>
  2507. /// Scales the positions (and normal vectors).
  2508. /// </summary>
  2509. /// <param name="scaleX">
  2510. /// The X scale factor.
  2511. /// </param>
  2512. /// <param name="scaleY">
  2513. /// The Y scale factor.
  2514. /// </param>
  2515. /// <param name="scaleZ">
  2516. /// The Z scale factor.
  2517. /// </param>
  2518. public void Scale(double scaleX, double scaleY, double scaleZ)
  2519. {
  2520. for (int i = ; i < this.Positions.Count; i++)
  2521. {
  2522. this.Positions[i] = new Vector3(
  2523. this.Positions[i].x * scaleX, this.Positions[i].y * scaleY, this.Positions[i].z * scaleZ);
  2524. }
  2525.  
  2526. if (this.Normals != null)
  2527. {
  2528. for (int i = ; i < this.Normals.Count; i++)
  2529. {
  2530. this.Normals[i] = new Vector3(
  2531. this.Normals[i].x * scaleX, this.Normals[i].y * scaleY, this.Normals[i].z * scaleZ);
  2532. this.Normals[i].Normalize();
  2533. }
  2534. }
  2535. }
  2536.  
  2537. /// <summary>
  2538. /// Performs a linear subdivision of the mesh.
  2539. /// </summary>
  2540. /// <param name="barycentric">
  2541. /// Add a vertex in the center if set to <c>true</c> .
  2542. /// </param>
  2543. public void SubdivideLinear(bool barycentric = false)
  2544. {
  2545. if (barycentric)
  2546. {
  2547. this.SubdivideBarycentric();
  2548. }
  2549. else
  2550. {
  2551. this.Subdivide4();
  2552. }
  2553. }
  2554.  
  2555. /// <summary>
  2556. /// Converts the geometry to a <see cref="MeshGeometry3D"/> .
  2557. /// </summary>
  2558. /// <param name="freeze">
  2559. /// freeze the mesh if set to <c>true</c> .
  2560. /// </param>
  2561. /// <returns>
  2562. /// A mesh geometry.
  2563. /// </returns>
  2564. public MeshGeometry3D ToMesh(bool freeze = false)
  2565. {
  2566. if (this.triangleIndices.Count == )
  2567. {
  2568. var emptyGeometry = new MeshGeometry3D();
  2569.  
  2570. return emptyGeometry;
  2571. }
  2572.  
  2573. if (this.normals != null && this.positions.Count != this.normals.Count)
  2574. {
  2575. throw new InvalidOperationException(WrongNumberOfNormals);
  2576. }
  2577.  
  2578. if (this.textureCoordinates != null && this.positions.Count != this.textureCoordinates.Count)
  2579. {
  2580. throw new InvalidOperationException(WrongNumberOfTextureCoordinates);
  2581. }
  2582.  
  2583. var mg = new MeshGeometry3D
  2584. {
  2585. Positions = new List<Vector3>(this.positions),
  2586. TriangleIndices = new List<int>(this.triangleIndices)
  2587. };
  2588. if (this.normals != null)
  2589. {
  2590. mg.Normals = new List<Vector3>(this.normals);
  2591. }
  2592.  
  2593. if (this.textureCoordinates != null)
  2594. {
  2595. mg.TextureCoordinates = new List<Vector2>(this.textureCoordinates);
  2596. }
  2597. return mg;
  2598. }
  2599.  
  2600. public Mesh ToMesh(string name, string materialName = "")
  2601. {
  2602. MeshGeometry3D g3d = ToMesh();
  2603. var mesh = g3d.ToMesh(name, materialName);
  2604. return mesh;
  2605. }
  2606.  
  2607. public void ToManualObjectSection(ManualObject mo, string material)
  2608. {
  2609. MeshGeometry3D g3d = ToMesh();
  2610. mo.Begin(material, OperationType.TriangleList);
  2611. foreach (var pos in g3d.Positions)
  2612. {
  2613. mo.Position(pos);
  2614. }
  2615. foreach (var tri in g3d.TriangleIndices)
  2616. {
  2617. mo.Index((ushort)tri);
  2618. }
  2619. mo.End();
  2620. }
  2621. /// <summary>
  2622. /// Gets a unit sphere from the cache.
  2623. /// </summary>
  2624. /// <param name="subdivisions">
  2625. /// The number of subdivisions.
  2626. /// </param>
  2627. /// <returns>
  2628. /// A unit sphere mesh.
  2629. /// </returns>
  2630. private static MeshGeometry3D GetUnitSphere(int subdivisions)
  2631. {
  2632. if (UnitSphereCache.Value.ContainsKey(subdivisions))
  2633. {
  2634. return UnitSphereCache.Value[subdivisions];
  2635. }
  2636.  
  2637. var mb = new MeshBuilder(false, false);
  2638. mb.AddRegularIcosahedron(new Vector3(), , false);
  2639. for (int i = ; i < subdivisions; i++)
  2640. {
  2641. mb.SubdivideLinear();
  2642. }
  2643.  
  2644. for (int i = ; i < mb.positions.Count; i++)
  2645. {
  2646. var v = mb.Positions[i];
  2647. v.Normalize();
  2648. mb.Positions[i] = v;
  2649. }
  2650.  
  2651. var mesh = mb.ToMesh();
  2652. UnitSphereCache.Value[subdivisions] = mesh;
  2653. return mesh;
  2654. }
  2655.  
  2656. /// <summary>
  2657. /// Adds normal vectors for a rectangular mesh.
  2658. /// </summary>
  2659. /// <param name="index0">
  2660. /// The index 0.
  2661. /// </param>
  2662. /// <param name="rows">
  2663. /// The number of rows.
  2664. /// </param>
  2665. /// <param name="columns">
  2666. /// The number of columns.
  2667. /// </param>
  2668. private void AddRectangularMeshNormals(int index0, int rows, int columns)
  2669. {
  2670. for (int i = ; i < rows; i++)
  2671. {
  2672. int i1 = i + ;
  2673. if (i1 == rows)
  2674. {
  2675. i1--;
  2676. }
  2677.  
  2678. int i0 = i1 - ;
  2679. for (int j = ; j < columns; j++)
  2680. {
  2681. int j1 = j + ;
  2682. if (j1 == columns)
  2683. {
  2684. j1--;
  2685. }
  2686.  
  2687. int j0 = j1 - ;
  2688. var u = Vector3.Subtract(
  2689. this.positions[index0 + (i1 * columns) + j0], this.positions[index0 + (i0 * columns) + j0]);
  2690. var v = Vector3.Subtract(
  2691. this.positions[index0 + (i0 * columns) + j1], this.positions[index0 + (i0 * columns) + j0]);
  2692. var normal = u.Cross(v);
  2693. normal.Normalize();
  2694. this.normals.Add(normal);
  2695. }
  2696. }
  2697. }
  2698.  
  2699. /// <summary>
  2700. /// Adds texture coordinates for a rectangular mesh.
  2701. /// </summary>
  2702. /// <param name="rows">
  2703. /// The number of rows.
  2704. /// </param>
  2705. /// <param name="columns">
  2706. /// The number of columns.
  2707. /// </param>
  2708. private void AddRectangularMeshTextureCoordinates(int rows, int columns)
  2709. {
  2710. for (int i = ; i < rows; i++)
  2711. {
  2712. double v = (double)i / (rows - );
  2713. for (int j = ; j < columns; j++)
  2714. {
  2715. double u = (double)j / (columns - );
  2716. this.textureCoordinates.Add(new Vector2(u, v));
  2717. }
  2718. }
  2719. }
  2720.  
  2721. /// <summary>
  2722. /// Add triangle indices for a rectangular mesh.
  2723. /// </summary>
  2724. /// <param name="index0">
  2725. /// The index offset.
  2726. /// </param>
  2727. /// <param name="rows">
  2728. /// The number of rows.
  2729. /// </param>
  2730. /// <param name="columns">
  2731. /// The number of columns.
  2732. /// </param>
  2733. /// <param name="isSpherical">
  2734. /// set the flag to true to create a sphere mesh (triangles at top and bottom).
  2735. /// </param>
  2736. private void AddRectangularMeshTriangleIndices(int index0, int rows, int columns, bool isSpherical = false)
  2737. {
  2738. for (int i = ; i < rows - ; i++)
  2739. {
  2740. for (int j = ; j < columns - ; j++)
  2741. {
  2742. int ij = (i * columns) + j;
  2743. if (!isSpherical || i > )
  2744. {
  2745. this.triangleIndices.Add(index0 + ij);
  2746. this.triangleIndices.Add(index0 + ij + + columns);
  2747. this.triangleIndices.Add(index0 + ij + );
  2748. }
  2749.  
  2750. if (!isSpherical || i < rows - )
  2751. {
  2752. this.triangleIndices.Add(index0 + ij + + columns);
  2753. this.triangleIndices.Add(index0 + ij);
  2754. this.triangleIndices.Add(index0 + ij + columns);
  2755. }
  2756. }
  2757. }
  2758. }
  2759.  
  2760. /// <summary>
  2761. /// Adds triangular indices for a rectangular mesh.
  2762. /// </summary>
  2763. /// <param name="index0">
  2764. /// The index 0.
  2765. /// </param>
  2766. /// <param name="rows">
  2767. /// The rows.
  2768. /// </param>
  2769. /// <param name="columns">
  2770. /// The columns.
  2771. /// </param>
  2772. /// <param name="rowsClosed">
  2773. /// True if rows are closed.
  2774. /// </param>
  2775. /// <param name="columnsClosed">
  2776. /// True if columns are closed.
  2777. /// </param>
  2778. private void AddRectangularMeshTriangleIndices(
  2779. int index0, int rows, int columns, bool rowsClosed, bool columnsClosed)
  2780. {
  2781. int m2 = rows - ;
  2782. int n2 = columns - ;
  2783. if (columnsClosed)
  2784. {
  2785. m2++;
  2786. }
  2787.  
  2788. if (rowsClosed)
  2789. {
  2790. n2++;
  2791. }
  2792.  
  2793. for (int i = ; i < m2; i++)
  2794. {
  2795. for (int j = ; j < n2; j++)
  2796. {
  2797. int i00 = index0 + (i * columns) + j;
  2798. int i01 = index0 + (i * columns) + ((j + ) % columns);
  2799. int i10 = index0 + (((i + ) % rows) * columns) + j;
  2800. int i11 = index0 + (((i + ) % rows) * columns) + ((j + ) % columns);
  2801. this.triangleIndices.Add(i00);
  2802. this.triangleIndices.Add(i11);
  2803. this.triangleIndices.Add(i01);
  2804.  
  2805. this.triangleIndices.Add(i11);
  2806. this.triangleIndices.Add(i00);
  2807. this.triangleIndices.Add(i10);
  2808. }
  2809. }
  2810. }
  2811.  
  2812. /// <summary>
  2813. /// Finds the average normal to the specified corner (experimental code).
  2814. /// </summary>
  2815. /// <param name="p">
  2816. /// The corner Vector2.
  2817. /// </param>
  2818. /// <param name="eps">
  2819. /// The corner search limit distance.
  2820. /// </param>
  2821. /// <returns>
  2822. /// The normal.
  2823. /// </returns>
  2824. private Vector3 FindCornerNormal(Vector3 p, double eps)
  2825. {
  2826. var sum = new Vector3();
  2827. int count = ;
  2828. var addedNormals = new HashSet<Vector3>();
  2829. for (int i = ; i < this.triangleIndices.Count; i += )
  2830. {
  2831. int i0 = i;
  2832. int i1 = i + ;
  2833. int i2 = i + ;
  2834. var p0 = this.positions[this.triangleIndices[i0]];
  2835. var p1 = this.positions[this.triangleIndices[i1]];
  2836. var p2 = this.positions[this.triangleIndices[i2]];
  2837.  
  2838. // check if any of the vertices are on the corner
  2839. double d0 = (p - p0).LengthSquared;
  2840. double d1 = (p - p1).LengthSquared;
  2841. double d2 = (p - p2).LengthSquared;
  2842. double mind = Math.Min(d0, Math.Min(d1, d2));
  2843. if (mind > eps)
  2844. {
  2845. continue;
  2846. }
  2847.  
  2848. // calculate the triangle normal and check if this face is already added
  2849. var normal = (p1 - p0).Cross(p2 - p0);
  2850. normal.Normalize();
  2851.  
  2852. // todo: need to use the epsilon value to compare the normals?
  2853. if (addedNormals.Contains(normal))
  2854. {
  2855. continue;
  2856. }
  2857.  
  2858. // todo: this does not work yet
  2859. // double dp = 1;
  2860. // foreach (var n in addedNormals)
  2861. // {
  2862. // dp = Math.Abs(Vector3.DotProduct(n, normal) - 1);
  2863. // if (dp < eps)
  2864. // continue;
  2865. // }
  2866. // if (dp < eps)
  2867. // {
  2868. // continue;
  2869. // }
  2870. count++;
  2871. sum += normal;
  2872. addedNormals.Add(normal);
  2873. }
  2874.  
  2875. if (count == )
  2876. {
  2877. return new Vector3();
  2878. }
  2879.  
  2880. return sum * (1.0 / count);
  2881. }
  2882.  
  2883. /// <summary>
  2884. /// Makes sure no triangles share the same vertex.
  2885. /// </summary>
  2886. private void NoSharedVertices()
  2887. {
  2888. var p = new List<Vector3>();
  2889. var ti = new List<int>();
  2890. List<Vector3> n = null;
  2891. if (this.normals != null)
  2892. {
  2893. n = new List<Vector3>();
  2894. }
  2895.  
  2896. List<Vector2> tc = null;
  2897. if (this.textureCoordinates != null)
  2898. {
  2899. tc = new List<Vector2>();
  2900. }
  2901.  
  2902. for (int i = ; i < this.triangleIndices.Count; i += )
  2903. {
  2904. int i0 = i;
  2905. int i1 = i + ;
  2906. int i2 = i + ;
  2907. int index0 = this.triangleIndices[i0];
  2908. int index1 = this.triangleIndices[i1];
  2909. int index2 = this.triangleIndices[i2];
  2910. var p0 = this.positions[index0];
  2911. var p1 = this.positions[index1];
  2912. var p2 = this.positions[index2];
  2913. p.Add(p0);
  2914. p.Add(p1);
  2915. p.Add(p2);
  2916. ti.Add(i0);
  2917. ti.Add(i1);
  2918. ti.Add(i2);
  2919. if (n != null)
  2920. {
  2921. n.Add(this.normals[index0]);
  2922. n.Add(this.normals[index1]);
  2923. n.Add(this.normals[index2]);
  2924. }
  2925.  
  2926. if (tc != null)
  2927. {
  2928. tc.Add(this.textureCoordinates[index0]);
  2929. tc.Add(this.textureCoordinates[index1]);
  2930. tc.Add(this.textureCoordinates[index2]);
  2931. }
  2932. }
  2933.  
  2934. this.positions = p;
  2935. this.triangleIndices = ti;
  2936. this.normals = n;
  2937. this.textureCoordinates = tc;
  2938. }
  2939.  
  2940. /// <summary>
  2941. /// Subdivides each triangle into four sub-triangles.
  2942. /// </summary>
  2943. private void Subdivide4()
  2944. {
  2945. // Each triangle is divided into four subtriangles, adding new vertices in the middle of each edge.
  2946. int ip = this.Positions.Count;
  2947. int ntri = this.TriangleIndices.Count;
  2948. for (int i = ; i < ntri; i += )
  2949. {
  2950. int i0 = this.TriangleIndices[i];
  2951. int i1 = this.TriangleIndices[i + ];
  2952. int i2 = this.TriangleIndices[i + ];
  2953. var p0 = this.Positions[i0];
  2954. var p1 = this.Positions[i1];
  2955. var p2 = this.Positions[i2];
  2956. var v01 = p1 - p0;
  2957. var v12 = p2 - p1;
  2958. var v20 = p0 - p2;
  2959. var p01 = p0 + (v01 * 0.5);
  2960. var p12 = p1 + (v12 * 0.5);
  2961. var p20 = p2 + (v20 * 0.5);
  2962.  
  2963. int i01 = ip++;
  2964. int i12 = ip++;
  2965. int i20 = ip++;
  2966.  
  2967. this.Positions.Add(p01);
  2968. this.Positions.Add(p12);
  2969. this.Positions.Add(p20);
  2970.  
  2971. if (this.normals != null)
  2972. {
  2973. var n = this.Normals[i0];
  2974. this.Normals.Add(n);
  2975. this.Normals.Add(n);
  2976. this.Normals.Add(n);
  2977. }
  2978.  
  2979. if (this.textureCoordinates != null)
  2980. {
  2981. var uv0 = this.TextureCoordinates[i0];
  2982. var uv1 = this.TextureCoordinates[i0 + ];
  2983. var uv2 = this.TextureCoordinates[i0 + ];
  2984. var t01 = uv1 - uv0;
  2985. var t12 = uv2 - uv1;
  2986. var t20 = uv0 - uv2;
  2987. var u01 = uv0 + (t01 * 0.5);
  2988. var u12 = uv1 + (t12 * 0.5);
  2989. var u20 = uv2 + (t20 * 0.5);
  2990. this.TextureCoordinates.Add(u01);
  2991. this.TextureCoordinates.Add(u12);
  2992. this.TextureCoordinates.Add(u20);
  2993. }
  2994.  
  2995. // TriangleIndices[i ] = i0;
  2996. this.TriangleIndices[i + ] = i01;
  2997. this.TriangleIndices[i + ] = i20;
  2998.  
  2999. this.TriangleIndices.Add(i01);
  3000. this.TriangleIndices.Add(i1);
  3001. this.TriangleIndices.Add(i12);
  3002.  
  3003. this.TriangleIndices.Add(i12);
  3004. this.TriangleIndices.Add(i2);
  3005. this.TriangleIndices.Add(i20);
  3006.  
  3007. this.TriangleIndices.Add(i01);
  3008. this.TriangleIndices.Add(i12);
  3009. this.TriangleIndices.Add(i20);
  3010. }
  3011. }
  3012.  
  3013. /// <summary>
  3014. /// Subdivides each triangle into six triangles. Adds a vertex at the midVector2 of each triangle.
  3015. /// </summary>
  3016. /// <remarks>
  3017. /// See <a href="http://en.wikipedia.org/wiki/Barycentric_subdivision">wikipedia</a>.
  3018. /// </remarks>
  3019. private void SubdivideBarycentric()
  3020. {
  3021. // The BCS of a triangle S divides it into six triangles; each part has one vertex v2 at the
  3022. // barycenter of S, another one v1 at the midVector2 of some side, and the last one v0 at one
  3023. // of the original vertices.
  3024. int im = this.Positions.Count;
  3025. int ntri = this.TriangleIndices.Count;
  3026. for (int i = ; i < ntri; i += )
  3027. {
  3028. int i0 = this.TriangleIndices[i];
  3029. int i1 = this.TriangleIndices[i + ];
  3030. int i2 = this.TriangleIndices[i + ];
  3031. var p0 = this.Positions[i0];
  3032. var p1 = this.Positions[i1];
  3033. var p2 = this.Positions[i2];
  3034. var v01 = p1 - p0;
  3035. var v12 = p2 - p1;
  3036. var v20 = p0 - p2;
  3037. var p01 = p0 + (v01 * 0.5);
  3038. var p12 = p1 + (v12 * 0.5);
  3039. var p20 = p2 + (v20 * 0.5);
  3040. var m = new Vector3((p0.x + p1.x + p2.x) / , (p0.y + p1.y + p2.y) / , (p0.z + p1.z + p2.z) / );
  3041.  
  3042. int i01 = im + ;
  3043. int i12 = im + ;
  3044. int i20 = im + ;
  3045.  
  3046. this.Positions.Add(m);
  3047. this.Positions.Add(p01);
  3048. this.Positions.Add(p12);
  3049. this.Positions.Add(p20);
  3050.  
  3051. if (this.normals != null)
  3052. {
  3053. var n = this.Normals[i0];
  3054. this.Normals.Add(n);
  3055. this.Normals.Add(n);
  3056. this.Normals.Add(n);
  3057. this.Normals.Add(n);
  3058. }
  3059.  
  3060. if (this.textureCoordinates != null)
  3061. {
  3062. var uv0 = this.TextureCoordinates[i0];
  3063. var uv1 = this.TextureCoordinates[i0 + ];
  3064. var uv2 = this.TextureCoordinates[i0 + ];
  3065. var t01 = uv1 - uv0;
  3066. var t12 = uv2 - uv1;
  3067. var t20 = uv0 - uv2;
  3068. var u01 = uv0 + (t01 * 0.5);
  3069. var u12 = uv1 + (t12 * 0.5);
  3070. var u20 = uv2 + (t20 * 0.5);
  3071. var uvm = new Vector2((uv0.x + uv1.x) * 0.5, (uv0.y + uv1.y) * 0.5);
  3072. this.TextureCoordinates.Add(uvm);
  3073. this.TextureCoordinates.Add(u01);
  3074. this.TextureCoordinates.Add(u12);
  3075. this.TextureCoordinates.Add(u20);
  3076. }
  3077.  
  3078. // TriangleIndices[i ] = i0;
  3079. this.TriangleIndices[i + ] = i01;
  3080. this.TriangleIndices[i + ] = im;
  3081.  
  3082. this.TriangleIndices.Add(i01);
  3083. this.TriangleIndices.Add(i1);
  3084. this.TriangleIndices.Add(im);
  3085.  
  3086. this.TriangleIndices.Add(i1);
  3087. this.TriangleIndices.Add(i12);
  3088. this.TriangleIndices.Add(im);
  3089.  
  3090. this.TriangleIndices.Add(i12);
  3091. this.TriangleIndices.Add(i2);
  3092. this.TriangleIndices.Add(im);
  3093.  
  3094. this.TriangleIndices.Add(i2);
  3095. this.TriangleIndices.Add(i20);
  3096. this.TriangleIndices.Add(im);
  3097.  
  3098. this.TriangleIndices.Add(i20);
  3099. this.TriangleIndices.Add(i0);
  3100. this.TriangleIndices.Add(im);
  3101.  
  3102. im += ;
  3103. }
  3104. }
  3105. }

HelixToolkit MeshBuilder

  1. public class MeshElement3D
  2. {
  3. public Material Material { get; set; }
  4.  
  5. public bool Visible { get; set; }
  6.  
  7. private MeshGeometry3D content;
  8.  
  9. protected virtual MeshGeometry3D Tessellate()
  10. {
  11. return this.content;
  12. }
  13.  
  14. public MeshElement3D()
  15. {
  16. content = new MeshGeometry3D();
  17. }
  18.  
  19. public Mesh ToMesh(string name, string materialName)
  20. {
  21. this.content = Tessellate();
  22. return this.content.ToMesh(name, materialName);
  23. }
  24.  
  25. public Mesh ToMesh(string name, ColorEx color)
  26. {
  27. return ToMesh(name, MaterialHelper.GetAbmientMaterial(color).Name);
  28. }
  29.  
  30. public Mesh ToMesh(string name, Color color)
  31. {
  32. return ToMesh(name, MaterialHelper.GetAbmientMaterial(color).Name);
  33. }
  34.  
  35. public virtual void ToManualObjectSection(ManualObject mo, string material)
  36. {
  37. MeshGeometry3D g3d = Tessellate();
  38. mo.Begin(material, OperationType.TriangleList);
  39. foreach (var pos in g3d.Positions)
  40. {
  41. mo.Position(pos);
  42. }
  43. foreach (var tri in g3d.TriangleIndices)
  44. {
  45. mo.Index((ushort)tri);
  46. }
  47. mo.End();
  48. }
  49.  
  50. public virtual void ToManualObjectSection(ManualObject mo, ColorEx color)
  51. {
  52. MeshGeometry3D g3d = Tessellate();
  53. foreach (var pos in g3d.Positions)
  54. {
  55. mo.Position(pos);
  56. mo.Color(color);
  57. }
  58. foreach (var tri in g3d.TriangleIndices)
  59. {
  60. mo.Index((ushort)tri);
  61. }
  62. }
  63. }
  64.  
  65. public class Arrow3D : MeshElement3D
  66. {
  67. public double Diameter { get; set; }
  68.  
  69. public double HeadLength { get; set; }
  70.  
  71. public Vector3 Point1 { get; set; }
  72.  
  73. public Vector3 Point2 { get; set; }
  74.  
  75. public int ThetaDiv { get; set; }
  76.  
  77. public Arrow3D()
  78. {
  79. this.Diameter = 1.0;
  80. this.HeadLength = 3.0;
  81. this.Point1 = Vector3.Zero;
  82. this.Point2 = new Vector3(, , );
  83. ThetaDiv = ;
  84. }
  85.  
  86. protected override MeshGeometry3D Tessellate()
  87. {
  88. if (this.Diameter <= )
  89. {
  90. return null;
  91. }
  92.  
  93. var builder = new MeshBuilder(true, true);
  94. builder.AddArrow(this.Point1, this.Point2, this.Diameter, this.HeadLength, this.ThetaDiv);
  95. return builder.ToMesh();
  96. }
  97. }
  98.  
  99. public class Box3D : MeshElement3D
  100. {
  101. public bool IsBottom { get; set; }
  102.  
  103. public bool IsTop { get; set; }
  104.  
  105. public Vector3 Center { get; set; }
  106.  
  107. public Vector3 LHW { get; set; }
  108.  
  109. public Box3D()
  110. {
  111. IsBottom = true;
  112. IsTop = true;
  113. Center = Vector3.Zero;
  114. LHW = Vector3.UnitScale;
  115. }
  116.  
  117. protected override MeshGeometry3D Tessellate()
  118. {
  119. var b = new MeshBuilder(false, true);
  120. b.AddCubeFace(
  121. this.Center, new Vector3(-, , ), new Vector3(, , ), LHW.x, LHW.z, LHW.y);
  122. b.AddCubeFace(
  123. this.Center, new Vector3(, , ), new Vector3(, , -), LHW.x, LHW.z, LHW.y);
  124. b.AddCubeFace(
  125. this.Center, new Vector3(, -, ), new Vector3(, , ), LHW.z, LHW.x, LHW.y);
  126. b.AddCubeFace(
  127. this.Center, new Vector3(, , ), new Vector3(, , -), LHW.z, LHW.x, LHW.y);
  128. if (this.IsTop)
  129. {
  130. b.AddCubeFace(
  131. this.Center, new Vector3(, , ), new Vector3(, -, ), LHW.y, LHW.x, LHW.z);
  132. }
  133.  
  134. if (this.IsBottom)
  135. {
  136. b.AddCubeFace(
  137. this.Center, new Vector3(, , -), new Vector3(, , ), LHW.y, LHW.x, LHW.z);
  138. }
  139.  
  140. return b.ToMesh();
  141. }
  142. }
  143.  
  144. public class Cube3D : MeshElement3D
  145. {
  146. public Vector3 Center { get; set; }
  147.  
  148. public double SideLength { get; set; }
  149.  
  150. public Cube3D()
  151. {
  152. Center = Vector3.Zero;
  153. SideLength = ;
  154. }
  155.  
  156. protected override MeshGeometry3D Tessellate()
  157. {
  158. var b = new MeshBuilder(false, true);
  159. b.AddCubeFace(
  160. this.Center,
  161. new Vector3(-, , ),
  162. new Vector3(, , ),
  163. this.SideLength,
  164. this.SideLength,
  165. this.SideLength);
  166. b.AddCubeFace(
  167. this.Center,
  168. new Vector3(, , ),
  169. new Vector3(, , -),
  170. this.SideLength,
  171. this.SideLength,
  172. this.SideLength);
  173. b.AddCubeFace(
  174. this.Center,
  175. new Vector3(, -, ),
  176. new Vector3(, , ),
  177. this.SideLength,
  178. this.SideLength,
  179. this.SideLength);
  180. b.AddCubeFace(
  181. this.Center,
  182. new Vector3(, , ),
  183. new Vector3(, , -),
  184. this.SideLength,
  185. this.SideLength,
  186. this.SideLength);
  187. b.AddCubeFace(
  188. this.Center,
  189. new Vector3(, , ),
  190. new Vector3(, -, ),
  191. this.SideLength,
  192. this.SideLength,
  193. this.SideLength);
  194. b.AddCubeFace(
  195. this.Center,
  196. new Vector3(, , -),
  197. new Vector3(, , ),
  198. this.SideLength,
  199. this.SideLength,
  200. this.SideLength);
  201.  
  202. return b.ToMesh();
  203. }
  204. }
  205.  
  206. public class Ellipsoid3D : MeshElement3D
  207. {
  208. public Vector3 Center { get; set; }
  209.  
  210. public int PhiDiv { get; set; }
  211.  
  212. public double RadiusX { get; set; }
  213.  
  214. public double RadiusY { get; set; }
  215.  
  216. public double RadiusZ { get; set; }
  217.  
  218. public int ThetaDiv { get; set; }
  219.  
  220. public Ellipsoid3D()
  221. {
  222. this.Center = Vector3.Zero;
  223. this.PhiDiv = ;
  224. this.RadiusX = 1.0;
  225. this.RadiusY = 1.0;
  226. this.RadiusZ = 1.0;
  227. this.ThetaDiv = ;
  228. }
  229.  
  230. protected override MeshGeometry3D Tessellate()
  231. {
  232. var builder = new MeshBuilder(false, true);
  233. builder.AddEllipsoid(this.Center, this.RadiusX, this.RadiusY, this.RadiusZ, this.ThetaDiv, this.PhiDiv);
  234. return builder.ToMesh();
  235. }
  236. }
  237.  
  238. public class GridLines3D : MeshElement3D
  239. {
  240. public Vector3 Center { get; set; }
  241.  
  242. public double MinorDistance { get; set; }
  243.  
  244. public Vector3 LengthDirection { get; set; }
  245.  
  246. public double Length { get; set; }
  247.  
  248. public double MajorDistance { get; set; }
  249.  
  250. public Vector3 Normal { get; set; }
  251.  
  252. public double Thickness { get; set; }
  253.  
  254. public double Width { get; set; }
  255.  
  256. private Vector3 lengthDirection;
  257.  
  258. private Vector3 widthDirection;
  259.  
  260. public GridLines3D()
  261. {
  262. Center = Vector3.Zero;
  263. this.MinorDistance = 2.5;
  264. this.LengthDirection = Vector3.UnitX;
  265. this.Length = ;
  266. this.MajorDistance = ;
  267. this.Normal = Vector3.UnitY;
  268. this.Thickness = 0.1;
  269. this.Width = ;
  270. }
  271.  
  272. protected override MeshGeometry3D Tessellate()
  273. {
  274. this.lengthDirection = this.LengthDirection;
  275. this.lengthDirection.Normalize();
  276. this.widthDirection = this.Normal.Cross(this.lengthDirection);
  277. this.widthDirection.Normalize();
  278.  
  279. var mesh = new MeshBuilder(true, false);
  280. double minX = -this.Width / ;
  281. double minY = -this.Length / ;
  282. double maxX = this.Width / ;
  283. double maxY = this.Length / ;
  284.  
  285. double x = minX;
  286. double eps = this.MinorDistance / ;
  287. while (x <= maxX + eps)
  288. {
  289. double t = this.Thickness;
  290. if (IsMultipleOf(x, this.MajorDistance))
  291. {
  292. t *= ;
  293. }
  294.  
  295. this.AddLineX(mesh, x, minY, maxY, t);
  296. x += this.MinorDistance;
  297. }
  298.  
  299. double y = minY;
  300. while (y <= maxY + eps)
  301. {
  302. double t = this.Thickness;
  303. if (IsMultipleOf(y, this.MajorDistance))
  304. {
  305. t *= ;
  306. }
  307.  
  308. this.AddLineY(mesh, y, minX, maxX, t);
  309. y += this.MinorDistance;
  310. }
  311.  
  312. var m = mesh.ToMesh();
  313. return m;
  314. }
  315.  
  316. private static bool IsMultipleOf(double y, double d)
  317. {
  318. double y2 = d * (int)(y / d);
  319. return Math.Abs(y - y2) < 1e-;
  320. }
  321.  
  322. private void AddLineX(MeshBuilder mesh, double x, double minY, double maxY, double thickness)
  323. {
  324. int i0 = mesh.Positions.Count;
  325. mesh.Positions.Add(this.GetPoint(x - (thickness / ), minY));
  326. mesh.Positions.Add(this.GetPoint(x - (thickness / ), maxY));
  327. mesh.Positions.Add(this.GetPoint(x + (thickness / ), maxY));
  328. mesh.Positions.Add(this.GetPoint(x + (thickness / ), minY));
  329. mesh.Normals.Add(this.Normal);
  330. mesh.Normals.Add(this.Normal);
  331. mesh.Normals.Add(this.Normal);
  332. mesh.Normals.Add(this.Normal);
  333. mesh.TriangleIndices.Add(i0);
  334. mesh.TriangleIndices.Add(i0 + );
  335. mesh.TriangleIndices.Add(i0 + );
  336. mesh.TriangleIndices.Add(i0 + );
  337. mesh.TriangleIndices.Add(i0 + );
  338. mesh.TriangleIndices.Add(i0);
  339. }
  340.  
  341. private void AddLineY(MeshBuilder mesh, double y, double minX, double maxX, double thickness)
  342. {
  343. int i0 = mesh.Positions.Count;
  344. mesh.Positions.Add(this.GetPoint(minX, y + (thickness / )));
  345. mesh.Positions.Add(this.GetPoint(maxX, y + (thickness / )));
  346. mesh.Positions.Add(this.GetPoint(maxX, y - (thickness / )));
  347. mesh.Positions.Add(this.GetPoint(minX, y - (thickness / )));
  348. mesh.Normals.Add(this.Normal);
  349. mesh.Normals.Add(this.Normal);
  350. mesh.Normals.Add(this.Normal);
  351. mesh.Normals.Add(this.Normal);
  352. mesh.TriangleIndices.Add(i0);
  353. mesh.TriangleIndices.Add(i0 + );
  354. mesh.TriangleIndices.Add(i0 + );
  355. mesh.TriangleIndices.Add(i0 + );
  356. mesh.TriangleIndices.Add(i0 + );
  357. mesh.TriangleIndices.Add(i0);
  358. }
  359.  
  360. /// <summary>
  361. /// Gets a point on the plane.
  362. /// </summary>
  363. /// <param name="x">The x coordinate.</param>
  364. /// <param name="y">The y coordinate.</param>
  365. /// <returns>A <see cref="Point3D"/>.</returns>
  366. private Vector3 GetPoint(double x, double y)
  367. {
  368. return this.Center + (this.widthDirection * x) + (this.lengthDirection * y);
  369. }
  370. }
  371.  
  372. public class PieSlice3D : MeshElement3D
  373. {
  374. public Vector3 Center { get; set; }
  375.  
  376. public double EndAngle { get; set; }
  377.  
  378. public double InnerRadius { get; set; }
  379.  
  380. public Vector3 Normal { get; set; }
  381.  
  382. public double OuterRadius { get; set; }
  383.  
  384. public double StartAngle { get; set; }
  385.  
  386. public int ThetaDiv { get; set; }
  387.  
  388. public Vector3 UpVector { get; set; }
  389.  
  390. public PieSlice3D()
  391. {
  392. this.Center = Vector3.Zero;
  393. this.EndAngle = ;
  394. this.InnerRadius = 0.5;
  395. this.Normal = Vector3.UnitZ;
  396. this.OuterRadius = 1.0;
  397. this.StartAngle = ;
  398. this.ThetaDiv = ;
  399. this.UpVector = Vector3.UnitY;
  400. }
  401.  
  402. protected override MeshGeometry3D Tessellate()
  403. {
  404. var pts = new List<Vector3>();
  405. var right = this.UpVector.Cross(this.Normal);
  406. for (int i = ; i < this.ThetaDiv; i++)
  407. {
  408. double angle = this.StartAngle + ((this.EndAngle - this.StartAngle) * i / (this.ThetaDiv - ));
  409. double angleRad = angle / * Math.PI;
  410. var dir = (right * Math.Cos(angleRad)) + (this.UpVector * Math.Sin(angleRad));
  411. pts.Add(this.Center + (dir * this.InnerRadius));
  412. pts.Add(this.Center + (dir * this.OuterRadius));
  413. }
  414.  
  415. var b = new MeshBuilder(false, false);
  416. b.AddTriangleStrip(pts);
  417. return b.ToMesh();
  418. }
  419. }
  420.  
  421. public class Pipe3D : MeshElement3D
  422. {
  423. public double Diameter { get; set; }
  424.  
  425. public double InnerDiameter { get; set; }
  426.  
  427. public Vector3 Point1 { get; set; }
  428.  
  429. public Vector3 Point2 { get; set; }
  430.  
  431. public int ThetaDiv { get; set; }
  432.  
  433. public Pipe3D()
  434. {
  435. this.Diameter = 1.0;
  436. this.InnerDiameter = 0.0;
  437. this.Point1 = Vector3.Zero;
  438. this.Point2 = new Vector3(, , );
  439. this.ThetaDiv = ;
  440. }
  441.  
  442. protected override MeshGeometry3D Tessellate()
  443. {
  444. var builder = new MeshBuilder(false, true);
  445. builder.AddPipe(this.Point1, this.Point2, this.InnerDiameter, this.Diameter, this.ThetaDiv);
  446. return builder.ToMesh();
  447. }
  448. }
  449.  
  450. public class Quad3D : MeshElement3D
  451. {
  452. public Vector3 Point1 { get; set; }
  453.  
  454. public Vector3 Point2 { get; set; }
  455.  
  456. public Vector3 Point3 { get; set; }
  457.  
  458. public Vector3 Point4 { get; set; }
  459.  
  460. public Quad3D()
  461. {
  462. Point1 = Vector3.Zero;
  463. Point2 = Vector3.UnitX;
  464. Point3 = new Vector3(, , );
  465. Point4 = Vector3.UnitY;
  466. }
  467.  
  468. protected override MeshGeometry3D Tessellate()
  469. {
  470. var builder = new MeshBuilder(false, true);
  471. builder.AddQuad(
  472. this.Point1,
  473. this.Point2,
  474. this.Point3,
  475. this.Point4,
  476. new Vector2(, ),
  477. new Vector2(, ),
  478. new Vector2(, ),
  479. new Vector2(, ));
  480. return builder.ToMesh();
  481. }
  482. }
  483.  
  484. public class Rectangle3D : MeshElement3D
  485. {
  486. public int DivLength { get; set; }
  487.  
  488. public int DivWidth { get; set; }
  489.  
  490. public Vector3 LengthDirection { get; set; }
  491.  
  492. public double Length { get; set; }
  493.  
  494. public Vector3 Normal { get; set; }
  495.  
  496. public Vector3 Origin { get; set; }
  497.  
  498. public double Width { get; set; }
  499.  
  500. public Rectangle3D()
  501. {
  502. this.DivLength = ;
  503. this.DivWidth = ;
  504. this.LengthDirection = Vector3.UnitX;
  505. this.Length = 10.0;
  506. this.Normal = Vector3.UnitZ;
  507. this.Origin = Vector3.Zero;
  508. this.Width = 10.0;
  509. }
  510.  
  511. protected override MeshGeometry3D Tessellate()
  512. {
  513. Vector3 u = this.LengthDirection;
  514. Vector3 w = this.Normal;
  515. Vector3 v = w.Cross(u);
  516. u = v.Cross(w);
  517.  
  518. u.Normalize();
  519. v.Normalize();
  520. w.Normalize();
  521.  
  522. double le = this.Length;
  523. double wi = this.Width;
  524.  
  525. var pts = new List<Vector3>();
  526. for (int i = ; i < this.DivLength; i++)
  527. {
  528. double fi = -0.5 + ((double)i / (this.DivLength - ));
  529. for (int j = ; j < this.DivWidth; j++)
  530. {
  531. double fj = -0.5 + ((double)j / (this.DivWidth - ));
  532. pts.Add(this.Origin + (u * le * fi) + (v * wi * fj));
  533. }
  534. }
  535.  
  536. var builder = new MeshBuilder(false, true);
  537. builder.AddRectangularMesh(pts, this.DivWidth);
  538.  
  539. return builder.ToMesh();
  540. }
  541. }
  542.  
  543. public class Sphere3D : MeshElement3D
  544. {
  545. public Vector3 Center { get; set; }
  546.  
  547. public int PhiDiv { get; set; }
  548.  
  549. public double Radius { get; set; }
  550.  
  551. public int ThetaDiv { get; set; }
  552.  
  553. public Sphere3D()
  554. {
  555. this.Center = Vector3.Zero;
  556. this.PhiDiv = ;
  557. this.Radius = 1.0;
  558. this.ThetaDiv = ;
  559. }
  560.  
  561. protected override MeshGeometry3D Tessellate()
  562. {
  563. var builder = new MeshBuilder(true, true);
  564. builder.AddSphere(this.Center, this.Radius, this.ThetaDiv, this.PhiDiv);
  565. return builder.ToMesh();
  566. }
  567. }
  568.  
  569. public class TruncatedCone3D : MeshElement3D
  570. {
  571. public bool BaseCap { get; set; }
  572.  
  573. public double BaseRadius { get; set; }
  574.  
  575. public double Height { get; set; }
  576.  
  577. public Vector3 Normal { get; set; }
  578.  
  579. public Vector3 Origin { get; set; }
  580.  
  581. public int ThetaDiv { get; set; }
  582.  
  583. public bool TopCap { get; set; }
  584.  
  585. public double TopRadius { get; set; }
  586.  
  587. public TruncatedCone3D()
  588. {
  589. this.BaseCap = true;
  590. this.BaseRadius = 1.0;
  591. this.Height = 2.0;
  592. this.Normal = Vector3.UnitZ;
  593. this.Origin = Vector3.Zero;
  594. this.ThetaDiv = ;
  595. this.TopCap = true;
  596. this.TopRadius = 0.0;
  597. }
  598.  
  599. protected override MeshGeometry3D Tessellate()
  600. {
  601. var builder = new MeshBuilder(false, true);
  602. builder.AddCone(
  603. this.Origin,
  604. this.Normal,
  605. this.BaseRadius,
  606. this.TopRadius,
  607. this.Height,
  608. this.BaseCap,
  609. this.TopCap,
  610. this.ThetaDiv);
  611. return builder.ToMesh();
  612. }
  613. }
  614.  
  615. public abstract class ParametricSurface3D : MeshElement3D
  616. {
  617. public int MeshSizeV { get; set; }
  618.  
  619. public int MeshSizeU { get; set; }
  620.  
  621. public ParametricSurface3D()
  622. {
  623. this.MeshSizeU = ;
  624. this.MeshSizeV = ;
  625. }
  626.  
  627. protected abstract Vector3 Evaluate(double u, double v, out Vector2 textureCoord);
  628.  
  629. protected override MeshGeometry3D Tessellate()
  630. {
  631. var mesh = new MeshGeometry3D();
  632. mesh.Positions = new List<Vector3>();
  633. mesh.TextureCoordinates = new List<Vector2>();
  634. mesh.TriangleIndices = new List<int>();
  635.  
  636. int n = this.MeshSizeU;
  637. int m = this.MeshSizeV;
  638. var p = new Vector3[m * n];
  639. var tc = new Vector2[m * n];
  640.  
  641. // todo: use MeshBuilder
  642.  
  643. // todo: parallel execution...
  644. // Parallel.For(0, n, (i) =>
  645. for (int i = ; i < n; i++)
  646. {
  647. double u = 1.0 * i / (n - );
  648.  
  649. for (int j = ; j < m; j++)
  650. {
  651. double v = 1.0 * j / (m - );
  652. int ij = (i * m) + j;
  653. p[ij] = this.Evaluate(u, v, out tc[ij]);
  654. }
  655. }
  656.  
  657. // );
  658. int idx = ;
  659. for (int i = ; i < n; i++)
  660. {
  661. for (int j = ; j < m; j++)
  662. {
  663. mesh.Positions.Add(p[idx]);
  664. mesh.TextureCoordinates.Add(tc[idx]);
  665. idx++;
  666. }
  667. }
  668.  
  669. for (int i = ; i + < n; i++)
  670. {
  671. for (int j = ; j + < m; j++)
  672. {
  673. int x0 = i * m;
  674. int x1 = (i + ) * m;
  675. int y0 = j;
  676. int y1 = j + ;
  677. AddTriangle(mesh, x0 + y0, x0 + y1, x1 + y0);
  678. AddTriangle(mesh, x1 + y0, x0 + y1, x1 + y1);
  679. }
  680. }
  681.  
  682. return mesh;
  683. }
  684.  
  685. private static void AddTriangle(MeshGeometry3D mesh, int i1, int i2, int i3)
  686. {
  687. var p1 = mesh.Positions[i1];
  688. if (!IsDefined(p1))
  689. {
  690. return;
  691. }
  692.  
  693. var p2 = mesh.Positions[i2];
  694. if (!IsDefined(p2))
  695. {
  696. return;
  697. }
  698.  
  699. var p3 = mesh.Positions[i3];
  700. if (!IsDefined(p3))
  701. {
  702. return;
  703. }
  704.  
  705. mesh.TriangleIndices.Add(i1);
  706. mesh.TriangleIndices.Add(i2);
  707. mesh.TriangleIndices.Add(i3);
  708. }
  709.  
  710. private static bool IsDefined(Vector3 point)
  711. {
  712. return !double.IsNaN(point.x) && !double.IsNaN(point.y) && !double.IsNaN(point.z);
  713. }
  714. }
  715.  
  716. public class Helix3D : ParametricSurface3D
  717. {
  718. public Vector3 Origin { get; set; }
  719.  
  720. public double Diameter { get; set; }
  721.  
  722. public double Length { get; set; }
  723.  
  724. public double Phase { get; set; }
  725.  
  726. public double Radius { get; set; }
  727.  
  728. public double Turns { get; set; }
  729.  
  730. public Helix3D()
  731. {
  732. this.Origin = Vector3.Zero;
  733. this.Diameter = 0.5;
  734. this.Length = 1.0;
  735. this.Phase = 0.0;
  736. this.Radius = 1.0;
  737. this.Turns = 1.0;
  738. }
  739.  
  740. protected override Vector3 Evaluate(double u, double v, out Vector2 texCoord)
  741. {
  742. double color = u;
  743. v *= * Math.PI;
  744.  
  745. double b = this.Turns * * Math.PI;
  746. double r = this.Radius / ;
  747. double d = this.Diameter;
  748. double dr = this.Diameter / r;
  749. double p = this.Phase / * Math.PI;
  750.  
  751. double x = r * Math.Cos((b * u) + p) * ( + (dr * Math.Cos(v)));
  752. double y = r * Math.Sin((b * u) + p) * ( + (dr * Math.Cos(v)));
  753. double z = (u * this.Length) + (d * Math.Sin(v));
  754.  
  755. texCoord = new Vector2(color, );
  756. return this.Origin + new Vector3(x, y, z);
  757. }
  758. }
  759.  
  760. public class Extruded3D : MeshElement3D
  761. {
  762. public List<double> Diameters { get; set; }
  763.  
  764. public Vector3 SectionXAxis { get; set; }
  765.  
  766. public List<double> Angles { get; set; }
  767.  
  768. public bool IsPathClosed { get; set; }
  769.  
  770. public bool IsSectionClosed { get; set; }
  771.  
  772. public List<Vector3> Path { get; set; }
  773.  
  774. public List<Vector2> Section { get; set; }
  775.  
  776. public List<double> TextureCoordinates { get; set; }
  777.  
  778. public Extruded3D()
  779. {
  780. this.Diameters = null;
  781. this.SectionXAxis = new Vector3();
  782. this.Angles = null;
  783. this.IsPathClosed = false;
  784. this.IsSectionClosed = true;
  785. this.Path = new List<Vector3>();
  786. this.Section = new List<Vector2>();
  787. this.TextureCoordinates = null;
  788. }
  789.  
  790. protected override MeshGeometry3D Tessellate()
  791. {
  792. if (this.Path == null || this.Path.Count == )
  793. {
  794. return null;
  795. }
  796.  
  797. // See also "The GLE Tubing and Extrusion Library":
  798. // http://linas.org/gle/
  799. // http://sharpmap.codeplex.com/Thread/View.aspx?ThreadId=18864
  800. var builder = new MeshBuilder(false, this.TextureCoordinates != null);
  801.  
  802. var sectionXAxis = this.SectionXAxis;
  803. if (sectionXAxis.Length < 1e-)
  804. {
  805. sectionXAxis = new Vector3(, , );
  806. }
  807.  
  808. var forward = this.Path[] - this.Path[];
  809. var up = forward.Cross(sectionXAxis);
  810. if (up.LengthSquared < 1e-)
  811. {
  812. sectionXAxis = forward.FindAnyPerpendicular();
  813. }
  814.  
  815. builder.AddTube(
  816. this.Path,
  817. this.Angles,
  818. this.TextureCoordinates,
  819. this.Diameters,
  820. this.Section,
  821. sectionXAxis,
  822. this.IsPathClosed,
  823. this.IsSectionClosed);
  824. return builder.ToMesh();
  825. }
  826. }
  827.  
  828. public class TubeVisual3D : Extruded3D
  829. {
  830. public double Diameter { get; set; }
  831.  
  832. public int ThetaDiv { get; set; }
  833.  
  834. public TubeVisual3D()
  835. {
  836. this.Diameter = 1.0;
  837. this.ThetaDiv = ;
  838. }
  839.  
  840. protected override MeshGeometry3D Tessellate()
  841. {
  842. var pc = new List<Vector2>();
  843. var circle = MeshBuilder.GetCircle(this.ThetaDiv);
  844.  
  845. // If Diameters is set, create a unit circle
  846. // otherwise, create a circle with the specified diameter
  847. double r = this.Diameters != null ? : this.Diameter / ;
  848. for (int j = ; j < this.ThetaDiv; j++)
  849. {
  850. pc.Add(new Vector2(circle[j].x * r, circle[j].y * r));
  851. }
  852.  
  853. this.Section = pc;
  854. return base.Tessellate();
  855. }
  856. }

HelixToolkit 基本元素封装

  配合我们上面的几个类一起使用,就是我们最前面看到的效果,顶点上显示元素,FPS几乎没有任何变化.要知道整合快300多个球体后,如上图,顶点索引达到快20W了,用ManualObject直接渲染出错,因为ManualObject的索引固定只能是Int16,最高索引值只用6W多点.

  1. string pointName = this.Parent.MeshName + "/point/Show";
  2. Root.Instance.SceneManager.DeleteEntityAndMesh(pointName);
  3. MergerBatch merBatch = new MergerBatch();
  4. foreach (var vect in this)
  5. {
  6. //SelectPoint和别的顶点混在一起,直接改变SelectPoint可能被别的遮住.
  7. //单独渲染这个被选择了的
  8. if (vect == this.SelectPoint)
  9. {
  10. ManualObject mo = new ManualObject(this.Parent.MeshName + "point/Show/Select");
  11. //以Overlay层渲染,几乎不会被遮住
  12. mo.RenderQueueGroup = RenderQueueGroupID.Overlay;
  13. var sphereX1 = new Sphere3D();
  14. sphereX1.Center = vect.VertexInfo.Position;
  15. sphereX1.ThetaDiv = ;
  16. sphereX1.Radius = this.Scale;
  17. sphereX1.ToManualObjectSection(mo, "Vertex/Selected");
  18. this.Parent.PointNode.AttachObject(mo);
  19. continue;
  20. }
  21. var builder = new MeshBuilder(false, false);
  22. builder.AddSphere(vect.VertexInfo.Position, Scale, , );
  23. merBatch.AddBatch(builder.Positions, builder.TriangleIndices, vect.Color);
  24. }
  25. Mesh mesh = merBatch.ToMesh(pointName);
  26. Entity ent = Root.Instance.SceneManager.CreateEntity(pointName, mesh);
  27. //一般的是Main,50
  28. ent.RenderQueueGroup = RenderQueueGroupID.Six;
  29. this.Parent.PointNode.AttachObject(ent);

  现在可以说达到我想要的效果,FPS不掉,顶点上能显示任何几何模型(虽然图上全是点,但是每个顶点可以显示不同的也在同一批次),顶点上几何模型能独立设置颜色.上面代码中添加的SelectPoint是单独作用一个批次渲染,其实是可以合到一起,但是会出现一个问题,如果多个顶点在你摄像机前重合在一起,你后面的拾取到的顶点,你可能并没有看到他变颜色,因为被遮住了,现在单独渲染,提高被选择顶点的渲染优先级.

  但是这个MergerBatch合并限制和缺点也很多,如只能合并同材质的,合并后的拾取只针对统一整体,单独需要自己计算,摄像机的可见测试也是以整体来测试.但是这些对于这种编辑显示顶点的来说不需要考虑.

Axiom3D:Ogre公告板集与合并批次的更多相关文章

  1. BZOJ2733[HNOI2012]永无乡——线段树合并+并查集+启发式合并

    题目描述 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以从一个岛 到达 ...

  2. BZOJ 3673: 可持久化并查集(可持久化并查集+启发式合并)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3673 题意: 思路: 可持久化数组可以用可持久化线段树来实现,并查集的查询操作和原来的一般并查集操作 ...

  3. Axiom3D:Ogre动画基本流程与骨骼动画

    在Axiom中,Animation类用于管理动画,在此对象中主要管理着AnimationTrack对象,此对象用于管理动画的各种类型的每一桢.在Axiom中,动画类型主要有变形动画,姿态动画,骨骼动画 ...

  4. BZOJ 4668: 冷战 并查集启发式合并/LCT

    挺好想的,最简单的方法是并查集启发式合并,加暴力跳父亲. 然而,这个代码量比较小,比较好写,所以我写了 LCT,更具挑战性. #include <cstdio> #include < ...

  5. [HDU 3712] Fiolki (带边权并查集+启发式合并)

    [HDU 3712] Fiolki (带边权并查集+启发式合并) 题面 化学家吉丽想要配置一种神奇的药水来拯救世界. 吉丽有n种不同的液体物质,和n个药瓶(均从1到n编号).初始时,第i个瓶内装着g[ ...

  6. [BZOJ 4668]冷战(带边权并查集+启发式合并)

    [BZOJ 4668]冷战(并查集+启发式合并) 题面 一开始有n个点,动态加边,同时查询u,v最早什么时候联通.强制在线 分析 用并查集维护连通性,每个点x还要另外记录tim[x],表示x什么时间与 ...

  7. Codeforces 1166F 并查集 启发式合并

    题意:给你一张无向图,无向图中每条边有颜色.有两种操作,一种是询问从x到y是否有双彩虹路,一种是在x到y之间添加一条颜色为z的边.双彩虹路是指:如果给这条路径的点编号,那么第i个点和第i - 1个点相 ...

  8. Axiom3D:Ogre射线与点,线,面相交,鼠标操作3维空间.

    在第一篇网络分解成点,线,面.第二篇分别点以球形,线以圆柱,面分别以MergerBatch整合批次显示.因为整合批次显示后,相应的点,线,面不能以Ogre本身的射线来选取,因为整合后,以点举例,多个点 ...

  9. Axiom3D:Ogre中Mesh网格分解成点线面。

    这个需求可能比较古怪,一般Mesh我们组装好顶点,索引数据后,直接放入索引缓冲渲染就好了.但是如果有些特殊需要,如需要标注出Mesh的顶点,线,面这些信息,以及特殊显示这些信息. 最开始我想的是自己分 ...

随机推荐

  1. ActionBar 笔记

    博客地址: http://blog.csdn.net/eclipsexys/article/details/8688538 官方文档: http://developer.android.com/gui ...

  2. Shell中整数比較

    -eq             等于,如:if ["$a" -eq "$b" ] -ne             不等于,如:if ["$a" ...

  3. git rm与git rm --cached

    当我们需要删除暂存区或分支上的文件, 同时工作区也不需要这个文件了, 可以使用 git rm file_path git commit -m 'delete somefile' git push 当我 ...

  4. ES6,新增数据结构Set的用法

    ES6 提供了新的数据结构 Set. 特性 似于数组,但它的一大特性就是所有元素都是唯一的,没有重复. 我们可以利用这一唯一特性进行数组的去重工作. 单一数组的去重. let set6 = new S ...

  5. android 调用系统相机拍照 获取原图

      好吧,为了这个问题又折腾了一整天.之前在网上找来的方法,如果在onActivityResult中直接用data.getData()的方式来生成bitmap,其实获取的是拍照生成的缩略图!看看尺寸就 ...

  6. 如何添加Samba用户

    Window系统连上我们的开发机Linux,自然需要在Samba里添加一个新用户. linux-06bq:/usr/local/services/samba/bin # ./smbpasswd -a ...

  7. Django服务端读取excel文件并且传输到接口

    path_name = "opboss_download_" + str(int(time.time())) + ".csv" print(path_name) ...

  8. Python 操作redis 常用方法

    Python 操作redis 1.字符串 #!/usr/bin/env python # -*- coding:utf-8 -*- import redis # python 操作str class ...

  9. pandas简单应用

    机器学习离不开数据,数据分析离不开pandas.昨天感受了一下,真的方便.按照一般的使用过程,将pandas的常用方法说明一下. 首先,我们拿到一个excel表,我们将之另存为csv文件.因为文件是实 ...

  10. Python爬虫技巧

    Python爬虫技巧一之设置ADSL拨号服务器代理 reference: https://zhuanlan.zhihu.com/p/25286144 爬取数据时,是不是只能每个网站每个网站的分析,有没 ...