public static PickRay operator *(Matrix4 trans, PickRay ray) { PickRay result = new PickRay(); result.Origin = (trans * new Vector4(ray.Origin, 1)).DivW.XYZ; result.Direction = (trans * new Vector4(ray.Direction, 0)).XYZ; return(result); }
public Vector3?Pick(PickRay ray) { Vector3?closestPoint = null; float closestDist = 0; if (_primitiveTopology == PrimitiveTopology.TriangleList) { for (int i = 0; i < (IsIndexed ? _indices.Length : _vertices.Length); i += 3) { Vector3 p0; Vector3 p1; Vector3 p2; if (IsIndexed) { p0 = _vertices[_indices[i + 0]].Position; p1 = _vertices[_indices[i + 1]].Position; p2 = _vertices[_indices[i + 2]].Position; } else { p0 = _vertices[i + 0].Position; p1 = _vertices[i + 1].Position; p2 = _vertices[i + 2].Position; } Vector3?point = RayTriIntersect(ray.Origin, ray.Direction, p0, p1, p2); if (point != null) { float dist = (ray.Origin - point.Value).Length; if (closestPoint == null || dist < closestDist) { closestPoint = point; closestDist = dist; } } } } else if (_primitiveTopology == PrimitiveTopology.LineList) { for (int i = 0; i < (IsIndexed ? _indices.Length : _vertices.Length); i += 2) { Vector3 p0; Vector3 p1; if (IsIndexed) { p0 = _vertices[_indices[i + 0]].Position; p1 = _vertices[_indices[i + 1]].Position; } else { p0 = _vertices[i + 0].Position; p1 = _vertices[i + 1].Position; } Vector3?point = RayLineIntersect(ray.Origin, ray.Direction, p0, p1); if (point != null) { float dist = (ray.Origin - point.Value).Length; if (closestPoint == null || dist < closestDist) { closestPoint = point; closestDist = dist; } } } } else { // not implemented } return(closestPoint); }