Guest User

Untitled

a guest
Nov 14th, 2025
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. float3 rayOrigin = float3(0, 0, 0);
  2. float3 rayPosCamSpace = float3(pixelCamX, pixelCamY, -1.0f);
  3.  
  4. // (Converted with inverse view matrix)
  5. // Ray position on the near-plane
  6. float3 rayPosWorld = mul(ubo.CTWMatrix, float4(rayPosCamSpace, 1)).xyz;
  7.  
  8. // The position of the ray (on the camera's axis)
  9. float3 rayOrigWorld = mul(ubo.CTWMatrix, float4(rayOrigin, 1)).xyz;
  10.  
  11. float3 rayDir = normalize(rayPosWorld - rayOrigWorld);
  12.  
  13. // DDA ray tracing
  14.  
  15. // The distance to the next boundary
  16. float3 deltaDist = float3(
  17. sqrt(voxelSize * voxelSize + (rayDir.y * rayDir.y + rayDir.z * rayDir.z) / (rayDir.x * rayDir.x)),
  18. sqrt(voxelSize * voxelSize + (rayDir.x * rayDir.x + rayDir.z * rayDir.z) / (rayDir.y * rayDir.y)),
  19. sqrt(voxelSize * voxelSize + (rayDir.x * rayDir.x + rayDir.y * rayDir.y) / (rayDir.z * rayDir.z))
  20. );
  21.  
  22. // This is basically "floored", but till voxelSize fraction.
  23. float3 voxelMap = float3(
  24. floor(rayPosWorld.x) + (floor(rayPosWorld.x) + voxelSize <= rayPosWorld.x ? voxelSize : 0),
  25. floor(rayPosWorld.y) + (floor(rayPosWorld.y) + voxelSize <= rayPosWorld.y ? voxelSize : 0),
  26. floor(rayPosWorld.z) + (floor(rayPosWorld.z) + voxelSize <= rayPosWorld.z ? voxelSize : 0)
  27. );
  28.  
  29. // Step size.
  30. float3 voxelStep = sign(rayDir) * voxelSize;
  31.  
  32. // The initial distance to a boundary
  33. float3 sideDist = float3(
  34. (rayDir.x < 0 ? (rayPosWorld.x - voxelMap.x) : (voxelSize - (rayPosWorld.x - voxelMap.x))) * deltaDist.x,
  35. (rayDir.y < 0 ? (rayPosWorld.y - voxelMap.y) : (voxelSize - (rayPosWorld.y - voxelMap.y))) * deltaDist.y,
  36. (rayDir.z < 0 ? (rayPosWorld.z - voxelMap.z) : (voxelSize - (rayPosWorld.z - voxelMap.z))) * deltaDist.z
  37. );
  38.  
  39. float maxDistance = 50.f;
  40. float currentDistance = 0;
  41. while (currentDistance < maxDistance)
  42. {
  43. if (sideDist.x < sideDist.y)
  44. {
  45. if (sideDist.x < sideDist.z)
  46. {
  47. voxelMap.x += voxelStep.x;
  48. currentDistance = sideDist.x;
  49. sideDist.x += deltaDist.x;
  50. }
  51. else
  52. {
  53. voxelMap.z += voxelStep.z;
  54. currentDistance = sideDist.z;
  55. sideDist.z += deltaDist.z;
  56. }
  57. }
  58. else
  59. {
  60. if (sideDist.y < sideDist.z)
  61. {
  62. voxelMap.y += voxelStep.y;
  63. currentDistance = sideDist.y;
  64. sideDist.y += deltaDist.y;
  65. }
  66. else
  67. {
  68. voxelMap.z += voxelStep.z;
  69. currentDistance = sideDist.z;
  70. sideDist.z += deltaDist.z;
  71. }
  72. }
  73.  
  74. const int3 voxelConvMap = int3(voxelMap / voxelSize); // Convert to voxel index so that I could access them
Advertisement
Add Comment
Please, Sign In to add comment