/// <summary> /// Triangle lines /// </summary> private void FillLineList() { var forward = Vector3.Forward; for (int startIndex = 0; startIndex < this.linePoints.Count - 1; startIndex += MaxLinePointsPerMesh) { int nPoints = Math.Min(MaxLinePointsPerMesh, this.linePoints.Count - startIndex); // Set vertex buffer var vertices = new VertexPositionColorTextureAxis[nPoints * VerticesPerPoint]; var indices = new ushort[nPoints * IndicesPerPoint]; var startPoint = this.linePoints[startIndex + 0]; var endPoint = this.linePoints[startIndex + 1]; float uCoord = startIndex / (float)this.linePoints.Count; Vector3 thicknessDirection; int pointIndex = 0; int indexArray = 0; for (int i = 0; i < nPoints - 1; i += 2) { startPoint = this.linePoints[startIndex + i]; endPoint = this.linePoints[startIndex + i + 1]; thicknessDirection = endPoint.Position - startPoint.Position; if (!this.IsCameraAligned) { thicknessDirection.Normalize(); Vector3.Cross(ref forward, ref thicknessDirection, out thicknessDirection); thicknessDirection.Normalize(); } int vertexIndex = pointIndex; this.AddVertex(ref startPoint, ref thicknessDirection, 0, pointIndex, vertices); pointIndex += 2; this.AddVertex(ref endPoint, ref thicknessDirection, 1, pointIndex, vertices); pointIndex += 2; indices[indexArray + 0] = (ushort)(vertexIndex + 0); indices[indexArray + 1] = (ushort)(vertexIndex + 1); indices[indexArray + 2] = (ushort)(vertexIndex + 2); indices[indexArray + 3] = (ushort)(vertexIndex + 2); indices[indexArray + 4] = (ushort)(vertexIndex + 1); indices[indexArray + 5] = (ushort)(vertexIndex + 3); indexArray += IndicesPerPoint * 2; } this.AddMesh(vertices, indices, PrimitiveType.TriangleList); } }
/// <summary> /// Strip lines /// </summary> private void FillStripLines() { if (this.isLoop) { this.linePoints.Add(this.linePoints[0]); } var forward = Vector3.Forward; LinePointInfo currentPoint; Vector3 direction; Vector3 prevPosition; Vector3 currentPosition; Vector3 nextPosition; float totalLenght = 0; float[] lenghtByPoint = new float[this.linePoints.Count]; prevPosition = this.linePoints[0].Position; for (int p = 1; p < lenghtByPoint.Length; p++) { currentPosition = this.linePoints[p].Position; direction = currentPosition - prevPosition; totalLenght += direction.Length(); lenghtByPoint[p] = totalLenght; prevPosition = currentPosition; } for (int startIndex = 0; startIndex < (this.linePoints.Count - 1); startIndex += MaxLinePointsPerMesh) { int nPoints = Math.Min(MaxLinePointsPerMesh, this.linePoints.Count - startIndex); var lastIndex = startIndex + nPoints - 1; // Set vertex buffer var vertices = new VertexPositionColorTextureAxis[nPoints * VerticesPerPoint]; for (int p = startIndex; p <= lastIndex; p++) { currentPoint = this.linePoints[p]; currentPosition = currentPoint.Position; if (p == 0) { prevPosition = this.isLoop ? this.linePoints[lastIndex - 1].Position : currentPosition; } if (p < lastIndex) { nextPosition = this.linePoints[p + 1].Position; } else { nextPosition = this.isLoop ? this.linePoints[1].Position : currentPosition; } direction = nextPosition - currentPosition; direction.Normalize(); var prevDirection = prevPosition - currentPosition; prevDirection.Normalize(); var thicknessDirection = direction - prevDirection; if (!this.IsCameraAligned) { thicknessDirection.Normalize(); Vector3.Cross(ref forward, ref thicknessDirection, out thicknessDirection); thicknessDirection.Normalize(); } float thicknessFactor = 1; if ((p > 0 && p < lastIndex) || this.isLoop) { var angle = Vector3.Angle(ref direction, ref prevDirection); thicknessFactor = 1f / (float)Math.Sin(angle * 0.5); } this.AddVertex(ref currentPoint, ref thicknessDirection, lenghtByPoint[p] / totalLenght, p * 2, vertices, thicknessFactor); prevPosition = currentPosition; } // Set index buffer var indices = new ushort[vertices.Length]; for (int i = 1; i < vertices.Length; i++) { indices[i] = (ushort)i; } this.AddMesh(vertices, indices, PrimitiveType.TriangleStrip); } if (this.isLoop) { this.linePoints.RemoveAt(this.linePoints.Count - 1); } }