File tree Expand file tree Collapse file tree 5 files changed +114
-0
lines changed
PerlinNoiseFlowField/ShardulNalegave Expand file tree Collapse file tree 5 files changed +114
-0
lines changed Original file line number Diff line number Diff line change 1+
2+ # Perlin Noise Flow-Field
3+
4+ ![ Demo] ( https://i.ibb.co/xLf1jY8/demo.png )
Original file line number Diff line number Diff line change 1+ < html lang ="en ">
2+ < head >
3+ < meta charset ="UTF-8 ">
4+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
5+ < title > Perlin Noise Flow-Field</ title >
6+ </ head >
7+ < body >
8+ < script src ="https://cdn.jsdelivr.net/npm/p5@1.7.0/lib/p5.js "> </ script >
9+ < script src ="./particle.js "> </ script >
10+ < script src ="./index.js "> </ script >
11+ </ body >
12+ </ html >
Original file line number Diff line number Diff line change 1+
2+ const inc = 0.1 ;
3+ let zoff = 0 ;
4+ const scale = 40 ;
5+ let rows , cols ;
6+ let fr ;
7+
8+ const flowField = [ ] ;
9+ const numParticles = 600 ;
10+ const particles = [ ] ;
11+
12+ function setup ( ) {
13+ createCanvas ( 1000 , 800 ) ;
14+ cols = floor ( width / scale ) ;
15+ rows = floor ( height / scale ) ;
16+ fr = createP ( '' ) ;
17+
18+ for ( let i = 0 ; i < numParticles ; i ++ ) {
19+ particles [ i ] = new Particle ( ) ;
20+ }
21+ }
22+
23+ function draw ( ) {
24+ background ( 0 ) ;
25+
26+ let yoff = 0 ;
27+ for ( let y = 0 ; y < rows ; y ++ ) {
28+ let xoff = 0 ;
29+
30+ for ( let x = 0 ; x < cols ; x ++ ) {
31+ let angle = noise ( xoff , yoff , zoff ) * TWO_PI * 4 ;
32+ let v = p5 . Vector . fromAngle ( angle ) ;
33+ v . setMag ( 0.5 ) ;
34+ xoff += inc ;
35+
36+ let index = x + ( y * cols ) ;
37+ flowField [ index ] = v ;
38+
39+ stroke ( 255 , 50 ) ;
40+ strokeWeight ( 1 ) ;
41+ push ( ) ;
42+ translate ( x * scale , y * scale ) ;
43+ rotate ( v . heading ( ) ) ;
44+ line ( 0 , 0 , scale , 0 ) ;
45+ pop ( ) ;
46+ }
47+
48+ yoff += inc ;
49+ zoff += 0.0003 ;
50+ }
51+
52+ for ( let i = 0 ; i < numParticles ; i ++ ) {
53+ particles [ i ] . follow ( flowField , scale ) ;
54+ particles [ i ] . update ( ) ;
55+ particles [ i ] . draw ( ) ;
56+ }
57+
58+ fr . html ( floor ( frameRate ( ) ) ) ;
59+ }
Original file line number Diff line number Diff line change 1+
2+ class Particle {
3+ constructor ( ) {
4+ this . pos = createVector ( random ( width ) , random ( height ) ) ;
5+ this . vel = createVector ( 0 , 0 ) ;
6+ this . accel = createVector ( 0 , 0 ) ;
7+ }
8+
9+ update ( ) {
10+ this . vel . add ( this . accel ) ;
11+ this . pos . add ( this . vel ) ;
12+ this . accel . mult ( 0 ) ;
13+ this . vel . limit ( 4 ) ; // max-speed
14+
15+ if ( this . pos . x > width ) this . pos . x = 0 ;
16+ if ( this . pos . x < 0 ) this . pos . x = width ;
17+
18+ if ( this . pos . y > height ) this . pos . y = 0 ;
19+ if ( this . pos . y < 0 ) this . pos . y = height ;
20+ }
21+
22+ follow ( flowField , scale ) {
23+ let x = floor ( this . pos . x / scale ) ;
24+ let y = floor ( this . pos . y / scale ) ;
25+ let index = x + ( y * cols ) ;
26+ let force = flowField [ index ] ;
27+ this . applyForce ( force ) ;
28+ }
29+
30+ applyForce ( force ) {
31+ this . accel . add ( force ) ;
32+ }
33+
34+ draw ( ) {
35+ strokeWeight ( 2 ) ;
36+ stroke ( 255 ) ;
37+ point ( this . pos . x , this . pos . y ) ;
38+ }
39+ }
You can’t perform that action at this time.
0 commit comments