Skip to content

Commit

Permalink
refactor snow filter
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroSG94 committed Sep 26, 2023
1 parent cc134a0 commit a5a0cf9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,10 @@ public class SnowFilterRender extends BaseFilterRender {
private int uSTMatrixHandle = -1;
private int uSamplerHandle = -1;
private int uTimeHandle = -1;
private int uSnowHandle = -1;
private int uResolutionHandle = -1;

private long START_TIME = System.currentTimeMillis();
private TextureLoader textureLoader = new TextureLoader();
private StreamObjectBase streamObjectBase;
private int[] snowTextureId = new int[] { -1 };
private final long START_TIME = System.currentTimeMillis();
private float speed = 1f;

public SnowFilterRender() {
squareVertex = ByteBuffer.allocateDirect(squareVertexDataFilter.length * FLOAT_SIZE_BYTES)
Expand All @@ -85,11 +83,7 @@ protected void initGlFilter(Context context) {
uSTMatrixHandle = GLES20.glGetUniformLocation(program, "uSTMatrix");
uSamplerHandle = GLES20.glGetUniformLocation(program, "uSampler");
uTimeHandle = GLES20.glGetUniformLocation(program, "uTime");
uSnowHandle = GLES20.glGetUniformLocation(program, "uSnow");
Bitmap snowBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.snow_flakes);
streamObjectBase = new ImageStreamObject();
((ImageStreamObject) streamObjectBase).load(snowBitmap);
snowTextureId = textureLoader.load(streamObjectBase.getBitmaps());
uResolutionHandle = GLES20.glGetUniformLocation(program, "uResolution");
}

@Override
Expand All @@ -108,28 +102,24 @@ protected void drawFilter() {

GLES20.glUniformMatrix4fv(uMVPMatrixHandle, 1, false, MVPMatrix, 0);
GLES20.glUniformMatrix4fv(uSTMatrixHandle, 1, false, STMatrix, 0);
GLES20.glUniform2f(uResolutionHandle, getWidth(), getHeight());
float time = ((float) (System.currentTimeMillis() - START_TIME)) / 1000f;
if (time >= 2) START_TIME += 2000;
GLES20.glUniform1f(uTimeHandle, time);
GLES20.glUniform1f(uTimeHandle, time * speed);

GLES20.glUniform1i(uSamplerHandle, 4);
GLES20.glActiveTexture(GLES20.GL_TEXTURE4);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, previousTexId);

GLES20.glUniform1i(uSnowHandle, 5);
GLES20.glActiveTexture(GLES20.GL_TEXTURE5);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, snowTextureId[0]);
}

@Override
public void release() {
GLES20.glDeleteProgram(program);
releaseTextureId();
if (streamObjectBase != null) streamObjectBase.recycle();
}

private void releaseTextureId() {
if (snowTextureId != null) GLES20.glDeleteTextures(1, snowTextureId, 0);
snowTextureId = new int[] { -1 };
/**
* @param value fall speed, default 1.0f
*/
public void setSpeed(float value) {
this.speed = value;
}
}
Binary file removed encoder/src/main/res/drawable/snow_flakes.png
Binary file not shown.
63 changes: 32 additions & 31 deletions encoder/src/main/res/raw/snow_fragment.glsl
Original file line number Diff line number Diff line change
@@ -1,47 +1,48 @@
precision mediump float;

uniform sampler2D uSampler;
uniform sampler2D uSnow;
uniform vec2 uResolution;
uniform float uTime;

varying vec2 vTextureCoord;

/*
Returns a texel of snowflake.
*/
vec4 getSnow(vec2 pos) {
// Get a texel of the noise image.
vec3 texel;
if (pos.x < 0.0 || pos.x > 1.0 || pos.y < 0.0 || pos.y > 1.0) {
texel = vec3(0.0, 0.0, 0.0);
} else {
texel = texture2D(uSnow, pos).rgb;
#define pi 3.1415926

vec2 hash(vec2 p) {
p = vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3)));
return fract(sin(p) * 18.5453);
}

float simplegridnoise(vec2 v) {
float s = 1.0 / 256.;
vec2 fl = floor(v), fr = fract(v);
float mindist = 1e9;
for(int y = -1; y <= 1; y++) {
for(int x = -1; x <= 1; x++) {
vec2 offset = vec2(x, y);
vec2 pos = 0.5 + 0.5 * cos(2.0 * pi * (uTime * 0.1 + hash(fl+offset)) + vec2(0, 1.6));
mindist = min(mindist, length(pos+offset -fr));
}
}
// Only use extremely bright values.
texel = smoothstep(0.6, 1.0, texel);
// Okay how can this give a transparent rgba value?
return vec4(texel, 1.0);
return mindist;
}

/*
Provides a 2D vector with which to warp the sampling location
of the snow_flakes texture.
*/
vec2 warpSpeed(float time, float gravity, vec2 pos) {
// Do some things to stretch out the timescale based on 2D position and actual time.
return vec2(-time * 5.55 + sin(pos.x * 10.0 * sin(time * 0.2)) * 0.4,
time * gravity + sin(pos.y * 10.0 * sin(time * 0.4)) * 0.4);
float blobnoise(vec2 v, float s) {
return pow(0.5 + 0.5 * cos(pi * clamp(simplegridnoise(v) * 2.0, 0.0, 1.0)), s);
}

vec4 getSnowField(vec2 pos) {
// Just some not-so-magic inversely related values.
// That's all they are.
return getSnow(pos - 0.75 + uTime * 0.25) +
getSnow(pos - 0.5 + uTime * 0.25) +
getSnow(pos - 0.25 + uTime * 0.25) +
getSnow(pos - 0.0 + uTime * 0.25);
float fractalblobnoise(vec2 v, float s) {
float val = 0.0;
const float n = 4.0;
for(float i = 0.0; i < n; i++) {
val += pow(0.5, i + 1.0) * blobnoise(exp2(i) * v + vec2(0, uTime), s);
}
return val;
}

void main() {
gl_FragColor = max(texture2D(uSampler, vTextureCoord), getSnowField(vTextureCoord));
vec2 r = vec2(1.0, uResolution.y / uResolution.x);
vec2 uv = vTextureCoord.xy / uResolution.xy;
float val = fractalblobnoise(r * vTextureCoord * 20.0, 5.0); //more snowflakes r * uv * 40.0, 1.25
gl_FragColor = mix(texture2D(uSampler, vTextureCoord), vec4(1.0), vec4(val));
}

0 comments on commit a5a0cf9

Please sign in to comment.