Skip to content

Commit def0e0a

Browse files
committed
Some improvements from Three.JS port
* Incorporating improvements from the Three.JS devs here: mrdoob/three.js#32888 * Use Poisson disk sampling for the depth-aware blend * Replace blue noise texture with a computed noise implementation
1 parent 3ee2b1a commit def0e0a

File tree

4 files changed

+22
-41
lines changed

4 files changed

+22
-41
lines changed

src/bluenoise.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/compositor.frag

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,28 @@ void main() {
4141
float rawDepth = texture2D(sceneDepth, vUv).x;
4242
float correctDepth = linearize_depth(rawDepth, near, far);
4343

44+
const vec2 poissonDisk[8] = vec2[8](
45+
vec2( 0.493393, 0.394269),
46+
vec2( 0.798547, 0.885922),
47+
vec2( 0.259143, 0.650754),
48+
vec2( 0.605322, 0.023588),
49+
vec2(-0.574681, 0.137452),
50+
vec2(-0.430397, -0.638423),
51+
vec2(-0.849487, -0.366258),
52+
vec2( 0.170621, -0.569941)
53+
);
54+
4455
vec2 pushDir = vec2(0.0);
4556
float count = 0.0;
46-
for (float x = -edgeRadius; x <= edgeRadius; x++) {
47-
for (float y = -edgeRadius; y <= edgeRadius; y++) {
48-
vec2 sampleUv = (vUv * resolution + vec2(x, y)) / resolution;
49-
float sampleDepth = texelFetch(sceneDepth, ivec2(sampleUv * resolution), 0).x;
50-
sampleDepth = linearize_depth(sampleDepth, near, far);
51-
if (abs(sampleDepth - correctDepth) < 0.05 * correctDepth) {
52-
pushDir += vec2(x, y);
53-
count += 1.0;
54-
}
57+
vec2 pixelStep = 1.0 / resolution;
58+
for (int i = 0; i < 8; i++) {
59+
vec2 offset = poissonDisk[i] * edgeRadius;
60+
vec2 sampleUv = vUv + offset * pixelStep;
61+
float sampleDepth = texture2D(sceneDepth, sampleUv).x;
62+
sampleDepth = linearize_depth(sampleDepth, near, far);
63+
if (abs(sampleDepth - correctDepth) < 0.05 * correctDepth) {
64+
pushDir += offset;
65+
count += 1.0;
5566
}
5667
}
5768

src/godrays.frag

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
varying vec2 vUv;
99

1010
uniform sampler2D sceneDepth;
11-
uniform sampler2D blueNoise;
1211
uniform vec3 lightPos;
1312
uniform vec3 cameraPos;
1413
uniform vec2 resolution;
@@ -19,7 +18,6 @@ uniform samplerCube shadowMap;
1918
#else
2019
uniform sampler2D shadowMap;
2120
#endif
22-
uniform vec2 noiseResolution;
2321
uniform float texelSizeY;
2422
uniform float lightCameraNear;
2523
uniform float lightCameraFar;
@@ -232,8 +230,8 @@ void main() {
232230
}
233231
float illum = 0.0;
234232

235-
vec4 blueNoiseSample = texture2D(blueNoise, vUv * (resolution / noiseResolution));
236-
float samplesFloat = round(raymarchSteps + ((raymarchSteps / 8.) + 2.) * blueNoiseSample.x);
233+
float noise = fract(52.9829189 * fract(0.06711056 * gl_FragCoord.x + 0.00583715 * gl_FragCoord.y));
234+
float samplesFloat = round(raymarchSteps + ((raymarchSteps / 8.) + 2.) * noise);
237235
int samples = int(samplesFloat);
238236
for (int i = 0; i < samples; i++) {
239237
vec3 samplePos = mix(startPos, worldPos, float(i) / samplesFloat);

src/illumPass.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Pass, type Resizable } from 'postprocessing';
22
import * as THREE from 'three';
33

4-
import { getBlueNoiseTexture } from './bluenoise';
54
import GodraysFragmentShader from './godrays.frag';
65
import GodraysVertexShader from './godrays.vert';
76
import type { GodraysPassParams } from './index';
@@ -33,8 +32,6 @@ class GodraysMaterial extends THREE.ShaderMaterial {
3332
lightCameraFar: { value: 1000 },
3433
near: { value: 0.1 },
3534
far: { value: 1000.0 },
36-
blueNoise: { value: null as THREE.Texture | null },
37-
noiseResolution: { value: new THREE.Vector2(1, 1) },
3835
fNormals: { value: DIRECTIONS.map(() => new THREE.Vector3()) },
3936
fConstants: { value: DIRECTIONS.map(() => 0) },
4037
raymarchSteps: { value: 60 },
@@ -59,13 +56,6 @@ class GodraysMaterial extends THREE.ShaderMaterial {
5956
defines: defines as any,
6057
});
6158

62-
getBlueNoiseTexture().then(blueNoiseTexture => {
63-
uniforms.blueNoise.value = blueNoiseTexture;
64-
uniforms.noiseResolution.value.set(
65-
blueNoiseTexture.image.width,
66-
blueNoiseTexture.image.height
67-
);
68-
});
6959
}
7060
}
7161

0 commit comments

Comments
 (0)