Created
June 1, 2019 11:58
-
-
Save KrabCode/752728d80ad116e8a1e808efd8e7f6c4 to your computer and use it in GitHub Desktop.
2D water ripple shader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import processing.core.PApplet; | |
| import processing.core.PGraphics; | |
| import processing.opengl.PShader; | |
| public class MainApp extends PApplet { | |
| public static void main(String[] args) { | |
| PApplet.main("MainApp"); | |
| } | |
| PShader waterShader; | |
| PGraphics[] waterBuffers; | |
| int waterBufferWidth = 800; | |
| int waterBufferHeight = 800; | |
| float damping = .999f; | |
| public void settings() { | |
| size(800,800,P2D); | |
| smooth(8); | |
| } | |
| public void setup(){ | |
| setupWater(); | |
| } | |
| public void draw(){ | |
| updateWater(); | |
| image(waterBuffers[1].get(), 0, 0, width, height); | |
| } | |
| private void setupWater() { | |
| waterShader = loadShader("waterFrag.glsl"); | |
| waterShader.set("resolution", (float)waterBufferWidth, (float)waterBufferHeight); | |
| waterShader.set("damping", damping); | |
| waterBuffers = new PGraphics[2]; | |
| for(int i = 0; i < waterBuffers.length; i++){ | |
| waterBuffers[i] = createGraphics(waterBufferWidth, waterBufferHeight, P2D); | |
| waterBuffers[i].beginDraw(); | |
| waterBuffers[i].background(0); | |
| waterBuffers[i].endDraw(); | |
| } | |
| } | |
| private void updateWater() { | |
| updateBuffer1(); | |
| swapBuffers(); | |
| } | |
| private void updateBuffer1() { | |
| waterShader.set("buffer0", waterBuffers[0]); | |
| waterShader.set("buffer1", waterBuffers[1]); | |
| waterBuffers[1].beginDraw(); | |
| waterBuffers[1].shader(waterShader); | |
| waterBuffers[1].noStroke(); | |
| waterBuffers[1].fill(0); | |
| waterBuffers[1].rect(0,0,waterBufferWidth,waterBufferHeight); | |
| waterBuffers[1].resetShader(); | |
| waterBuffers[1].stroke(255); | |
| waterBuffers[1].noFill(); | |
| waterBuffers[1].ellipse(mouseX, mouseY, 50,50); | |
| waterBuffers[1].endDraw(); | |
| } | |
| private void swapBuffers() { | |
| PGraphics temp = waterBuffers[1]; | |
| waterBuffers[1] = waterBuffers[0]; | |
| waterBuffers[0] = temp; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifdef GL_ES | |
| precision mediump float; | |
| #endif | |
| uniform vec2 resolution; | |
| uniform sampler2D buffer0; | |
| uniform sampler2D buffer1; | |
| uniform float damping; | |
| #define PI 3.14159265359 | |
| float get(sampler2D sampler, vec2 uv, vec2 off){ | |
| return texture2D(sampler, vec2(uv.x+off.x, uv.y+off.y)).x; | |
| } | |
| void main(void) { | |
| vec2 uv = gl_FragCoord.xy / resolution.xy; | |
| float pixelSizeX = 1 / resolution.x; | |
| float pixelSizeY = 1 / resolution.y; | |
| float color = (get(buffer0, uv, vec2(-pixelSizeX,0)) + | |
| get(buffer0, uv, vec2(pixelSizeX,0)) + | |
| get(buffer0, uv, vec2(0,pixelSizeY)) + | |
| get(buffer0, uv, vec2(0,-pixelSizeY))) / 2 - get(buffer1, uv, vec2(0.)); | |
| color *= damping; | |
| gl_FragColor = vec4(vec3(color),1.); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment