Skip to content

Instantly share code, notes, and snippets.

Created June 3, 2012 13:32
Show Gist options
  • Save anonymous/2863517 to your computer and use it in GitHub Desktop.
Save anonymous/2863517 to your computer and use it in GitHub Desktop.
HertBeat Wallpaper
package au.com.dealsmap.lwp.water_ripple_pro;
import android.opengl.GLES20;
import rajawali.materials.SimpleMaterial;
public class CustomMaterial extends SimpleMaterial {
protected static final String mCustomFShader =
"precision mediump float;\n" +
"uniform float uTime;\n" +
"uniform float uColor;\n" +
"varying vec2 vTextureCoord;\n" +
"void main() {\n" +
" vec2 res = vec2(490.0,384.0);\n" +
" vec2 p = (2.0*gl_FragCoord.xy-res)/res.y;\n" +
// animate
"float tt = mod(uTime,2.0)/2.0;\n" +
"float ss = pow(tt,.2)*0.5 + 0.5;\n" +
"ss -= ss*0.2*sin(tt*6.2831*5.0)*exp(-tt*6.0);\n" +
"p *= vec2(0.5,1.5) + ss*vec2(0.5,-0.5);\n" +
"float a = atan(p.x,p.y)/3.141593;\n" +
"float r = length(p);\n" +
// shape
"float h = abs(a);\n" +
"float d = (13.0*h - 22.0*h*h + 10.0*h*h*h)/(6.0-5.0*h);\n" +
// color
"float f = step(r,d) * pow(1.0-r/d,0.25);\n" +
"float g = sin(uColor);\n" +
" gl_FragColor = vec4(f,g,0.0,1.0);\n" +
"}\n";
protected int muTimeHandle;
protected int muColorHandle;
public CustomMaterial() {
super(mVShader, mCustomFShader);
setShaders();
}
@Override
public void setShaders(String vertexShader, String fragmentShader)
{
super.setShaders(vertexShader, fragmentShader);
muTimeHandle = GLES20.glGetUniformLocation(mProgram, "uTime");
muColorHandle = GLES20.glGetUniformLocation(mProgram, "uColor");
if(muTimeHandle == -1) {
throw new RuntimeException("Could not get uniform location for uTime");
}
}
public void setTime(float time) {
GLES20.glUniform1f(muTimeHandle, time);
}
public void setTouch(float color) {
// TODO Auto-generated method stub
GLES20.glUniform1f(muColorHandle, color);
}
}
package au.com.dealsmap.lwp.water_ripple_pro;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import rajawali.Camera2D;
import rajawali.primitives.Plane;
import rajawali.renderer.RajawaliRenderer;
import rajawali.util.RajLog;
import android.content.Context;
import android.view.MotionEvent;
public class MyWallpaperRenderer extends RajawaliRenderer {
private float mTime;
private CustomMaterial mCustomMaterial;
public MyWallpaperRenderer(Context context) {
super(context);
setCamera(new Camera2D());
setFrameRate(60);
mTime = 0;
}
protected void initScene() {
mCustomMaterial = new CustomMaterial();
Plane plane = new Plane(1, 1, 1, 1, 1);
plane.setMaterial(mCustomMaterial);
addChild(plane);
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
super.onSurfaceCreated(gl, config);
}
public void onDrawFrame(GL10 glUnused) {
super.onDrawFrame(glUnused);
mTime += .2f;
mCustomMaterial.setTime(mTime);
}
@Override
public void onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
super.onTouchEvent(event);
mCustomMaterial.setTouch(event.getX());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment