Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions src/effects/MatcapEffect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@

import { Effect, EffectAttribute } from 'postprocessing';
import { Uniform } from 'three';

export class MatcapEffect extends Effect {

constructor( matcapTextureUniform, camera ) {

super(
'MatcapEffect',
/* glsl */ `
uniform mat3 camera_normalMatrix;
uniform mat4 camera_projectionMatrixInverse;

uniform sampler2D matcapTextureUniform;

// Matcap Material
vec2 get_vN(const in vec3 eye, const in vec3 normal) {
vec3 r_en = reflect( eye, normal ); // or r_en = e - 2. * dot( n, e ) * n;
float m = 2. * sqrt(pow( r_en.x, 2. ) + pow( r_en.y, 2. ) + pow( r_en.z + 1., 2. ));
vec2 vN = r_en.xy / m + .5;
return vN;
}
vec3 get_matcap(const in vec2 uv, const in vec4 normalVec) {
// Get matcap lookup coords from normal and depth
vec3 normal = normalVec.xyz * 2. - 1.;
float d = readDepth(uv) * 2. - 1.;
vec4 uvd = vec4(uv * 2. - 1., d, 1.);
vec4 xyz = camera_projectionMatrixInverse * uvd;
vec3 eye = vec3(xyz / xyz.w);
vec2 vN = get_vN(eye, normal);

// Read matcap texture at lookup position
return texture2D(matcapTextureUniform, vN).rgb;
}

vec3 get_camera_space_coords_mat3 (const in vec2 uv_co) {
float d = readDepth(uv_co); // * 2. - 1.;
vec3 uvd = vec3(uv_co * 2. - 1., d);
vec3 xyz = camera_normalMatrix * uvd;
return xyz;
}

vec3 get_camera_space_coords_mat4 (const in vec2 uv_co) {
float d = readDepth(uv_co) * 2. - 1.;
vec4 uvd = vec4(uv_co * 2. - 1., d, 1.);
vec4 xyz = camera_projectionMatrixInverse * uvd;
return vec3(xyz / xyz.w);
}

vec3 get_camera_space_coords (const in vec2 uv_co) {
return get_camera_space_coords_mat4(uv_co);
}

void mainImage(const in vec4 inputColor, const in vec2 uv, const in float depth, out vec4 outputColor) {
// uniforms.resolution.value.set(width, height);
// uniforms.texelSize.value.set(1.0 / width, 1.0 / height);
// v * v is component-wise
// inputColor should be normal texture, eventually NormalScreenSpace
outputColor = vec4(get_matcap(uv, inputColor), 1.);
}
`, {

uniforms: new Map( [
[ 'matcapTextureUniform', new Uniform( matcapTextureUniform ) ],
[ 'camera_normalMatrix', new Uniform( camera.normalMatrix ) ],
[ 'camera_projectionMatrixInverse', new Uniform( camera.projectionMatrixInverse ) ],

] ),
attributes: EffectAttribute.CONVOLUTION | EffectAttribute.DEPTH

}
);

}

}
200 changes: 200 additions & 0 deletions src/effects/ScreenSpaceNormalEffect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@

import { Effect, EffectAttribute } from 'postprocessing';
import { Uniform } from 'three';

// https://github.com/pmndrs/postprocessing/wiki/Custom-Effects#depth
export class ScreenSpaceNormalEffect extends Effect {

constructor( maskTexture, camera ) {

// Good references:
// https://wickedengine.net/2019/09/22/improved-normal-reconstruction-from-depth/
// https://atyuwen.github.io/posts/normal-reconstruction/
// https://www.shadertoy.com/view/fsVczR

super(
'ScreenSpaceNormalEffect',
/* glsl */ `
uniform sampler2D maskTexture;
uniform mat3 camera_normalMatrix;
uniform mat4 camera_projectionMatrixInverse;

vec3 get_camera_space_coords_mat3 (const in vec2 uv_co) {
float d = readDepth(uv_co); // * 2. - 1.;
vec3 uvd = vec3(uv_co * 2. - 1., d);
vec3 xyz = camera_normalMatrix * uvd;
return xyz;
}

vec3 get_camera_space_coords_mat4 (const in vec2 uv_co) {
float d = readDepth(uv_co) * 2. - 1.;
vec4 uvd = vec4(uv_co * 2. - 1., d, 1.);
vec4 xyz = camera_projectionMatrixInverse * uvd;
return vec3(xyz / xyz.w);
}

vec3 get_camera_space_coords (const in vec2 uv_co) {
return get_camera_space_coords_mat4(uv_co);
}

vec3 get_normal_simple(const in vec2 uv_co) {
// https://wickedengine.net/2019/09/22/improved-normal-reconstruction-from-depth/
vec3 xyz = get_camera_space_coords (uv_co);
vec3 xyz_right = get_camera_space_coords (uv_co + texelSize * vec2(1., 0.));
vec3 xyz_up = get_camera_space_coords (uv_co + texelSize * vec2(0., 1.));
vec3 norm = normalize(cross(xyz_right - xyz, xyz_up - xyz));
return norm;
}

vec3 get_normal_besttri(const in vec2 uv_co) {
// https://wickedengine.net/2019/09/22/improved-normal-reconstruction-from-depth/
vec3 xyz = get_camera_space_coords (uv_co);

float d_center = readDepth(uv_co);

vec2 uv_right = uv_co + texelSize * vec2(1., 0.);
vec2 uv_left = uv_co + texelSize * vec2(-1., 0.);
vec2 uv_up = uv_co + texelSize * vec2(0., 1.);
vec2 uv_down = uv_co + texelSize * vec2(0., -1.);

vec3 xyz_right = get_camera_space_coords (uv_right);
vec3 xyz_left = get_camera_space_coords (uv_left);
vec3 xyz_up = get_camera_space_coords (uv_up);
vec3 xyz_down = get_camera_space_coords (uv_down);

vec3 xyz_1, xyz_2; // rather than horiz/verti, suffix 1 and 2 which should always be in winding order
if (abs(readDepth(uv_right) - d_center) < abs(readDepth(uv_left) - d_center)) {

if ( abs(readDepth(uv_up) - d_center) < abs(readDepth(uv_down) - d_center)) {
xyz_1 = xyz_right;
xyz_2 = xyz_up;
} else {
xyz_2 = xyz_right;
xyz_1 = xyz_down;
}
} else {
if ( abs(readDepth(uv_up) - d_center) < abs(readDepth(uv_down) - d_center)) {
xyz_2 = xyz_left;
xyz_1 = xyz_up;
} else {
xyz_1 = xyz_left;
xyz_2 = xyz_down;
}
}
vec3 norm = normalize(cross(xyz_1 - xyz, xyz_2 - xyz));
return norm;
}

vec3 get_normal_accurate_bad_bands(const in vec2 uv_co) {
// https://atyuwen.github.io/posts/normal-reconstruction/
// Also see https://www.shadertoy.com/view/fsVczR
// Try reconstructing normal accurately from depth buffer.
// input DepthBuffer: stores linearized depth in range (0, 1).
// 5 taps on each direction: | z | x | * | y | w |, '*' denotes the center sample.

// Say if your want caculate depth at | * | from taps | z | x |, let A = depth at | * |, B = depth at | z |, C = depth at | x |, we have:
// 1 / C = 0.5 * (1 / A + 1 / B); // perspective correct interpolation
// Solve A from above equation we get: A = C * B / (2 * B - C);

float depth = readDepth(uv_co); // c0

vec4 H;
H.x = readDepth(uv_co + texelSize * vec2(-1., 0.)); // L1
H.y = readDepth(uv_co + texelSize * vec2(+1., 0.)); // R1
H.z = readDepth(uv_co + texelSize * vec2(-2., 0.)); // L2
H.w = readDepth(uv_co + texelSize * vec2(+2., 0.)); // R2

vec2 he = abs(H.xy * H.zw / (2. * H.zw - H.xy) - depth);
vec3 hDeriv;
if (he.x > he.y)
hDeriv = get_camera_space_coords ( uv_co ) - get_camera_space_coords ( uv_co + texelSize * vec2(-1., 0.) );
else
hDeriv = - get_camera_space_coords ( uv_co ) + get_camera_space_coords ( uv_co + texelSize * vec2(+1., 0.) );

vec4 V;
V.x = readDepth(uv_co + texelSize * vec2( 0., -1.));
V.y = readDepth(uv_co + texelSize * vec2( 0., +1.));
V.z = readDepth(uv_co + texelSize * vec2( 0., -2.));
V.w = readDepth(uv_co + texelSize * vec2( 0., +2.));
vec2 ve = abs(V.xy * V.zw / (2. * V.zw - V.xy) - depth);
vec3 vDeriv;
if (abs(ve.y - ve.x) > 0.0001)
hDeriv = get_camera_space_coords (uv_co + texelSize * vec2(0., +1.)) - get_camera_space_coords ( uv_co );
else
vDeriv = get_camera_space_coords ( uv_co ) - get_camera_space_coords ( uv_co + texelSize * vec2(0., -1.) );

return normalize(cross(hDeriv, vDeriv));
}

vec3 get_normal_accurate_iq(const in vec2 uv_co) {
// https://atyuwen.github.io/posts/normal-reconstruction/
// Also see https://www.shadertoy.com/view/fsVczR
// Try reconstructing normal accurately from depth buffer.
// input DepthBuffer: stores linearized depth in range (0, 1).
// 5 taps on each direction: | z | x | * | y | w |, '*' denotes the center sample.

// Say if your want caculate depth at | * | from taps | z | x |, let A = depth at | * |, B = depth at | z |, C = depth at | x |, we have:
// 1 / C = 0.5 * (1 / A + 1 / B); // perspective correct interpolation
// Solve A from above equation we get: A = C * B / (2 * B - C);

float depth = readDepth(uv_co); // c0

float c0 = readDepth(uv_co);
float l2 = readDepth(uv_co + texelSize * vec2(-2., 0.));
float l1 = readDepth(uv_co + texelSize * vec2(-1., 0.));
float r1 = readDepth(uv_co + texelSize * vec2(+1., 0.));
float r2 = readDepth(uv_co + texelSize * vec2(+2., 0.));
float b2 = readDepth(uv_co + texelSize * vec2(0., -2.));
float b1 = readDepth(uv_co + texelSize * vec2(0., -1.));
float t1 = readDepth(uv_co + texelSize * vec2(0., +1.));
float t2 = readDepth(uv_co + texelSize * vec2(0., +2.));

float dl = abs(l1*l2/(2.0*l2-l1)-c0);
float dr = abs(r1*r2/(2.0*r2-r1)-c0);
float db = abs(b1*b2/(2.0*b2-b1)-c0);
float dt = abs(t1*t2/(2.0*t2-t1)-c0);

vec3 ce = get_camera_space_coords ( uv_co );

vec3 dpdx = (dl<dr) ? ce - get_camera_space_coords ( uv_co + texelSize * vec2(-1., 0.) ) :
- ce + get_camera_space_coords ( uv_co + texelSize * vec2(+1., 0.) ) ;
vec3 dpdy = (db<dt) ? ce - get_camera_space_coords ( uv_co + texelSize * vec2(0., -1.) ) :
-ce +get_camera_space_coords ( uv_co + texelSize * vec2(0., +1.) ) ;

return normalize(cross(dpdx,dpdy));
}


void mainImage(const in vec4 inputColor, const in vec2 uv, const in float depth, out vec4 outputColor) {
// uniforms.resolution.value.set(width, height);
// uniforms.texelSize.value.set(1.0 / width, 1.0 / height);
// v * v is component-wise

// vec3 norm = get_normal_simple(uv);
vec3 norm = get_normal_besttri(uv);
// vec3 norm = get_normal_accurate_bad_bands(uv);
// vec3 norm = get_normal_accurate_iq(uv);

// norm = ( camera_matrixWorldInverse * vec4(norm, 0.)).xyz;
// norm = ( camera_matrixWorld * vec4(norm, 0.)).xyz;
// norm = (norm + 1.) * 0.5;
norm = (norm + 0.5);

outputColor = vec4(norm, 1.);
}
`, {

uniforms: new Map( [
[ 'maskTexture', new Uniform( maskTexture ) ],
[ 'camera_normalMatrix', new Uniform( camera.normalMatrix ) ],
[ 'camera_projectionMatrixInverse', new Uniform( camera.projectionMatrixInverse ) ],

] ),
attributes: EffectAttribute.CONVOLUTION | EffectAttribute.DEPTH

}
);

}

}
63 changes: 63 additions & 0 deletions src/effects/SobelEffect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

import { Effect } from 'postprocessing';

// Implementation from threejs postprocessing SobelOperatorShader https://github.com/mrdoob/three.js/blob/master/examples/jsm/shaders/SobelOperatorShader.js
export class SobelEffect extends Effect {

constructor() {

super(
'SobelEffect',
/* glsl */ `
void mainImage(const in vec4 inputColor, const in vec2 uv, out vec4 outputColor) {
vec2 texel = vec2( 1.0 / resolution.x, 1.0 / resolution.y );

// kernel definition (in glsl matrices are filled in column-major order)

const mat3 Gx = mat3( -1, -2, -1, 0, 0, 0, 1, 2, 1 ); // x direction kernel
const mat3 Gy = mat3( -1, 0, 1, -2, 0, 2, -1, 0, 1 ); // y direction kernel

// fetch the 3x3 neighbourhood of a fragment

// first column

float tx0y0 = texture2D( inputBuffer, uv + texel * vec2( -1, -1 ) ).r;
float tx0y1 = texture2D( inputBuffer, uv + texel * vec2( -1, 0 ) ).r;
float tx0y2 = texture2D( inputBuffer, uv + texel * vec2( -1, 1 ) ).r;

// second column

float tx1y0 = texture2D( inputBuffer, uv + texel * vec2( 0, -1 ) ).r;
float tx1y1 = texture2D( inputBuffer, uv + texel * vec2( 0, 0 ) ).r;
float tx1y2 = texture2D( inputBuffer, uv + texel * vec2( 0, 1 ) ).r;

// third column

float tx2y0 = texture2D( inputBuffer, uv + texel * vec2( 1, -1 ) ).r;
float tx2y1 = texture2D( inputBuffer, uv + texel * vec2( 1, 0 ) ).r;
float tx2y2 = texture2D( inputBuffer, uv + texel * vec2( 1, 1 ) ).r;

// gradient value in x direction

float valueGx = Gx[0][0] * tx0y0 + Gx[1][0] * tx1y0 + Gx[2][0] * tx2y0 +
Gx[0][1] * tx0y1 + Gx[1][1] * tx1y1 + Gx[2][1] * tx2y1 +
Gx[0][2] * tx0y2 + Gx[1][2] * tx1y2 + Gx[2][2] * tx2y2;

// gradient value in y direction

float valueGy = Gy[0][0] * tx0y0 + Gy[1][0] * tx1y0 + Gy[2][0] * tx2y0 +
Gy[0][1] * tx0y1 + Gy[1][1] * tx1y1 + Gy[2][1] * tx2y1 +
Gy[0][2] * tx0y2 + Gy[1][2] * tx1y2 + Gy[2][2] * tx2y2;

// magnitute of the total gradient

float G = sqrt( ( valueGx * valueGx ) + ( valueGy * valueGy ) );

outputColor = vec4( vec3( G ), 1 );
}
`,
);

}

}
5 changes: 4 additions & 1 deletion src/effects/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@ export * from "./HueSaturationEffect.js";
export * from "./LensDistortionEffect.js";
export * from "./LUT1DEffect.js";
export * from "./LUT3DEffect.js";
export * from "./MatcapEffect.js";
export * from "./NoiseEffect.js";
export * from "./OutlineEffect.js";
export * from "./PixelationEffect.js";
export * from "./RealisticBokehEffect.js";
export * from "./ScanlineEffect.js";
export * from "./ShockWaveEffect.js";
export * from "./ScreenSpaceNormalEffect.js";
export * from "./SelectiveBloomEffect.js";
export * from "./SepiaEffect.js";
export * from "./ShockWaveEffect.js";
export * from "./SMAAEffect.js";
export * from "./SobelEffect.js";
export * from "./SSAOEffect.js";
export * from "./TextureEffect.js";
export * from "./TiltShiftEffect.js";
Expand Down