-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Distance-based Gaussian weighting for Ambient Occlusion #12316
Open
jjhembd
wants to merge
18
commits into
main
Choose a base branch
from
ao-sampling-disc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
95b5c65
Gaussian-weighted ambient occlusion with distance-dependent variance
025da98
Merge tag 'pre-prettier-v3' into ao-sampling-disc
0a809f6
Merge tag 'post-prettier-v3' into ao-sampling-disc
d2e0b48
Merge branch 'main' into ao-sampling-disc
288bfc9
Normalize ambient occlusion by accumulated window weights
717cadc
Merge branch 'main' into ao-sampling-disc
68b2137
Minor cleanup
3939f57
Add stepCount and directionCount uniforms to AmbientOcclusion
8a84e30
Fix AmbientOcclusion stepCount scaling, update WebGL2 loops
3b9eed6
Compute AmbientOcclusion step length from window and sample count
8da2b06
Compute Ambient Occlusion weight normalization analytically
58d7e08
Fix spec for ambient occlusion uniforms
321a42f
Fix far plane artifact, clean up uniforms for Ambient Occlusion
bfc1570
Merge branch 'main' into ao-sampling-disc
7f75e9b
Remove blur stage from Ambient Occlusion
e96f13e
Merge branch 'main' into ao-sampling-disc
02d7d5d
Update docs and CHANGES.md
45ccf4d
Merge branch 'main' into ao-sampling-disc
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -7,6 +7,8 @@ uniform float bias; | |||
uniform float lengthCap; | ||||
uniform float stepSize; | ||||
uniform float frustumLength; | ||||
uniform int stepCount; | ||||
uniform int directionCount; | ||||
|
||||
vec3 pixelToEye(vec2 screenCoordinate) | ||||
{ | ||||
|
@@ -40,6 +42,13 @@ vec3 getNormalXEdge(vec3 positionEC) | |||
return normalize(cross(dx, dy)); | ||||
} | ||||
|
||||
const float sqrtTwoPi = sqrt(czm_twoPi); | ||||
|
||||
float gaussian(float x, float standardDeviation) { | ||||
float argument = x / standardDeviation; | ||||
return exp(-0.5 * argument * argument) / (sqrtTwoPi * standardDeviation); | ||||
} | ||||
|
||||
void main(void) | ||||
{ | ||||
vec3 positionEC = pixelToEye(gl_FragCoord.xy); | ||||
|
@@ -51,11 +60,11 @@ void main(void) | |||
} | ||||
|
||||
vec3 normalEC = getNormalXEdge(positionEC); | ||||
float gaussianVariance = lengthCap * sqrt(-positionEC.z); | ||||
// TODO: mix of units. Steps are in pixels; variance is in meters. | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does |
||||
//float stepLength = max(1.0, 3.0 * gaussianVariance / (float(stepCount) + 1.0)); | ||||
|
||||
float ao = 0.0; | ||||
|
||||
const int ANGLE_STEPS = 4; | ||||
float angleStepScale = 1.0 / float(ANGLE_STEPS); | ||||
float angleStepScale = 1.0 / float(directionCount); | ||||
float angleStep = angleStepScale * czm_twoPi; | ||||
float cosStep = cos(angleStep); | ||||
float sinStep = sin(angleStep); | ||||
|
@@ -67,16 +76,35 @@ void main(void) | |||
float randomVal = texture(randomTexture, randomTexCoord).x; | ||||
vec2 sampleDirection = vec2(cos(angleStep * randomVal), sin(angleStep * randomVal)); | ||||
|
||||
float ao = 0.0; | ||||
// Loop over sampling directions | ||||
for (int i = 0; i < ANGLE_STEPS; i++) | ||||
#if __VERSION__ == 300 | ||||
for (int i = 0; i < directionCount; i++) | ||||
{ | ||||
#else | ||||
for (int i = 0; i < 64; i++) | ||||
{ | ||||
if (i >= directionCount) { | ||||
break; | ||||
} | ||||
#endif | ||||
sampleDirection = rotateStep * sampleDirection; | ||||
|
||||
float localAO = 0.0; | ||||
float accumulatedWindowWeights = 0.0; | ||||
//vec2 radialStep = stepLength * sampleDirection; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
vec2 radialStep = stepSize * sampleDirection; | ||||
|
||||
for (int j = 0; j < 6; j++) | ||||
#if __VERSION__ == 300 | ||||
for (int j = 0; j < stepCount; j++) | ||||
{ | ||||
#else | ||||
for (int j = 0; j < 128; j++) | ||||
{ | ||||
if (j >= stepCount) { | ||||
break; | ||||
} | ||||
#endif | ||||
// Step along sampling direction, away from output pixel | ||||
vec2 newCoords = floor(gl_FragCoord.xy + float(j + 1) * radialStep) + vec2(0.5); | ||||
|
||||
|
@@ -86,26 +114,28 @@ void main(void) | |||
break; | ||||
} | ||||
|
||||
// Compute step vector from output point to sampled point | ||||
vec3 stepPositionEC = pixelToEye(newCoords); | ||||
vec3 stepVector = stepPositionEC - positionEC; | ||||
float stepLength = length(stepVector); | ||||
|
||||
if (stepLength > lengthCap) | ||||
{ | ||||
break; | ||||
} | ||||
|
||||
// Estimate the angle from the surface normal. | ||||
float dotVal = clamp(dot(normalEC, normalize(stepVector)), 0.0, 1.0); | ||||
if (dotVal < bias) | ||||
{ | ||||
dotVal = 0.0; | ||||
} | ||||
|
||||
float weight = stepLength / lengthCap; | ||||
weight = 1.0 - weight * weight; | ||||
localAO = max(localAO, dotVal * weight); | ||||
// Weight contribution based on the distance from the output point | ||||
float sampleDistance = length(stepVector); | ||||
float weight = gaussian(sampleDistance, gaussianVariance); | ||||
localAO += weight * dotVal; | ||||
|
||||
// Compute lateral distance from output point, for weight normalization | ||||
// TODO: This is slow! Better to analytically compute window scales | ||||
float lateralDistance = length(stepPositionEC.xy - positionEC.xy); | ||||
accumulatedWindowWeights += gaussian(lateralDistance, gaussianVariance); | ||||
} | ||||
ao += localAO; | ||||
ao += localAO / accumulatedWindowWeights; | ||||
} | ||||
|
||||
ao *= angleStepScale; | ||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method of setting default values is not equivalent to the previous
defaultValue
calls when the value isnull
. I just want to make sure that's intended