-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
276 lines (209 loc) · 6.33 KB
/
test.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
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
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Sample Three.js</title>
<style>
#container {
background: #000;
width: 1024px;
height: 768px;
}
</style>
</head>
<body>
<div id="container">
</div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script src="Three.js"></script>
<script type="text/javascript">
var websocketUri = "ws://totoro.local:8080/p5websocket";
var websocket = new WebSocket(websocketUri);
websocket.onopen = function(evt) { websocketOpen(evt) };
websocket.onmessage = function(evt) { websocketMessage(evt) };
function websocketOpen(evt) {
console.log("opened");
}
function websocketMessage(evt) {
// data is in evt.data
var Count = 3375-50;
while(Count--) {
index = Count * 6 + 2
// red = parseInt(evt.data.substring(index, index+2),16);
// green = parseInt(evt.data.substring(index+2, index+4),16);
// blue = parseInt(evt.data.substring(index+4, index+6),16);
var c = particles.colors[Count];
// c.setRGB(red/255, green/255, blue/255);
c.setHex(parseInt(evt.data.substring(index,index+6),16));
}
}
// @see http://paulirish.com/2011/requestanimationframe-for-smart-animating/
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(/* function */ callback, /* DOMElement */ element){
window.setTimeout(callback, 1000 / 60);
};
})();
// set the scene size
var WIDTH = 1024,
HEIGHT = 768;
// set some camera attributes
var VIEW_ANGLE = 25,
ASPECT = WIDTH / HEIGHT,
NEAR = 0.1,
FAR = 10000;
// get the DOM element to attach to
// - assume we've got jQuery to hand
var $container = $('#container');
// create a WebGL renderer, camera
// and a scene
var renderer = new THREE.WebGLRenderer();
var scene = new THREE.Scene();
// Camera from:
// http://wecreategames.com/games/Three45/examples/misc_camera_trackball.html
camera = new THREE.PerspectiveCamera( VIEW_ANGLE,
ASPECT,
NEAR,
FAR);
camera.position.z = 500;
camera.rotation.z = 20;
controls = new THREE.TrackballControls( camera );
controls.rotateSpeed = 1.0;
controls.zoomSpeed = 1.2;
controls.panSpeed = 0.8;
controls.noZoom = false;
controls.noPan = false;
controls.staticMoving = true;
controls.dynamicDampingFactor = 0.3;
controls.keys = [ 65, 83, 68 ];
scene.add(camera);
// start the renderer
renderer.setSize(WIDTH, HEIGHT);
// attach the render-supplied DOM element
$container.append(renderer.domElement);
// create a point light
var pointLight = new THREE.PointLight( 0xFFFFFF );
// set its position
pointLight.position.x = 10;
pointLight.position.y = 50;
pointLight.position.z = 130;
// add to the scene
scene.add(pointLight);
scene.fog = new THREE.Fog( 0x000000, 1500, 4000 );
// ground
var groundTexture = THREE.ImageUtils.loadTexture('LostLake.jpg');
groundTexture.wrapS = THREE.RepeatWrapping;
groundTexture.wrapT = THREE.RepeatWrapping;
groundTexture.repeat.x = 100;
groundTexture.repeat.y = 100;
var material = new THREE.MeshBasicMaterial({map: groundTexture});
var geometry = new THREE.PlaneGeometry(10000, 10000);
var mesh = new THREE.Mesh(geometry, material);
mesh.rotation.x = -Math.PI / 2;
scene.add(mesh);
// lights
var lights_per_strip_ = 45*3;
var strips_ = 25;
var radius_ = 100;
var HALF_PI = Math.PI/2;
// create the particle variables
var particleCount = lights_per_strip_ * strips_,
particles = new THREE.Geometry();
// create the particle variables
var pMaterial =
new THREE.ParticleBasicMaterial({
color: 0xFFFFFF,
vertexColors : 1,
size: 4,
map: THREE.ImageUtils.loadTexture(
"light.png"
),
blending: THREE.AdditiveBlending,
transparent: true
});
var count = 0;
for (var light = lights_per_strip_ -1; light > 0; light--) {
for (var strip = 0; strip < strips_; strip++) {
var inclination = HALF_PI + (HALF_PI)*(light/lights_per_strip_);
var azimuth = (2*Math.PI)*(strip/strips_);
var pX = radius_ * Math.cos(azimuth) * Math.sin(inclination);
var pZ = radius_ * Math.sin(azimuth) * Math.sin(inclination);
var pY = -radius_ * Math.cos(inclination);
var particle = new THREE.Vertex(
new THREE.Vector3(pX, pY, pZ)
);
// create a velocity vector
particle.velocity = new THREE.Vector3(
0, // x
0, // y: random vel
0); // z
var color = new THREE.Color( light * 256 * 256 + light );
particles.colors.push(color);
// add it to the geometry
particles.vertices.push(particle);
}
}
// create the particle system
var particleSystem =
new THREE.ParticleSystem(
particles,
pMaterial);
// also update the particle system to
// sort the particles which enables
// the behaviour we want
particleSystem.sortParticles = true;
// add it to the scene
scene.add(particleSystem);
function update() {
// add some rotation to the system
// particleSystem.rotation.z += 0.01;
/*
//var pCount = particleCount;
var pCount = 2000;
while(pCount--) {
// get the particle
var particle =
particles.vertices[pCount];
// check if we need to reset
if(particle.position.y < -200) {
particle.position.y = 200;
particle.velocity.y = 0;
}
// update the velocity with
// a splat of randomniz
particle.velocity.y -=
Math.random() * .1;
// and the position
particle.position.addSelf(
particle.velocity);
}
// flag to the particle system
// that we've changed its vertices.
particleSystem.
geometry.
__dirtyVertices = true;
*/
controls.update();
// draw!
renderer.render(scene, camera);
// set up the next call
requestAnimFrame(update);
//updateColors();
}
function updateColors() {
// update colors!
var Count = 3375-50;
while(Count--) {
var c = particles.colors[Count];
c.setHSV(Math.random(), 1 + 0*Math.random(), Math.random());
}
particleSystem.geometry.__dirtyColors = true;
}
update();
</script>
</html>