-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.html
428 lines (344 loc) · 15.8 KB
/
index.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
<!DOCTYPE html>
<html lang="en">
<head>
<title>SimBox - Robosub Simulator</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
color: #000;
font-family:Monospace;
font-size:13px;
margin: 0px;
overflow: hidden;
}
#info {
position: absolute;
top: 0px; width: 100%;
text-align:center;
padding: 5px;
}
#inset {
width: 200px;
height: 200px;
background-color: #fff; /* or transparent; will show through only if renderer alpha: true */
border: 1px solid black; /* or none; */
margin: 20px;
padding: 0px;
position: absolute;
left: 0px;
bottom: 0px;
z-index: 100;
}
a {
color: #000000;
}
</style>
</head>
<body>
<div id="info"><a href="https://github.com/cantren/cantren.github.io/" target="_blank">Code</a></a> - Please "fork" as this page changes often.</div>
<div id="container">
<div id="inset">
<script src="../js/three.min.js"></script>
<script src="../js/FirstPersonControls.js"></script>
<script src="../js/Mirror.js"></script>
<script src="../js/WaterShader.js"></script>
<script src="../js/Detector.js"></script>
<script src="../js/stats.min.js"></script>
<script src="js/MTLLoader.js"></script>
<script src="js/OBJMTLLoader.js"></script>
<script>
if ( ! Detector.webgl ) {
Detector.addGetWebGLMessage();
document.getElementById( 'container' ).innerHTML = "";
}
var container, container2, stats;//cargo cult code comment out this line please(may be desired in future)
var camera, camera2, camControls, camControls2, scene, scene2, renderer, renderer2;//these are actually usefull
var sphere;//cargo cult code delete this line please
var CANVAS_WIDTH = 200,
CANVAS_HEIGHT = 200,
CAM_DISTANCE = 300;
var clock = new THREE.Clock();//I spent a long time debugging because I forgot to initialize this
//smartjava.org
//var ws = new WebSocket("ws://localhost:8889/");
//var frame = 0;
//var isOpen = false;
//ws.onopen = function(e) {
// console.log('opening');
// isOpen = true;
// };
//ws.onclose = function(e) {
// console.log('closing');
// console.log(e);
// isOpen = false;
// };
//ws.onerror = function(e) {
// console.log('error');
// console.log(e);
// };
//smartjava.org
var parameters = {
width: 2000,
height: 2000,
widthSegments: 250,
heightSegments: 250,
depth: 1500,
param: 4,
filterparam: 1
}
var waterNormals;
init();
animate();
//setInterval(animate(), 2000);//uncomment out this line if on autopilot
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( (window.innerWidth), (window.innerHeight) );
container.appendChild( renderer.domElement );
container2 = document.getElementById('inset');
// renderer
renderer2 = new THREE.WebGLRenderer();
renderer2.setClearColor( 0xf0f0f0, 1 );
renderer2.setSize( CANVAS_WIDTH, CANVAS_HEIGHT );
container2.appendChild( renderer2.domElement );
scene = new THREE.Scene();
scene2 = new THREE.Scene();
//add water colored fog
//scene.fog = new THREE.FogExp2( 0x008080, 0.1875 );
scene.fog = new THREE.FogExp2( 0x008080, 0.1375 );
scene2.fog = new THREE.FogExp2( 0x008080, 0.1875 );
camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 0.5, 3000000);
camera.position.x = 0;//pool line of symetry is x axis
camera.position.y = -2;//water depth
camera.position.z = -28;//how close to the wall are you
camera2 = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.5, 3000000);
camera2.up = camera.up; // important!
camera2.position.x = 0;//pool line of symetry is x axis
camera2.position.y = -2;//water depth
camera2.position.z = -28;//how close to the wall are you
//camera.position.set( 0, -2, -28 );
camControls = new THREE.FirstPersonControls( camera );
camControls.lookSpeed = 0.05; //Pitch Thrust to drag ratio
camControls.movementSpeed = 0.25; // FWD BKWD Thrust to drag ratio
camControls.verticalMin = -100.0; //basement floor level
camControls.verticalMax = 50.0; //sky ceiling level
camControls2 = new THREE.FirstPersonControls( camera2 );
camControls2.lookSpeed = 0.05; //Pitch Thrust to drag ratio
camControls2.movementSpeed = 0.25; // FWD BKWD Thrust to drag ratio
camControls2.verticalMin = -100.0; //basement floor level
camControls2.verticalMax = 50.0; //sky ceiling level
//Add scene lighting
var light = new THREE.HemisphereLight( 0xffffbb, 0x008080, 1 );//I tried changing the light color here to see what would happen
light.position.set( - 1, 1, - 1 ); //my hemisphere flipped upside down
scene.add( light );//I changed the name to distinguish a negative y value from "sunlight"
var light2 = new THREE.HemisphereLight( 0xffffbb, 0x008080, 1 );//I tried changing the light color here to see what would happen
light2.position.set( - 1, 1, - 1 ); //my hemisphere flipped upside down
scene2.add( light2 );//I changed the name to distinguish a negative y value from "sunlight"
//More scene lighting
var ambientLight = new THREE.AmbientLight(0x383838);
scene.add(ambientLight);
var ambientLight2 = new THREE.AmbientLight(0x383838);
scene2.add(ambientLight2);
// add spotlight for the shadows
var spotLight = new THREE.SpotLight(0xffffff);
spotLight.position.set(100, 140, 130);
spotLight.intensity = 2;
scene.add(spotLight);
var spotLight2 = new THREE.SpotLight(0xffffff);
spotLight2.position.set(100, 140, 130);
spotLight2.intensity = 2;
scene2.add(spotLight2);
//load and insert the obj meshes and mtl textures
//the "secret sauce" is in these .obj and .mtl files.
var loader = new THREE.OBJMTLLoader(); //this object does the work
loader.load('../obj/transdec OBJ/transdec test.obj', '../obj/transdec OBJ/transdec test.mtl', function ( object ) {
object.position.y = -12.975;//experimentally generated via trial and error
scene.add( object );//to make a Robosub you would likely need to change it's name from "object"
});
loader.load('../obj/robosub obstacles/obstacles.obj', '../obj/robosub obstacles/obstacles.mtl', function ( object ) {
object.position.y = -12.975;//experimentally generated via trial and error
scene.add( object );//to make a Robosub you would likely need to change it's name from "object"
});
loader.load('../obj/transdec OBJ/transdec test.obj', '../obj/transdec OBJ/transdec test.mtl', function ( object ) {
object.position.y = -12.975;//experimentally generated via trial and error
scene2.add( object );//to make a Robosub you would likely need to change it's name from "object"
});
loader.load('../obj/robosub obstacles/obstacles.obj', '../obj/robosub obstacles/obstacles.mtl', function ( object ) {
object.position.y = -12.975;//experimentally generated via trial and error
scene2.add( object );//to make a Robosub you would likely need to change it's name from "object"
});
waterNormals = new THREE.ImageUtils.loadTexture( 'textures/waternormals.jpg' ); //potential for easy modification/obstacle reflections
waterNormals.wrapS = waterNormals.wrapT = THREE.RepeatWrapping; //recycle that .jpg like a TV re-run
//initialize/declare water shader and provide parameters
water = new THREE.Water( renderer, camera, scene, { //three.js has a specific thing for water, this is it
textureWidth: 512, //size of waves I think, maybe the waternormals.jpg stretch?
textureHeight: 512, //size of waves I think, maybe the waternormals.jpg stretch?
waterNormals: waterNormals, //provides the .jpg Texture
alpha: 1.0, //how transparent is the water?
//sunDirection: new THREE.Vector3( 0.70707, -0.70707, 0 ),
sunDirection: light.position.clone().normalize(), // I think this makes the reflection normal to the texture
sunColor: 0xffffff, //Light has to hit the water, what color is it?
waterColor: 0xe7e7ec,//,0x000000,//0x001e0f,//0xe7e7ec,//0x001e0f,//water color for different effects
distortionScale: 50.0, //also related to size of waves?
});
//initialize/declare new planes for holding the "water" texture mapping
mirrorMesh = new THREE.Mesh(
new THREE.PlaneBufferGeometry( parameters.width * 500, parameters.height * 500 ),
water.material
);
mirrorMesh2 = new THREE.Mesh(
new THREE.PlaneBufferGeometry( parameters.width * 500, parameters.height * 500 ),
water.material
);
mirrorMesh.add( water ); //add water shader to otherwise boring underwater PlaneBufferGeometry
mirrorMesh2.add( water ); //add water shader to otherwise boring PlaneBufferGeometry
mirrorMesh.rotation.x = - Math.PI * 1.5; //rotate undwater water surface to point down
mirrorMesh2.rotation.x = - Math.PI * 0.5; //rotate water surface to point up
scene.add( mirrorMesh ); //add underwater water surface
scene.add( mirrorMesh2); //add water surface
// load skybox &/or (start example code copypasta: http://threejs.org/examples/#webgl_shaders_ocean)
var cubeMap = new THREE.CubeTexture( [] );//initialize/declare the texture object to be mapped to the skybox mesh var skyBox = new THREE.Mesh
cubeMap.format = THREE.RGBFormat;//don't know or care
cubeMap.flipY = false;//if you make this true the walls of the box go below the horizon
//var cubeMap2 = new THREE.CubeTexture( [] );//initialize/declare the texture object to be mapped to the skybox mesh var skyBox = new THREE.Mesh
//cubeMap2.format = THREE.RGBFormat;//don't know or care
//cubeMap2.flipY = true;//if you make this true the walls of the box go below the horizon
var loader = new THREE.ImageLoader();
loader.load( '../textures/skyboxsun25degtest.png', function ( image ) {
var getSide = function ( x, y ) {
var size = 1024;
var canvas = document.createElement( 'canvas' );
canvas.width = size;
canvas.height = size;
var context = canvas.getContext( '2d' );
context.drawImage( image, - x * size, - y * size );
return canvas;
};
cubeMap.images[ 0 ] = getSide( 2, 1 ); // px
cubeMap.images[ 1 ] = getSide( 0, 1 ); // nx
cubeMap.images[ 2 ] = getSide( 1, 0 ); // py
cubeMap.images[ 3 ] = getSide( 1, 2 ); // ny
cubeMap.images[ 4 ] = getSide( 1, 1 ); // pz
cubeMap.images[ 5 ] = getSide( 3, 1 ); // nz
cubeMap.needsUpdate = true;
} );
//var loader2 = new THREE.ImageLoader();
//loader2.load( '../textures/skyboxsun25degtest.png', function ( image2 ) {
// var getSide = function ( x, y ) {
// var size = 1024;
// var canvas2 = document.createElement( 'canvas2' );
// canvas2.width = size;
// canvas2.height = size;
// var context2 = canvas2.getContext( '2d' );
// context2.drawImage( image2, - x * size, - y * size );
// return canvas2;
// };
// cubeMap2.images[ 0 ] = getSide( 2, 1 ); // px
// cubeMap2.images[ 1 ] = getSide( 0, 1 ); // nx
// cubeMap2.images[ 2 ] = getSide( 1, 0 ); // py
// cubeMap2.images[ 3 ] = getSide( 1, 2 ); // ny
// cubeMap2.images[ 4 ] = getSide( 1, 1 ); // pz
// cubeMap2.images[ 5 ] = getSide( 3, 1 ); // nz
// cubeMap2.needsUpdate = true;
// } );
//Initialize/declare the shader for the skybox
var cubeShader = THREE.ShaderLib['cube'];
cubeShader.uniforms['tCube'].value = cubeMap;
// var cubeShader2 = THREE.ShaderLib['cube'];
// cubeShader2.uniforms['tCube'].value = cubeMap2;
//Create skybox material
var skyBoxMaterial = new THREE.ShaderMaterial( {
fragmentShader: cubeShader.fragmentShader,
vertexShader: cubeShader.vertexShader,
uniforms: cubeShader.uniforms,
depthWrite: false,
side: THREE.BackSide
});
// var skyBoxMaterial2 = new THREE.ShaderMaterial( {
// fragmentShader: cubeShader2.fragmentShader,
// vertexShader: cubeShader2.vertexShader,
// uniforms: cubeShader2.uniforms,
// depthWrite: false,
// side: THREE.BackSide
// });
//Create Mesh to map skybox material on to
var skyBox = new THREE.Mesh(
new THREE.BoxGeometry( 10000, 10000, 10000 ),
skyBoxMaterial
);
scene.add( skyBox ); //create Point Loma Sky
// var skyBox2 = new THREE.Mesh(
// new THREE.BoxGeometry( 10000, 10000, 10000 ),
// skyBoxMaterial2
// );
//var skyBox2 = new THREE.Mesh(
// new THREE.BoxGeometry( 10000, -10000, 10000 ),
// skyBoxMaterial
//);
// scene.add( skyBox2 );
}
//
var fps = 1000; //source: http://creativejs.com/resources/requestanimationframe/
function animate() {
requestAnimationFrame( animate ); //animate the frame
render(); //render the frame
/* Autopilot specific
setTimeout(function() {//source: http://creativejs.com/resources/requestanimationframe/
requestAnimationFrame( animate ); //animate the frame
//setInterval(requestAnimationFrame( animate ), 2000); //animate the frame
render(); //render the frame
//setInterval(render(), 2000);//uncomment out this line if on autopilot
}, 1000 / fps);*/
}
function render() {
camControls.update( clock.getDelta() ); //update First Person Camera
camControls2.update( clock.getDelta() ); //update First Person Camera
camera2.position.copy( camera.position );
camera2.rotation.copy( camera.rotation );
camera2.up = new THREE.Vector3(-1,-1,-1);
camera2.rotateX(Math.PI/-2);
camera2.rotateZ((Math.PI/-2));
//camera2.rotateY(Math.PI/-1);
//camera2.up = new THREE.Vector3(0,-1,0);
//camera2.lookAt(new THREE.Vector3(-1, -1, -1));
//camera2.rotation.y = Math.PI;
camera2.rotateZ(Math.PI/2);
var time = performance.now() * 0.001; //Measure time for water
water.material.uniforms.time.value += 1.0 / 60.0; //speed of water motion(wind chop)
//setInterval(water.render(), 2000); //animate the frame
water.render(); //render water surface
//AutoPilot Specific Code:
renderer.render( scene, camera ); //render everything else
//^comment out this line if on autopilot
renderer2.render( scene2, camera2 );
//^comment out this line if on autopilot
//setInterval(renderer.render( scene, camera ), 2000);//uncomment out this line if on autopilot
//setInterval(renderer2.render( scene2, camera2 ), 2000);//uncomment out this line if on autopilot
//uncomment these lines if code is running on autopilot
/*sendToServer();//smartjava.org
//requestAnimationFrame( render );//smartjava.org
//renderer2.render( scene2, camera2 );
//sendToServer();//smartjava.org
//requestAnimationFrame( render );//smartjava.org
*/
}
function sendToServer() {//this whole thing = smartjava.org
var asString = renderer.domElement.toDataURL();
if (isOpen) {
frame++;
ws.send(str2ab(frame+asString));
}
}
function str2ab(str) {//this whole thing = smartjava.org
var buf = new ArrayBuffer(str.length);
var bufView = new Uint8Array(buf);
for (var i=0, strLen=str.length; i<strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
</script>
</body>
</html>