public void SetColor(int x, int y, int z) { if (y == 1) { UpPlane.SetActive(true); } else if (y == -1) { DownPlane.SetActive(true); } if (z == 1) { LeftPlane.SetActive(true); } else if (z == -1) { RightPlane.SetActive(true); } if (x == 1) { BackPlane.SetActive(true); } else if (x == -1) { FrontPlane.SetActive(true); } }
/// <summary> /// Calculates the volume world space points for the specified camera. /// Must be called after the world planes have been calculated. /// </summary> private void CalculateWorldPoints(Camera camera) { Transform camTransform = camera.transform; // Cast a ray from the camera position towards the far plane. The intersection point // bewteen the ray and the plane represents the far plane middle point. We have to // take into account that all volume planes point inside the volume, so the ray will // be cast along the reverse of the far plane normal. Plane farPlane = FarPlane; Ray ray = new Ray(camTransform.position, -farPlane.normal); float t; if (farPlane.Raycast(ray, out t)) { Vector3 ptOnMidFar = ray.GetPoint(t); Vector3 ptOnMidTopFar = Vector3.zero, ptOnMidRightFar = Vector3.zero; // We have the point which sits in the middle of the far plane. The next step is // to calculate the points which sit to the right and up of this point. These will // be used to calculate the half dimension of the far plane. ray = new Ray(ptOnMidFar, camTransform.up); if (TopPlane.Raycast(ray, out t)) { ptOnMidTopFar = ray.GetPoint(t); } ray = new Ray(ptOnMidFar, camTransform.right); if (RightPlane.Raycast(ray, out t)) { ptOnMidRightFar = ray.GetPoint(t); } // Calculate the half plane dimensions using the points we calculated earlier float planeHalfWidth = (ptOnMidRightFar - ptOnMidFar).magnitude; float planeHalfHeight = (ptOnMidTopFar - ptOnMidFar).magnitude; // Move from the far plane middle point left/right and bottom/top to calculate the // far plane corner points. Because the camera volume is rotated along with the camera // coordinate system, the camera local axes are used to move left/right and top/bottom. _worldPoints[(int)VPoint.FarTopLeft] = ptOnMidFar - camTransform.right * planeHalfWidth + camTransform.up * planeHalfHeight; _worldPoints[(int)VPoint.FarTopRight] = ptOnMidFar + camTransform.right * planeHalfWidth + camTransform.up * planeHalfHeight; _worldPoints[(int)VPoint.FarBottomRight] = ptOnMidFar + camTransform.right * planeHalfWidth - camTransform.up * planeHalfHeight; _worldPoints[(int)VPoint.FarBottomLeft] = ptOnMidFar - camTransform.right * planeHalfWidth - camTransform.up * planeHalfHeight; } // Do the same for the near plane. // Note: For an ortho camera, the near plane can reside behind the camera position. So an additional // step is needed to identify the ray direction vector. We check if the distance from the plane // to the camera position is >= 0. If it is, it means the camera position is in front of the plane. // Considering that the plane points inwards, it means we have to travel along the reverse of the // plane normal to hit the plane. If the distance is negative, the camera position lies behind the // plane and we can travel along the plane normal to hit the plane. Plane nearPlane = NearPlane; bool camInFrontOfNearPlane = nearPlane.GetDistanceToPoint(camTransform.position) >= 0.0f; Vector3 rayDir = camInFrontOfNearPlane ? -nearPlane.normal : nearPlane.normal; ray = new Ray(camTransform.position, rayDir); if (nearPlane.Raycast(ray, out t)) { Vector3 ptOnMidNear = ray.GetPoint(t); Vector3 ptOnMidTopNear = Vector3.zero, ptOnMidRightFar = Vector3.zero; ray = new Ray(ptOnMidNear, camTransform.up); if (TopPlane.Raycast(ray, out t)) { ptOnMidTopNear = ray.GetPoint(t); } ray = new Ray(ptOnMidNear, camTransform.right); if (RightPlane.Raycast(ray, out t)) { ptOnMidRightFar = ray.GetPoint(t); } float planeHalfWidth = (ptOnMidRightFar - ptOnMidNear).magnitude; float planeHalfHeight = (ptOnMidTopNear - ptOnMidNear).magnitude; _worldPoints[(int)VPoint.NearTopLeft] = ptOnMidNear - camTransform.right * planeHalfWidth + camTransform.up * planeHalfHeight; _worldPoints[(int)VPoint.NearTopRight] = ptOnMidNear + camTransform.right * planeHalfWidth + camTransform.up * planeHalfHeight; _worldPoints[(int)VPoint.NearBottomRight] = ptOnMidNear + camTransform.right * planeHalfWidth - camTransform.up * planeHalfHeight; _worldPoints[(int)VPoint.NearBottomLeft] = ptOnMidNear - camTransform.right * planeHalfWidth - camTransform.up * planeHalfHeight; } }