-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path11-three-processing.html
More file actions
279 lines (200 loc) · 5.56 KB
/
11-three-processing.html
File metadata and controls
279 lines (200 loc) · 5.56 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<!DOCTYPE HTML>
<html>
<head>
<script src="js/libs/processing.js"></script>
<script src="js/three/build/three.js"></script>
<style>
body{
background-color:black;
color:grey;
text-align:center;
font-size:16;
font-family:sans-serif;
}
#move {
text-align:center;
}
canvas {
display: inline;
}
</style>
</head>
<body>
<p>
This script combines threejs and processingjs - click to randomize
</p>
<div id="move">
<canvas id="processing-canvas"> </canvas>
</div>
<p style="color:#333">
<strong>you:</strong> what the?<br>
<strong>me:</strong> This script runs a processing sketch that fills an array <br>
with the positions of elements. This is then read and rendered by threejs <br>
<strong>you:</strong> why are the physics so bad?<br>
<strong>me:</strong> Sorry! This is an older processing sketch, I thought it would work well as a demo even if it is a little hinky<br>
</p>
</body>
<script>
//declaring variables to share between three and pjs
var pVar = [];
var mouser = {x:0,y:0};
</script>
<script type="text/processing" data-processing-target="processing-canvas">
Ball myball;
int numBalls = 30;
Ball[] myballs = new Ball[numBalls];
void setup() {
size(500, 500);
myball = new Ball(width/2, height/2, 20);
for (int i = 0; i<myballs.length; i++) {
myballs[i] = new Ball(random(width), random(height), 50, i, myballs);
}
}
void draw() {
mouser.x = mouseX;
mouser.y = mouseY;
if(mousePressed == true){
for (int i = 0; i<myballs.length; i++) {
myballs[i] = new Ball(random(width), random(height), 50, i, myballs);
}
}
background(0);
smooth(4);
fill(255,200,50);
ellipse(mouseX,mouseY,50,50);
for (int i = 0; i<myballs.length; i++) {
myballs[i].display();
myballs[i].move(mouseX, mouseY);
myballs[i].collide();
}
//this lets us share with three
pVar = myballs;
}
class Ball {
float tx;
float ty;
float rad, origtx, origty;
Ball[] others;
int id;
Ball(float mytx, float myty, float myrad) {
tx = mytx;
ty = myty;
origtx = mytx;
origty = myty;
rad = myrad;
}
Ball(float mytx, float myty, float myrad, int myid, Ball[] myball) {
tx = mytx;
ty = myty;
origtx = mytx;
origty = myty;
rad = myrad;
id = myid;
others = myball;
}
void display() {
fill(255);
ellipse(tx,ty,rad,rad);
}
void move(float movetx, float movety) {
float thisx = movetx;
float thisy = movety;
float mox = tx-thisx;
float moy = ty-thisy;
if (dist(tx, ty, thisx, thisy)<120) {
tx+=mox/dist(tx, ty, thisx, thisy);
ty+=moy/dist(tx, ty, thisx, thisy);
}
if(dist(tx, ty, thisx, thisy)<50){
tx+=mox/5;
ty+=moy/5;
}
}
void collide() {
for (int i = id+1;i<numBalls;i++) {
float thisx = others[i].tx -tx;
float thisy = others[i].ty - ty;
float distance = sqrt (thisx*thisx + thisy*thisy);
float minDist = others[i].rad/2+rad/2+15;
float angle = atan2(thisy, thisx);
float targetX = tx + cos(angle)*minDist;
float targetY = ty + sin(angle)*minDist;
float ax = (targetX - others[i].tx)*.21;
float ay = (targetY - others[i].ty)*.21;
if (distance < minDist) {
tx-=ax;
ty-=ay;
}
}
}
}
</script>
<script>
//THREE script
var balls = [];
var done = false;
var container = document.getElementById("move");
console.log(container);
var renderer = new THREE.WebGLRenderer({antialias:true});
renderer.setSize( 500, 500 );
container.appendChild(renderer.domElement);
var scene = new THREE.Scene();
var camera = new THREE.OrthographicCamera( 0,500,0,500,0,1000);
//actual position doesn't matter - just the resulting vector
camera.position.set( 0, 0,10 );
camera.lookAt( scene.position );
var geometry = new THREE.SphereGeometry( 25, 12, 12 );
/* this makes reflective balls - sadly it was looking strange - maybe because of the orthographic cam
var path = "textures/bmap.";
var format = '.jpg';
var urls = [
path + '04' + format, path + '02' + format,
path + '05' + format, path + '06' + format,
path + '01' + format, path + '03' + format
];
var textureCube = THREE.ImageUtils.loadTextureCube( urls );
var material = new THREE.MeshBasicMaterial( { color: 0xffffff, envMap: textureCube } );
*/
var material = new THREE.MeshLambertMaterial( { color: 0xFFffff } );
var mesh = new THREE.Mesh( geometry, material );
mesh.updateMatrix();
mesh.matrixAutoUpdate = true;
var light = new THREE.DirectionalLight( 0xFFFFff );
light.position.set( 1, -1, 1 );
scene.add( light );
var light = new THREE.DirectionalLight( 0x555555 );
light.position.set( 0, 0, -1 );
scene.add( light );
var light = new THREE.DirectionalLight( 0xFFFFff );
light.position.set( -1, 1, 1 );
scene.add( light );
function addBalls(){
for(var i = 0 ; i < pVar.length+1 ; i++){
var mesh = new THREE.Mesh( geometry, material );
mesh.updateMatrix();
mesh.matrixAutoUpdate = true;
balls.push(mesh);
scene.add(mesh);
}
}
draw();
function draw(){
renderer.render( scene, camera );
//pVar begins it's life being empty - I assume that means three is rendered before pjs
//in any case I need to tell it to wait until pVar has some values before creating all the ball geo
if(pVar[0]!=undefined){
if(pVar.length == 30 && done==false){
addBalls();
done = true;
}
for(var i = 0 ; i < pVar.length ; i++){
balls[i].position.x = pVar[i].tx;
balls[i].position.y = pVar[i].ty;
}
balls[30].position.x = mouser.x;
balls[30].position.y = mouser.y;
}
requestAnimationFrame( draw );
}
</script>
</html>