Skip to content

Instantly share code, notes, and snippets.

@moritzsalla
Last active May 25, 2022 20:37
Show Gist options
  • Save moritzsalla/f5736ad1db2e5b9bc0550acc43f60570 to your computer and use it in GitHub Desktop.
Save moritzsalla/f5736ad1db2e5b9bc0550acc43f60570 to your computer and use it in GitHub Desktop.
Contour tracer shader
// contour isoline cartography shader
export const frag = `
  #extension GL_OES_standard_derivatives : enable
  #ifdef GL_ES
   precision highp float;
  #endif

  varying vec2 var_vertTexCoord;

  uniform sampler2D u_texture;
  uniform sampler2D u_buffer0;

  uniform vec2 u_direction;
  uniform vec2 u_resolution;
  uniform float u_time;

  vec4 rgb(float col) {
    float c = col / 255.;
    return vec4(c, c, c, 1.);
  }

  vec4 rgb(float r, float g, float b) {
    return vec4(r/255., g/255., b/255., 1.);
  }

  void main(void) {
    vec2 uv = gl_FragCoord.xy/u_resolution.xy;
    vec4 tex = texture2D(u_texture, uv);
    vec4 color = tex;

    vec4 contour = smoothstep(color-color, fwidth(color) * 100., abs(sin(10.* 4.*color - 1.))) - color;
    color += contour;

    color += rgb(78.);


    // --- buffer ---

    vec4 buffer = texture2D(u_buffer0, uv);
    color *= buffer;

    gl_FragColor = color;
  }
`;

WIP: Painting with buffers

#version 150

uniform vec2 resolution;
uniform vec2 mouse;

uniform sampler2D prevFrame;
uniform sampler2D prevPass;

in VertexData
{
    vec4 v_position;
    vec3 v_normal;
    vec2 v_texcoord;
} inData;

out vec4 fragColor;

#define FALLOFF 0.96

void main(void)
{
    vec2 st = inData.v_texcoord;
    
    float pct = distance(st,vec2(mouse.x, 1-mouse.y));
    
    vec4 cout = vec4(vec3(pct), 1.0);
    
    // mix in prevFrame texture
    vec4 p = texture(prevFrame, st);
    cout += p * FALLOFF;
    
    fragColor = cout;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment