Skip to content

Commit

Permalink
svmfilter_webgl uses glsl-template loader
Browse files Browse the repository at this point in the history
  • Loading branch information
yofreke committed Sep 19, 2016
1 parent fa54b59 commit 852fc5a
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 235 deletions.
4 changes: 2 additions & 2 deletions js/Tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import numeric from 'numeric';
import MosseFilter from './utils/mosse';
import MosseFilterResponses from './utils/mosseFilterResponses';
import JsfeatFace from './jsfeat/JsfeatFace';
import WebglFilter from './svmfilter_webgl';
import SvmFilter from './svmfilter_fft';
import WebglFilter from './svmfilter/svmfilter_webgl';
import SvmFilter from './svmfilter/svmfilter_fft';

// filters
// import entireFaceFilter from './filters/entire_face_filter.json';
Expand Down
35 changes: 35 additions & 0 deletions js/svmfilter/shaders/drawResponses.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
precision mediump float;

// our responses
uniform sampler2D u_responses;

// the texCoords passed in from the vertex shader.
varying vec2 v_texCoord;
varying float v_select;

const vec4 bit_shift = vec4(256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0);
const vec4 bit_mask = vec4(0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0);

// packing code from here http://stackoverflow.com/questions/9882716/packing-float-into-vec4-how-does-this-code-work
void main() {
vec4 colorSum = texture2D(u_responses, v_texCoord);
float value = 0.0;
if (v_select < 0.1) {
value = colorSum[0];
} else if (v_select > 0.9 && v_select < 1.1) {
value = colorSum[1];
} else if (v_select > 1.9 && v_select < 2.1) {
value = colorSum[2];
} else if (v_select > 2.9 && v_select < 3.1) {
value = colorSum[3];
} else {
value = 1.0;
}

vec4 res = fract(value * bit_shift);
res -= res.xxyz * bit_mask;

//gl_FragColor = vec4(value, value, value, value);
//gl_FragColor = vec4(1.0, value, 1.0, 1.0);
gl_FragColor = res;
}
27 changes: 27 additions & 0 deletions js/svmfilter/shaders/drawResponses.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
attribute vec2 a_texCoord_draw;
attribute vec2 a_position_draw;
attribute float a_patchChoice_draw;

uniform vec2 u_resolutiondraw;

varying vec2 v_texCoord;
varying float v_select;

void main() {
// convert the rectangle from pixels to 0.0 to 1.0
vec2 zeroToOne = a_position_draw / u_resolutiondraw;

// convert from 0->1 to 0->2
vec2 zeroToTwo = zeroToOne * 2.0;

// convert from 0->2 to -1->+1 (clipspace)
vec2 clipSpace = zeroToTwo - 1.0;

// transform coordinates to regular coordinates
gl_Position = vec4(clipSpace * vec2(1.0, 1.0), 0, 1);

// pass the texCoord to the fragment shader
v_texCoord = a_texCoord_draw;

v_select = a_patchChoice_draw;
}
32 changes: 32 additions & 0 deletions js/svmfilter/shaders/gradientResponse.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
precision mediump float;

uniform vec2 u_onePixelPatches;

// our patches
uniform sampler2D u_patches;

// the texCoords passed in from the vertex shader.
varying vec2 v_texCoord;

void main() {
vec4 bottomLeft = texture2D(u_patches, v_texCoord + vec2(-$opp0, $opp1));
vec4 bottomRight = texture2D(u_patches, v_texCoord + vec2($opp0, $opp1));
vec4 topLeft = texture2D(u_patches, v_texCoord + vec2(-$opp0, -$opp1));
vec4 topRight = texture2D(u_patches, v_texCoord + vec2($opp0, -$opp1));
vec4 dx = (
bottomLeft +
(texture2D(u_patches, v_texCoord + vec2(-$opp0, 0.0))*vec4(2.0,2.0,2.0,2.0)) +
topLeft -
bottomRight -
(texture2D(u_patches, v_texCoord + vec2($opp0, 0.0))*vec4(2.0,2.0,2.0,2.0)) -
topRight)/4.0;
vec4 dy = (
bottomLeft +
(texture2D(u_patches, v_texCoord + vec2(0.0, $opp1))*vec4(2.0,2.0,2.0,2.0)) +
bottomRight -
topLeft -
(texture2D(u_patches, v_texCoord + vec2(0.0, -$opp1))*vec4(2.0,2.0,2.0,2.0)) -
topRight)/4.0;
vec4 gradient = sqrt((dx*dx) + (dy*dy));
gl_FragColor = gradient;
}
12 changes: 12 additions & 0 deletions js/svmfilter/shaders/gradientResponse.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
attribute vec2 a_texCoord;
attribute vec2 a_position;

varying vec2 v_texCoord;

void main() {
// transform coordinates to regular coordinates
gl_Position = vec4(a_position, 0.0, 1.0);

// pass the texCoord to the fragment shader
v_texCoord = a_texCoord;
}
25 changes: 25 additions & 0 deletions js/svmfilter/shaders/lbpResponse.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
precision mediump float;

uniform vec2 u_onePixelPatches;

// our patches
uniform sampler2D u_patches;

// the texCoords passed in from the vertex shader.
varying vec2 v_texCoord;

void main() {
vec4 topLeft = texture2D(u_patches, v_texCoord + vec2(-$opp0, -$opp1));
vec4 topMid = texture2D(u_patches, v_texCoord + vec2(0.0, -$opp1));
vec4 topRight = texture2D(u_patches, v_texCoord + vec2($opp0, -$opp1));
vec4 midLeft = texture2D(u_patches, v_texCoord + vec2(-$opp0, 0.0));
vec4 midMid = texture2D(u_patches, v_texCoord);
vec4 midRight = texture2D(u_patches, v_texCoord + vec2($opp0, 0.0));
vec4 bottomLeft = texture2D(u_patches, v_texCoord + vec2(-$opp0, $opp1));
vec4 bottomMid = texture2D(u_patches, v_texCoord + vec2(0.0, $opp1));
vec4 bottomRight = texture2D(u_patches, v_texCoord + vec2($opp0, $opp1));
vec4 lbp = step(midMid, midRight) * 1.0 + step(midMid, topRight) * 2.0 + step(midMid, topMid) * 4.0;
lbp = lbp + step(midMid, topLeft) * 8.0 + step(midMid, midLeft) * 16.0 + step(midMid, bottomLeft) * 32.0;
lbp = lbp + step(midMid, bottomMid) * 64.0 + step(midMid, bottomRight) * 128.0;
gl_FragColor = lbp;
}
12 changes: 12 additions & 0 deletions js/svmfilter/shaders/lbpResponse.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
attribute vec2 a_texCoord;
attribute vec2 a_position;

varying vec2 v_texCoord;

void main() {
// transform coordinates to regular coordinates
gl_Position = vec4(a_position, 0.0, 1.0);

// pass the texCoord to the fragment shader
v_texCoord = a_texCoord;
}
40 changes: 40 additions & 0 deletions js/svmfilter/shaders/patchResponse.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
precision mediump float;

const vec2 u_onePixelPatches = vec2($onePixelPatchesX, $onePixelPatchesY);
const vec2 u_onePixelFilters = vec2($onePixelFiltersX, $onePixelFiltersY);
const float u_halffilterwidth = $halfFilterWidth;
const float u_halffilterheight = $halfFilterHeight;

// our patches
uniform sampler2D u_patches;
// our filters
uniform sampler2D u_filters;

// the texCoords passed in from the vertex shader.
varying vec2 v_texCoord;
varying vec2 v_texCoordFilters; // this should give us correct filter

void main() {
vec4 colorSum = vec4(0.0, 0.0, 0.0, 0.0);
vec4 maxn = vec4(0.0, 0.0, 0.0, 0.0);
vec4 minn = vec4(256.0, 256.0, 256.0, 256.0);
vec4 scale = vec4(0.0, 0.0, 0.0, 0.0);
vec4 patchValue = vec4(0.0, 0.0, 0.0, 0.0);
vec4 filterValue = vec4(0.0, 0.0, 0.0, 0.0);
vec4 filterTemp = vec4(0.0, 0.0, 0.0, 0.0);
for (int w = 0; w < $filterWidth; w++) {
for (int h = 0; h < $filterHeight; h++) {
patchValue = texture2D(u_patches, v_texCoord + u_onePixelPatches * vec2(float(w)-u_halffilterwidth, float(h)-u_halffilterheight));
filterValue = texture2D(u_filters, v_texCoordFilters + u_onePixelFilters * vec2(float(w)-u_halffilterwidth, float(h)-u_halffilterheight));
maxn = max(patchValue, maxn);
minn = min(patchValue, minn);
colorSum += patchValue*filterValue;
filterTemp += filterValue;
}
}
scale = maxn-minn;
colorSum = (colorSum-(minn*filterTemp))/scale;
// logistic transformation
colorSum = 1.0/(1.0 + exp(- (colorSum) ));
gl_FragColor = colorSum;
}
30 changes: 30 additions & 0 deletions js/svmfilter/shaders/patchResponse.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
attribute vec2 a_texCoord;
attribute vec2 a_position;

const vec2 u_resolution = vec2($resolutionX, $resolutionY);
const float u_patchHeight = $patchHeight;
const float u_filterHeight = $filterHeight;
const vec2 u_midpoint = vec2(0.5, $midpointY);

varying vec2 v_texCoord;
varying vec2 v_texCoordFilters;

void main() {
// convert the rectangle from pixels to 0.0 to 1.0
vec2 zeroToOne = a_position / u_resolution;

// convert from 0->1 to 0->2
vec2 zeroToTwo = zeroToOne * 2.0;

// convert from 0->2 to -1->+1 (clipspace)
vec2 clipSpace = zeroToTwo - 1.0;

// transform coordinates to regular coordinates
gl_Position = vec4(clipSpace * vec2(1.0, 1.0), 0, 1);

// pass the texCoord to the fragment shader
v_texCoord = a_texCoord;

// set the filtertexture coordinate based on number filter to use
v_texCoordFilters = u_midpoint + vec2(0.0, u_filterHeight * floor(a_texCoord[1]/u_patchHeight));
}
File renamed without changes.
Loading

0 comments on commit 852fc5a

Please sign in to comment.