Skip to content

Commit d50d0cb

Browse files
committed
Add more line examples
1 parent 420bac4 commit d50d0cb

File tree

3 files changed

+103
-15
lines changed

3 files changed

+103
-15
lines changed

src/webgl/material.js

Lines changed: 98 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,23 +1272,14 @@ p5.prototype.colorShader = function() {
12721272
* function setup() {
12731273
* createCanvas(200, 200, WEBGL);
12741274
* myShader = strokeShader().modify({
1275-
* fragmentDeclarations: `
1276-
* vec2 myCenter;
1277-
* vec2 myPosition;
1278-
* `,
12791275
* '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) {
12861276
* float opacity = 1.0 - smoothstep(
12871277
* 0.0,
12881278
* 15.0,
1289-
* length(myPosition - myCenter)
1279+
* length(inputs.position - inputs.center)
12901280
* );
1291-
* return c * opacity;
1281+
* inputs.color *= opacity;
1282+
* return inputs;
12921283
* }`
12931284
* });
12941285
* }
@@ -1306,6 +1297,101 @@ p5.prototype.colorShader = function() {
13061297
* }
13071298
* </code>
13081299
* </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>
13091395
*/
13101396
p5.prototype.strokeShader = function() {
13111397
this._assert3d('strokeShader');

src/webgl/shaders/line.frag

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ precision mediump int;
33
uniform vec4 uMaterialColor;
44
uniform int uStrokeCap;
55
uniform int uStrokeJoin;
6-
uniform float uStrokeWeight;
76

87
IN vec4 vColor;
98
IN vec2 vTangent;
109
IN vec2 vCenter;
1110
IN vec2 vPosition;
11+
IN float vStrokeWeight;
1212
IN float vMaxDist;
1313
IN float vCap;
1414
IN float vJoin;
@@ -34,7 +34,7 @@ void main() {
3434
inputs.tangent = vTangent;
3535
inputs.center = vCenter;
3636
inputs.position = vPosition;
37-
inputs.strokeWeight = uStrokeWeight;
37+
inputs.strokeWeight = vStrokeWeight;
3838
inputs = HOOK_getPixelInputs(inputs);
3939

4040
if (vCap > 0.) {
@@ -68,6 +68,6 @@ void main() {
6868
discard;
6969
}
7070
}
71-
OUT_COLOR = HOOK_getFinalColor(vec4(vColor.rgb, 1.) * vColor.a);
71+
OUT_COLOR = HOOK_getFinalColor(vec4(inputs.color.rgb, 1.) * inputs.color.a);
7272
HOOK_afterFragment();
7373
}

src/webgl/shaders/line.vert

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ OUT vec2 vPosition;
4444
OUT float vMaxDist;
4545
OUT float vCap;
4646
OUT float vJoin;
47+
OUT float vStrokeWeight;
4748

4849
vec2 lineIntersection(vec2 aPoint, vec2 aDir, vec2 bPoint, vec2 bDir) {
4950
// Rotate and translate so a starts at the origin and goes out to the right
@@ -81,6 +82,7 @@ void main() {
8182
vec4 posqIn = posp + uModelViewMatrix * vec4(aTangentIn, 0);
8283
vec4 posqOut = posp + uModelViewMatrix * vec4(aTangentOut, 0);
8384
float strokeWeight = HOOK_getStrokeWeight(uStrokeWeight);
85+
vStrokeWeight = strokeWeight;
8486

8587
float facingCamera = pow(
8688
// The word space tangent's z value is 0 if it's facing the camera

0 commit comments

Comments
 (0)