• Stars
    star
    750
  • Rank 60,494 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A collection of heavily commented WebGL shaders created with p5.js and GLSL

p5.js Shader Examples

A collection of heavily commented 2d shaders in p5.js. I tried to write shaders that might be useful for those learning GLSL and wanting to get into the basics of 2d image manipulation. For now these are only 2d / texture shaders.

Shader functions in p5

There are only 4 functions you need to know to work with shaders in p5.js. Everything else is just learning the shader language GLSL.

  1. loadShader('vertexShader.vert', 'fragmentShader.frag') loads your shaders from files and returns them as a p5 shader object in a variable. The file extensions don't really matter, you can call them .glsl or .shader or .vertex. What does matter is that the path to the files is correct. loadShader() should be called from within preload().

  2. createShader(vertString, fragString) loads your shaders from strings. I prefer not to work this way because you won't have any syntax highlighting on your shaders. You can call this method from within setup. The easiest way to write shaders as strings is probably to use javascripts template string syntax.

  3. shader(myShader) sets the currently active shader. You can also call this function on createGraphics() layers if you have them in your scene.

  4. setUniform('uniformName', dataGoingToShader) is the only method of the shader object in p5. You can use it to send data to your shaders from your p5 program. For the vec# data types, you send the data as an array. Here's how to send different types of data

// sending an int
myShader.setUniform('myInt', 2);

// sending a float
myShader.setUniform('myFloat', 0.1);

// sending a vec2
myShader.setUniform('myVec2', [-1, 13]);

// sending a vec3
myShader.setUniform('myVec3', [27, 80, 230]);

// sending a vec4
myShader.setUniform('myVec4', [mouseX, mouseY, mouseX/width, mouseY/height]);

Additionally, some of these examples use extra graphics layers for more complex effects. You may want to look into the createGraphics() function to learn how it works.

Basics

These shaders are about as stripped down as can be. They mostly just render colors to the screen. Start here if you've never seen or used a shader before.

  1. Red
  2. Gray
  3. Cyan
  4. Functions

Texture Coordinates

These shaders show how to manipulate texture coordinates in a variety of different way. They start out simple, and gradually become more complex.

  1. Basic
  2. Tiles
  3. Gradient
  4. Random
  5. Noise
  6. Checker

Uniforms

These shaders show how to send data to the shader in uniform variables. All examples after this section will use uniforms to talk to the shader.

  1. Mouse
  2. texture2D
  3. time

Image Effects

This section is a collection of shaders that manipulate images or video in some way. Some of them are simple examples like inverting colors, and others are more complicated effects like bloom. Most of these examples use the webcam.

  1. Invert
  2. RGB Split
  3. Sinewave Distortion
  4. Video Mirror
  5. Texcoord Stripes
  6. Pixelate / Mosaic
  7. Displacement Map
  8. RGB to HSB conversion
  9. Single Pass Blur
  10. Multi-Pass Blur
  11. Bloom
  12. Threshold
  13. Frame Differencing
  14. RGB to Grayscale conversion
  15. Convolution Kernel Effects (Blur, Sharpen, Emboss, Edge Detect)
  16. Video Feedback
  17. Texture Delay
  18. Discard
  19. Slitscan
  20. Fly's Eye Mosaic

Shapes

These shaders show how to create basic shapes just using math within the shader.

  1. Rectangle
  2. Circle
  3. Polygon

3d

These examples show how to use shaders with 3d geometry

  1. Box
  2. Vertex Displacement
  3. Vertex Displacement With Texture
  4. Matcap Shader

More Repositories

1

sparksl-shader-examples

A collection of shader examples in SparkAR's shader language (SparkSL)
JavaScript
175
star
2

openImageDownloader

Tutorial for making data sets from the Open Image Database
Python
39
star
3

shaderExamples

GLSL Shader examples for Processing
GLSL
37
star
4

spark-ar-lookat-example

How to use the Look AT api in Spark AR
JavaScript
26
star
5

p5Fbo

Framebuffers for p5.js
JavaScript
25
star
6

computational-photography

Workshop materials for the Computational Photography workshop at the Anderson ranch
HTML
22
star
7

genuary

JavaScript
17
star
8

AVFoundationVideoGrabberFastTexture

Objective-C++
8
star
9

p5Recorder

A light wrapper around the Media Recorder API for use with p5.js
JavaScript
6
star
10

ofxVision

Objective-C++
3
star
11

midiSproutViz

JavaScript
2
star
12

learningMetal

Swift
2
star
13

mnist3d

3d visualization of mnist digits via t-SNE
C++
2
star
14

ofxCanny

Realtime canny edge detector in openframeworks / glsl
GLSL
2
star
15

182

JavaScript
1
star
16

seams

Seam carving in open frameworks
C++
1
star
17

curses

1
star
18

ofxScribbler

C++
1
star
19

fastSketch

generating lots of drawings
C++
1
star
20

noiseFb

Three.js feedback loop
JavaScript
1
star
21

ofxAVFoundationGrabberFast

Objective-C++
1
star
22

deepdream

Python
1
star
23

formToMarkdown

Quick utility for converting google form csv to markdown lists
Python
1
star
24

nonBlockingIO

C++
1
star
25

tiledSaver

A processing sketch that will split a large sketch into tiles and save them out to a unique folder
Processing
1
star
26

compsToProjectFiles

An After Effects script that exports all compositions in a project to individual project files.
JavaScript
1
star