-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathriverKanies_simple_multiRender.html
245 lines (203 loc) · 6.99 KB
/
riverKanies_simple_multiRender.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<html>
<head>
<title>Art / Dev - Motion 2</title>
<script src="p5.min.js"></script>
<script>
var IMAGE_WIDTH = 640; // Don't change this.
var IMAGE_HEIGHT = 640; // Don't change this.
//may need to scale models based on different unit values
//just assume all same scale for now
var unit = 2;
o = {x:320,y:320};
rad = 200;
angle = .2;
tilt = .2;
//========= Parent Model
function setup() {
createCanvas(IMAGE_WIDTH, IMAGE_HEIGHT); // don't change this
//call all setup methods here
models = [];
setupFuncs = [setupRiver,setupModel,setupTri,setupQuad,setupModel];//everyone's setup functions will go here
drawFuncs = [drawRiver,drawModel,drawTri,drawQuad,drawModel];//everyone's draw functions will go here
//loop through for each model and init the model with it's setup function:
for(var i=0;i<setupFuncs.length;i++){
models[i] = setupFuncs[i](unit);//all sizing values: radius, magnitue, lenght, velocity, ... should be multiplied by a unit that can be controlled from the outside
}
}
function draw() {
clear();
var origins = [];
angle += .002;
var zs = []
for(var i=0;i<setupFuncs.length;i++){
var rot = angle + i*TWO_PI/setupFuncs.length
origins[i] = {
x:o.x+rad*cos(rot),
y:o.y+rad*sin(rot)*tilt,
rot:rot
};
//angle+=TWO_PI/setupFuncs.length;
zs[i] = {i:i,z:sin(rot)}
}
function sortNumber(a,b) {
return a.z - b.z;
}
zs.sort(sortNumber)
console.log
for(var i=0;i<setupFuncs.length;i++){
var ind = zs[i].i
drawFuncs[ind](models[ind],origins[ind],unit)
}
/*
//calculate distance of origins and render in ascending order:
var lastZ = 2;
var rendered = {};
var next
//do once for each model
for(var i=0; i<models.length;i++){
//render the next furthest away model
for(var j=0; j<models.length;j++){
var rot = angle + j*TWO_PI/setupFuncs.length
var z = sin(rot)
if (z<lastZ&&!rendered[j]){
lastZ = z
next = j
//console.log(j)
}
}
drawFuncs[next](models[next],origins[next],unit)
rendered[next]=true
}
*/
}
//=========Individual Models
function setupModel(unit){//every model has it's onw setupModel function
//no globals! all 'global' variables must be decalred within your setup function
//and should be namespaced as part of your individual model object (m below)
var m = {
hue: random(0,1),
rad: 100
};
//return the model object created in your setupModel function,
//it will be added to the models array
return m;
}
function drawModel(m,o,unit){
//*** this was in the global draw function, include this kind of stuff in your individual drawModel function instead
noStroke()
colorMode(HSB, 1)
//**
fill(.5,1,.4,0.01);
//rect(0,0,width,height);
fill(m.hue,.5,1,0)
ellipse(o.x,o.y,m.rad*unit,m.rad*unit)
}
function setupTri(unit){//every model has it's onw setupModel function
//no globals! all 'global' variables must be decalred within your setup function
//and should be namespaced as part of your individual model object (m below)
var m = {
hue: random(0,1),
rad: 100
};
//return the model object created in your setupModel function,
//it will be added to the models array
return m;
}
function drawTri(m,o,unit){
//*** this was in the global draw function, include this kind of stuff in your individual drawModel function instead
noStroke()
colorMode(HSB, 1)
//**
fill(.5,1,.4,0.01);
//rect(0,0,width,height);
fill(m.hue,.5,1,0)
triangle(o.x,o.y-m.rad,o.x+m.rad*cos(PI/6),o.y+m.rad*sin(PI/6),o.x-m.rad*cos(PI/6),o.y+m.rad*sin(PI/6))
}
function setupQuad(unit){//every model has it's onw setupModel function
//no globals! all 'global' variables must be decalred within your setup function
//and should be namespaced as part of your individual model object (m below)
var m = {
hue: random(0,1),
rad: 100
};
//return the model object created in your setupModel function,
//it will be added to the models array
return m;
}
function drawQuad(m,o,unit){
//*** this was in the global draw function, include this kind of stuff in your individual drawModel function instead
noStroke()
colorMode(HSB, 1)
//**
fill(.5,1,.4,0.01);
//rect(0,0,width,height);
fill(m.hue,.5,1,0)
//triangle(o.x,o.y-m.rad,o.x+m.rad*cos(PI/6),o.y+m.rad*sin(PI/6),o.x-m.rad*cos(PI/6),o.y+m.rad*sin(PI/6))
quad(o.x-sin(o.rot)*m.rad,o.y-m.rad,o.x+sin(o.rot)*m.rad,o.y-m.rad,o.x-sin(o.rot)*m.rad,o.y+m.rad,o.x+sin(o.rot)*m.rad,o.y+m.rad)
}
//======== River's individual stuff
function setupRiver(unit){//every model has it's onw setupModel function
//no globals! all 'global' variables must be decalred within your setup function
//and should be namespaced as part of your individual model object (m below)
var m = {};
//m.o={x:320,y:100};
m.particleCount = 50;
m.particles = [];
m.particleCounter = 0;
for (var i=0; i<m.particleCount; i++) {
var magnitude = random(0,2)*unit;//note use of unit
var angle = random(0,TWO_PI);
m.particles[i] = {
x:-1000,
y:-1000,
vx: magnitude*cos(angle),
vy: magnitude*sin(angle),
r: random(5,12)*unit,//note use of unit
hue: random(0.4,0.45),
};
}
//return the model object created in your setupModel function,
//it will be added to the models array
return m;
}
function drawRiver(m,o,unit){
//*** this was in the global draw function, include this kind of stuff in your individual drawModel function instead
noStroke()
colorMode(HSB, 1)
//**
fill(.5,1,.4,0.01);
//rect(0,0,width,height);
var magnitude = random(0,2)*unit;//note use of unit
var angle = random(0,TWO_PI);
var currentParticle = m.particles[m.particleCounter];
m.particleCounter = (m.particleCounter+1) % m.particleCount;
currentParticle.x = o.x;//320//mouseX;
currentParticle.y = o.y;//100//mouseY;
currentParticle.vx = magnitude*cos(angle)
currentParticle.vy = magnitude*sin(angle)
m.particles.forEach(drawParticle);
//move all functions into your drawModel function (to make my job easier and avoid naming conflicts)
function calculateParticle(particle) {
particle.x += particle.vx
particle.y += particle.vy
particle.vy += .01*unit
}
function drawParticle(particle) {
strokeWeight(5)
stroke(particle.hue,.5,1,0)
calculateParticle(particle)
fill(particle.hue,.5,1,0)//hue,sat,bright,alpha
ellipse(particle.x,particle.y,particle.r,particle.r)
line(o.x,o.y,particle.x,particle.y)
}
}
// =============== STOP WORKING HERE ==========================
</script>
<style>
body { padding: 0; margin: 5% 0 0; background-color: #666;}
canvas { margin: 0 auto; display:block; }
</style>
</head>
<body>
</body>
</html>