Skip to content

Commit 689f106

Browse files
committed
Make texelSize uniform instead of varying, remove max_varyings check
1 parent 208f28c commit 689f106

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

src/rd-renderer.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,6 @@ export class ReactionDiffusionRenderer {
125125
if (this.renderer.capabilities.maxVertexTextures === 0) {
126126
throw new Error("System does not support vertex shader textures!");
127127
}
128-
if (this.renderer.capabilities.maxVaryings < 5) {
129-
throw new Error("System does not support the number of varying vectors (>= 5) needed to function!");
130-
}
131128

132129
// Detect color_buffer_float support
133130
// if (this.renderer.capabilities.isWebGL2 && !this.renderer.extensions.get("EXT_color_buffer_float")) {
@@ -240,12 +237,17 @@ export class ReactionDiffusionRenderer {
240237
this.computeRenderTargets.push(newTarget);
241238
}
242239

243-
this.displayMaterialUniforms.resolution.value = new THREE.Vector2(width * this.internalResolutionMultiplier, height * this.internalResolutionMultiplier);
240+
// Determine actual resolution
241+
let realResolution = new THREE.Vector2(width * this.internalResolutionMultiplier, height * this.internalResolutionMultiplier);
242+
// Determine texel size (size of a pixel when resolution is normalized between 0 and 1)
243+
let texelSize = new THREE.Vector2(1.0 / realResolution.width, 1.0 / realResolution.height);
244+
245+
this.displayMaterialUniforms.resolution.value = realResolution;
244246
console.log(`Display texture sized to (${this.displayMaterialUniforms.resolution.value.x}, ${this.displayMaterialUniforms.resolution.value.y})`);
245247

246-
this.computeUniforms.resolution.value = new THREE.Vector2(width * this.internalResolutionMultiplier, height * this.internalResolutionMultiplier);
248+
this.computeUniforms.resolution.value = realResolution;
249+
this.computeUniforms.texelSize.value = texelSize;
247250
//console.log(`Compute texture sized to (${this.computeUniforms.resolution.value.x}, ${this.computeUniforms.resolution.value.y})`);
248-
249251
}
250252

251253
CreateMaterials() {
@@ -279,6 +281,10 @@ export class ReactionDiffusionRenderer {
279281
type: "v2",
280282
value: new THREE.Vector2()
281283
},
284+
texelSize: {
285+
type: "v2",
286+
value: new THREE.Vector2()
287+
},
282288
time: {
283289
type: "f",
284290
value: 1.0
@@ -473,12 +479,13 @@ export class ReactionDiffusionRenderer {
473479
for (var i = 0; i < width; i++) {
474480
for (var j = 0; j < height; j++) {
475481

476-
let nx = i / width - 0.5;
477-
let ny = j / height - 0.5;
482+
let nx = i / width;
483+
let ny = j / height;
478484

479485
let r = simplex.noise2D(frequency * nx, frequency * ny) + 1 / 2; // Normalize from [-1, 1] to [0, 1]
480486
r = Math.pow(r, 20); // Makes peaks more dramatic. See https://www.redblobgames.com/maps/terrain-from-noise/
481-
r = Math.min(r, 1); // Cap value at 1.0
487+
if (r > 1.0) r = 1; // Cap value at 1.0
488+
if (r < 0.5) r = 0; // High pass at 0.5
482489

483490
pixels[px + 1] = r;
484491

src/resources/shaders/compute-frag.glsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
//
1010

1111
varying vec2 v_uv;
12-
varying vec2 texelSize;
1312

14-
uniform sampler2D sourceTexture;
1513
uniform vec2 resolution;
14+
uniform vec2 texelSize;
15+
16+
uniform sampler2D sourceTexture;
1617
uniform float time;
1718

1819
uniform float feed; // Growth rate for B
@@ -56,7 +57,6 @@ float when_ge(float x, float y) {
5657
vec4 convolve5(vec4 centerPixel, vec3[3] kernel) {
5758
vec4 result = vec4(0.0, 0.0, 0.0, 1.0);
5859

59-
// Orthogonal texels
6060
result += texture2D( sourceTexture, v_uv + vec2( 0.0, texelSize.y ) ) * kernel[0][1];
6161
result += texture2D( sourceTexture, v_uv + vec2( 0.0, -texelSize.y ) ) * kernel[2][1];
6262
result += texture2D( sourceTexture, v_uv + vec2( texelSize.x, 0.0 ) ) * kernel[1][0];
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
varying vec2 v_uv;
2-
varying vec2 texelSize;
32

3+
uniform vec2 texelSize;
44
uniform vec2 resolution;
55

66
// Provided by Three.js:
@@ -9,11 +9,6 @@ uniform vec2 resolution;
99
// uniform mat4 modelViewMatrix;
1010

1111
void main() {
12-
texelSize = 1.0 / resolution.xy;
13-
texelSize *= 1.0; //Default 1.0. Change the multiplier for fun times
14-
1512
v_uv = uv;
16-
1713
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
18-
1914
}

0 commit comments

Comments
 (0)