public static RayHit RayPlaneIntersection(Ray ray, Vector3 pNormal, Vector3 pPoint) { // From the distance formula of ray plane intersection float divisor = Vector3.Dot(pNormal, ray.direction); float dividend = Vector3.Dot(pPoint - ray.origin, pNormal); RayHit rayHit = RayHit.CreateRayHit(); float Epsilon = 1.19e-07f; if (Mathf.Abs(divisor) <= Epsilon) { // The line and plane is parallel, check if the line lies on the plane if (Mathf.Abs(dividend) <= Epsilon) { rayHit.distance = 0; rayHit.hitPoint = Vector3.zero; return(rayHit); } else { rayHit.distance = float.PositiveInfinity; rayHit.hitPoint = Vector3.positiveInfinity; return(rayHit); } } rayHit.distance = dividend / divisor; rayHit.hitPoint = SpatialGridTrace.GetPoint(ray, rayHit.distance); return(rayHit); }
private void Update() { Vector3 origin = transform.position; Vector3 direction = transform.forward; if (origin != cacheOrigin || direction != cacheDirection) { Ray cameraRay = new Ray(origin, direction); SpatialGrid spatialGrid = SpatialGrid.CreateSpatialGrid(CubeGlobalData.Instance.boxMin, CubeGlobalData.Instance.boxMax, CubeGlobalData.Instance.GetDimension()); m_bestRayHit = SpatialGridTrace.Trace(cameraRay, spatialGrid, _GeometryCountList, _GeometryGridList, -1); cacheOrigin = origin; cacheDirection = direction; } voxelsHitList.ForEach((controller => { controller.ApplyColor(Color.red); })); }
public static void DistanceBetweenBoundary(Ray ray, GridStep steps, SpatialGridIndex entryGridIndex, SpatialGrid grids, Vector3 firstHitX, Vector3 firstHitY, Vector3 firstHitZ, ref RayHit rayHitX, ref RayHit rayHitY, ref RayHit rayHitZ) { Vector3 min = GetGridLocalMin(grids, entryGridIndex); Vector3 max = GetGridLocalMax(grids, entryGridIndex); Ray rX = SpatialGridTrace.CreateRay(firstHitX, ray.direction); Ray rY = SpatialGridTrace.CreateRay(firstHitY, ray.direction); Ray rZ = SpatialGridTrace.CreateRay(firstHitZ, ray.direction); if (steps.x >= 0) { rayHitX = RayPlaneIntersectionCompute.RayPlaneIntersection(rX, GridNormalLeft, max + GridNormalRight); } else { rayHitX = RayPlaneIntersectionCompute.RayPlaneIntersection(rX, GridNormalRight, min + GridNormalLeft); } if (steps.y >= 0) { rayHitY = RayPlaneIntersectionCompute.RayPlaneIntersection(rY, GridNormalDown, max + GridNormalUp); } else { rayHitY = RayPlaneIntersectionCompute.RayPlaneIntersection(rY, GridNormalUp, min + GridNormalDown); } if (steps.z >= 0) { rayHitZ = RayPlaneIntersectionCompute.RayPlaneIntersection(rZ, GridNormalBack, max + GridNormalForward); } else { rayHitZ = RayPlaneIntersectionCompute.RayPlaneIntersection(rZ, GridNormalForward, min + GridNormalBack); } }