Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update shaders
Browse files Browse the repository at this point in the history
Migrate to GLSL3 and make use of custom shader chunks
vanruesc committed Dec 17, 2023
1 parent ced7197 commit d3ffa71
Showing 28 changed files with 349 additions and 602 deletions.
5 changes: 3 additions & 2 deletions src/materials/shaders/adaptive-luminance.frag
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <pp_output_pars_fragment>
#include <pp_precision_fragment>
#include <pp_default_output_pars_fragment>
#include <packing>

#define packFloatToRGBA(v) packDepthToRGBA(v)
@@ -16,7 +17,7 @@ in vec2 vUv;
void main() {

// This 1x1 buffer contains the previous luminance.
float l0 = unpackRGBAToFloat(texture2D(luminanceBuffer0, vUv));
float l0 = unpackRGBAToFloat(texture(luminanceBuffer0, vUv));

// Get the current average scene luminance.
float l1 = textureLod(luminanceBuffer1, vUv, MIP_LEVEL_1X1).r;
53 changes: 9 additions & 44 deletions src/materials/shaders/circle-of-confusion.frag
Original file line number Diff line number Diff line change
@@ -1,45 +1,14 @@
#include <pp_precision_fragment>
#include <pp_camera_pars_fragment>
#include <pp_default_output_pars_fragment>
#include <pp_depth_buffer_pars_fragment>
#include <pp_depth_utils_pars_fragment>
#include <common>

#ifdef GL_FRAGMENT_PRECISION_HIGH

uniform highp sampler2D depthBuffer;

#else

uniform mediump sampler2D depthBuffer;

#endif

uniform float focusDistance;
uniform float focusRange;
uniform vec2 cameraParams;

varying vec2 vUv;

float readDepth(const in vec2 uv) {

#if DEPTH_PACKING == 3201

float depth = unpackRGBAToDepth(texture2D(depthBuffer, uv));

#else

float depth = texture2D(depthBuffer, uv).r;

#endif

#ifdef LOG_DEPTH

float d = pow(2.0, depth * log2(cameraFar + 1.0)) - 1.0;
float a = cameraFar / (cameraFar - cameraNear);
float b = cameraFar * cameraNear / (cameraNear - cameraFar);
depth = a + b / d;

#endif

return depth;

}
in vec2 vUv;

void main() {

@@ -48,18 +17,14 @@ void main() {
#ifdef PERSPECTIVE_CAMERA

float viewZ = perspectiveDepthToViewZ(depth, cameraParams.x, cameraParams.y);
float linearDepth = viewZToOrthographicDepth(viewZ, cameraParams.x, cameraParams.y);

#else

float linearDepth = depth;
depth = viewZToOrthographicDepth(viewZ, cameraParams.x, cameraParams.y);

#endif

float signedDistance = linearDepth - focusDistance;
float signedDistance = depth - focusDistance;
float magnitude = smoothstep(0.0, focusRange, abs(signedDistance));

gl_FragColor.rg = magnitude * vec2(
outputColor.rg = magnitude * vec2(
step(signedDistance, 0.0),
step(0.0, signedDistance)
);
40 changes: 17 additions & 23 deletions src/materials/shaders/convolution.bokeh.frag
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
#ifdef FRAMEBUFFER_PRECISION_HIGH

uniform mediump sampler2D inputBuffer;

#else

uniform lowp sampler2D inputBuffer;

#endif
#include <pp_precision_fragment>
#include <pp_default_output_pars_fragment>
#include <pp_input_buffer_pars_fragment>
#include <pp_resolution_pars_fragment>

#if PASS == 1

@@ -19,39 +14,38 @@
#endif

uniform lowp sampler2D cocBuffer;
uniform vec2 texelSize;
uniform float scale;

varying vec2 vUv;
in vec2 vUv;

void main() {

#ifdef FOREGROUND

vec2 cocNearFar = texture2D(cocBuffer, vUv).rg * scale;
vec2 cocNearFar = texture(cocBuffer, vUv).rg * scale;
float coc = cocNearFar.x;

#else

float coc = texture2D(cocBuffer, vUv).g * scale;
float coc = texture(cocBuffer, vUv).g * scale;

#endif

if(coc == 0.0) {

// Skip blurring.
gl_FragColor = texture2D(inputBuffer, vUv);
outputColor = texture(inputBuffer, vUv);

} else {

#ifdef FOREGROUND

// Use far CoC to avoid weak blurring around foreground objects.
vec2 step = texelSize * max(cocNearFar.x, cocNearFar.y);
vec2 step = resolution.zw * max(cocNearFar.x, cocNearFar.y);

#else

vec2 step = texelSize * coc;
vec2 step = resolution.zw * coc;

#endif

@@ -65,34 +59,34 @@ void main() {
vec4 kernel = kernel64[i];

vec2 uv = step * kernel.xy + vUv;
acc += texture2D(inputBuffer, uv);
acc += texture(inputBuffer, uv);

uv = step * kernel.zw + vUv;
acc += texture2D(inputBuffer, uv);
acc += texture(inputBuffer, uv);

}

gl_FragColor = acc / 64.0;
outputColor = acc / 64.0;

#else

vec4 maxValue = texture2D(inputBuffer, vUv);
vec4 maxValue = texture(inputBuffer, vUv);

// Each vector contains two sampling points (16 in total).
for(int i = 0; i < 8; ++i) {

vec4 kernel = kernel16[i];

vec2 uv = step * kernel.xy + vUv;
maxValue = max(texture2D(inputBuffer, uv), maxValue);
maxValue = max(texture(inputBuffer, uv), maxValue);

uv = step * kernel.zw + vUv;
maxValue = max(texture2D(inputBuffer, uv), maxValue);
maxValue = max(texture(inputBuffer, uv), maxValue);


}

gl_FragColor = maxValue;
outputColor = maxValue;

#endif

75 changes: 25 additions & 50 deletions src/materials/shaders/convolution.box.frag
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
#ifdef FRAMEBUFFER_PRECISION_HIGH

uniform mediump sampler2D inputBuffer;

#else

uniform lowp sampler2D inputBuffer;

#endif
#include <pp_precision_fragment>
#include <pp_default_output_pars_fragment>
#include <pp_input_buffer_pars_fragment>

#ifdef BILATERAL

uniform vec2 cameraParams;
#include <pp_camera_pars_fragment>

#ifdef NORMAL_DEPTH

@@ -24,74 +18,55 @@

#endif

#define getDepth(uv) texture2D(normalDepthBuffer, uv).a
#define getDepth(uv) texture(normalDepthBuffer, uv).a

#else

#ifdef GL_FRAGMENT_PRECISION_HIGH

uniform highp sampler2D depthBuffer;

#else

uniform mediump sampler2D depthBuffer;

#endif

#define getDepth(uv) texture2D(depthBuffer, uv).r
#include <pp_depth_buffer_pars_fragment>
#define getDepth(uv) texture(depthBuffer, uv).r

#endif

float getViewZ(const in float depth) {

#ifdef PERSPECTIVE_CAMERA

return perspectiveDepthToViewZ(depth, cameraParams.x, cameraParams.y);

#else

return orthographicDepthToViewZ(depth, cameraParams.x, cameraParams.y);

#endif

}
#include <packing>

#ifdef PERSPECTIVE_CAMERA

#define getViewZ(depth) perspectiveDepthToViewZ(depth, cameraParams.x, cameraParams.y)
#define linearDepth(uv) viewZToOrthographicDepth(getViewZ(getDepth(uv)), cameraParams.x, cameraParams.y)

#else

#define getViewZ(depth) orthographicDepthToViewZ(depth, cameraParams.x, cameraParams.y)
#define linearDepth(uv) getDepth(uv)

#endif

#endif

#define getTexel(uv) texture2D(inputBuffer, uv)
#define getTexel(uv) texture(inputBuffer, uv)

#if KERNEL_SIZE == 3

// Optimized 3x3
varying vec2 vUv00, vUv01, vUv02;
varying vec2 vUv03, vUv04, vUv05;
varying vec2 vUv06, vUv07, vUv08;
in vec2 vUv00, vUv01, vUv02;
in vec2 vUv03, vUv04, vUv05;
in vec2 vUv06, vUv07, vUv08;

#elif KERNEL_SIZE == 5 && MAX_VARYING_VECTORS >= 13

// Optimized 5x5
varying vec2 vUv00, vUv01, vUv02, vUv03, vUv04;
varying vec2 vUv05, vUv06, vUv07, vUv08, vUv09;
varying vec2 vUv10, vUv11, vUv12, vUv13, vUv14;
varying vec2 vUv15, vUv16, vUv17, vUv18, vUv19;
varying vec2 vUv20, vUv21, vUv22, vUv23, vUv24;
in vec2 vUv00, vUv01, vUv02, vUv03, vUv04;
in vec2 vUv05, vUv06, vUv07, vUv08, vUv09;
in vec2 vUv10, vUv11, vUv12, vUv13, vUv14;
in vec2 vUv15, vUv16, vUv17, vUv18, vUv19;
in vec2 vUv20, vUv21, vUv22, vUv23, vUv24;

#else

// General case
uniform vec2 texelSize;
#include <pp_resolution_pars_fragment>
uniform float scale;
varying vec2 vUv;
in vec2 vUv;

#endif

@@ -164,7 +139,7 @@ void main() {

// General case
float centerDepth = linearDepth(vUv);
vec2 s = texelSize * scale;
vec2 s = resolution.zw * scale;

for(int x = -KERNEL_SIZE_HALF; x <= KERNEL_SIZE_HALF; ++x) {

@@ -184,7 +159,7 @@ void main() {

#endif

gl_FragColor = result / max(w, 1.0);
outputColor = result / max(w, 1.0);

#else

@@ -200,7 +175,7 @@ void main() {
#else

// General case
vec2 s = texelSize * scale;
vec2 s = resolution.zw * scale;

for(int x = -KERNEL_SIZE_HALF; x <= KERNEL_SIZE_HALF; ++x) {

@@ -214,7 +189,7 @@ void main() {

#endif

gl_FragColor = result * INV_KERNEL_SIZE_SQ;
outputColor = result * INV_KERNEL_SIZE_SQ;

#endif

25 changes: 13 additions & 12 deletions src/materials/shaders/convolution.box.vert
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
uniform vec2 texelSize;
#include <pp_resolution_pars_fragment>

uniform float scale;

#if KERNEL_SIZE == 3

// Optimized 3x3
varying vec2 vUv00, vUv01, vUv02;
varying vec2 vUv03, vUv04, vUv05;
varying vec2 vUv06, vUv07, vUv08;
out vec2 vUv00, vUv01, vUv02;
out vec2 vUv03, vUv04, vUv05;
out vec2 vUv06, vUv07, vUv08;

#elif KERNEL_SIZE == 5 && MAX_VARYING_VECTORS >= 13

// Optimized 5x5
varying vec2 vUv00, vUv01, vUv02, vUv03, vUv04;
varying vec2 vUv05, vUv06, vUv07, vUv08, vUv09;
varying vec2 vUv10, vUv11, vUv12, vUv13, vUv14;
varying vec2 vUv15, vUv16, vUv17, vUv18, vUv19;
varying vec2 vUv20, vUv21, vUv22, vUv23, vUv24;
out vec2 vUv00, vUv01, vUv02, vUv03, vUv04;
out vec2 vUv05, vUv06, vUv07, vUv08, vUv09;
out vec2 vUv10, vUv11, vUv12, vUv13, vUv14;
out vec2 vUv15, vUv16, vUv17, vUv18, vUv19;
out vec2 vUv20, vUv21, vUv22, vUv23, vUv24;

#else

// General case
varying vec2 vUv;
out vec2 vUv;

#endif

@@ -30,7 +31,7 @@ void main() {

#if KERNEL_SIZE == 3

vec2 s = texelSize * scale;
vec2 s = resolution.zw * scale;

// Optimized 3x3
vUv00 = uv + s * vec2(-1.0, -1.0);
@@ -47,7 +48,7 @@ void main() {

#elif KERNEL_SIZE == 5

vec2 s = texelSize * scale;
vec2 s = resolution.zw * scale;

// Optimized 5x5
vUv00 = uv + s * vec2(-2.0, -2.0);
57 changes: 21 additions & 36 deletions src/materials/shaders/convolution.downsampling.frag
Original file line number Diff line number Diff line change
@@ -1,31 +1,16 @@
#ifdef FRAMEBUFFER_PRECISION_HIGH

uniform mediump sampler2D inputBuffer;

#else

uniform lowp sampler2D inputBuffer;

#endif
#include <pp_precision_fragment>
#include <pp_default_output_pars_fragment>
#include <pp_input_buffer_pars_fragment>

// (1 / 4) * 0.5 = 0.125
#define WEIGHT_INNER 0.125
// (1 / 9) * 0.5 = 0.0555555
#define WEIGHT_OUTER 0.0555555

varying vec2 vUv;
varying vec2 vUv00;
varying vec2 vUv01;
varying vec2 vUv02;
varying vec2 vUv03;
varying vec2 vUv04;
varying vec2 vUv05;
varying vec2 vUv06;
varying vec2 vUv07;
varying vec2 vUv08;
varying vec2 vUv09;
varying vec2 vUv10;
varying vec2 vUv11;
in vec2 vUv;
in vec2 vUv00, vUv01, vUv02, vUv03;
in vec2 vUv04, vUv05, vUv06, vUv07;
in vec2 vUv08, vUv09, vUv10, vUv11;

float clampToBorder(const in vec2 uv) {

@@ -44,10 +29,10 @@ void main() {
clampToBorder(vUv03)
);

c += w.x * texture2D(inputBuffer, vUv00);
c += w.y * texture2D(inputBuffer, vUv01);
c += w.z * texture2D(inputBuffer, vUv02);
c += w.w * texture2D(inputBuffer, vUv03);
c += w.x * texture(inputBuffer, vUv00);
c += w.y * texture(inputBuffer, vUv01);
c += w.z * texture(inputBuffer, vUv02);
c += w.w * texture(inputBuffer, vUv03);

w = WEIGHT_OUTER * vec4(
clampToBorder(vUv04),
@@ -56,10 +41,10 @@ void main() {
clampToBorder(vUv07)
);

c += w.x * texture2D(inputBuffer, vUv04);
c += w.y * texture2D(inputBuffer, vUv05);
c += w.z * texture2D(inputBuffer, vUv06);
c += w.w * texture2D(inputBuffer, vUv07);
c += w.x * texture(inputBuffer, vUv04);
c += w.y * texture(inputBuffer, vUv05);
c += w.z * texture(inputBuffer, vUv06);
c += w.w * texture(inputBuffer, vUv07);

w = WEIGHT_OUTER * vec4(
clampToBorder(vUv08),
@@ -68,12 +53,12 @@ void main() {
clampToBorder(vUv11)
);

c += w.x * texture2D(inputBuffer, vUv08);
c += w.y * texture2D(inputBuffer, vUv09);
c += w.z * texture2D(inputBuffer, vUv10);
c += w.w * texture2D(inputBuffer, vUv11);
c += w.x * texture(inputBuffer, vUv08);
c += w.y * texture(inputBuffer, vUv09);
c += w.z * texture(inputBuffer, vUv10);
c += w.w * texture(inputBuffer, vUv11);

c += WEIGHT_OUTER * texture2D(inputBuffer, vUv);
gl_FragColor = c;
c += WEIGHT_OUTER * texture(inputBuffer, vUv);
outputColor = c;

}
43 changes: 17 additions & 26 deletions src/materials/shaders/convolution.downsampling.vert
Original file line number Diff line number Diff line change
@@ -1,36 +1,27 @@
uniform vec2 texelSize;
#include <pp_resolution_pars_fragment>

varying vec2 vUv;
varying vec2 vUv00;
varying vec2 vUv01;
varying vec2 vUv02;
varying vec2 vUv03;
varying vec2 vUv04;
varying vec2 vUv05;
varying vec2 vUv06;
varying vec2 vUv07;
varying vec2 vUv08;
varying vec2 vUv09;
varying vec2 vUv10;
varying vec2 vUv11;
out vec2 vUv;
out vec2 vUv00, vUv01, vUv02, vUv03;
out vec2 vUv04, vUv05, vUv06, vUv07;
out vec2 vUv08, vUv09, vUv10, vUv11;

void main() {

vUv = position.xy * 0.5 + 0.5;

vUv00 = vUv + texelSize * vec2(-1.0, 1.0);
vUv01 = vUv + texelSize * vec2(1.0, 1.0);
vUv02 = vUv + texelSize * vec2(-1.0, -1.0);
vUv03 = vUv + texelSize * vec2(1.0, -1.0);
vUv00 = vUv + resolution.zw * vec2(-1.0, 1.0);
vUv01 = vUv + resolution.zw * vec2(1.0, 1.0);
vUv02 = vUv + resolution.zw * vec2(-1.0, -1.0);
vUv03 = vUv + resolution.zw * vec2(1.0, -1.0);

vUv04 = vUv + texelSize * vec2(-2.0, 2.0);
vUv05 = vUv + texelSize * vec2(0.0, 2.0);
vUv06 = vUv + texelSize * vec2(2.0, 2.0);
vUv07 = vUv + texelSize * vec2(-2.0, 0.0);
vUv08 = vUv + texelSize * vec2(2.0, 0.0);
vUv09 = vUv + texelSize * vec2(-2.0, -2.0);
vUv10 = vUv + texelSize * vec2(0.0, -2.0);
vUv11 = vUv + texelSize * vec2(2.0, -2.0);
vUv04 = vUv + resolution.zw * vec2(-2.0, 2.0);
vUv05 = vUv + resolution.zw * vec2(0.0, 2.0);
vUv06 = vUv + resolution.zw * vec2(2.0, 2.0);
vUv07 = vUv + resolution.zw * vec2(-2.0, 0.0);
vUv08 = vUv + resolution.zw * vec2(2.0, 0.0);
vUv09 = vUv + resolution.zw * vec2(-2.0, -2.0);
vUv10 = vUv + resolution.zw * vec2(0.0, -2.0);
vUv11 = vUv + resolution.zw * vec2(2.0, -2.0);

gl_Position = vec4(position.xy, 1.0, 1.0);

24 changes: 9 additions & 15 deletions src/materials/shaders/convolution.gaussian.frag
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
#ifdef FRAMEBUFFER_PRECISION_HIGH

uniform mediump sampler2D inputBuffer;

#else

uniform lowp sampler2D inputBuffer;

#endif
#include <pp_precision_fragment>
#include <pp_default_output_pars_fragment>
#include <pp_input_buffer_pars_fragment>

uniform vec2 kernel[STEPS];

varying vec2 vOffset;
varying vec2 vUv;
in vec2 vOffset;
in vec2 vUv;

void main() {

vec4 result = texture2D(inputBuffer, vUv) * kernel[0].y;
vec4 result = texture(inputBuffer, vUv) * kernel[0].y;

for(int i = 1; i < STEPS; ++i) {

vec2 offset = kernel[i].x * vOffset;
vec4 c0 = texture2D(inputBuffer, vUv + offset);
vec4 c1 = texture2D(inputBuffer, vUv - offset);
vec4 c0 = texture(inputBuffer, vUv + offset);
vec4 c1 = texture(inputBuffer, vUv - offset);
result += (c0 + c1) * kernel[i].y;

}

gl_FragColor = result;
outputColor = result;

}
9 changes: 5 additions & 4 deletions src/materials/shaders/convolution.gaussian.vert
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
uniform vec2 texelSize;
#include <pp_resolution_pars_fragment>

uniform vec2 direction;
uniform float scale;

varying vec2 vOffset;
varying vec2 vUv;
out vec2 vOffset;
out vec2 vUv;

void main() {

vOffset = direction * texelSize * scale;
vOffset = direction * resolution.zw * scale;
vUv = position.xy * 0.5 + 0.5;
gl_Position = vec4(position.xy, 1.0, 1.0);

27 changes: 9 additions & 18 deletions src/materials/shaders/convolution.kawase.frag
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
#ifdef FRAMEBUFFER_PRECISION_HIGH
#include <pp_precision_fragment>
#include <pp_default_output_pars_fragment>
#include <pp_input_buffer_pars_fragment>

uniform mediump sampler2D inputBuffer;

#else

uniform lowp sampler2D inputBuffer;

#endif

varying vec2 vUv0;
varying vec2 vUv1;
varying vec2 vUv2;
varying vec2 vUv3;
in vec2 vUv0, vUv1, vUv2, vUv3;

void main() {

vec4 sum = texture2D(inputBuffer, vUv0); // Top left
sum += texture2D(inputBuffer, vUv1); // Top right
sum += texture2D(inputBuffer, vUv2); // Bottom right
sum += texture2D(inputBuffer, vUv3); // Bottom left
gl_FragColor = sum * 0.25; // Compute the average
vec4 sum = texture(inputBuffer, vUv0); // Top left
sum += texture(inputBuffer, vUv1); // Top right
sum += texture(inputBuffer, vUv2); // Bottom right
sum += texture(inputBuffer, vUv3); // Bottom left
outputColor = sum * 0.25; // Compute the average

}
10 changes: 4 additions & 6 deletions src/materials/shaders/convolution.kawase.vert
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
uniform vec4 texelSize; // XY = texel size, ZW = half texel size
#include <pp_resolution_pars_fragment>

uniform float kernel;
uniform float scale;

varying vec2 vUv0;
varying vec2 vUv1;
varying vec2 vUv2;
varying vec2 vUv3;
out vec2 vUv0, vUv1, vUv2, vUv3;

void main() {

vec2 uv = position.xy * 0.5 + 0.5;
vec2 dUv = (texelSize.xy * vec2(kernel) + texelSize.zw) * scale;
vec2 dUv = (texelSize.zw * vec2(kernel) + texelSize.zw * vec2(0.5)) * scale;

vUv0 = vec2(uv.x - dUv.x, uv.y + dUv.y);
vUv1 = vec2(uv.x + dUv.x, uv.y + dUv.y);
31 changes: 12 additions & 19 deletions src/materials/shaders/convolution.tilt-shift.frag
Original file line number Diff line number Diff line change
@@ -1,33 +1,26 @@
#ifdef FRAMEBUFFER_PRECISION_HIGH

uniform mediump sampler2D inputBuffer;

#else

uniform lowp sampler2D inputBuffer;

#endif
#include <pp_precision_fragment>
#include <pp_default_output_pars_fragment>
#include <pp_input_buffer_pars_fragment>

uniform vec4 maskParams;

varying vec2 vUv;
varying vec2 vUv2;
varying vec2 vOffset;
in vec2 vUv;
in vec2 vUv2;
in vec2 vOffset;

float linearGradientMask(const in float x) {

return smoothstep(maskParams.x, maskParams.y, x) -
smoothstep(maskParams.w, maskParams.z, x);
return smoothstep(maskParams.x, maskParams.y, x) - smoothstep(maskParams.w, maskParams.z, x);

}

void main() {

vec2 dUv = vOffset * (1.0 - linearGradientMask(vUv2.y));
vec4 sum = texture2D(inputBuffer, vec2(vUv.x - dUv.x, vUv.y + dUv.y)); // Top left
sum += texture2D(inputBuffer, vec2(vUv.x + dUv.x, vUv.y + dUv.y)); // Top right
sum += texture2D(inputBuffer, vec2(vUv.x + dUv.x, vUv.y - dUv.y)); // Bottom right
sum += texture2D(inputBuffer, vec2(vUv.x - dUv.x, vUv.y - dUv.y)); // Bottom left
gl_FragColor = sum * 0.25; // Compute the average
vec4 sum = texture(inputBuffer, vec2(vUv.x - dUv.x, vUv.y + dUv.y)); // Top left
sum += texture(inputBuffer, vec2(vUv.x + dUv.x, vUv.y + dUv.y)); // Top right
sum += texture(inputBuffer, vec2(vUv.x + dUv.x, vUv.y - dUv.y)); // Bottom right
sum += texture(inputBuffer, vec2(vUv.x - dUv.x, vUv.y - dUv.y)); // Bottom left
outputColor = sum * 0.25; // Compute the average

}
11 changes: 6 additions & 5 deletions src/materials/shaders/convolution.tilt-shift.vert
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
uniform vec4 texelSize; // XY = texel size, ZW = half texel size
#include <pp_resolution_pars_fragment>

uniform float kernel;
uniform float scale;
uniform float aspect;
uniform vec2 rotation;

varying vec2 vUv;
varying vec2 vUv2;
varying vec2 vOffset;
out vec2 vUv;
out vec2 vUv2;
out vec2 vOffset;

void main() {

@@ -15,7 +16,7 @@ void main() {
vUv = uv;
vUv2 = (uv - 0.5) * 2.0 * vec2(aspect, 1.0);
vUv2 = vec2(dot(rotation, vUv2), dot(rotation, vec2(vUv2.y, -vUv2.x)));
vOffset = (texelSize.xy * vec2(kernel) + texelSize.zw) * scale;
vOffset = (texelSize.zw * vec2(kernel) + texelSize.zw * vec2(0.5)) * scale;

gl_Position = vec4(position.xy, 1.0, 1.0);

42 changes: 19 additions & 23 deletions src/materials/shaders/convolution.upsampling.frag
Original file line number Diff line number Diff line change
@@ -1,41 +1,37 @@
#include <pp_precision_fragment>
#include <pp_default_output_pars_fragment>
#include <pp_input_buffer_pars_fragment>

#ifdef FRAMEBUFFER_PRECISION_HIGH

uniform mediump sampler2D inputBuffer;
uniform mediump sampler2D supportBuffer;

#else

uniform lowp sampler2D inputBuffer;
uniform lowp sampler2D supportBuffer;

#endif

uniform float radius;

varying vec2 vUv;
varying vec2 vUv0;
varying vec2 vUv1;
varying vec2 vUv2;
varying vec2 vUv3;
varying vec2 vUv4;
varying vec2 vUv5;
varying vec2 vUv6;
varying vec2 vUv7;
in vec2 vUv;
in vec2 vUv0, vUv1, vUv2, vUv3;
in vec2 vUv4, vUv5, vUv6, vUv7;

void main() {

vec4 c = vec4(0.0);
c += texture2D(inputBuffer, vUv0) * 0.0625;
c += texture2D(inputBuffer, vUv1) * 0.125;
c += texture2D(inputBuffer, vUv2) * 0.0625;
c += texture2D(inputBuffer, vUv3) * 0.125;
c += texture2D(inputBuffer, vUv) * 0.25;
c += texture2D(inputBuffer, vUv4) * 0.125;
c += texture2D(inputBuffer, vUv5) * 0.0625;
c += texture2D(inputBuffer, vUv6) * 0.125;
c += texture2D(inputBuffer, vUv7) * 0.0625;

vec4 baseColor = texture2D(supportBuffer, vUv);
gl_FragColor = mix(baseColor, c, radius);
c += texture(inputBuffer, vUv0) * 0.0625;
c += texture(inputBuffer, vUv1) * 0.125;
c += texture(inputBuffer, vUv2) * 0.0625;
c += texture(inputBuffer, vUv3) * 0.125;
c += texture(inputBuffer, vUv) * 0.25;
c += texture(inputBuffer, vUv4) * 0.125;
c += texture(inputBuffer, vUv5) * 0.0625;
c += texture(inputBuffer, vUv6) * 0.125;
c += texture(inputBuffer, vUv7) * 0.0625;

vec4 baseColor = texture(supportBuffer, vUv);
outputColor = mix(baseColor, c, radius);

}
30 changes: 12 additions & 18 deletions src/materials/shaders/convolution.upsampling.vert
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
uniform vec2 texelSize;
#include <pp_resolution_pars_fragment>

varying vec2 vUv;
varying vec2 vUv0;
varying vec2 vUv1;
varying vec2 vUv2;
varying vec2 vUv3;
varying vec2 vUv4;
varying vec2 vUv5;
varying vec2 vUv6;
varying vec2 vUv7;
out vec2 vUv;
out vec2 vUv0, vUv1, vUv2, vUv3;
out vec2 vUv4, vUv5, vUv6, vUv7;

void main() {

vUv = position.xy * 0.5 + 0.5;

vUv0 = vUv + texelSize * vec2(-1.0, 1.0);
vUv1 = vUv + texelSize * vec2(0.0, 1.0);
vUv2 = vUv + texelSize * vec2(1.0, 1.0);
vUv3 = vUv + texelSize * vec2(-1.0, 0.0);
vUv0 = vUv + resolution.zw * vec2(-1.0, 1.0);
vUv1 = vUv + resolution.zw * vec2(0.0, 1.0);
vUv2 = vUv + resolution.zw * vec2(1.0, 1.0);
vUv3 = vUv + resolution.zw * vec2(-1.0, 0.0);

vUv4 = vUv + texelSize * vec2(1.0, 0.0);
vUv5 = vUv + texelSize * vec2(-1.0, -1.0);
vUv6 = vUv + texelSize * vec2(0.0, -1.0);
vUv7 = vUv + texelSize * vec2(1.0, -1.0);
vUv4 = vUv + resolution.zw * vec2(1.0, 0.0);
vUv5 = vUv + resolution.zw * vec2(-1.0, -1.0);
vUv6 = vUv + resolution.zw * vec2(0.0, -1.0);
vUv7 = vUv + resolution.zw * vec2(1.0, -1.0);

gl_Position = vec4(position.xy, 1.0, 1.0);

23 changes: 5 additions & 18 deletions src/materials/shaders/copy.frag
Original file line number Diff line number Diff line change
@@ -1,32 +1,19 @@
#include <pp_precision_fragment>
#include <pp_gbuffer_output_pars_fragment>
#include <pp_colorspace_pars_fragment>
#include <pp_default_output_pars_fragment>
#include <pp_input_buffer_pars_fragment>

#include <common>
#include <colorspace_pars_fragment>
#include <dithering_pars_fragment>

#ifdef FRAMEBUFFER_PRECISION_HIGH

uniform mediump sampler2D inputBuffer;

#else

uniform lowp sampler2D inputBuffer;

#endif

in vec2 vUv;

void main() {

#ifdef GBUFFER_COLOR

outputColor = texture(inputBuffer, vUv);

#include <colorspace_fragment>
#include <dithering_fragment>
outputColor = texture(inputBuffer, vUv);

#endif
#include <colorspace_fragment>
#include <dithering_fragment>

}
22 changes: 8 additions & 14 deletions src/materials/shaders/depth-copy.frag
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
varying vec2 vUv;
#include <pp_precision_fragment>
#include <pp_default_output_pars_fragment>

in vec2 vUv;

#ifdef NORMAL_DEPTH

@@ -12,26 +15,17 @@ varying vec2 vUv;

#endif

#define getDepth(uv) texture2D(normalDepthBuffer, uv).a
#define getDepth(uv) texture(normalDepthBuffer, uv).a

#else

#ifdef GL_FRAGMENT_PRECISION_HIGH

uniform highp sampler2D depthBuffer;

#else

uniform mediump sampler2D depthBuffer;

#endif

#define getDepth(uv) texture2D(depthBuffer, uv).r
#include <pp_depth_buffer_pars_fragment>
#define getDepth(uv) texture(depthBuffer, uv).r

#endif

void main() {

gl_FragColor = vec4(getDepth(vUv));
outputColor = vec4(getDepth(vUv));

}
2 changes: 1 addition & 1 deletion src/materials/shaders/depth-copy.vert
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
varying vec2 vUv;
out vec2 vUv;

#if DEPTH_COPY_MODE == 1

45 changes: 11 additions & 34 deletions src/materials/shaders/depth-downsampling.frag
Original file line number Diff line number Diff line change
@@ -1,39 +1,16 @@
#include <pp_precision_fragment>
#include <pp_default_output_pars_fragment>
#include <pp_depth_buffer_pars_fragment>
#include <pp_depth_utils_pars_fragment>
#include <packing>

#ifdef GL_FRAGMENT_PRECISION_HIGH

uniform highp sampler2D depthBuffer;

#else

uniform mediump sampler2D depthBuffer;

#endif

#ifdef DOWNSAMPLE_NORMALS

uniform lowp sampler2D normalBuffer;
uniform mediump sampler2D normalBuffer;

#endif

varying vec2 vUv0;
varying vec2 vUv1;
varying vec2 vUv2;
varying vec2 vUv3;

float readDepth(const in vec2 uv) {

#if DEPTH_PACKING == 3201

return unpackRGBAToDepth(texture2D(depthBuffer, uv));

#else

return texture2D(depthBuffer, uv).r;

#endif

}
in vec2 vUv0, vUv1, vUv2, vUv3;

/**
* Returns the index of the most representative depth in the 2x2 neighborhood.
@@ -129,10 +106,10 @@ void main() {

// Gather all corresponding normals to avoid dependent texel fetches.
vec3 n[4];
n[0] = texture2D(normalBuffer, vUv0).rgb;
n[1] = texture2D(normalBuffer, vUv1).rgb;
n[2] = texture2D(normalBuffer, vUv2).rgb;
n[3] = texture2D(normalBuffer, vUv3).rgb;
n[0] = texture(normalBuffer, vUv0).rgb;
n[1] = texture(normalBuffer, vUv1).rgb;
n[2] = texture(normalBuffer, vUv2).rgb;
n[3] = texture(normalBuffer, vUv3).rgb;

#else

@@ -144,6 +121,6 @@ void main() {

#endif

gl_FragColor = vec4(n[index], d[index]);
outputColor = vec4(n[index], d[index]);

}
13 changes: 5 additions & 8 deletions src/materials/shaders/depth-downsampling.vert
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
uniform vec2 texelSize;
#include <pp_resolution_pars_fragment>

varying vec2 vUv0;
varying vec2 vUv1;
varying vec2 vUv2;
varying vec2 vUv3;
out vec2 vUv0, vUv1, vUv2, vUv3;

void main() {

vec2 uv = position.xy * 0.5 + 0.5;

vUv0 = uv;
vUv1 = vec2(uv.x, uv.y + texelSize.y);
vUv2 = vec2(uv.x + texelSize.x, uv.y);
vUv3 = uv + texelSize;
vUv1 = vec2(uv.x, uv.y + resolution.w);
vUv2 = vec2(uv.x + resolution.z, uv.y);
vUv3 = uv + resolution.zw;

gl_Position = vec4(position.xy, 1.0, 1.0);

96 changes: 59 additions & 37 deletions src/materials/shaders/edge-detection.frag
Original file line number Diff line number Diff line change
@@ -1,53 +1,75 @@
varying vec2 vUv;
varying vec2 vUv0;
varying vec2 vUv1;
#include <pp_precision_fragment>
#include <pp_default_output_pars_fragment>

in vec2 vUv;
in vec2 vUv0;
in vec2 vUv1;

#if EDGE_DETECTION_MODE != 0

varying vec2 vUv2;
varying vec2 vUv3;
varying vec2 vUv4;
varying vec2 vUv5;
in vec2 vUv2, vUv3, vUv4, vUv5;

#endif

#if EDGE_DETECTION_MODE == 1

// Include the luminance function.
#include <common>

#endif

#if EDGE_DETECTION_MODE == 0 || PREDICATION_MODE == 1

#ifdef GL_FRAGMENT_PRECISION_HIGH
#include <pp_depth_buffer_pars_fragment>
#include <pp_depth_utils_pars_fragment>
#include <packing>

uniform highp sampler2D depthBuffer;
float getOrthographicDepth(sampler2D depthBuffer, const in vec2 uv, const in float near, const in float far) {

#else
float depth = readDepth(depthBuffer, uv);

uniform mediump sampler2D depthBuffer;
#ifdef PERSPECTIVE_CAMERA

#endif
float viewZ = perspectiveDepthToViewZ(depth, near, far);
return viewZToOrthographicDepth(viewZ, near, far);

#else

return depth;

#endif

}

uniform vec2 cameraParams;

vec3 gatherNeighbors() {

float p = texture2D(depthBuffer, vUv).r;
float pLeft = texture2D(depthBuffer, vUv0).r;
float pTop = texture2D(depthBuffer, vUv1).r;
float p = getOrthographicDepth(depthBuffer, vUv, cameraParams.x, cameraParams.y);
float pLeft = getOrthographicDepth(depthBuffer, vUv0, cameraParams.x, cameraParams.y);
float pTop = getOrthographicDepth(depthBuffer, vUv1, cameraParams.x, cameraParams.y);

return vec3(p, pLeft, pTop);

}

#elif PREDICATION_MODE == 2

uniform sampler2D predicationBuffer;
#ifdef PREDICATIONBUFFER_PRECISION_HIGH

uniform mediump sampler2D predicationBuffer;

#else

uniform lowp sampler2D predicationBuffer;

#endif

vec3 gatherNeighbors() {

float p = texture2D(predicationBuffer, vUv).r;
float pLeft = texture2D(predicationBuffer, vUv0).r;
float pTop = texture2D(predicationBuffer, vUv1).r;
float p = texture(predicationBuffer, vUv).r;
float pLeft = texture(predicationBuffer, vUv0).r;
float pTop = texture(predicationBuffer, vUv1).r;

return vec3(p, pLeft, pTop);

@@ -71,7 +93,7 @@ varying vec2 vUv1;

#if EDGE_DETECTION_MODE != 0

uniform sampler2D inputBuffer;
#include <pp_input_buffer_pars_fragment>

#endif

@@ -105,15 +127,15 @@ void main() {

}

gl_FragColor = vec4(edges, 0.0, 1.0);
outputColor = vec4(edges, 0.0, 1.0);

#elif EDGE_DETECTION_MODE == 1

// Luma-based edge detection.

float l = luminance(texture2D(inputBuffer, vUv).rgb);
float lLeft = luminance(texture2D(inputBuffer, vUv0).rgb);
float lTop = luminance(texture2D(inputBuffer, vUv1).rgb);
float l = luminance(texture(inputBuffer, vUv).rgb);
float lLeft = luminance(texture(inputBuffer, vUv0).rgb);
float lTop = luminance(texture(inputBuffer, vUv1).rgb);

vec4 delta;
delta.xy = abs(l - vec2(lLeft, lTop));
@@ -127,16 +149,16 @@ void main() {
}

// Calculate right and bottom deltas.
float lRight = luminance(texture2D(inputBuffer, vUv2).rgb);
float lBottom = luminance(texture2D(inputBuffer, vUv3).rgb);
float lRight = luminance(texture(inputBuffer, vUv2).rgb);
float lBottom = luminance(texture(inputBuffer, vUv3).rgb);
delta.zw = abs(l - vec2(lRight, lBottom));

// Calculate the maximum delta in the direct neighborhood.
vec2 maxDelta = max(delta.xy, delta.zw);

// Calculate left-left and top-top deltas.
float lLeftLeft = luminance(texture2D(inputBuffer, vUv4).rgb);
float lTopTop = luminance(texture2D(inputBuffer, vUv5).rgb);
float lLeftLeft = luminance(texture(inputBuffer, vUv4).rgb);
float lTopTop = luminance(texture(inputBuffer, vUv5).rgb);
delta.zw = abs(vec2(lLeft, lTop) - vec2(lLeftLeft, lTopTop));

// Calculate the final maximum delta.
@@ -146,20 +168,20 @@ void main() {
// Local contrast adaptation.
edges.xy *= step(finalDelta, LOCAL_CONTRAST_ADAPTATION_FACTOR * delta.xy);

gl_FragColor = vec4(edges, 0.0, 1.0);
outputColor = vec4(edges, 0.0, 1.0);

#elif EDGE_DETECTION_MODE == 2

// Chroma-based edge detection.

vec4 delta;
vec3 c = texture2D(inputBuffer, vUv).rgb;
vec3 c = texture(inputBuffer, vUv).rgb;

vec3 cLeft = texture2D(inputBuffer, vUv0).rgb;
vec3 cLeft = texture(inputBuffer, vUv0).rgb;
vec3 t = abs(c - cLeft);
delta.x = max(max(t.r, t.g), t.b);

vec3 cTop = texture2D(inputBuffer, vUv1).rgb;
vec3 cTop = texture(inputBuffer, vUv1).rgb;
t = abs(c - cTop);
delta.y = max(max(t.r, t.g), t.b);

@@ -172,23 +194,23 @@ void main() {
}

// Calculate right and bottom deltas.
vec3 cRight = texture2D(inputBuffer, vUv2).rgb;
vec3 cRight = texture(inputBuffer, vUv2).rgb;
t = abs(c - cRight);
delta.z = max(max(t.r, t.g), t.b);

vec3 cBottom = texture2D(inputBuffer, vUv3).rgb;
vec3 cBottom = texture(inputBuffer, vUv3).rgb;
t = abs(c - cBottom);
delta.w = max(max(t.r, t.g), t.b);

// Calculate the maximum delta in the direct neighborhood.
vec2 maxDelta = max(delta.xy, delta.zw);

// Calculate left-left and top-top deltas.
vec3 cLeftLeft = texture2D(inputBuffer, vUv4).rgb;
vec3 cLeftLeft = texture(inputBuffer, vUv4).rgb;
t = abs(c - cLeftLeft);
delta.z = max(max(t.r, t.g), t.b);

vec3 cTopTop = texture2D(inputBuffer, vUv5).rgb;
vec3 cTopTop = texture(inputBuffer, vUv5).rgb;
t = abs(c - cTopTop);
delta.w = max(max(t.r, t.g), t.b);

@@ -199,7 +221,7 @@ void main() {
// Local contrast adaptation.
edges *= step(finalDelta, LOCAL_CONTRAST_ADAPTATION_FACTOR * delta.xy);

gl_FragColor = vec4(edges, 0.0, 1.0);
outputColor = vec4(edges, 0.0, 1.0);

#endif

25 changes: 11 additions & 14 deletions src/materials/shaders/edge-detection.vert
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
uniform vec2 texelSize;
#include <pp_resolution_pars_fragment>

varying vec2 vUv;
varying vec2 vUv0;
varying vec2 vUv1;
out vec2 vUv;
out vec2 vUv0;
out vec2 vUv1;

#if EDGE_DETECTION_MODE != 0

varying vec2 vUv2;
varying vec2 vUv3;
varying vec2 vUv4;
varying vec2 vUv5;
out vec2 vUv2, vUv3, vUv4, vUv5;

#endif

@@ -18,18 +15,18 @@ void main() {
vUv = position.xy * 0.5 + 0.5;

// Left and top texel coordinates.
vUv0 = vUv + texelSize * vec2(-1.0, 0.0);
vUv1 = vUv + texelSize * vec2(0.0, -1.0);
vUv0 = vUv + resolution.zw * vec2(-1.0, 0.0);
vUv1 = vUv + resolution.zw * vec2(0.0, -1.0);

#if EDGE_DETECTION_MODE != 0

// Right and bottom texel coordinates.
vUv2 = vUv + texelSize * vec2(1.0, 0.0);
vUv3 = vUv + texelSize * vec2(0.0, 1.0);
vUv2 = vUv + resolution.zw * vec2(1.0, 0.0);
vUv3 = vUv + resolution.zw * vec2(0.0, 1.0);

// Left-left and top-top texel coordinates.
vUv4 = vUv + texelSize * vec2(-2.0, 0.0);
vUv5 = vUv + texelSize * vec2(0.0, -2.0);
vUv4 = vUv + resolution.zw * vec2(-2.0, 0.0);
vUv5 = vUv + resolution.zw * vec2(0.0, -2.0);

#endif

102 changes: 12 additions & 90 deletions src/materials/shaders/effect.frag
Original file line number Diff line number Diff line change
@@ -1,114 +1,36 @@
#include <pp_precision_fragment>
#include <pp_default_output_pars_fragment>
#include <pp_camera_pars_fragment>
#include <pp_resolution_pars_fragment>
#include <pp_depth_utils_pars_fragment>
#include <pp_colorspace_conversion_pars_fragment>
#include <pp_colorspace_pars_fragment>

#include <common>
#include <packing>
#include <dithering_pars_fragment>

#define packFloatToRGBA(v) packDepthToRGBA(v)
#define unpackRGBAToFloat(v) unpackRGBAToDepth(v)

#ifdef FRAMEBUFFER_PRECISION_HIGH

uniform mediump sampler2D inputBuffer;

#else

uniform lowp sampler2D inputBuffer;

#endif

#if defined(GL_FRAGMENT_PRECISION_HIGH)
// TODO GData struct uniform

uniform highp sampler2D depthBuffer;

#else

uniform mediump sampler2D depthBuffer;

#endif

uniform vec4 resolution; // XY = resolution, ZW = texelSize
uniform vec3 cameraParams; // near, far, aspect
uniform float time;

varying vec2 vUv;

vec4 sRGBToLinear(const in vec4 value) {

return vec4(mix(
pow(value.rgb * 0.9478672986 + vec3(0.0521327014), vec3(2.4)),
value.rgb * 0.0773993808,
vec3(lessThanEqual(value.rgb, vec3(0.04045)))
), value.a);

}

float getViewZ(const in float depth) {

#ifdef PERSPECTIVE_CAMERA

return perspectiveDepthToViewZ(depth, cameraNear, cameraFar);

#else

return orthographicDepthToViewZ(depth, cameraNear, cameraFar);

#endif

}

/**
* Based on work by Sam Hocevar, Emil Persson and Ian Taylor.
* https://www.chilliant.com/rgb2hsv.html
*/

vec3 RGBToHCV(const in vec3 RGB) {

vec4 P = mix(vec4(RGB.bg, -1.0, 2.0 / 3.0), vec4(RGB.gb, 0.0, -1.0 / 3.0), step(RGB.b, RGB.g));
vec4 Q = mix(vec4(P.xyw, RGB.r), vec4(RGB.r, P.yzx), step(P.x, RGB.r));
float C = Q.x - min(Q.w, Q.y);
float H = abs((Q.w - Q.y) / (6.0 * C + EPSILON) + Q.z);
return vec3(H, C, Q.x);

}

vec3 RGBToHSL(const in vec3 RGB) {

vec3 HCV = RGBToHCV(RGB);
float L = HCV.z - HCV.y * 0.5;
float S = HCV.y / (1.0 - abs(L * 2.0 - 1.0) + EPSILON);
return vec3(HCV.x, S, L);

}

vec3 HueToRGB(const in float H) {

float R = abs(H * 6.0 - 3.0) - 1.0;
float G = 2.0 - abs(H * 6.0 - 2.0);
float B = 2.0 - abs(H * 6.0 - 4.0);
return clamp(vec3(R, G, B), 0.0, 1.0);

}

vec3 HSLToRGB(const in vec3 HSL) {

vec3 RGB = HueToRGB(HSL.x);
float C = (1.0 - abs(2.0 * HSL.z - 1.0)) * HSL.y;
return (RGB - 0.5) * C + HSL.z;

}
in vec2 vUv;

FRAGMENT_HEAD

void main() {

FRAGMENT_MAIN_UV

vec4 color0 = texture2D(inputBuffer, UV);
vec4 color0 = texture(inputBuffer, UV);
vec4 color1 = vec4(0.0);

FRAGMENT_MAIN_IMAGE

color0.a = clamp(color0.a, 0.0, 1.0);
gl_FragColor = color0;
outputColor = color0;

#ifdef COLOR_SPACE_CONVERSION

8 changes: 4 additions & 4 deletions src/materials/shaders/effect.vert
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
uniform vec4 resolution; // XY = resolution, ZW = texelSize
uniform vec3 cameraParams; // near, far, aspect
uniform float time;
#include <pp_camera_pars_fragment>
#include <pp_resolution_pars_fragment>

varying vec2 vUv;
uniform float time;
out vec2 vUv;

VERTEX_HEAD

21 changes: 7 additions & 14 deletions src/materials/shaders/luminance.frag
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
#include <pp_precision_fragment>
#include <pp_default_output_pars_fragment>
#include <pp_input_buffer_pars_fragment>
#include <common>

#ifdef FRAMEBUFFER_PRECISION_HIGH

uniform mediump sampler2D inputBuffer;

#else

uniform lowp sampler2D inputBuffer;

#endif

#ifdef THRESHOLD

uniform float threshold;
uniform float smoothing;

#endif

varying vec2 vUv;
in vec2 vUv;

void main() {

vec4 texel = texture2D(inputBuffer, vUv);
vec4 texel = texture(inputBuffer, vUv);
float l = luminance(texel.rgb);

#ifdef THRESHOLD
@@ -32,11 +25,11 @@ void main() {

#ifdef COLOR

gl_FragColor = vec4(texel.rgb * l, l);
outputColor = vec4(texel.rgb * l, l);

#else

gl_FragColor = vec4(l);
outputColor = vec4(l);

#endif

30 changes: 12 additions & 18 deletions src/materials/shaders/mask.frag
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
#ifdef FRAMEBUFFER_PRECISION_HIGH

uniform mediump sampler2D inputBuffer;

#else

uniform lowp sampler2D inputBuffer;

#endif
#include <pp_precision_fragment>
#include <pp_default_output_pars_fragment>
#include <pp_input_buffer_pars_fragment>

#ifdef MASK_PRECISION_HIGH

@@ -24,25 +18,25 @@

#endif

varying vec2 vUv;
in vec2 vUv;

void main() {

#if COLOR_CHANNEL == 0

float mask = texture2D(maskTexture, vUv).r;
float mask = texture(maskTexture, vUv).r;

#elif COLOR_CHANNEL == 1

float mask = texture2D(maskTexture, vUv).g;
float mask = texture(maskTexture, vUv).g;

#elif COLOR_CHANNEL == 2

float mask = texture2D(maskTexture, vUv).b;
float mask = texture(maskTexture, vUv).b;

#else

float mask = texture2D(maskTexture, vUv).a;
float mask = texture(maskTexture, vUv).a;

#endif

@@ -75,18 +69,18 @@ void main() {
#if MASK_FUNCTION == 3

// MULTIPLY_RGB
vec4 texel = texture2D(inputBuffer, vUv);
gl_FragColor = vec4(mask * texel.rgb, texel.a);
vec4 texel = texture(inputBuffer, vUv);
outputColor = vec4(mask * texel.rgb, texel.a);

#elif MASK_FUNCTION == 2

// MULTIPLY_RGB_SET_ALPHA
gl_FragColor = vec4(mask * texture2D(inputBuffer, vUv).rgb, mask);
outputColor = vec4(mask * texture(inputBuffer, vUv).rgb, mask);

#else

// MULTIPLY
gl_FragColor = mask * texture2D(inputBuffer, vUv);
outputColor = mask * texture(inputBuffer, vUv);

#endif

76 changes: 30 additions & 46 deletions src/materials/shaders/smaa-weights.frag
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
#define sampleLevelZeroOffset(t, coord, offset) texture2D(t, coord + offset * resolution.zw)
#include <pp_precision_fragment>
#include <pp_default_output_pars_fragment>
#include <pp_input_buffer_pars_fragment>

#if __VERSION__ < 300

#define round(v) floor(v + 0.5)

#endif

#ifdef FRAMEBUFFER_PRECISION_HIGH

uniform mediump sampler2D inputBuffer;

#else

uniform lowp sampler2D inputBuffer;

#endif
#define sampleLevelZeroOffset(t, coord, offset) texture(t, coord + offset * resolution.zw)

uniform lowp sampler2D areaTexture;
uniform lowp sampler2D searchTexture;

uniform vec4 resolution; // XY = resolution, ZW = texelSize

varying vec2 vUv;
varying vec4 vOffset[3];
varying vec2 vPixCoord;
in vec2 vUv;
in vec4 vOffset[3];
in vec2 vPixCoord;

/**
* Moves values to a target vector based on a given conditional vector.
@@ -46,8 +34,7 @@ void movec(const in bvec4 c, inout vec4 variable, const in vec4 value) {
/**
* Allows to decode two binary values from a bilinear-filtered access.
*
* Bilinear access for fetching 'e' have a 0.25 offset, and we are interested
* in the R and G edges:
* Bilinear access for fetching 'e' have a 0.25 offset, and we are interested in the R and G edges:
*
* +---G---+-------+
* | x o R x |
@@ -76,7 +63,7 @@ vec4 decodeDiagBilinearAccess(in vec4 e) {
}

/**
* Diagonal pattern searches.
* Diagonal pattern search.
*/

vec2 searchDiag1(const in vec2 texCoord, const in vec2 dir, out vec2 e) {
@@ -93,7 +80,7 @@ vec2 searchDiag1(const in vec2 texCoord, const in vec2 dir, out vec2 e) {
}

coord.xyz = t * vec3(dir, 1.0) + coord.xyz;
e = texture2D(inputBuffer, coord.xy).rg;
e = texture(inputBuffer, coord.xy).rg;
coord.w = dot(e, vec2(0.5));

}
@@ -120,11 +107,11 @@ vec2 searchDiag2(const in vec2 texCoord, const in vec2 dir, out vec2 e) {

// @SearchDiag2Optimization
// Fetch both edges at once using bilinear filtering.
e = texture2D(inputBuffer, coord.xy).rg;
e = texture(inputBuffer, coord.xy).rg;
e = decodeDiagBilinearAccess(e);

// Non-optimized version:
// e.g = texture2D(inputBuffer, coord.xy).g;
// e.g = texture(inputBuffer, coord.xy).g;
// e.r = SMAASampleLevelZeroOffset(inputBuffer, coord.xy, vec2(1, 0)).r;

coord.w = dot(e, vec2(0.5));
@@ -136,8 +123,7 @@ vec2 searchDiag2(const in vec2 texCoord, const in vec2 dir, out vec2 e) {
}

/**
* Calculates the area corresponding to a certain diagonal distance and crossing
* edges 'e'.
* Calculates the area corresponding to a certain diagonal distance and crossing edges 'e'.
*/

vec2 areaDiag(const in vec2 dist, const in vec2 e, const in float offset) {
@@ -153,7 +139,7 @@ vec2 areaDiag(const in vec2 dist, const in vec2 e, const in float offset) {
// Move to the proper place, according to the subpixel offset.
texCoord.y += AREATEX_SUBTEX_SIZE * offset;

return texture2D(areaTexture, texCoord).rg;
return texture(areaTexture, texCoord).rg;

}

@@ -249,8 +235,8 @@ vec2 calculateDiagWeights(const in vec2 texCoord, const in vec2 e, const in vec4
/**
* Determines how much length should be added in the last step of the searches.
*
* Takes the bilinearly interpolated edge (see @PSEUDO_GATHER4), and adds 0, 1
* or 2 depending on which edges and crossing edges are active.
* Takes the bilinearly interpolated edge (see @PSEUDO_GATHER4), and adds 0, 1 or 2 depending on which edges and
* crossing edges are active.
*/

float searchLength(const in vec2 e, const in float offset) {
@@ -268,7 +254,7 @@ float searchLength(const in vec2 e, const in float offset) {
scale *= 1.0 / SEARCHTEX_PACKED_SIZE;
bias *= 1.0 / SEARCHTEX_PACKED_SIZE;

return texture2D(searchTexture, scale * e + bias).r;
return texture(searchTexture, scale * e + bias).r;

}

@@ -279,10 +265,9 @@ float searchLength(const in vec2 e, const in float offset) {
float searchXLeft(in vec2 texCoord, const in float end) {

/* @PSEUDO_GATHER4
This texCoord has been offset by (-0.25, -0.125) in the vertex shader to
sample between edges, thus fetching four edges in a row.
Sampling with different offsets in each direction allows to disambiguate
which edges are active from the four fetched ones. */
This texCoord has been offset by (-0.25, -0.125) in the vertex shader to sample between edges, thus fetching four
edges in a row. Sampling with different offsets in each direction allows to disambiguate which edges are active from
the four fetched ones. */

vec2 e = vec2(0.0, 1.0);

@@ -294,7 +279,7 @@ float searchXLeft(in vec2 texCoord, const in float end) {

}

e = texture2D(inputBuffer, texCoord).rg;
e = texture(inputBuffer, texCoord).rg;
texCoord = vec2(-2.0, 0.0) * resolution.zw + texCoord;

}
@@ -327,7 +312,7 @@ float searchXRight(vec2 texCoord, const in float end) {

}

e = texture2D(inputBuffer, texCoord).rg;
e = texture(inputBuffer, texCoord).rg;
texCoord = vec2(2.0, 0.0) * resolution.zw + texCoord;

}
@@ -354,7 +339,7 @@ float searchYUp(vec2 texCoord, const in float end) {

}

e = texture2D(inputBuffer, texCoord).rg;
e = texture(inputBuffer, texCoord).rg;
texCoord = -vec2(0.0, 2.0) * resolution.zw + texCoord;

}
@@ -377,7 +362,7 @@ float searchYDown(vec2 texCoord, const in float end) {

}

e = texture2D(inputBuffer, texCoord).rg;
e = texture(inputBuffer, texCoord).rg;
texCoord = vec2(0.0, 2.0) * resolution.zw + texCoord;

}
@@ -403,7 +388,7 @@ vec2 area(const in vec2 dist, const in float e1, const in float e2, const in flo
// Move to the proper place, according to the subpixel offset.
texCoord.y = AREATEX_SUBTEX_SIZE * offset + texCoord.y;

return texture2D(areaTexture, texCoord).rg;
return texture(areaTexture, texCoord).rg;

}

@@ -458,16 +443,15 @@ void main() {

vec4 weights = vec4(0.0);
vec4 subsampleIndices = vec4(0.0);
vec2 e = texture2D(inputBuffer, vUv).rg;
vec2 e = texture(inputBuffer, vUv).rg;

if(e.g > 0.0) {

// Edge at north.

#if !defined(DISABLE_DIAG_DETECTION)

/* Diagonals have both north and west edges, so searching for them in one of
the boundaries is enough. */
// Diagonals have both north and west edges, so searching for them in one of the boundaries is enough.
weights.rg = calculateDiagWeights(vUv, e, subsampleIndices);

// Skip horizontal/vertical processing if there is a diagonal.
@@ -485,7 +469,7 @@ void main() {

/* Now fetch the left crossing edges, two at a time using bilinear filtering. Sampling at -0.25
(see @CROSSING_OFFSET) enables to discern what value each edge has. */
float e1 = texture2D(inputBuffer, coords.xy).r;
float e1 = texture(inputBuffer, coords.xy).r;

// Find the distance to the right.
coords.z = searchXRight(vOffset[0].zw, vOffset[2].y);
@@ -533,7 +517,7 @@ void main() {
d.x = coords.y;

// Fetch the top crossing edges.
float e1 = texture2D(inputBuffer, coords.xy).g;
float e1 = texture(inputBuffer, coords.xy).g;

// Find the distance to the bottom.
coords.z = searchYDown(vOffset[1].zw, vOffset[2].w);
@@ -557,6 +541,6 @@ void main() {

}

gl_FragColor = weights;
outputColor = weights;

}
6 changes: 3 additions & 3 deletions src/materials/shaders/smaa-weights.vert
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
uniform vec4 resolution; // XY = resolution, ZW = texelSize

varying vec2 vUv;
varying vec4 vOffset[3];
varying vec2 vPixCoord;
out vec2 vUv;
out vec4 vOffset[3];
out vec2 vPixCoord;

void main() {

0 comments on commit d3ffa71

Please sign in to comment.