Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 450
  2.  
  3. layout (binding = 0, rgba8) uniform writeonly image2D resultImage;
  4. layout (binding = 1, r32ui) uniform readonly uimage3D worldImage;
  5.  
  6. layout(push_constant) uniform PushConstants {
  7.     vec3 rayOrigin;
  8.     float viewGridTopLeftCornerX;
  9.  
  10.     vec3 viewGridRight;
  11.     float viewGridTopLeftCornerY;
  12.  
  13.     vec3 viewGridDown;
  14.     float viewGridTopLeftCornerZ;
  15.  
  16.     float viewGridDensity;
  17. } push;
  18.  
  19.  
  20.  
  21. void main() {
  22.     vec3 viewGridTopLeftCorner = vec3(push.viewGridTopLeftCornerX, push.viewGridTopLeftCornerY, push.viewGridTopLeftCornerZ);
  23.     vec3 samplePoint = viewGridTopLeftCorner + push.viewGridDensity * (gl_GlobalInvocationID.x * push.viewGridRight + gl_GlobalInvocationID.y * push.viewGridDown);
  24.  
  25.     vec3 ray = normalize(samplePoint - push.rayOrigin);
  26.     vec3 inverseRay = 1 / ray;
  27.  
  28.     ivec3 voxelCoords = ivec3(floor(push.rayOrigin.x), floor(push.rayOrigin.y), floor(push.rayOrigin.z));
  29.     ivec3 rayOrientation = ivec3(sign(ray));
  30.     ivec3 rayPositivity = (1 + rayOrientation) >> 1;
  31.     vec3 withinVoxelCoords = push.rayOrigin - voxelCoords;
  32.  
  33.     uint blockID = 0;
  34.     int smallestComponentIndex;
  35.     do {
  36.         // Calculate how far each of the neighbouring voxels are
  37.         vec3 distanceFactor = (rayPositivity - withinVoxelCoords) * inverseRay;
  38.  
  39.         // Pinpoint the closest voxel of the three candidates
  40.         smallestComponentIndex = distanceFactor.x < distanceFactor.y
  41.         ? (distanceFactor.x < distanceFactor.z ? 0 : 2)
  42.         : (distanceFactor.y < distanceFactor.z ? 1 : 2);
  43.  
  44.         // Move to that voxel
  45.         voxelCoords[smallestComponentIndex] += rayOrientation[smallestComponentIndex];
  46.  
  47.         // Advance the ray the distance to the closest voxel in all dimensions
  48.         withinVoxelCoords += ray * distanceFactor[smallestComponentIndex];
  49.  
  50.         // The axis towards the next voxel will now have value -1 or 1. Next line resets it to 0.
  51.         // It can be imagined as going jumping the side of the previus voxel to the side of the next one.
  52.         withinVoxelCoords[smallestComponentIndex] = 1 - rayPositivity[smallestComponentIndex];
  53.        
  54.         blockID = imageLoad(worldImage, voxelCoords).r;
  55.     } while (blockID == 0);
  56.  
  57.     switch (blockID) {
  58.         case 1:
  59.             {
  60.             vec3 normal = vec3(0, 0, 0);
  61.             normal[smallestComponentIndex] = rayOrientation[smallestComponentIndex];
  62.             imageStore(resultImage, ivec2(gl_GlobalInvocationID.xy), vec4(0.0, 1.0 * dot(ray, normal), 0.0, 1.0));
  63.             break;
  64.             }
  65.         case 2:
  66.             {
  67.             vec3 normal = vec3(0, 0, 0);
  68.             normal[smallestComponentIndex] = rayOrientation[smallestComponentIndex];
  69.             imageStore(resultImage, ivec2(gl_GlobalInvocationID.xy), vec4(1.0 * dot(ray, normal), 0.0, 0.0, 1.0));
  70.             break;
  71.             }
  72.         default:
  73.             imageStore(resultImage, ivec2(gl_GlobalInvocationID.xy), vec4(0.0, 0.0, 1.0, 1.0));
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement