diff --git a/examples/js/shaders/EdgeShader.js b/examples/js/shaders/EdgeShader.js new file mode 100644 index 0000000000000000000000000000000000000000..df4cb279bf25c30dad6efa94a0aacda6f1950666 --- /dev/null +++ b/examples/js/shaders/EdgeShader.js @@ -0,0 +1,93 @@ +/** + * @author zz85 / https://github.com/zz85 | https://www.lab4games.net/zz85/blog + * + * Edge Detection Shader using Frei-Chen filter + * Based on http://rastergrid.com/blog/2011/01/frei-chen-edge-detector + * + * aspect: vec2 of (1/width, 1/height) + */ + +THREE.EdgeShader = { + + uniforms: { + + "tDiffuse": { type: "t", value: null }, + "aspect": { type: "v2", value: new THREE.Vector2( 512, 512 ) }, + }, + + vertexShader: [ + + "varying vec2 vUv;", + + "void main() {", + + "vUv = uv;", + "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );", + + "}" + + ].join("\n"), + + fragmentShader: [ + + "uniform sampler2D tDiffuse;", + "varying vec2 vUv;", + + "uniform vec2 aspect;", + + "vec2 texel = vec2(1.0 / aspect.x, 1.0 / aspect.y);", + + + "mat3 G[9];", + + // hard coded matrix values!!!! as suggested in https://github.com/neilmendoza/ofxPostProcessing/blob/master/src/EdgePass.cpp#L45 + + "const mat3 g0 = mat3( 0.3535533845424652, 0, -0.3535533845424652, 0.5, 0, -0.5, 0.3535533845424652, 0, -0.3535533845424652 );", + "const mat3 g1 = mat3( 0.3535533845424652, 0.5, 0.3535533845424652, 0, 0, 0, -0.3535533845424652, -0.5, -0.3535533845424652 );", + "const mat3 g2 = mat3( 0, 0.3535533845424652, -0.5, -0.3535533845424652, 0, 0.3535533845424652, 0.5, -0.3535533845424652, 0 );", + "const mat3 g3 = mat3( 0.5, -0.3535533845424652, 0, -0.3535533845424652, 0, 0.3535533845424652, 0, 0.3535533845424652, -0.5 );", + "const mat3 g4 = mat3( 0, -0.5, 0, 0.5, 0, 0.5, 0, -0.5, 0 );", + "const mat3 g5 = mat3( -0.5, 0, 0.5, 0, 0, 0, 0.5, 0, -0.5 );", + "const mat3 g6 = mat3( 0.1666666716337204, -0.3333333432674408, 0.1666666716337204, -0.3333333432674408, 0.6666666865348816, -0.3333333432674408, 0.1666666716337204, -0.3333333432674408, 0.1666666716337204 );", + "const mat3 g7 = mat3( -0.3333333432674408, 0.1666666716337204, -0.3333333432674408, 0.1666666716337204, 0.6666666865348816, 0.1666666716337204, -0.3333333432674408, 0.1666666716337204, -0.3333333432674408 );", + "const mat3 g8 = mat3( 0.3333333432674408, 0.3333333432674408, 0.3333333432674408, 0.3333333432674408, 0.3333333432674408, 0.3333333432674408, 0.3333333432674408, 0.3333333432674408, 0.3333333432674408 );", + + "void main(void)", + "{", + + "G[0] = g0,", + "G[1] = g1,", + "G[2] = g2,", + "G[3] = g3,", + "G[4] = g4,", + "G[5] = g5,", + "G[6] = g6,", + "G[7] = g7,", + "G[8] = g8;", + + "mat3 I;", + "float cnv[9];", + "vec3 sample;", + + /* fetch the 3x3 neighbourhood and use the RGB vector's length as intensity value */ + "for (float i=0.0; i<3.0; i++) {", + "for (float j=0.0; j<3.0; j++) {", + "sample = texture2D(tDiffuse, vUv + texel * vec2(i-1.0,j-1.0) ).rgb;", + "I[int(i)][int(j)] = length(sample);", + "}", + "}", + + /* calculate the convolution values for all the masks */ + "for (int i=0; i<9; i++) {", + "float dp3 = dot(G[i][0], I[0]) + dot(G[i][1], I[1]) + dot(G[i][2], I[2]);", + "cnv[i] = dp3 * dp3;", + "}", + + "float M = (cnv[0] + cnv[1]) + (cnv[2] + cnv[3]);", + "float S = (cnv[4] + cnv[5]) + (cnv[6] + cnv[7]) + (cnv[8] + M);", + + "gl_FragColor = vec4(vec3(sqrt(M/S)), 1.0);", + "}", + + ].join("\n") +}; diff --git a/examples/js/shaders/EdgeShader2.js b/examples/js/shaders/EdgeShader2.js new file mode 100644 index 0000000000000000000000000000000000000000..d5b6a237fc01f9432fb560ee0e99f13f8d52d9b4 --- /dev/null +++ b/examples/js/shaders/EdgeShader2.js @@ -0,0 +1,73 @@ +/** + * @author zz85 / https://github.com/zz85 | https://www.lab4games.net/zz85/blog + * + * Edge Detection Shader using Sobel filter + * Based on http://rastergrid.com/blog/2011/01/frei-chen-edge-detector + * + * aspect: vec2 of (1/width, 1/height) + */ + +THREE.EdgeShader2 = { + + uniforms: { + + "tDiffuse": { type: "t", value: null }, + "aspect": { type: "v2", value: new THREE.Vector2( 512, 512 ) }, + }, + + vertexShader: [ + + "varying vec2 vUv;", + + "void main() {", + + "vUv = uv;", + "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );", + + "}" + + ].join("\n"), + + fragmentShader: [ + + "uniform sampler2D tDiffuse;", + "varying vec2 vUv;", + "uniform vec2 aspect;", + + + "vec2 texel = vec2(1.0 / aspect.x, 1.0 / aspect.y);", + + "mat3 G[2];", + + "const mat3 g0 = mat3( 1.0, 2.0, 1.0, 0.0, 0.0, 0.0, -1.0, -2.0, -1.0 );", + "const mat3 g1 = mat3( 1.0, 0.0, -1.0, 2.0, 0.0, -2.0, 1.0, 0.0, -1.0 );", + + + "void main(void)", + "{", + "mat3 I;", + "float cnv[2];", + "vec3 sample;", + + "G[0] = g0;", + "G[1] = g1;", + + /* fetch the 3x3 neighbourhood and use the RGB vector's length as intensity value */ + "for (float i=0.0; i<3.0; i++)", + "for (float j=0.0; j<3.0; j++) {", + "sample = texture2D( tDiffuse, vUv + texel * vec2(i-1.0,j-1.0) ).rgb;", + "I[int(i)][int(j)] = length(sample);", + "}", + + /* calculate the convolution values for all the masks */ + "for (int i=0; i<2; i++) {", + "float dp3 = dot(G[i][0], I[0]) + dot(G[i][1], I[1]) + dot(G[i][2], I[2]);", + "cnv[i] = dp3 * dp3; ", + "}", + + "gl_FragColor = vec4(0.5 * sqrt(cnv[0]*cnv[0]+cnv[1]*cnv[1]));", + "} ", + + ].join("\n") + +}; diff --git a/examples/webgl_postprocessing2.html b/examples/webgl_postprocessing2.html new file mode 100644 index 0000000000000000000000000000000000000000..f52b302282a16311022d4916ca43daa1cadd348a --- /dev/null +++ b/examples/webgl_postprocessing2.html @@ -0,0 +1,177 @@ + + + + three.js webgl - postprocessing + + + + + + + + + + + + + + + + + + +
+
+
+
+
+ +
+ + + +