public void SelectLine(WCell.Util.Graphics.Vector3 p1, WCell.Util.Graphics.Vector3 p2, Color color, bool doNotReplace = true) { const float halfLineWidth = 0.5f; var s1 = p1 - halfLineWidth; var s2 = p1 + halfLineWidth; var t1 = p2 - halfLineWidth; var t2 = p2 + halfLineWidth; s1.Z = s2.Z = Math.Max(s1.Z, s2.Z); t1.Z = t2.Z = Math.Max(t1.Z, t2.Z); var tri1 = new Triangle(s1, s2, t2); var tri2 = new Triangle(t2, t1, s1); Select(ref tri1, doNotReplace, color); Select(ref tri2, true, color); }
public void SelectPoint(WCell.Util.Graphics.Vector3 p, Color color, bool doNotReplace = true) { const float sideDist = 0.5f; var p1 = p; var p2 = p; var p3 = p; p1.X += sideDist; p2.X -= sideDist; p2.Y -= sideDist; p3.X -= sideDist; p3.Y += sideDist; var tri1 = new Triangle(p1, p2, p3); Select(ref tri1, doNotReplace, color); }
public void Select(ref Triangle tri, bool doNotReplace, Color color) { var v = _cachedVertices.Length; var i = _cachedIndices.Length; if (doNotReplace) { Array.Resize(ref _cachedVertices, _cachedVertices.Length + 3); Array.Resize(ref _cachedIndices, _cachedIndices.Length + 3); } else { v = i = 0; _cachedVertices = new VertexPositionNormalColored[3]; _cachedIndices = new int[3]; } // add vertices var normal = WCell.Util.Graphics.Vector3.Cross(tri.Point3 - tri.Point1, tri.Point2 - tri.Point1).ToXna(); normal.Normalize(); _cachedVertices[v] = new VertexPositionNormalColored(tri.Point1.ToXna(), color, normal); _cachedVertices[v+1] = new VertexPositionNormalColored(tri.Point2.ToXna(), color, normal); _cachedVertices[v+2] = new VertexPositionNormalColored(tri.Point3.ToXna(), color, normal); // add indices _cachedIndices[i] = v; _cachedIndices[i+1] = v+1; _cachedIndices[i+2] = v+2; }
public void GetTriangle(int triIndex, out Triangle triangle) { triangle = new Triangle { Point1 = TerrainVertices[TerrainIndices[triIndex++]], Point2 = TerrainVertices[TerrainIndices[triIndex++]], Point3 = TerrainVertices[TerrainIndices[triIndex]] }; }
public void Select(ref Triangle tri, bool doNotReplace = true) { Select(ref tri, doNotReplace, SelectedTriangleColor); }
/// <summary> /// Intersects a ray with the given triangle /// I think it does not currently work? /// </summary> /// <param name="dist">The distance of the given point</param> public static bool RayTriangleIntersect2(Ray ray, Triangle tri, out float dist) { // See http://www.cs.princeton.edu/courses/archive/fall00/cs426/lectures/raycast/sld017.htm var normal = tri.CalcNormalizedNormal(); var d = Vector3.Dot(normal, tri.Point1); dist = -(Vector3.Dot(ray.Position, normal) + d) / (Vector3.Dot(ray.Direction, normal)); if (float.IsNaN(dist) || dist < 0) { // no intersection return false; } // intersection point var p = ray.Position + ray.Direction * dist; // See http://www.cs.princeton.edu/courses/archive/fall00/cs426/lectures/raycast/sld018.htm var n1 = Vector3.Cross(tri.Point1 - ray.Position, tri.Point2 - ray.Position); n1.Normalize(); var d1 = -Vector3.Dot(ray.Position, n1); if (Vector3.Dot(p, n1) + d1 < 0) { return false; } var n2 = Vector3.Cross(tri.Point2 - ray.Position, tri.Point3 - ray.Position); n2.Normalize(); var d2 = -Vector3.Dot(ray.Position, n2); if (Vector3.Dot(p, n2) + d2 < 0) { return false; } var n3 = Vector3.Cross(tri.Point3 - ray.Position, tri.Point1 - ray.Position); n3.Normalize(); var d3 = -Vector3.Dot(ray.Position, n3); if (Vector3.Dot(p, n3) + d3 < 0) { return false; } return true; }
public static int GetSharedEdgeMask(this Triangle tri1, Triangle tri2) { var mask = 0; if (tri1.Point1.Equals(tri2.Point1) || tri1.Point1.Equals(tri2.Point2) || tri1.Point1.Equals(tri2.Point3)) { mask = TrianglePointA; } if (tri1.Point2.Equals(tri2.Point1) || tri1.Point2.Equals(tri2.Point2) || tri1.Point2.Equals(tri2.Point3)) { mask |= TrianglePointB; } if (tri1.Point3.Equals(tri2.Point1) || tri1.Point3.Equals(tri2.Point2) || tri1.Point3.Equals(tri2.Point3)) { mask |= TrianglePointC; } return mask; }