Skip to content

Commit

Permalink
Fix constant step voxel rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeshurun Hembd committed Nov 22, 2023
1 parent e8f3734 commit 7178d35
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions packages/engine/Source/Shaders/Voxels/VoxelFS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ vec4 getNextRayPosition(inout Ray viewRay, in SampleData sampleData, in RayShape
#if defined(CONSTANT_STEP)
// Shrink the step size for points closer to the camera
// float dt = min(lodStep, lodStep * max(0.04, 4.0 * currentT));
return vec4(viewRay.dir, currentT + lodStep);
viewRay.pos += lodStep * viewRay.dir;
return vec4(viewRay.dir, lodStep);
#else
#if defined(SHAPE_BOX)
VoxelBounds voxel = constructVoxelBounds(sampleData.tileCoords, sampleData.tileUv);
Expand Down Expand Up @@ -84,6 +85,13 @@ void main()
SampleData sampleDatas[SAMPLE_COUNT];
traverseOctreeFromBeginning(positionUvShapeSpace, traversalData, sampleDatas);

#if defined(JITTER)
float noise = hash(screenCoord); // [0,1]
float lodStep = u_stepSize / pow(2.0, float(sampleDatas[0].tileCoords.w));
currT += noise * lodStep;
viewRayUv.pos += noise * lodStep * viewDirUv;
#endif

FragmentInput fragmentInput;
#if defined(STATISTICS)
setStatistics(fragmentInput.metadata.statistics);
Expand Down Expand Up @@ -125,8 +133,12 @@ void main()
}

// Keep raymarching
cellIntersection = getNextRayPosition(viewRayUv, sampleDatas[0], shapeIntersection, currT);
cellIntersection = getNextRayPosition(viewRayUv, sampleDatas[0], shapeIntersection, currT);
currT += cellIntersection.w;
#if defined(CONSTANT_STEP)
// Convert to accumulation of dT. Adding small dPositions accumulates direction errors
viewRayUv.pos = viewPosUv + currT * viewDirUv;
#endif

// Check if there's more intersections.
if (currT >= shapeIntersection.exit.w) {
Expand All @@ -144,6 +156,11 @@ void main()
cellIntersection.w = dt;
dPosition = (currT + 0.01 * u_stepSize) * viewDirUv;
viewRayUv.pos = viewPosUv + dPosition;
#if defined(JITTER)
lodStep = u_stepSize / pow(2.0, float(sampleDatas[0].tileCoords.w));
currT += noise * lodStep;
viewRayUv.pos += noise * lodStep * viewDirUv;
#endif
}
#endif
}
Expand Down

0 comments on commit 7178d35

Please sign in to comment.