Skip to content

Commit

Permalink
Added support for closed iso surfaces with ambient occlusion renderer…
Browse files Browse the repository at this point in the history
…. Fixed closed iso surface normal calculation.
  • Loading branch information
chrismile committed Sep 15, 2024
1 parent 3af227e commit 3a0f04d
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 12 deletions.
8 changes: 7 additions & 1 deletion Data/Shaders/VPT/CheckIsosurfaceHit.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#ifdef CLOSE_ISOSURFACES
if (isFirstPointFromOutside) {
// TODO: Normals
isFirstPoint = false;
lastScalarSign = sign(-parameters.isoValue);
} else
Expand Down Expand Up @@ -42,13 +41,20 @@
firstEvent.hasValue = true;
firstEvent.density = parameters.extinction.x;
firstEvent.depth = tMax - d + distance(x1, x);
#ifdef CLOSE_ISOSURFACES
firstEvent.normal = surfaceNormalGlobal;
firstEvent.isIsosurface = true;
#endif
}
if (rayBoxIntersect(parameters.boxMin, parameters.boxMax, x, w, tMin, tMax)) {
x += w * tMin;
d = tMax - tMin;
}
}
//foundHit = true;
#ifdef CLOSE_ISOSURFACES
isFirstPointFromOutside = false;
#endif
break;
}

