-
Notifications
You must be signed in to change notification settings - Fork 5
/
transform.html
347 lines (306 loc) · 10.3 KB
/
transform.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<!DOCTYPE html>
<html lang="en">
<head>
<title>FORMation : TransFORM</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
margin: 0px;
background: 0x000000;
overflow: hidden;
}
#example {
background:0x000000;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
#editor {
position:absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
opacity: 0.75;
z-index: 20;
}
</style>
</head>
<body>
<div id="example"></div>
<div id="editor">
//add global variables here:
var i = 0;
//this function will be executed with each frame
return function () {
i += 0.075;
for (var j = 0; j < xForm.x_size; j++) {
for (var k = 0; k < xForm.y_size; k++) {
xForm.setPinHeight(j,k,(0.5 + 0.5 * Math.cos(i + j * 0.5)));
}
}
};
</div>
<script type="x-shader/x-vertex" id="vertexShader">
varying vec3 vWorldPosition;
void main() {
vec4 worldPosition = modelMatrix * vec4( position, 1.0 );
vWorldPosition = worldPosition.xyz;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
</script>
<script type="x-shader/x-fragment" id="fragmentShader">
uniform vec3 topColor;
uniform vec3 bottomColor;
uniform float offset;
uniform float exponent;
varying vec3 vWorldPosition;
void main() {
float h = normalize(vWorldPosition + offset).y;
gl_FragColor = vec4(mix(bottomColor, topColor, max(pow(max(h, 0.0), exponent), 0.0)), 1.0);
}
</script>
<script src="js/3d/three.min.js"></script>
<script src="js/3d/ShapeDisplay.js"></script>
<script src="js/3d/Box.js"></script>
<script src="js/utils/dat.gui.min.js"></script>
<script src="js/utils/sockets.utils.js"></script>
<script src="js/ace/ace.js" type="text/javascript" charset="utf-8"></script>
<script>
var socket; // handles messages with shape display
var fps = 30;
var camera, scene, renderer;
var element, container;
var clock = new THREE.Clock();
var controlVars;
var mapImageContext;
var xForm;
var running = false;
var pause = false;
var editor = ace.edit("editor");
editor.setTheme("ace/theme/twilight");
editor.getSession().setMode("ace/mode/javascript");
editor.setOption("showPrintMargin", false);
editor.setOption("fontSize", 13);
//make control+m hide the editor
editor.commands.addCommand({
name: 'hideEditor',
bindKey: 'Ctrl-M',
exec: function(editor) {
document.getElementById('editor').style.display = 'none';
}
});
init();
animate();
function init() {
setupSocket();
// Basic elements of 3JS: Renderer, Scene, Camera
//----------------------------------------------------------------------
renderer = new THREE.WebGLRenderer();
element = renderer.domElement;
container = document.getElementById('example');
container.appendChild(element);
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(90, 1, 0.001, 5000);
camera.rotation.y = -Math.PI/2;
camera.position.set(-10, 31, 0);
scene.add(camera);
// Shape Display!!
//----------------------------------------------------------------------
xForm = new Transform(2,scene);
xForm.container.position.x = 40;
// Lights
//----------------------------------------------------------------------
var hemiLight = new THREE.HemisphereLight( 0x999999, 0xffffff, 1);
hemiLight.position.y = 500;
scene.add(hemiLight);
var directionalLight = new THREE.DirectionalLight();
directionalLight.position.copy(new THREE.Vector3(-5000, 30, 0));
scene.add(directionalLight);
// Mighty Skydome
// //----------------------------------------------------------------------
var vertexShader = document.getElementById( 'vertexShader' ).textContent;
var fragmentShader = document.getElementById( 'fragmentShader' ).textContent;
var uniforms = {
topColor: { type: "c", value: new THREE.Color( 0x000000 ) },
bottomColor: { type: "c", value: new THREE.Color( 0x000000 ) },
offset: { type: "f", value: 400 },
exponent: { type: "f", value: 0.6 }
};
uniforms.topColor.value.copy( hemiLight.color );
var skyGeo = new THREE.SphereGeometry( 1000, 32, 15 );
var skyMat = new THREE.ShaderMaterial( {
uniforms: uniforms,
vertexShader: vertexShader,
fragmentShader: fragmentShader,
side: THREE.BackSide
} );
var sky = new THREE.Mesh( skyGeo, skyMat );
scene.add( sky );
// Floor
//----------------------------------------------------------------------
var texture = THREE.ImageUtils.loadTexture('textures/grid.png');
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.repeat = new THREE.Vector2(20, 20);
texture.anisotropy = renderer.getMaxAnisotropy();
var material = new THREE.MeshPhongMaterial({
color: 0xffffff,
specular: 0x333333,
shininess: 20,
shading: THREE.FlatShading,
map: texture
});
var geometry = new THREE.PlaneBufferGeometry(1000, 1000);
var mesh = new THREE.Mesh(geometry, material);
mesh.position.y = -40;
mesh.rotation.x = -Math.PI / 2;
scene.add(mesh);
// Peripheral stuff
//----------------------------------------------------------------------
window.addEventListener('resize', resize, false);
setTimeout(resize, 1);
// control variables
// control variables
controlVars = new function () {
this.sendToPhysical = false;
};
var gui = new dat.GUI();
gui.add(controlVars, 'sendToPhysical', false);
}
// Helper Functions
//--------------------------------------------------------------------------
function setupSocket() {
console.log('Setup Socket');
socket = new WebSocket( get_appropriate_ws_url());
try {
socket.onopen = function() {
console.log("Websocket connection OPEN");
}
socket.onmessage = function got_packet(msg) {
console.log(msg);
//handleMsg(msg.data);
}
socket.onclose = function() {
console.log("websocket connection CLOSED");
}
} catch (exception) {
console.log("Something went wrong " + exception);
}
}
function resize() {
var width = container.offsetWidth;
var height = container.offsetHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
}
// Rendering!
//--------------------------------------------------------------------------
function update(dt) {
resize();
camera.updateProjectionMatrix();
if (running && controlVars.sendToPhysical) {
msg = xForm.getHeightsMsgForPhysical();
if (msg.length > 0) {
socket.send("P"+msg);
}
}
}
function animate(t) {
setTimeout(function() {
requestAnimationFrame(animate);
if (!pause) {
try {
this.code();
}
catch (err) {
console.log(err.toString());
running = false;
}
}
update(clock.getDelta());
renderer.render(scene, camera);
}, 1000 / fps);
}
function fullscreen() {
if (container.requestFullscreen) {
container.requestFullscreen();
} else if (container.msRequestFullscreen) {
container.msRequestFullscreen();
} else if (container.mozRequestFullScreen) {
container.mozRequestFullScreen();
} else if (container.webkitRequestFullscreen) {
container.webkitRequestFullscreen();
}
}
windowX = 0
windowY = 0
window.addEventListener('keydown', function(e) {
var codingWindowVisible = document.getElementById('editor').style.display;
//only move map if coding window not visible
if (codingWindowVisible === 'none') {
console.log(e.keyCode);
switch(e.keyCode) {
case 68:
this.code = function() {};
xForm.clearDisplay();
running = false;
case 32:
if (pause) {
running = true;
pause = false;
}
else {
pause = true;
running = false;
}
case 37:
windowX += 1;
break;
case 39:
windowX -= 1;
break;
case 38:
windowY += 1;
break;
case 40:
windowY -= 1;
break;
//Enter runs the code
case 13:
this.changeCode();
break;
//if Ctrl+M is pressed, show editor
case 77:
if (e.ctrlKey) {
document.getElementById('editor').style.display = 'block';
editor.focus();
}
break;
}
}
});
this.code = function() {};
this.changeCode = function() {
xForm.clearDisplay();
var _executedCode;
try {
var userCode = new Function('"use strict";\n' + editor.getValue());
executedCode = userCode();
running = true;
pause = false;
}
catch (err) {
console.log(err.toString());
}
if (executedCode) {
this.code = executedCode;
}
}
</script>
</body>
</html>