I am building a 3d renderer in C++ using Vulkan.hpp. I have implemented Spherical…
- User
I am building a 3d renderer in C++ using Vulkan.hpp. I have implemented Spherical Harmonics (SH) coefficients for diffuse radiance as part of global illumination. I am now trying to implement depth checking of the SH coefficients in order to generate a for of ambient shadows. I believe this is sometimes called probe occlusion or something similar. Right now I am implementing the whole process for a single probe to ensure it's correct before extending it to work with multiple probes. I have code to do the probe occlusion and it seems to be working somewhat okay. However, I have noticed that it isn't taking into account any form of face culling in how to determines occlusion. This means that on relatively thin objects it can cause the ambient light to bleed through to the other side of the object. Can you look at the code below and adjust it to prevent occlusion on the opposing side of objects? I expect this could potentially be done with some form of normal check compared to the probe direction. I have included the g-buffer input definition for the normal image in case this is useful for that purpose.
layout(input_attachment_index = 1, set = 0, binding = 1) uniform subpassInputMS gNormal;
// Returns ambient occlusion factor [0..1] // 1 = unoccluded, 0 = fully occluded float ComputeProbeOcclusion(vec3 fragPosWorld) {
codevec3 dir = normalize(fragPosWorld - probePosition.probePos); dir.y = -dir.y; // Distance from probe to fragment float fragDistance = length(fragPosWorld - probePosition.probePos); // Sample depth from cubemap [0,1] float sampledDepth = length(texture(SHposMap, dir).xyz - probePosition.probePos); // Convert sampled depth back to linear depth in world units - Not needed since it is already in linear units float depthFromProbe = sampledDepth; // Optional: Add bias to avoid self-shadowing artifacts const float depthBias = 0.2; // If fragment is farther than depth stored in cubemap + bias => occluded //float occlusion = fragDistance > depthFromProbe + depthBias ? 0.0 : 1.0; // Optional: smooth transition with a smoothstep or linear ramp to avoid harsh edges float epsilon = 0.5; // distance tolerance float occlusion = smoothstep(depthFromProbe + depthBias + epsilon, depthFromProbe + depthBias, fragDistance); occlusion = max(occlusion,0.1); return occlusion; - Assistant
- User
I've tried the approach you have suggested and am now generating a normal cubemap from the probe coordinates. However, I have noticed an issue when sampling this cubemap. The sampling is done based on the 0,0,0 coordinates, not the probe coordinates. This isn't an issue with the position cubemap since I can just adjust the output position by the probe coordinates. How can I fix this issue?
- Assistant
- User
- Assistant