-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvrmain.html
306 lines (266 loc) · 10.6 KB
/
vrmain.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
<!DOCTYPE html>
<!-- @author quinnciccoretti -->
<!-- @author mrdoob -->
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>physvr</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<!-- Origin Trial Token, feature = WebVR (For Chrome M62+), origin = https://threejs.org, expires = 2018-01-21 -->
<meta http-equiv="origin-trial" data-feature="WebVR (For Chrome M62+)" data-expires="2018-01-21" content="Ap+tmQjuEXRrzmu+ZuReB/eY8fZmTEOybJ2hME1RgEZAPzhZ4/IAKEHRebiTp6o4LoZOMBexL4GAL7flIkAUlQYAAABQeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJWUjEuMU02MiIsImV4cGlyeSI6MTUxNjUzODYxMX0=">
<style>
body {
font-family: Monospace;
background-color: #101010;
color: #fff;
margin: 0px;
overflow: hidden;
}
a {
color: #f00;
}
</style>
<!-- For threejs, physics, and webvr -->
<script src="./js/three.min.js"></script>
<script type="text/javascript" src="js/stats.js"></script>
<script type="text/javascript" src="js/scene.js"></script>
<script src = "./js/ui.js"></script>
<script type="text/javascript" src="./js/phys/physi.js"></script>
<script src="./js/WebVR.js"></script>
<!-- Lower level controller classes -->
<script src="./js/controllers/ViveController.js"></script>
<script src="./js/controllers/BasicController.js"></script>
<!-- Custom controller classes, extend BasicController -->
<script src="./js/controllers/PaintController.js"></script>
<script src="./js/controllers/MoveController.js"></script>
<script src="./js/controllers/DragController.js"></script>
<script src="./js/controllers/SlingshotController.js"></script>
<script src="./js/controllers/LBowController.js"></script>
<script src="./js/controllers/RBowController.js"></script>
<script src="./js/controllers/CreateController.js"></script>
<script src="./js/controllers/RefreshController.js"></script>
<!-- <script src="./js/controllers/BatController.js"></script> -->
<script src="./js/controllers/GravityController.js"></script>
<!-- model loaders -->
<script src="./js/OBJLoader.js"></script>
<script src="./js/MTLLoader.js"></script>
</head>
<body>
<script>
'use strict';
// initialize all global vars
Physijs.scripts.worker = '/js/phys/physijs_worker.js';
Physijs.scripts.ammo = '/js/phys/ammo.js';
var container;
var camera, scene, renderer;
var user;
var basic_controller_model1, basic_controller_model2, basic_controller_models = [];
var controller1, controller2;
var handleController;
var controller_offset_z = -0.07;
var controller_offset_y = 0.03;
//for dragcontroller
var intersected = [];
var raycaster = new THREE.Raycaster();
var tempMatrix = new THREE.Matrix4();
//for paintcontroller
var count;
var block_material, block_geometry;
var phys_obj1, phys_obj2;
var phys_obj_list = [];
var load_model;
var line;
var shapes = {};
var createTower;
var up = new THREE.Vector3( 0, 1, 0 );
var vector = new THREE.Vector3();
var vector1 = new THREE.Vector3();
var vector2 = new THREE.Vector3();
var vector3 = new THREE.Vector3();
var vector4 = new THREE.Vector3();
var point4 = new THREE.Vector3();
var point5 = new THREE.Vector3();
window.onload = preload();
var render_stats, physics_stats;
/**
* Create stats element
*/
function init_stats(){
render_stats = new Stats();
render_stats.domElement.style.position = 'absolute';
render_stats.domElement.style.top = '1px';
render_stats.domElement.style.zIndex = 100;
container.appendChild( render_stats.domElement );
physics_stats = new Stats();
physics_stats.domElement.style.position = 'absolute';
physics_stats.domElement.style.top = '50px';
physics_stats.domElement.style.zIndex = 100;
container.appendChild( physics_stats.domElement );
}
function preload(){
console.log("preloading...")
init_font_loader();
init();
}
/**
* Main initialization
*/
function init() {
console.log("init....")
container = document.createElement( 'div' );
document.body.appendChild( container );
init_stats();
load_materials();
scene = new Physijs.Scene;
scene.background = new THREE.Color( 0x162354 );
// scene = new Physijs.Scene({ fixedTimeStep: 1 / 120 });
scene.setGravity(new THREE.Vector3( 0, -10, 0 ));
user = new THREE.Group();
user.position.set( 3, 1.6, 0 ); //a little to the side of the origin
scene.add( user );
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 50 );
user.add( camera );
//add floor, grid, table, etc.
create_scene_objects(0,1.0,0);
scene.add( new THREE.HemisphereLight( 0x888877, 0x777788, .4) );
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.gammaInput = true;
renderer.gammaOutput = true;
renderer.shadowMap.enabled = true;
renderer.vr.enabled = true; //VR REQUIRED
container.appendChild( renderer.domElement );
document.body.appendChild( WEBVR.createButton( renderer ) ); //VR REQUIRED
//Set up basic controllers while a list of other controllers loads
controller1 = new BasicController( 0 );
controller1.userData.points = [ new THREE.Vector3(), new THREE.Vector3() ];
controller1.userData.matrices = [ new THREE.Matrix4(), new THREE.Matrix4() ];
controller1.position.set(0,10,0); //start controller high and away to avoid collisions
user.add( controller1 );
controller2 = new BasicController( 1 );
controller2.userData.points = [ new THREE.Vector3(), new THREE.Vector3() ];
controller2.userData.matrices = [ new THREE.Matrix4(), new THREE.Matrix4() ];
controller2.position.set(0,10,0);
user.add( controller2 ); //start controller high and away to avoid collisions
//gets the model for the vive controller. This needs to be done beforehand as it is slow and asyncy
load_basic_model();
//toggle the controller mode
controller1.addEventListener( 'menudown', on_menu_up1 );
controller2.addEventListener( 'menudown', on_menu_up2 );
initGeometry();
// createTower(10);
/**
* If you want the controllers to be able to push things, you need to make an
* invisible box that follows them around. You can't use .add becuase the
* physics engine wants its own coords. So you just update the position
* of two boxes so they follow the controls, and make them invisible
*/
var physGeom = new THREE.CubeGeometry(0.12, 0.1, 0.22);
var physMaterial = new Physijs.createMaterial(new THREE.MeshBasicMaterial({}), 0.5, 0.5);
physMaterial.visible = false; //Make them invisible
phys_obj1 = new Physijs.BoxMesh(physGeom, physMaterial, 100);
phys_obj2 = new Physijs.BoxMesh(physGeom, physMaterial, 100);
phys_obj1.addEventListener( 'collision', function( other_object, relative_velocity, relative_rotation, contact_normal ) {
controller1.pulse(.25, 25);
//by setting object.userData.ctrlr_nocollide to true, you can make it not collide with the controller
if(typeof other_object.userData.ctrlr_nocollide !== "undefined"){
other_object.setLinearVelocity(0,0,0);
}
});
phys_obj1.material.wireframe = true;
phys_obj2.addEventListener( 'collision', function( other_object, relative_velocity, relative_rotation, contact_normal ) {
controller2.pulse(.25, 25);
//by setting object.userData.ctrlr_nocollide to true, you can make it not collide with the controller
if(typeof other_object.userData.ctrlr_nocollide !== "undefined"){
other_object.setLinearVelocity(0,0,0);
}
});
scene.add(phys_obj1);
scene.add(phys_obj2);
//helps updated phys objs in basiccontroller
phys_obj_list = [phys_obj1,phys_obj2];
// Start simulation
requestAnimationFrame( render );
scene.simulate();
}
var ctrlr1list = [
new PaintController( 0 ),
new MoveController( 0 ),
new DragController( 0 ),
new SlingshotController( 0 ),
new LBowController(0),
new CreateController(0),
new RefreshController(0),
new GravityController( 0 )
// new THREE.BatController(0)
];
var current_controller1 = 0;
/**
* Toggles 1st ctrlr through various controller objects
*/
function on_menu_up1(){
//The below line is essential. It is located in BasicController
// and may be overriden
controller1.on_deactivate();
current_controller1++;
if(current_controller1 == ctrlr1list.length){
current_controller1 = 0;
}
controller1 = ctrlr1list[current_controller1];
//The below line is essential. It is located in BasicController
// and may be overriden
controller1.on_activate();
controller1.addEventListener( 'menuup', on_menu_up1 );
}
var current_controller2 = 0;
var ctrlr2list = [
new PaintController( 1 ),
new MoveController( 1 ),
new DragController( 1 ),
new SlingshotController( 1 ),
new RefreshController(1),
new RBowController(1),
new GravityController( 1 ),
new CreateController(1)
];
/**
* Toggles 2nd ctrlr through various controller objects
*/
function on_menu_up2(){
//The below line is essential. It is located in BasicController
// and may be overriden
controller2.on_deactivate();
current_controller2++;
if(current_controller2 == ctrlr2list.length){
current_controller2 = 0;
}
controller2 = ctrlr2list[current_controller2];
//The below line is essential. It is located in BasicController
// and may be overriden
controller2.on_activate();
controller2.addEventListener( 'menuup', on_menu_up2 );
}
/**
* Main loop. Called everytime the physics sim updates. All update code runs from here.
*/
function render() {
//controllers define their own handle method which calls .update() and updates physics objs
count = line.geometry.drawRange.count;
controller1.handle_update();
controller2.handle_update();
scene.simulate(); // run physics
requestAnimationFrame( render );
renderer.render( scene, camera );
render_stats.update(); //update stats
physics_stats.update();
}
</script>
<div>
<div style="position: absolute; top: 10px; width: 100%; text-align: center;">
<a href="./../" target="_blank" rel="noopener">physvr</a>
</div>
<canvas width="3024" height="1680" style="width: 1280px; height: 918px;"></canvas>
</div>
</body>
</html>