private void CalculateRaysForOrthographic() { var aspectRatio = (float)width / height; // In orthographic rendering direction is same for all rays. var direction = transform.rotation * Vector3.forward; for (int j = 0; j < height; j++) { for (int i = 0; i < width; i++) { // Firstly we transform from texture space into normalized texture space. var pixelNormalizedX = ((i + 0.5f)) / width; var pixelNormalizedY = ((j + 0.5f)) / height; // Then we offset the space from [0:1] to [-1:1] var pixelScreenX = pixelNormalizedX * 2 - 1; var pixelScreenY = pixelNormalizedY * 2 - 1; // Taking field of view into calculation and aspect ration in case screen is not uniform. var pixelCameraX = pixelScreenX * aspectRatio * orthographicSize; var pixelCameraY = pixelScreenY * orthographicSize; var position = new Vector3(pixelCameraX, pixelCameraY, 0); position += transform.position; // To world space. var rayData = new RayCastingRayData(); rayData.position = position; rayData.direction = direction; rayDatas[j * width + i] = rayData; } } }
private void CalculateRaysForPerspective() { // Calculate the field of view transformation. // TODO: Cache. var degress = fieldOfView * Mathf.Deg2Rad / 2; var sizeOfOffset = Mathf.Tan(degress); var aspectRatio = (float)Screen.width / Screen.height; // In projection rendering position is same for all rays. var position = transform.position; for (int j = 0; j < height; j++) { for (int i = 0; i < width; i++) { // Firstly we transform from texture space into normalized texture space. var pixelNormalizedX = ((i + 0.5f)) / width; var pixelNormalizedY = ((j + 0.5f)) / height; // Then we offset the space from [0:1] to [-1:1] var pixelScreenX = pixelNormalizedX * 2 - 1; var pixelScreenY = pixelNormalizedY * 2 - 1; // Taking field of view into calculation and aspect ration in case screen is not uniform. var pixelCameraX = pixelScreenX * sizeOfOffset * aspectRatio; var pixelCameraY = pixelScreenY * sizeOfOffset; var direction = new Vector3(pixelCameraX, pixelCameraY, 1); direction = transform.rotation * direction.normalized; // To world space. var rayData = new RayCastingRayData(); rayData.position = position; rayData.direction = direction; rayDatas[j * width + i] = rayData; } } }