1
+ ---
2
+ title : learn
3
+ slug : learn/
4
+ ---
5
+
6
+ <div id =" learn-page" >
7
+
8
+ {{> sidebar }}
9
+
10
+ <div class =" column-span" >
11
+
12
+ <main >
13
+ <h1 >Introduction to Shaders</h1 >
14
+
15
+ <p >Shaders are programs that run on the GPU that can do some incredible things. Shaders were first created to
16
+ address the challenges that come along with simulating light and shadow on polygons, but today they are used for
17
+ a wide variety of effects. </p >
18
+
19
+
20
+ <p >Shaders don't need to be complicated! This document will try to outline the basics of shader programming and
21
+ point you towards other resources. </p >
22
+
23
+ <p >Shaders are separate programs that run on the graphics card, and as a result, they have a very different syntax
24
+ and structure than what we are familiar with in p5.js. They are written in a shader language, which has a syntax
25
+ similar to the programming language, C. They are also programs that run on a single pixel, and as a result, they
26
+ sometimes require you to think a little bit differently. </p >
27
+
28
+ <p >A shader program consists of two parts, a vertex shader and a fragment shader. The vertex shader affects where
29
+ the 3D geometry is drawn on the screen. </p >
30
+
31
+ https://github.com/aferriss/p5jsShaderExamples/blob/gh-pages/1_basics/1-1_red/basic.vert
32
+ <pre >
33
+
34
+ // this is an attribute sent to the shader by p5
35
+ // it contains all of our vertex position information
36
+ // it is a vec3, meaning it contains x, y, and z data
37
+ // attribute signals that this is a global variable sent by the sketch
38
+ // it is read only, meaning it cannot be changed directly (you can copy it though)
39
+ // attributes exist in vertex shaders only
40
+ attribute vec3 aPosition;
41
+
42
+ // all shaders have one main function
43
+ // the vertex shader requires there to be a vec4 output called gl_Position
44
+ void main() {
45
+
46
+ // copy the position data into a vec4, using 1.0 as the w component
47
+ vec4 positionVec4 = vec4(aPosition, 1.0);
48
+
49
+ // scale the rect by two, and move it to the center of the screen
50
+ // if we don't do this, it will appear with its bottom left corner in the center of the sketch
51
+ // try commenting this line out to see what happens
52
+ positionVec4.xy = positionVec4.xy * 2.0 - 1.0;
53
+
54
+ // send the vertex information on to the fragment shader
55
+ gl_Position = positionVec4;
56
+ }
57
+ </pre >
58
+
59
+ <p >The fragment shader, which you'll likely be working with the most, decides what color a pixel will be.</p >
60
+
61
+
62
+ https://github.com/aferriss/p5jsShaderExamples/blob/gh-pages/1_basics/1-1_red/basic.frag
63
+ <pre >
64
+ // webgl requires that the first line of the fragment shader specify the precision
65
+ // precision is dependent on device, but higher precision variables have more zeros
66
+ // sometimes you'll see bugs if you use lowp so stick to mediump or highp
67
+ precision mediump float;
68
+
69
+ // the fragment shader has one main function too
70
+ // this is kinda of like the draw() function in p5
71
+ // main outputs a variable called gl_FragColor which will contain all the colors of our shader
72
+ // the word void means that the function doesn't return a value
73
+ // this function is always called main()
74
+ void main() {
75
+
76
+ // lets just send the color red out
77
+ // colors in shaders go from 0.0 to 1.0
78
+ // glsl is very finicky about the decimal points
79
+ // gl_FragColor is a vec4 and is expecting red, green, blue, alpha
80
+ // the line below will make a solid red color for every pixel, with full alpha
81
+ vec4 redColor = vec4(1.0, 0.0, 0.0, 1.0);
82
+
83
+ // assign redColor to be output to the screen
84
+ gl_FragColor = redColor;
85
+
86
+ // how would you make a solid green screen?
87
+ // yellow?
88
+ // gray?
89
+ }
90
+ <pre >
91
+
92
+ </main >
93
+
94
+ {{> footer }}
95
+
96
+ </div > <!-- end column-span -->
97
+
98
+ {{> asterisk }}
99
+
100
+ </div ><!-- end id="get-started-page" -->
0 commit comments