Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #version 450
- layout (binding = 0, rgba8) uniform writeonly image2D resultImage;
- layout (binding = 1, r32ui) uniform readonly uimage3D worldImage;
- layout(push_constant) uniform PushConstants {
- vec3 rayOrigin;
- float viewGridTopLeftCornerX;
- vec3 viewGridRight;
- float viewGridTopLeftCornerY;
- vec3 viewGridDown;
- float viewGridTopLeftCornerZ;
- float viewGridDensity;
- } push;
- void main() {
- vec3 viewGridTopLeftCorner = vec3(push.viewGridTopLeftCornerX, push.viewGridTopLeftCornerY, push.viewGridTopLeftCornerZ);
- vec3 samplePoint = viewGridTopLeftCorner + push.viewGridDensity * (gl_GlobalInvocationID.x * push.viewGridRight + gl_GlobalInvocationID.y * push.viewGridDown);
- vec3 ray = normalize(samplePoint - push.rayOrigin);
- vec3 inverseRay = 1 / ray;
- ivec3 voxelCoords = ivec3(floor(push.rayOrigin.x), floor(push.rayOrigin.y), floor(push.rayOrigin.z));
- ivec3 rayOrientation = ivec3(sign(ray));
- ivec3 rayPositivity = (1 + rayOrientation) >> 1;
- vec3 withinVoxelCoords = push.rayOrigin - voxelCoords;
- uint blockID = 0;
- int smallestComponentIndex;
- do {
- // Calculate how far each of the neighbouring voxels are
- vec3 distanceFactor = (rayPositivity - withinVoxelCoords) * inverseRay;
- // Pinpoint the closest voxel of the three candidates
- smallestComponentIndex = distanceFactor.x < distanceFactor.y
- ? (distanceFactor.x < distanceFactor.z ? 0 : 2)
- : (distanceFactor.y < distanceFactor.z ? 1 : 2);
- // Move to that voxel
- voxelCoords[smallestComponentIndex] += rayOrientation[smallestComponentIndex];
- // Advance the ray the distance to the closest voxel in all dimensions
- withinVoxelCoords += ray * distanceFactor[smallestComponentIndex];
- // The axis towards the next voxel will now have value -1 or 1. Next line resets it to 0.
- // It can be imagined as going jumping the side of the previus voxel to the side of the next one.
- withinVoxelCoords[smallestComponentIndex] = 1 - rayPositivity[smallestComponentIndex];
- blockID = imageLoad(worldImage, voxelCoords).r;
- } while (blockID == 0);
- switch (blockID) {
- case 1:
- {
- vec3 normal = vec3(0, 0, 0);
- normal[smallestComponentIndex] = rayOrientation[smallestComponentIndex];
- imageStore(resultImage, ivec2(gl_GlobalInvocationID.xy), vec4(0.0, 1.0 * dot(ray, normal), 0.0, 1.0));
- break;
- }
- case 2:
- {
- vec3 normal = vec3(0, 0, 0);
- normal[smallestComponentIndex] = rayOrientation[smallestComponentIndex];
- imageStore(resultImage, ivec2(gl_GlobalInvocationID.xy), vec4(1.0 * dot(ray, normal), 0.0, 0.0, 1.0));
- break;
- }
- default:
- imageStore(resultImage, ivec2(gl_GlobalInvocationID.xy), vec4(0.0, 0.0, 1.0, 1.0));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement