Skip to content

Instantly share code, notes, and snippets.

@AVGP
Created April 17, 2016 17:37
Show Gist options
  • Save AVGP/471b963335c1e527b51df1ceb37513a6 to your computer and use it in GitHub Desktop.
Save AVGP/471b963335c1e527b51df1ceb37513a6 to your computer and use it in GitHub Desktop.
A few nice shader snippets for Shaderpad

Basics

simple red horizontal gradient

precision highp float;
varying highp vec2 vTextureCoord;

uniform sampler2D uSampler;

void main(void) {
  gl_FragColor = vec4(vTextureCoord.x, 0.0, 0.0, 1.0);
}

simple green vertical gradient

precision highp float;
varying highp vec2 vTextureCoord;

uniform sampler2D uSampler;

void main(void) {
  gl_FragColor = vec4(0.0, vTextureCoord.y, 0.0, 1.0);
}

pretty colors

precision highp float;
varying highp vec2 vTextureCoord;

uniform sampler2D uSampler;

void main(void) {
  gl_FragColor = vec4(vTextureCoord.x, vTextureCoord.y, 1.0 - (vTextureCoord.x * vTextureCoord.y), 1.0);
}

Textures

scale texture

precision highp float;
varying highp vec2 vTextureCoord;
uniform sampler2D uSampler;
void main(void) {
  vec4 wtf = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
  gl_FragColor = wtf * 2.0;
}

change colours / transparency

precision highp float;
varying highp vec2 vTextureCoord;
uniform sampler2D uSampler;
void main(void) {
  vec4 wtf = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
  gl_FragColor = wtf.xyzw;
}

offset / scaling

attribute vec2 aVertexPosition;
attribute vec2 aTextureCoord;

varying highp vec2 vTextureCoord;

uniform float uA;
uniform float uB;
uniform float uC;

void main(void) {
  vec2 offset = vec2(uB, uC) - 0.5;
  vTextureCoord = aTextureCoord;
  gl_Position = vec4(2.0 * uA * (aVertexPosition + offset), 0.0, 1.0);
}

grayscale

precision highp float;
varying highp vec2 vTextureCoord;

uniform sampler2D uSampler;

void main(void) {
  vec3 color = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t)).rgb;
  float val = (color.r + color.g + color.b) / 3.0;

  gl_FragColor = vec4(val, val, val, 1.0);
}

fill dark / bright areas

precision highp float;
varying highp vec2 vTextureCoord;

uniform sampler2D uSampler;

void main(void) {
  vec3 color = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t)).rgb;
  float val = (color.r + color.g + color.b) / 3.0;

  if(val < 0.5) {
    gl_FragColor = vec4(val, val, val, 1.0);
  } else {
    gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
  }
}

rotate

attribute vec2 aVertexPosition;
attribute vec2 aTextureCoord;

varying highp vec2 vTextureCoord;

uniform float uA;

mat2 rotate(float radians) {
  radians *= 2.0 * 3.1415;
  return mat2( cos(radians), sin(radians),
              -sin(radians), cos(radians));
}

void main(void) {
  vTextureCoord = aTextureCoord;
  gl_Position = vec4(aVertexPosition * rotate(uA), 0.0, 1.0);
}

gamma

precision highp float;
varying highp vec2 vTextureCoord;

uniform sampler2D uSampler;
uniform float uA;

void main(void) {
  vec3 color = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t)).rgb;
  gl_FragColor = vec4(pow(color, vec3(uA * 4.0)), 1.0);
}

waves

precision highp float;
varying highp vec2 vTextureCoord;

uniform sampler2D uSampler;

uniform float uA;
uniform float uB;

void main(void) {
  vec2 uv = vec2(vTextureCoord.s, vTextureCoord.t);
  uv.y =  uA * sin(10.0 * uB * uv.y);
  gl_FragColor = texture2D(uSampler, uv);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment