Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- float3 rayOrigin = float3(0, 0, 0);
- float3 rayPosCamSpace = float3(pixelCamX, pixelCamY, -1.0f);
- // (Converted with inverse view matrix)
- // Ray position on the near-plane
- float3 rayPosWorld = mul(ubo.CTWMatrix, float4(rayPosCamSpace, 1)).xyz;
- // The position of the ray (on the camera's axis)
- float3 rayOrigWorld = mul(ubo.CTWMatrix, float4(rayOrigin, 1)).xyz;
- float3 rayDir = normalize(rayPosWorld - rayOrigWorld);
- // DDA ray tracing
- // The distance to the next boundary
- float3 deltaDist = float3(
- sqrt(voxelSize * voxelSize + (rayDir.y * rayDir.y + rayDir.z * rayDir.z) / (rayDir.x * rayDir.x)),
- sqrt(voxelSize * voxelSize + (rayDir.x * rayDir.x + rayDir.z * rayDir.z) / (rayDir.y * rayDir.y)),
- sqrt(voxelSize * voxelSize + (rayDir.x * rayDir.x + rayDir.y * rayDir.y) / (rayDir.z * rayDir.z))
- );
- // This is basically "floored", but till voxelSize fraction.
- float3 voxelMap = float3(
- floor(rayPosWorld.x) + (floor(rayPosWorld.x) + voxelSize <= rayPosWorld.x ? voxelSize : 0),
- floor(rayPosWorld.y) + (floor(rayPosWorld.y) + voxelSize <= rayPosWorld.y ? voxelSize : 0),
- floor(rayPosWorld.z) + (floor(rayPosWorld.z) + voxelSize <= rayPosWorld.z ? voxelSize : 0)
- );
- // Step size.
- float3 voxelStep = sign(rayDir) * voxelSize;
- // The initial distance to a boundary
- float3 sideDist = float3(
- (rayDir.x < 0 ? (rayPosWorld.x - voxelMap.x) : (voxelSize - (rayPosWorld.x - voxelMap.x))) * deltaDist.x,
- (rayDir.y < 0 ? (rayPosWorld.y - voxelMap.y) : (voxelSize - (rayPosWorld.y - voxelMap.y))) * deltaDist.y,
- (rayDir.z < 0 ? (rayPosWorld.z - voxelMap.z) : (voxelSize - (rayPosWorld.z - voxelMap.z))) * deltaDist.z
- );
- float maxDistance = 50.f;
- float currentDistance = 0;
- while (currentDistance < maxDistance)
- {
- if (sideDist.x < sideDist.y)
- {
- if (sideDist.x < sideDist.z)
- {
- voxelMap.x += voxelStep.x;
- currentDistance = sideDist.x;
- sideDist.x += deltaDist.x;
- }
- else
- {
- voxelMap.z += voxelStep.z;
- currentDistance = sideDist.z;
- sideDist.z += deltaDist.z;
- }
- }
- else
- {
- if (sideDist.y < sideDist.z)
- {
- voxelMap.y += voxelStep.y;
- currentDistance = sideDist.y;
- sideDist.y += deltaDist.y;
- }
- else
- {
- voxelMap.z += voxelStep.z;
- currentDistance = sideDist.z;
- sideDist.z += deltaDist.z;
- }
- }
- 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