Skip to content

Commit

Permalink
Add filters
Browse files Browse the repository at this point in the history
  • Loading branch information
nekocode committed Aug 8, 2016
1 parent 364126d commit 55c1cf8
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ Realtime camera filters. Process frames by OpenGL shaders.
| Lichtenstein-esque | https://www.shadertoy.com/view/Mdf3zS | ![](art/15.png) |
| Ascii Art | https://www.shadertoy.com/view/lssGDj# | ![](art/16.png) |
| Money Filter | https://www.shadertoy.com/view/XlsXDN | ![](art/17.png) |
| Cracked | https://www.shadertoy.com/view/XdBSzW | ![](art/18.png) |
| Polygonization | https://www.shadertoy.com/view/4lsXR7# | ![](art/19.png) |
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import cn.nekocode.camerafilter.filter.CameraFilter;
import cn.nekocode.camerafilter.filter.ChromaticAberrationFilter;
import cn.nekocode.camerafilter.filter.ContrastFilter;
import cn.nekocode.camerafilter.filter.CrackedFilter;
import cn.nekocode.camerafilter.filter.CrosshatchFilter;
import cn.nekocode.camerafilter.filter.EMInterferenceFilter;
import cn.nekocode.camerafilter.filter.EdgeDetectionFilter;
Expand All @@ -33,6 +34,7 @@
import cn.nekocode.camerafilter.filter.NoiseWarpFilter;
import cn.nekocode.camerafilter.filter.OriginalFilter;
import cn.nekocode.camerafilter.filter.PixelizeFilter;
import cn.nekocode.camerafilter.filter.PolygonizationFilter;
import cn.nekocode.camerafilter.filter.RefractionFilter;
import cn.nekocode.camerafilter.filter.TileMosaicFilter;
import cn.nekocode.camerafilter.filter.TrianglesMosaicFilter;
Expand Down Expand Up @@ -128,6 +130,8 @@ public void run() {
cameraFilterMap.append(R.id.filter15, new LichtensteinEsqueFilter(context));
cameraFilterMap.append(R.id.filter16, new AsciiArtFilter(context));
cameraFilterMap.append(R.id.filter17, new MoneyFilter(context));
cameraFilterMap.append(R.id.filter18, new CrackedFilter(context));
cameraFilterMap.append(R.id.filter19, new PolygonizationFilter(context));
cameraFilter = cameraFilterMap.get(R.id.filter0);

try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cn.nekocode.camerafilter.filter;

import android.content.Context;

import cn.nekocode.camerafilter.MyGLUtils;
import cn.nekocode.camerafilter.R;

/**
* Created by nekocode on 16/8/6.
*/
public class CrackedFilter extends CameraFilter {
private int program;

public CrackedFilter(Context context) {
super(context);

// Build shaders
program = MyGLUtils.buildProgram(context, R.raw.vertext, R.raw.cracked);
}

@Override
public void draw(int textureId, int gwidth, int gheight) {
defaultDraw(program, textureId, gwidth, gheight);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cn.nekocode.camerafilter.filter;

import android.content.Context;

import cn.nekocode.camerafilter.MyGLUtils;
import cn.nekocode.camerafilter.R;

/**
* Created by nekocode on 16/8/6.
*/
public class PolygonizationFilter extends CameraFilter {
private int program;

public PolygonizationFilter(Context context) {
super(context);

// Build shaders
program = MyGLUtils.buildProgram(context, R.raw.vertext, R.raw.polygonization);
}

@Override
public void draw(int textureId, int gwidth, int gheight) {
defaultDraw(program, textureId, gwidth, gheight);
}
}
12 changes: 12 additions & 0 deletions app/src/main/res/menu/filter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,16 @@
android:title="MoneyFilter"
app:showAsAction="never"
/>

<item
android:id="@+id/filter18"
android:title="Cracked"
app:showAsAction="never"
/>

<item
android:id="@+id/filter19"
android:title="Polygonization"
app:showAsAction="never"
/>
</menu>
49 changes: 49 additions & 0 deletions app/src/main/res/raw/cracked.fsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#extension GL_OES_EGL_image_external : require
precision highp float;

uniform vec3 iResolution;
uniform float iGlobalTime;
uniform samplerExternalOES sTexture;
varying vec2 texCoord;

float rnd(vec2 s)
{
return 1.-2.*fract(sin(s.x*253.13+s.y*341.41)*589.19);
}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 p=(fragCoord.xy*2.-iResolution.xy)/iResolution.x;

vec2 v=vec2(1E3);
vec2 v2=vec2(1E4);
vec2 center=vec2(.1,-.5);
for(int c=0;c<30;c++)
{
float angle=floor(rnd(vec2(float(c),387.44))*16.)*3.1415*.4-.5;
float dist=pow(rnd(vec2(float(c),78.21)),2.)*.5;
vec2 vc=vec2(center.x+cos(angle)*dist+rnd(vec2(float(c),349.3))*7E-3,
center.y+sin(angle)*dist+rnd(vec2(float(c),912.7))*7E-3);
if(length(vc-p)<length(v-p))
{
v2=v;
v=vc;
}
else if(length(vc-p)<length(v2-p))
{
v2=vc;
}
}

float col=abs(length(dot(p-v,normalize(v-v2)))-length(dot(p-v2,normalize(v-v2))))+.002*length(p-center);
col=7E-4/col;
if(length(v-v2)<4E-3)col=0.;
// if(length(v-p)<4E-3)col=1E-6;
if(col<.3)col=0.;
vec4 tex=texture2D(sTexture,(fragCoord.xy)/iResolution.xy+rnd(v)*.02);
fragColor=col*vec4(vec3(1.-tex.xyz),1.)+(1.-col)*tex;
}

void main() {
mainImage(gl_FragColor, texCoord * iResolution.xy);
}
73 changes: 73 additions & 0 deletions app/src/main/res/raw/polygonization.fsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#extension GL_OES_EGL_image_external : require
precision highp float;

uniform vec3 iResolution;
uniform float iGlobalTime;
uniform samplerExternalOES sTexture;
varying vec2 texCoord;

vec2 hash2( vec2 p )
{
// procedural white noise
return fract(sin(vec2(dot(p,vec2(127.1,311.7)),dot(p,vec2(269.5,183.3))))*43758.5453);
}

vec2 voronoi( in vec2 x )
{
vec2 n = floor(x);
vec2 f = fract(x);

//----------------------------------
// regular voronoi
//----------------------------------
vec2 mg, mr;

float md = 8.0;
for( int j=-1; j<=1; j++ )
for( int i=-1; i<=1; i++ )
{
vec2 g = vec2(float(i),float(j));
vec2 o = hash2( n + g );
vec2 r = g + o - f;
float d = dot(r,r);

if( d<md )
{
md = d;
mr = r;
mg = g;
}
}

return mr;
}

vec3 VoronoiColor(float steps, vec2 p, vec2 uv)
{
vec2 c = voronoi( steps*p );

vec2 uv1 = uv;
uv1.x += c.x/steps;
uv1.y += c.y/steps * iResolution.x/iResolution.y;

return texture2D(sTexture, vec2(uv1.x, uv1.y)).xyz;
}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 p = fragCoord.xy/iResolution.xx;
vec2 uv = fragCoord.xy / iResolution.xy;

vec3 color = vec3(0.0,0.0,0.0);
for (float i=0.0; i<4.0; i+=1.0)
{
float steps = 30.0*pow(2.0,i);
color += VoronoiColor(steps, p, uv);
}

fragColor = vec4(color*0.25,1.0);
}

void main() {
mainImage(gl_FragColor, texCoord * iResolution.xy);
}
Binary file added art/18.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added art/19.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 55c1cf8

Please sign in to comment.