@@ -1272,23 +1272,14 @@ p5.prototype.colorShader = function() {
1272
1272
* function setup() {
1273
1273
* createCanvas(200, 200, WEBGL);
1274
1274
* myShader = strokeShader().modify({
1275
- * fragmentDeclarations: `
1276
- * vec2 myCenter;
1277
- * vec2 myPosition;
1278
- * `,
1279
1275
* 'Inputs getPixelInputs': `(Inputs inputs) {
1280
- * // Store a copy, then return unchanged
1281
- * myCenter = inputs.center;
1282
- * myPosition = inputs.position;
1283
- * return inputs;
1284
- * }`,
1285
- * 'vec4 getFinalColor': `(vec4 c) {
1286
1276
* float opacity = 1.0 - smoothstep(
1287
1277
* 0.0,
1288
1278
* 15.0,
1289
- * length(myPosition - myCenter )
1279
+ * length(inputs.position - inputs.center )
1290
1280
* );
1291
- * return c * opacity;
1281
+ * inputs.color *= opacity;
1282
+ * return inputs;
1292
1283
* }`
1293
1284
* });
1294
1285
* }
@@ -1306,6 +1297,101 @@ p5.prototype.colorShader = function() {
1306
1297
* }
1307
1298
* </code>
1308
1299
* </div>
1300
+ *
1301
+ * @example
1302
+ * <div modernizr='webgl'>
1303
+ * <code>
1304
+ * let myShader;
1305
+ *
1306
+ * function setup() {
1307
+ * createCanvas(200, 200, WEBGL);
1308
+ * myShader = strokeShader().modify({
1309
+ * declarations: `
1310
+ * uniform float time;
1311
+ * vec3 myPosition;
1312
+ * `,
1313
+ * 'vec3 getWorldPosition': `(vec3 pos) {
1314
+ * myPosition = pos;
1315
+ * return pos;
1316
+ * }`,
1317
+ * 'float getStrokeWeight': `(float w) {
1318
+ * // Add a somewhat random offset to the weight
1319
+ * // that varies based on position and time
1320
+ * float scale = 0.8 + 0.2*sin(10.0 * sin(
1321
+ * floor(time/250.) +
1322
+ * myPosition.x*0.01 +
1323
+ * myPosition.y*0.01
1324
+ * ));
1325
+ * return w * scale;
1326
+ * }`
1327
+ * });
1328
+ * }
1329
+ *
1330
+ * function draw() {
1331
+ * background(255);
1332
+ * shader(myShader);
1333
+ * myShader.setUniform('time', millis());
1334
+ * strokeWeight(10);
1335
+ * beginShape();
1336
+ * for (let i = 0; i <= 50; i++) {
1337
+ * let r = map(i, 0, 50, 0, width/3);
1338
+ * let x = r*cos(i*0.2);
1339
+ * let y = r*sin(i*0.2);
1340
+ * vertex(x, y);
1341
+ * }
1342
+ * endShape();
1343
+ * }
1344
+ * </code>
1345
+ * </div>
1346
+ *
1347
+ * @example
1348
+ * <div modernizr='webgl'>
1349
+ * <code>
1350
+ * let myShader;
1351
+ *
1352
+ * function setup() {
1353
+ * createCanvas(200, 200, WEBGL);
1354
+ * myShader = strokeShader().modify({
1355
+ * declarations: `
1356
+ * float random(vec2 p) {
1357
+ * vec3 p3 = fract(vec3(p.xyx) * .1031);
1358
+ * p3 += dot(p3, p3.yzx + 33.33);
1359
+ * return fract((p3.x + p3.y) * p3.z);
1360
+ * }
1361
+ * `,
1362
+ * 'Inputs getPixelInputs': `(Inputs inputs) {
1363
+ * // Replace alpha in the color with dithering by
1364
+ * // randomly setting pixel colors to 0 based on opacity
1365
+ * float a = inputs.color.a;
1366
+ * inputs.color.a = 1.0;
1367
+ * inputs.color *= random(inputs.position.xy) > a ? 0.0 : 1.0;
1368
+ * return inputs;
1369
+ * }`
1370
+ * });
1371
+ * }
1372
+ *
1373
+ * function draw() {
1374
+ * background(255);
1375
+ * shader(myShader);
1376
+ * myShader.setUniform('time', millis());
1377
+ * strokeWeight(10);
1378
+ * beginShape();
1379
+ * for (let i = 0; i <= 50; i++) {
1380
+ * stroke(
1381
+ * 0,
1382
+ * 255
1383
+ * * map(i, 0, 20, 0, 1, true)
1384
+ * * map(i, 30, 50, 1, 0, true)
1385
+ * );
1386
+ * vertex(
1387
+ * map(i, 0, 50, -1, 1) * width/3,
1388
+ * 50 * sin(i/10 + frameCount/100)
1389
+ * );
1390
+ * }
1391
+ * endShape();
1392
+ * }
1393
+ * </code>
1394
+ * </div>
1309
1395
*/
1310
1396
p5 . prototype . strokeShader = function ( ) {
1311
1397
this . _assert3d ( 'strokeShader' ) ;
0 commit comments