Expand Down
18 changes: 15 additions & 3 deletions Data/Shaders/VPT/Clouds.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ void pathTraceSample(int i, bool onlyFirstEvent, out ScatterEvent firstEvent){
// Get ray direction and volume entry point
vec3 x, w;
createCameraRay(screenCoord, x, w);
firstEvent = ScatterEvent(false, x, 0.0, w, 0.0, 0.0, 0.0);
firstEvent = ScatterEvent(
false, x, 0.0, w, 0.0, 0.0, 0.0
#ifdef CLOSE_ISOSURFACES
, vec3(0.0), false
#endif
);

#if defined(USE_ISOSURFACES) || defined(USE_HEADLIGHT)
cameraPosition = x;
Expand Down Expand Up @@ -242,8 +247,15 @@ void pathTraceSample(int i, bool onlyFirstEvent, out ScatterEvent firstEvent){
//imageStore(firstX, imageCoord, vec4(firstEvent.x, firstEvent.pdf_x));

#ifdef WRITE_NORMAL_MAP
#ifdef USE_ISOSURFACES
vec3 diff = -computeGradient((firstEvent.x - parameters.boxMin)/(parameters.boxMax -parameters.boxMin));
#if defined(CLOSE_ISOSURFACES)
vec3 diff;
if (firstEvent.isIsosurface) {
diff = firstEvent.normal;
} else {
diff = -computeGradient((firstEvent.x - parameters.boxMin) / (parameters.boxMax -parameters.boxMin));
}
#elif defined(USE_ISOSURFACES)
vec3 diff = -computeGradient((firstEvent.x - parameters.boxMin) / (parameters.boxMax -parameters.boxMin));
#else // !defined(USE_ISOSURFACES)
vec3 diff = getCloudFiniteDifference(firstEvent.x);
#endif // USE_ISOSURFACES
Expand Down
18 changes: 17 additions & 1 deletion Data/Shaders/VPT/IsosurfaceRendering.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ vec3 isosurfaceRendering(vec3 x, vec3 w, inout ScatterEvent firstEvent) {
vec3 weights = vec3(1, 1, 1);
float lastScalarSign, currentScalarSign;
bool isFirstPoint = true;
#ifdef CLOSE_ISOSURFACES
bool isFirstPointFromOutside = true;
#endif

ivec3 voxelGridSize = textureSize(gridImage, 0);
vec3 boxDelta = parameters.boxMax - parameters.boxMin;
Expand All @@ -20,10 +23,17 @@ vec3 isosurfaceRendering(vec3 x, vec3 w, inout ScatterEvent firstEvent) {
float scalarValue = sampleCloudIso(xNew);

currentScalarSign = sign(scalarValue - parameters.isoValue);
#ifdef CLOSE_ISOSURFACES
if (isFirstPoint) {
isFirstPoint = false;
lastScalarSign = sign(-parameters.isoValue);
}
#else
if (isFirstPoint) {
isFirstPoint = false;
lastScalarSign = currentScalarSign;
}
#endif

if (lastScalarSign != currentScalarSign) {
if (!firstEvent.hasValue) {
Expand All @@ -41,14 +51,20 @@ vec3 isosurfaceRendering(vec3 x, vec3 w, inout ScatterEvent firstEvent) {
break;
}

isFirstPointFromOutside = false;
x = xNew;
d -= t;
}
}

if (foundHit) {
vec3 surfaceNormal;
vec3 color = getIsoSurfaceHitDirect(x, w, surfaceNormal);
vec3 color = getIsoSurfaceHitDirect(
x, w, surfaceNormal
#ifdef CLOSE_ISOSURFACES
, isFirstPointFromOutside
#endif
);
weights *= color;
x += surfaceNormal * 1e-3;

Expand Down
24 changes: 23 additions & 1 deletion Data/Shaders/VPT/VptUtils.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ struct ScatterEvent {
vec3 w; float pdf_w;
float depth;
float density;
#ifdef CLOSE_ISOSURFACES
vec3 normal;
bool isIsosurface;
#endif
};


Expand Down Expand Up @@ -791,6 +795,7 @@ bool rayBoxIntersect(vec3 bMin, vec3 bMax, vec3 P, vec3 D, out float tMin, out f

#ifdef CLOSE_ISOSURFACES
void rayBoxIntersectionNormal(vec3 bMin, vec3 bMax, vec3 P, vec3 D, inout vec3 surfaceNormal) {
float tMinTest, tMaxTest;
D.x = abs(D).x <= 1e-6 ? 1e-6 : D.x;
D.y = abs(D).y <= 1e-6 ? 1e-6 : D.y;
D.z = abs(D).z <= 1e-6 ? 1e-6 : D.z;
Expand Down Expand Up @@ -1132,6 +1137,9 @@ float isoSurfaceOpacity = isoSurfaceColorAll.a;
#define UNIFORM_SAMPLING
//#define USE_MIS // only for specular BRDF sampling

#ifdef CLOSE_ISOSURFACES
vec3 surfaceNormalGlobal;
#endif

bool getIsoSurfaceHit(
vec3 currentPoint, inout vec3 w, inout vec3 throughput
Expand Down Expand Up @@ -1170,6 +1178,10 @@ bool getIsoSurfaceHit(
surfaceNormal = -surfaceNormal;
}

#ifdef CLOSE_ISOSURFACES
surfaceNormalGlobal = surfaceNormal;
#endif

vec3 surfaceTangent;
vec3 surfaceBitangent;
ComputeDefaultBasis(surfaceNormal, surfaceTangent, surfaceBitangent);
Expand Down Expand Up @@ -1316,10 +1328,20 @@ bool getIsoSurfaceHit(
#include "Lighting.glsl"

// Direct illumination
vec3 getIsoSurfaceHitDirect(vec3 currentPoint, vec3 w, inout vec3 surfaceNormal) {
vec3 getIsoSurfaceHitDirect(
vec3 currentPoint, vec3 w, inout vec3 surfaceNormal
#ifdef CLOSE_ISOSURFACES
, bool isFirstPointFromOutside
#endif
) {
vec3 texCoords = (currentPoint - parameters.boxMin) / (parameters.boxMax - parameters.boxMin);
texCoords = texCoords * (parameters.gridMax - parameters.gridMin) + parameters.gridMin;
surfaceNormal = computeGradient(texCoords); // computeGradientLegacy(texCoords);
#ifdef CLOSE_ISOSURFACES
if (isFirstPointFromOutside) {
rayBoxIntersectionNormal(parameters.boxMin, parameters.boxMax, cameraPosition, w, surfaceNormal);
} else
#endif
if (dot(cameraPosition - currentPoint, surfaceNormal) < 0.0) {
surfaceNormal = -surfaceNormal;
}
Expand Down
11 changes: 6 additions & 5 deletions src/PathTracer/VolumetricPathTracingPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2008,6 +2008,10 @@ void VolumetricPathTracingPass::_render() {
uniformData.maxAoDist = maxAoDist;
uniformData.numAoSamples = numAoSamples;

uniformData.useClipPlane = int32_t(useClipPlane);
uniformData.clipPlaneNormal = glm::normalize(clipPlaneNormal);
uniformData.clipPlaneDistance = clipPlaneDistance;

// Update BRDF parameters
uniformData.subsurface = subsurface;
uniformData.metallic = metallic;
Expand All @@ -2023,9 +2027,6 @@ void VolumetricPathTracingPass::_render() {
uniformBuffer->updateData(
sizeof(UniformData), &uniformData, renderer->getVkCommandBuffer());

uniformData.clipPlaneNormal = clipPlaneNormal;
uniformData.clipPlaneDistance = clipPlaneDistance;

if (useGlobalFrameNumber) {
frameInfo.globalFrameNumber = globalFrameNumber;
} else {
Expand Down Expand Up @@ -2817,10 +2818,10 @@ bool VolumetricPathTracingPass::renderGuiPropertyEditorNodes(sgl::PropertyEditor
setShaderDirty();
optionChanged = true;
}
if (useClipPlane && propertyEditor.addSliderFloat3("Clip Plane Normal", &clipPlaneNormal.x, 0.01f, 1.0f)) {
if (useClipPlane && propertyEditor.addSliderFloat3("Clip Plane Normal", &clipPlaneNormal.x, -1.0f, 1.0f)) {
optionChanged = true;
}
if (useClipPlane && propertyEditor.addSliderFloat("Clip Plane Distance", &clipPlaneDistance, -1.0f, 1.0f)) {
if (useClipPlane && propertyEditor.addSliderFloat("Clip Plane Distance", &clipPlaneDistance, -0.5f, 0.5f)) {
optionChanged = true;
}
propertyEditor.endNode();
Expand Down
2 changes: 1 addition & 1 deletion src/PathTracer/VolumetricPathTracingPass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ class VolumetricPathTracingPass : public sgl::vk::ComputePass {
int numAoSamples = 4;

// Clip Plane
int useClipPlane;
int32_t useClipPlane;
glm::vec3 clipPlaneNormal;
float clipPlaneDistance;

Expand Down

0 comments on commit 3a0f04d

Please sign in to comment.