You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/webgl/material.js
+77-23Lines changed: 77 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -191,12 +191,45 @@ p5.prototype.loadShader = function (
191
191
* The second parameter, `fragSrc`, sets the fragment shader. It’s a string
192
192
* that contains the fragment shader program written in GLSL.
193
193
*
194
+
* A shader can optionally describe *hooks,* which are functions in GLSL that
195
+
* users may choose to provide to customize the behavior of the shader using the
196
+
* <a href="#/p5.Shader/modify">`modify()`</a> method of `p5.Shader`. These are added by
197
+
* describing the hooks in a third parameter, `options`, and referencing the hooks in
198
+
* your `vertSrc` or `fragSrc`. Hooks for the vertex or fragment shader are described under
199
+
* the `vertex` and `fragment` keys of `options`. Each one is an object. where each key is
200
+
* the type and name of a hook function, and each value is a string with the
201
+
* parameter list and default implementation of the hook. For example, to let users
202
+
* optionally run code at the start of the vertex shader, the options object could
203
+
* include:
204
+
*
205
+
* ```js
206
+
* {
207
+
* vertex: {
208
+
* 'void beforeVertex': '() {}'
209
+
* }
210
+
* }
211
+
* ```
212
+
*
213
+
* Then, in your vertex shader source, you can run a hook by calling a function
214
+
* with the same name prefixed by `HOOK_`:
215
+
*
216
+
* ```glsl
217
+
* void main() {
218
+
* HOOK_beforeVertex();
219
+
* // Add the rest ofy our shader code here!
220
+
* }
221
+
* ```
222
+
*
194
223
* Note: Only filter shaders can be used in 2D mode. All shaders can be used
195
224
* in WebGL mode.
196
225
*
197
226
* @method createShader
198
227
* @param {String} vertSrc source code for the vertex shader.
199
228
* @param {String} fragSrc source code for the fragment shader.
229
+
* @param {Object} [options] An optional object describing how this shader can
230
+
* be augmented with hooks. It can include:
231
+
* - `vertex`: An object describing the available vertex shader hooks.
232
+
* - `fragment`: An object describing the available frament shader hooks.
200
233
* @returns {p5.Shader} new shader object created from the
201
234
* vertex and fragment shaders.
202
235
*
@@ -785,10 +818,26 @@ p5.prototype.shader = function (s) {
785
818
* - `vec4 getVertexColor`: Update the color of each vertex. It takes in a `vec4 color` and must return a modified version.
786
819
* - `void afterVertex`: Called at the end of the vertex shader.
787
820
* - `void beforeFragment`: Called at the start of the fragment shader.
788
-
* - `Inputs getPixelInputs`: Update the per-pixel inputs of the material. It takes in an `Inputs` struct, which includes `vec3 normal`, `vec2 texCoord`, `vec3 ambientColor`, `vec4 color`, `vec3 ambientMaterial`, `vec3 specularMaterial`, `vec3 emissiveMaterial`, `float shininess`, and `vec3 ambientLight`. The struct can be modified and returned.
821
+
* - `Inputs getPixelInputs`: Update the per-pixel inputs of the material. It takes in an `Inputs` struct, which includes:
822
+
* - `vec3 normal`, the direction pointing out of the surface
823
+
* - `vec2 texCoord`, a vector where `x` and `y` are between 0 and 1 describing the spot on a texture the pixel is mapped to, as a fraction of the texture size
824
+
* - `vec3 ambientLight`, the ambient light color on the vertex
825
+
* - `vec4 color`, the base material color of the pixel
826
+
* - `vec3 ambientMaterial`, the color of the pixel when affected by ambient light
827
+
* - `vec3 specularMaterial`, the color of the pixel when reflecting specular highlights
828
+
* - `vec3 emissiveMaterial`, the light color emitted by the pixel
829
+
* - `float shininess`, a number representing how sharp specular reflections should be
830
+
The struct can be modified and returned.
789
831
* - `vec4 combineColors`: Take in a `ColorComponents` struct containing all the different components of light, and combining them into
790
-
* a single final color. The struct contains `vec3 baseColor`, `float opacity`, `vec3 ambientColor`, `vec3 specularColor`,
791
-
* `vec3 diffuse`, `vec3 ambientLight`, `vec3 specular`, and `vec3 emissive`.
832
+
* a single final color. The struct contains:
833
+
* - `vec3 baseColor`, the base color of the pixel
834
+
* - `float opacity`, the opacity between 0 and 1 that it should be drawn at
835
+
* - `vec3 ambientColor`, the color of the pixel when affected by ambient light
836
+
* - `vec3 specularColor`, the color of the pixel when affected by specular reflections
837
+
* - `vec3 diffuse`, the amount of diffused light hitting the pixel
838
+
* - `vec3 ambient`, the amount of ambient light hitting the pixel
839
+
* - `vec3 specular`, the amount of specular reflection hitting the pixel
840
+
* - `vec3 emissive`, the amount of light emitted by the pixel
792
841
* - `vec4 getFinalColor`: Update the final color after mixing. It takes in a `vec4 color` and must return a modified version.
793
842
* - `void afterFragment`: Called at the end of the fragment shader.
794
843
*
@@ -834,18 +883,21 @@ p5.prototype.shader = function (s) {
834
883
* function setup() {
835
884
* createCanvas(200, 200, WEBGL);
836
885
* myShader = materialShader().modify({
837
-
* vertexDeclarations: 'out vec2 myUV;',
838
-
* fragmentDeclarations: 'in vec2 myUV;',
839
-
* 'vec2 getUV': `(vec2 uv) {
840
-
* // Store and return
841
-
* myUV = uv;
842
-
* return myUV;
843
-
* }`,
844
886
* 'Inputs getPixelInputs': `(Inputs inputs) {
845
887
* vec3 newNormal = inputs.normal;
846
888
* // Simple bump mapping: adjust the normal based on position
* - `vec4 getVertexColor`: Update the color of each vertex. It takes in a `vec4 color` and must return a modified version.
1031
1083
* - `void afterVertex`: Called at the end of the vertex shader.
1032
1084
* - `void beforeFragment`: Called at the start of the fragment shader.
1033
-
* - `Inputs getPixelInputs`: Update the inputs to the shader. It takes in a struct `Inputs inputs`, which includes `vec4 color`, `vec2 tangent`, `vec2 center`, `vec2 position`, and `float strokeWeight`.
1085
+
* - `Inputs getPixelInputs`: Update the inputs to the shader. It takes in a struct `Inputs inputs`, which includes:
1086
+
* - `vec4 color`, the color of the stroke
1087
+
* - `vec2 tangent`, the direction of the stroke in screen space
1088
+
* - `vec2 center`, the coordinate of the center of the stroke in screen space p5.js pixels
1089
+
* - `vec2 position`, the coordinate of the current pixel in screen space p5.js pixels
1090
+
* - `float strokeWeight`, the thickness of the stroke in p5.js pixels
1034
1091
* - `bool shouldDiscard`: Caps and joins are made by discarded pixels in the fragment shader to carve away unwanted areas. Use this to change this logic. It takes in a `bool willDiscard` and must return a modified version.
1035
1092
* - `vec4 getFinalColor`: Update the final color after mixing. It takes in a `vec4 color` and must return a modified version.
1036
1093
* - `void afterFragment`: Called at the end of the fragment shader.
0 commit comments