Skip to content

Commit ebe6f10

Browse files
authored
Examples: Clean up (#27318)
* GTAOShader: Clean up. * Example: Clean up.
1 parent 5538dd1 commit ebe6f10

File tree

2 files changed

+161
-162
lines changed

2 files changed

+161
-162
lines changed

examples/jsm/shaders/GTAOShader.js

+40-40
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ import {
1414
*
1515
* - other AO algorithms that are not implemented here:
1616
* - Screen Space Ambient Occlusion (SSAO), see also SSAOShader.js
17-
* - http://john-chapman-graphics.blogspot.com/2013/01/ssao-tutorial.html
18-
* - https://learnopengl.com/Advanced-Lighting/SSAO
19-
* - https://creativecoding.soe.ucsc.edu/courses/cmpm164/_schedule/AmbientOcclusion.pdf
20-
* - https://drive.google.com/file/d/1SyagcEVplIm2KkRD3WQYSO9O0Iyi1hfy/edit
17+
* - http://john-chapman-graphics.blogspot.com/2013/01/ssao-tutorial.html
18+
* - https://learnopengl.com/Advanced-Lighting/SSAO
19+
* - https://creativecoding.soe.ucsc.edu/courses/cmpm164/_schedule/AmbientOcclusion.pdf
20+
* - https://drive.google.com/file/d/1SyagcEVplIm2KkRD3WQYSO9O0Iyi1hfy/edit
2121
* - Scalable Ambient Occlusion (SAO), see also SAOShader.js
22-
* - https://casual-effects.com/research/McGuire2012SAO/index.html
22+
* - https://casual-effects.com/research/McGuire2012SAO/index.html
2323
* - https://research.nvidia.com/sites/default/files/pubs/2012-06_Scalable-Ambient-Obscurance/McGuire12SAO.pdf
2424
* - N8HO
25-
* - https://github.com/N8python/n8ao
25+
* - https://github.com/N8python/n8ao
2626
* - Horizon Based Ambient Occlusion (HBAO)
27-
* - http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.577.2286&rep=rep1&type=pdf
28-
* - https://www.derschmale.com/2013/12/20/an-alternative-implementation-for-hbao-2/
27+
* - http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.577.2286&rep=rep1&type=pdf
28+
* - https://www.derschmale.com/2013/12/20/an-alternative-implementation-for-hbao-2/
2929
*
3030
* - further reading
3131
* - https://ceur-ws.org/Vol-3027/paper5.pdf
@@ -66,7 +66,7 @@ const GTAOShader = {
6666
bias: { value: 0.001 },
6767
scale: { value: 1. },
6868
sceneBoxMin: { value: new Vector3( - 1, - 1, - 1 ) },
69-
sceneBoxMax: { value: new Vector3( 1, 1, 1 ) },
69+
sceneBoxMax: { value: new Vector3( 1, 1, 1 ) },
7070
},
7171

7272
vertexShader: /* glsl */`
@@ -130,27 +130,25 @@ const GTAOShader = {
130130
}
131131
132132
vec3 computeNormalFromDepth(const vec2 uv) {
133-
vec2 size = vec2(textureSize(tDepth, 0));
134-
ivec2 p = ivec2(uv * size);
135-
float c0 = fetchDepth(p);
136-
float l2 = fetchDepth(p - ivec2(2, 0));
137-
float l1 = fetchDepth(p - ivec2(1, 0));
138-
float r1 = fetchDepth(p + ivec2(1, 0));
139-
float r2 = fetchDepth(p + ivec2(2, 0));
140-
float b2 = fetchDepth(p - ivec2(0, 2));
141-
float b1 = fetchDepth(p - ivec2(0, 1));
142-
float t1 = fetchDepth(p + ivec2(0, 1));
143-
float t2 = fetchDepth(p + ivec2(0, 2));
144-
float dl = abs((2.0 * l1 - l2) - c0);
145-
float dr = abs((2.0 * r1 - r2) - c0);
146-
float db = abs((2.0 * b1 - b2) - c0);
147-
float dt = abs((2.0 * t1 - t2) - c0);
148-
vec3 ce = getViewPosition(uv, c0).xyz;
149-
vec3 dpdx = (dl < dr) ? ce - getViewPosition((uv - vec2(1.0 / size.x, 0.0)), l1).xyz
150-
: -ce + getViewPosition((uv + vec2(1.0 / size.x, 0.0)), r1).xyz;
151-
vec3 dpdy = (db < dt) ? ce - getViewPosition((uv - vec2(0.0, 1.0 / size.y)), b1).xyz
152-
: -ce + getViewPosition((uv + vec2(0.0, 1.0 / size.y)), t1).xyz;
153-
return normalize(cross(dpdx, dpdy));
133+
vec2 size = vec2(textureSize(tDepth, 0));
134+
ivec2 p = ivec2(uv * size);
135+
float c0 = fetchDepth(p);
136+
float l2 = fetchDepth(p - ivec2(2, 0));
137+
float l1 = fetchDepth(p - ivec2(1, 0));
138+
float r1 = fetchDepth(p + ivec2(1, 0));
139+
float r2 = fetchDepth(p + ivec2(2, 0));
140+
float b2 = fetchDepth(p - ivec2(0, 2));
141+
float b1 = fetchDepth(p - ivec2(0, 1));
142+
float t1 = fetchDepth(p + ivec2(0, 1));
143+
float t2 = fetchDepth(p + ivec2(0, 2));
144+
float dl = abs((2.0 * l1 - l2) - c0);
145+
float dr = abs((2.0 * r1 - r2) - c0);
146+
float db = abs((2.0 * b1 - b2) - c0);
147+
float dt = abs((2.0 * t1 - t2) - c0);
148+
vec3 ce = getViewPosition(uv, c0).xyz;
149+
vec3 dpdx = (dl < dr) ? ce - getViewPosition((uv - vec2(1.0 / size.x, 0.0)), l1).xyz : -ce + getViewPosition((uv + vec2(1.0 / size.x, 0.0)), r1).xyz;
150+
vec3 dpdy = (db < dt) ? ce - getViewPosition((uv - vec2(0.0, 1.0 / size.y)), b1).xyz : -ce + getViewPosition((uv + vec2(0.0, 1.0 / size.y)), t1).xyz;
151+
return normalize(cross(dpdx, dpdy));
154152
}
155153
156154
vec3 getViewNormal(const vec2 uv) {
@@ -182,14 +180,14 @@ const GTAOShader = {
182180
float radiusToUse = radius;
183181
float distanceFalloffToUse = thickness;
184182
#if SCREEN_SPACE_RADIUS == 1
185-
float radiusScale = getViewPosition(vec2(0.5 + float(SCREEN_SPACE_RADIUS_SCALE) / resolution.x, 0.0), depth).x;
183+
float radiusScale = getViewPosition(vec2(0.5 + float(SCREEN_SPACE_RADIUS_SCALE) / resolution.x, 0.0), depth).x;
186184
radiusToUse *= radiusScale;
187185
distanceFalloffToUse *= radiusScale;
188186
#endif
189187
190188
#if SCENE_CLIP_BOX == 1
191189
vec3 worldPos = (cameraWorldMatrix * vec4(viewPos, 1.0)).xyz;
192-
float boxDistance = length(max(vec3(0.0), max(sceneBoxMin - worldPos, worldPos - sceneBoxMax)));
190+
float boxDistance = length(max(vec3(0.0), max(sceneBoxMin - worldPos, worldPos - sceneBoxMax)));
193191
if (boxDistance > radiusToUse) {
194192
discard;
195193
return;
@@ -345,6 +343,7 @@ function generateMagicSquareNoise( size = 5 ) {
345343
const magicSquare = generateMagicSquare( noiseSize );
346344
const noiseSquareSize = magicSquare.length;
347345
const data = new Uint8Array( noiseSquareSize * 4 );
346+
348347
for ( let inx = 0; inx < noiseSquareSize; ++ inx ) {
349348

350349
const iAng = magicSquare[ inx ];
@@ -365,21 +364,22 @@ function generateMagicSquareNoise( size = 5 ) {
365364
noiseTexture.wrapS = RepeatWrapping;
366365
noiseTexture.wrapT = RepeatWrapping;
367366
noiseTexture.needsUpdate = true;
367+
368368
return noiseTexture;
369369

370370
}
371371

372372
function generateMagicSquare( size ) {
373373

374-
const noiseSize =
375-
Math.floor( size ) % 2 === 0 ? Math.floor( size ) + 1 : Math.floor( size );
374+
const noiseSize = Math.floor( size ) % 2 === 0 ? Math.floor( size ) + 1 : Math.floor( size );
376375
const noiseSquareSize = noiseSize * noiseSize;
377376
const magicSquare = Array( noiseSquareSize ).fill( 0 );
378377
let i = Math.floor( noiseSize / 2 );
379378
let j = noiseSize - 1;
379+
380380
for ( let num = 1; num <= noiseSquareSize; ) {
381381

382-
if ( i === - 1 && j === noiseSize ) {
382+
if ( i === - 1 && j === noiseSize ) {
383383

384384
j = noiseSize - 2;
385385
i = 0;
@@ -388,19 +388,19 @@ function generateMagicSquare( size ) {
388388

389389
if ( j === noiseSize ) {
390390

391-
j = 0;
391+
j = 0;
392392

393393
}
394394

395395
if ( i < 0 ) {
396396

397-
i = noiseSize - 1;
397+
i = noiseSize - 1;
398398

399399
}
400400

401401
}
402402

403-
if ( magicSquare[ i * noiseSize + j ] !== 0 ) {
403+
if ( magicSquare[ i * noiseSize + j ] !== 0 ) {
404404

405405
j -= 2;
406406
i ++;
@@ -412,8 +412,8 @@ function generateMagicSquare( size ) {
412412

413413
}
414414

415-
j ++;
416-
i --;
415+
j ++;
416+
i --;
417417

418418
}
419419

0 commit comments

Comments
 (0)