Skip to content

Commit a76f83e

Browse files
authored
Merge pull request #5265 from JetStarBlues/addResetShaderExample
Add an example for resetShader()
2 parents f6248d3 + 316913c commit a76f83e

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

src/webgl/material.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,79 @@ p5.prototype.shader = function(s) {
286286
*
287287
* @method resetShader
288288
* @chainable
289+
* @example
290+
* <div>
291+
* <code>
292+
* // This variable will hold our shader object
293+
* let shaderProgram;
294+
*
295+
* // This variable will hold our vertex shader source code
296+
* let vertSrc = `
297+
* attribute vec3 aPosition;
298+
* attribute vec2 aTexCoord;
299+
* uniform mat4 uProjectionMatrix;
300+
* uniform mat4 uModelViewMatrix;
301+
* varying vec2 vTexCoord;
302+
*
303+
* void main() {
304+
* vTexCoord = aTexCoord;
305+
* vec4 position = vec4(aPosition, 1.0);
306+
* gl_Position = uProjectionMatrix * uModelViewMatrix * position;
307+
* }
308+
* `;
309+
*
310+
* // This variable will hold our fragment shader source code
311+
* let fragSrc = `
312+
* precision mediump float;
313+
*
314+
* varying vec2 vTexCoord;
315+
*
316+
* void main() {
317+
* vec2 uv = vTexCoord;
318+
* vec3 color = vec3(uv.x, uv.y, min(uv.x + uv.y, 1.0));
319+
* gl_FragColor = vec4(color, 1.0);
320+
* }
321+
* `;
322+
*
323+
* function setup() {
324+
* // Shaders require WEBGL mode to work
325+
* createCanvas(100, 100, WEBGL);
326+
*
327+
* // Create our shader
328+
* shaderProgram = createShader(vertSrc, fragSrc);
329+
* }
330+
*
331+
* // prettier-ignore
332+
* function draw() {
333+
* // Clear the scene
334+
* background(200);
335+
*
336+
* // Draw a box using our shader
337+
* // shader() sets the active shader with our shader
338+
* shader(shaderProgram);
339+
* push();
340+
* translate(-width / 4, 0, 0);
341+
* rotateX(millis() * 0.00025);
342+
* rotateY(millis() * 0.0005);
343+
* box(width / 4);
344+
* pop();
345+
*
346+
* // Draw a box using the default fill shader
347+
* // resetShader() restores the default fill shader
348+
* resetShader();
349+
* fill(255, 0, 0);
350+
* push();
351+
* translate(width / 4, 0, 0);
352+
* rotateX(millis() * 0.00025);
353+
* rotateY(millis() * 0.0005);
354+
* box(width / 4);
355+
* pop();
356+
* }
357+
* </code>
358+
* </div>
359+
* @alt
360+
* Two rotating cubes. The left one is painted using a custom (user-defined) shader,
361+
* while the right one is painted using the default fill shader.
289362
*/
290363
p5.prototype.resetShader = function() {
291364
this._renderer.userFillShader = this._renderer.userStrokeShader = null;

0 commit comments

Comments
 (0)