Skip to content

Commit 5e62309

Browse files
committed
use millis() not uniform in strands examples
1 parent 12b54c1 commit 5e62309

File tree

3 files changed

+34
-42
lines changed

3 files changed

+34
-42
lines changed

src/strands/p5.strands.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ if (typeof p5 !== "undefined") {
210210
* }
211211
*
212212
* function material() {
213-
* let t = uniformFloat();
213+
* let t = millis();
214214
* worldInputs.begin();
215215
* // Move the vertex up and down in a wave in world space
216216
* // In world space, moving the object (e.g., with translate()) will affect these coordinates
@@ -222,7 +222,6 @@ if (typeof p5 !== "undefined") {
222222
* function draw() {
223223
* background(255);
224224
* shader(myShader);
225-
* myShader.setUniform('t', millis());
226225
* lights();
227226
* noStroke();
228227
* fill('red');
@@ -313,7 +312,7 @@ if (typeof p5 !== "undefined") {
313312
* @example
314313
* <div modernizr="webgl">
315314
* <code>
316-
* // Example 1: A soft vertical fade using smoothstep (no uniforms)
315+
* // Example 1: A soft vertical fade using smoothstep
317316
*
318317
* let fadeShader;
319318
*
@@ -345,18 +344,16 @@ if (typeof p5 !== "undefined") {
345344
* @example
346345
* <div modernizr="webgl">
347346
* <code>
348-
* // Example 2: Animate the smooth transition using a uniform
347+
* // Example 2: Animate the smooth transition over time
349348
*
350349
* let animatedShader;
351350
*
352351
* function animatedFadeCallback() {
353-
* const time = uniformFloat(() => millis() * 0.001);
354-
*
355352
* getColor((inputs) => {
356353
* let x = inputs.texCoord.x;
357354
*
358355
* // Move the smoothstep band back and forth over time
359-
* let center = 0.5 + 0.25 * sin(time);
356+
* let center = 0.5 + 0.25 * sin(millis() * 0.001);
360357
* let t = smoothstep(center - 0.05, center + 0.05, x);
361358
*
362359
* return [t, t, t, 1];
@@ -490,7 +487,7 @@ if (typeof p5 !== "undefined") {
490487
* }
491488
*
492489
* function material() {
493-
* let t = uniformFloat();
490+
* let t = millis();
494491
* pixelInputs.begin();
495492
* // Animate alpha (transparency) based on x position
496493
* pixelInputs.color.a = 0.5 + 0.5 *
@@ -501,7 +498,6 @@ if (typeof p5 !== "undefined") {
501498
* function draw() {
502499
* background(240);
503500
* shader(myShader);
504-
* myShader.setUniform('t', millis());
505501
* lights();
506502
* noStroke();
507503
* fill('purple');
@@ -692,7 +688,7 @@ if (typeof p5 !== "undefined") {
692688
* }
693689
*
694690
* function material() {
695-
* let t = uniformFloat();
691+
* let t = millis();
696692
* objectInputs.begin();
697693
* // Create a sine wave along the object
698694
* objectInputs.position.y += sin(t * 0.001 + objectInputs.position.x);
@@ -702,7 +698,6 @@ if (typeof p5 !== "undefined") {
702698
* function draw() {
703699
* background(220);
704700
* shader(myShader);
705-
* myShader.setUniform('t', millis());
706701
* noStroke();
707702
* fill('orange');
708703
* sphere(50);
@@ -734,7 +729,7 @@ if (typeof p5 !== "undefined") {
734729
* }
735730
*
736731
* function material() {
737-
* let t = uniformFloat();
732+
* let t = millis();
738733
* cameraInputs.begin();
739734
* // Move vertices in camera space based on their x position
740735
* cameraInputs.position.y += 30 * sin(cameraInputs.position.x * 0.05 + t * 0.001);
@@ -746,7 +741,6 @@ if (typeof p5 !== "undefined") {
746741
* function draw() {
747742
* background(200);
748743
* shader(myShader);
749-
* myShader.setUniform('t', millis());
750744
* noStroke();
751745
* fill('red');
752746
* sphere(50);

src/webgl/material.js

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ function material(p5, fn) {
411411
* // Make a version of the shader with a hook overridden
412412
* modifiedShader = myShader.modify(() => {
413413
* // Create new uniforms and override the getColor hook
414-
* let t = uniformFloat(() => millis() / 1000);
414+
* let t = millis() / 1000;
415415
* getColor(() => {
416416
* return [0, 0.5 + 0.5 * sin(t), 1, 1];
417417
* });
@@ -657,7 +657,7 @@ function material(p5, fn) {
657657
* }
658658
* ```
659659
*
660-
* You can also animate your filters over time by passing the time into the shader with `uniformFloat`.
660+
* You can also animate your filters over time using the `millis()` function.
661661
*
662662
* ```js example
663663
* let myFilter;
@@ -668,7 +668,7 @@ function material(p5, fn) {
668668
* }
669669
*
670670
* function gradient() {
671-
* let time = uniformFloat();
671+
* let time = millis();
672672
* filterColor.begin();
673673
* filterColor.set(mix(
674674
* [1, 0, 0, 1], // Red
@@ -679,12 +679,11 @@ function material(p5, fn) {
679679
* }
680680
*
681681
* function draw() {
682-
* myFilter.setUniform('time', millis());
683682
* filter(myFilter);
684683
* }
685684
* ```
686685
*
687-
* We can use the `noise()` function built into strands to generate a color for each pixel. (Again no need here for underlying content for the filter to operate on.) Again we'll animate by passing in an announced uniform variable `time` with `setUniform()`, each frame.
686+
* We can use the `noise()` function built into strands to generate a color for each pixel. (Again no need here for underlying content for the filter to operate on.) Again we'll animate by using the millis() function to get an up-to-date time value.
688687
*
689688
* ```js example
690689
* let myFilter;
@@ -696,7 +695,7 @@ function material(p5, fn) {
696695
* }
697696
*
698697
* function noiseShaderCallback() {
699-
* let time = uniformFloat();
698+
* let time = millis();
700699
* filterColor.begin();
701700
* let coord = filterColor.texCoord;
702701
*
@@ -713,7 +712,6 @@ function material(p5, fn) {
713712
* }
714713
*
715714
* function draw() {
716-
* myFilter.setUniform("time", millis());
717715
* filter(myFilter);
718716
* }
719717
* ```
@@ -927,7 +925,7 @@ function material(p5, fn) {
927925
* }
928926
*
929927
* function material() {
930-
* let time = uniformFloat();
928+
* let time = millis() / 1000;
931929
* finalColor.begin();
932930
* let r = 0.2 + 0.5 * abs(sin(time + 0));
933931
* let g = 0.2 + 0.5 * abs(sin(time + 1));
@@ -938,7 +936,6 @@ function material(p5, fn) {
938936
*
939937
* function draw() {
940938
* background(245, 245, 220);
941-
* myShader.setUniform('time', millis() / 1000);
942939
* shader(myShader);
943940
*
944941
* rectMode(CENTER);
@@ -1414,7 +1411,7 @@ function material(p5, fn) {
14141411
* }
14151412
*
14161413
* function material() {
1417-
* let time = uniformFloat();
1414+
* let time = millis();
14181415
* worldInputs.begin();
14191416
* worldInputs.position.y +=
14201417
* 20 * sin(time * 0.001 + worldInputs.position.x * 0.05);
@@ -1424,7 +1421,6 @@ function material(p5, fn) {
14241421
* function draw() {
14251422
* background(255);
14261423
* shader(myShader);
1427-
* myShader.setUniform('time', millis());
14281424
* lights();
14291425
* noStroke();
14301426
* fill('red');
@@ -1593,7 +1589,6 @@ function material(p5, fn) {
15931589
* function draw() {
15941590
* background(255);
15951591
* shader(myShader);
1596-
* myShader.setUniform('time', millis());
15971592
* lights();
15981593
* noStroke();
15991594
* fill('red');
@@ -1607,7 +1602,7 @@ function material(p5, fn) {
16071602
*
16081603
* ```js
16091604
* // myMaterial.js
1610-
* let time = uniformFloat();
1605+
* let time = millis();
16111606
* worldInputs.begin();
16121607
* worldInputs.position.y +=
16131608
* 20 * sin(time * 0.001 + worldInputs.position.x * 0.05);
@@ -1716,7 +1711,7 @@ function material(p5, fn) {
17161711
* }
17171712
*
17181713
* function material() {
1719-
* let time = uniformFloat();
1714+
* let time = millis();
17201715
* worldInputs.begin();
17211716
* worldInputs.position.y +=
17221717
* 20. * sin(time * 0.001 + worldInputs.position.x * 0.05);
@@ -1726,7 +1721,6 @@ function material(p5, fn) {
17261721
* function draw() {
17271722
* background(255);
17281723
* shader(myShader);
1729-
* myShader.setUniform('time', millis());
17301724
* noStroke();
17311725
* sphere(50);
17321726
* }
@@ -1812,7 +1806,6 @@ function material(p5, fn) {
18121806
* function draw() {
18131807
* background(255);
18141808
* shader(myShader);
1815-
* myShader.setUniform('time', millis());
18161809
* lights();
18171810
* noStroke();
18181811
* fill('red');
@@ -1826,7 +1819,7 @@ function material(p5, fn) {
18261819
*
18271820
* ```js
18281821
* // myMaterial.js
1829-
* let time = uniformFloat();
1822+
* let time = millis();
18301823
* worldInputs.begin();
18311824
* worldInputs.position.y +=
18321825
* 20 * sin(time * 0.001 + worldInputs.position.x * 0.05);
@@ -1919,7 +1912,7 @@ function material(p5, fn) {
19191912
* }
19201913
*
19211914
* function material() {
1922-
* let time = uniformFloat();
1915+
* let time = millis();
19231916
* worldInputs.begin();
19241917
* worldInputs.position.y +=
19251918
* 20 * sin(time * 0.001 + worldInputs.position.x * 0.05);
@@ -1929,7 +1922,6 @@ function material(p5, fn) {
19291922
* function draw() {
19301923
* background(255);
19311924
* shader(myShader);
1932-
* myShader.setUniform('time', millis());
19331925
* noStroke();
19341926
* fill('red');
19351927
* circle(0, 0, 50);
@@ -1978,7 +1970,6 @@ function material(p5, fn) {
19781970
* function draw() {
19791971
* background(255);
19801972
* shader(myShader);
1981-
* myShader.setUniform('time', millis());
19821973
* lights();
19831974
* noStroke();
19841975
* fill('red');
@@ -1992,7 +1983,7 @@ function material(p5, fn) {
19921983
*
19931984
* ```js
19941985
* // myMaterial.js
1995-
* let time = uniformFloat();
1986+
* let time = millis();
19961987
* worldInputs.begin();
19971988
* worldInputs.position.y +=
19981989
* 20 * sin(time * 0.001 + worldInputs.position.x * 0.05);
@@ -2164,7 +2155,7 @@ function material(p5, fn) {
21642155
* }
21652156
*
21662157
* function material() {
2167-
* let time = uniformFloat();
2158+
* let time = millis();
21682159
* worldInputs.begin();
21692160
* // Add a somewhat random offset to the weight
21702161
* // that varies based on position and time
@@ -2180,7 +2171,6 @@ function material(p5, fn) {
21802171
* function draw() {
21812172
* background(255);
21822173
* strokeShader(myShader);
2183-
* myShader.setUniform('time', millis());
21842174
* strokeWeight(10);
21852175
* beginShape();
21862176
* for (let i = 0; i <= 50; i++) {

src/webgl/p5.Shader.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,31 +206,39 @@ class Shader {
206206
*
207207
* In addition to calling hooks, you can create uniforms, which are special variables
208208
* used to pass data from p5.js into the shader. They can be created by calling `uniform` + the
209-
* type of the data, such as `uniformFloat` for a number of `uniformVector2` for a two-component vector.
209+
* type of the data, such as `uniformFloat` for a number or `uniformVector2` for a two-component vector.
210210
* They take in a function that returns the data for the variable. You can then reference these
211211
* variables in your hooks, and their values will update every time you apply
212-
* the shader with the result of your function.
212+
* the shader with the result of your function.
213+
*
214+
* Move the mouse over this sketch to increase the moveCounter which will be passed to the shader as a uniform.
213215
*
214216
* ```js example
215217
* let myShader;
216-
*
218+
* //count of frames in which mouse has been moved
219+
* let moveCounter = 0;
220+
*
217221
* function setup() {
218222
* createCanvas(200, 200, WEBGL);
219223
* myShader = baseMaterialShader().modify(() => {
220-
* // Get the current time from p5.js
221-
* let t = uniformFloat(() => millis());
224+
* // Get the move counter from our sketch
225+
* let count = uniformFloat(() => moveCounter);
222226
*
223227
* getPixelInputs((inputs) => {
224228
* inputs.color = [
225229
* inputs.texCoord,
226-
* sin(t * 0.01) / 2 + 0.5,
230+
* sin(count/100) / 2 + 0.5,
227231
* 1,
228232
* ];
229233
* return inputs;
230234
* });
231235
* });
232236
* }
233237
*
238+
* function mouseDragged(){
239+
* moveCounter += 1;
240+
* }
241+
*
234242
* function draw() {
235243
* background(255);
236244
* noStroke(255);

0 commit comments

Comments
 (0)