-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paththree.setup.js
390 lines (294 loc) · 10.3 KB
/
three.setup.js
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
"use strict";
module.exports = exports = function( VR ) {
if( VR ) {
require( './three.js/AltSpace_WebVR_fill.js');
//if( AltSpace )
// require( './three.js/js/controls/AltSpaceControls.js' );
//else
// require( './three.js/js/controls/VRControls.js' );
// require( './three.js/js/effects/VREffect.js' );
// require( './three.js/js/effects/StereoEffect.js' );
require( './three.js/js/vr/ViveController.js' );
//require( './three.js/js/vr/PaintViveController.js' );
require( './src/VoxelariumViveController.js' );
require( './three.js/js/vr/WebVR.js' );
require( './three.js/js/loaders/OBJLoader.js' );
}
if( !VR ) {
//require( "./controls/gameMouse.js" )
require( "./controls/orbit_controls.js" )
require( "./controls/NaturalCamera.js" )
}
const physics = require( "./oimo_physics.js" );
var controlNatural;
var controlOrbit;
var controlGame;
var controls;
var user;
//var effect; // the actual thing to render for VR?
var render; // callback for animate.
var scene;
var scene2;
var scene3;
var camera, renderer;
var space; // the actual space objects are in.
var light;
var geometry, material, mesh = [];
var frame_target = [];
var slow_animate = false;
var frame = 0;
var tests = [];
var screen = { width:window.innerWidth, height:window.innerHeight };
//const totalUnit = Math.PI/(2*60);
//const unit = totalUnit;
var delay_counter = 60*3;
//const pause_counter = delay_counter + 120;
var single_counter = 60;
var totalUnit = Math.PI/2;
var unit = totalUnit / single_counter;
var pause_counter = 120;
var counter= 0;
var clock = new THREE.Clock()
function setControls1() {
controls && controls.disable();
camera.matrixAutoUpdate = false;
controls = controlNatural;
controls && controls.enable(camera);
}
function setControls2() {
controls && controls.disable();
camera.matrixAutoUpdate = false; // current mode doesn't auto update
controls = controlOrbit;
controls && controls.enable(camera);
}
function setControls3() {
controls && controls.disable();
camera.matrixAutoUpdate = false; // current mode doesn't auto update
controls = controlGame;
controls && controls.enable(camera);
}
var status_line;
function init() {
document.getElementById( "controls1").onclick = setControls1;
document.getElementById( "controls2").onclick = setControls2;
document.getElementById( "controls3").onclick = setControls3;
scene = new THREE.Scene();
space = new THREE.Group();
scene.add( space );
scene2 = new THREE.Scene();
scene3 = new THREE.Scene();
var user = new THREE.Group();
user.position.set( 0, 0, 0 );
scene.add( user );
if( VR )
//space.scale.set( 0.0254, 0.0254, 0.0254 ); // make 1 an inch
space.scale.set( 0.0254*4, 0.0254*4, 0.0254*4 ); // make 1 an inch
camera = new THREE.PerspectiveCamera( 90, window.innerWidth / window.innerHeight, 0.001, 10000 );
user.add( camera );
if( !VR ){
camera.matrixAutoUpdate = false;
camera.position.z = 5;
camera.matrixWorldNeedsUpdate = true;
}
// for phong hello world test....
var light = new THREE.PointLight( 0xffFFFF, 1, 1000 );
light.position.set( 0, -100, 100 );
scene.add( light );
var light = new THREE.PointLight( 0xffFFFF, 1, 1000 );
light.position.set( 0, 100, 100 );
scene.add( light );
var light = new THREE.PointLight( 0xffFFFF, 1, 1000 );
light.position.set( 0, -100, -100 );
scene.add( light );
var light = new THREE.PointLight( 0xffFFFF, 1, 1000 );
light.position.set( -500, 500, 1 );
scene.add( light );
var light = new THREE.PointLight( 0xffFFFF, 1, 1000 );
light.position.set( 500, -500, 1 );
scene.add( light );
/* INIT GOES HERE? */
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.vr.enabled = VR;
//if ( !renderer.extensions.get('WEBGL_depth_texture') ) {
// supportsExtension = false;
// console.log( "depth texture not available" );
// document.querySelector('#error').style.display = 'block';
// return;
// }
document.body.appendChild( renderer.domElement );
if ( typeof WEBVR !== "undefined" ) {
var button = WEBVR.createButton( renderer, ()=>{
require( "./controls/orbit_controls.js" )
require( "./controls/NaturalCamera.js" )
if( THREE.NaturalControls ) {
controlNatural = new THREE.NaturalControls( camera, renderer.domElement );
controlNatural.disable();
}
if( THREE.OrbitControls ) {
controlOrbit = new THREE.OrbitControls( camera, renderer.domElement );
controlOrbit.disable();
}
if( THREE.GameMouse ) {
controlGame = new THREE.GameMouse( camera, renderer.domElement );
controlGame.disable();
}
space.scale.set( 1,1,1 ); // make 1 an inch
render = fallbackRender;
renderer.animate( render );
scene.remove( user );
scene.add( camera );
document.body.removeChild( button );
} );
document.body.appendChild( button );
}
if( THREE.NaturalControls ) {
controlNatural = new THREE.NaturalControls( camera, renderer.domElement );
controlNatural.disable();
}
if( THREE.OrbitControls ) {
controlOrbit = new THREE.OrbitControls( camera, renderer.domElement );
controlOrbit.disable();
}
if( THREE.GameMouse ) {
controlGame = new THREE.GameMouse( camera, renderer.domElement );
controlGame.disable();
}
if( VR ) {
function handleController( controller ) {
// update controller object from VR input (gamepad)
controller.update();
return;
var pivot = controller.getObjectByName( 'pivot' );
if ( pivot ) {
pivot.material.color.copy( controller.getColor() );
var matrix = pivot.matrixWorld;
var point1 = controller.userData.points[ 0 ];
var point2 = controller.userData.points[ 1 ];
var matrix1 = controller.userData.matrices[ 0 ];
var matrix2 = controller.userData.matrices[ 1 ];
point1.setFromMatrixPosition( matrix );
matrix1.lookAt( point2, point1, THREE.Vector3Up );
if ( controller.getButtonState( 'trigger' ) ) {
// this is the old drawing code...
//stroke( controller, point1, point2, matrix1, matrix2 );
}
point2.copy( point1 );
matrix2.copy( matrix1 );
}
}
var controller1;
var controller2;
function setupViveControls( scene ) {
// update camera poosition from VR inputs...
//if( Voxelarium.Settings.AltSpace ) {
// controls = new THREE.AltSpaceControls( camera );
//} else
//effect = new THREE.VREffect( renderer );
//effect = new THREE.StereoEffect( renderer );
//effect.autoSubmitFrame = false;
//effect.autoClear = true;
//effect.setSize( window.innerWidth*4/5, window.innerHeight*4/5 );
//controls = new THREE.VRControls( camera );
//controls.standing = true;
//scene.add( new THREE.HemisphereLight( 0x888877, 0x777788 ) );
var headLight = new THREE.DirectionalLight( 0xffffff );
headLight.position.set( 0, -0.02, 0 );
//headLight.castShadow = true;
headLight.shadow.camera.top = 2;
headLight.shadow.camera.bottom = -2;
headLight.shadow.camera.right = 2;
headLight.shadow.camera.left = -2;
headLight.shadow.mapSize.set( 4096, 4096 );
//camera.add( headLight );
// controllers
controller1 = new VESL.ViveController( 0 );
controller1.standingMatrix = renderer.vr.getStandingMatrix();
controller1.userData.points = [ new THREE.Vector3(), new THREE.Vector3() ];
controller1.userData.matrices = [ new THREE.Matrix4(), new THREE.Matrix4() ];
controller1.userData.altspace = { collider: { enabled: false } };
user.add( controller1 );
controller2 = new VESL.ViveController( 1 );
controller2.standingMatrix = renderer.vr.getStandingMatrix();
controller2.userData.points = [ new THREE.Vector3(), new THREE.Vector3() ];
controller2.userData.matrices = [ new THREE.Matrix4(), new THREE.Matrix4() ];
controller2.userData.altspace = { collider: { enabled: false } };
user.add( controller2 );
var loader = new THREE.OBJLoader();
loader.setPath( 'models/obj/vive-controller/' );
loader.load( 'vr_controller_vive_1_5.obj', function ( object ) {
var loader = new THREE.TextureLoader();
loader.setPath( 'models/obj/vive-controller/' );
var controller = object.children[ 0 ];
controller.material.map = loader.load( 'onepointfive_texture.png' );
controller.material.specularMap = loader.load( 'onepointfive_spec.png' );
controller.castShadow = true;
controller.receiveShadow = true;
// var pivot = new THREE.Group();
// var pivot = new THREE.Mesh( new THREE.BoxGeometry( 0.01, 0.01, 0.01 ) );
var pivot = new THREE.Mesh( new THREE.IcosahedronGeometry( 0.002, 2 ) );
pivot.name = 'pivot';
pivot.position.y = -0.016;
pivot.position.z = -0.043;
pivot.rotation.x = Math.PI / 5.5;
controller.add( pivot );
controller1.add( controller.clone() );
pivot.material = pivot.material.clone();
controller2.add( controller.clone() );
} );
}
setupViveControls( scene );
var _tick;
function _render( tick ) {
let delta;
if( !_tick ) { _tick = tick; return; }
else delta = ( tick - _tick ) / 1000;
_tick = tick;
//if( controls )
// controls.update(delta);
if( controller1 ) {
handleController( controller1 );
handleController( controller2 );
}
//console.log( "tick")
//if( frame++ > 10 ) return
//if( slow_animate )
// requestAnimationFrame( slowanim );
// else
// requestAnimationFrame( animate );
//var unit = Math.PI/2; //worst case visible
//renderer.clear();
//console.log( "camera matrix:", JSON.stringify( camera.matrix ) );
renderer.render( scene, camera );
}
render = _render;
}
else {
controls = controlNatural;
if( controls )
controls.enable();
render = fallbackRender;
}
}
var _tick;
function fallbackRender( tick ) {
let delta;
if( !_tick ) { _tick = tick; return; }
else delta = ( tick - _tick ) / 1000;
_tick = tick;
if( controls )
controls.update(delta);
physics.update( delta );
renderer.render( scene, camera );
}
var nFrame = 0;
var nTarget = 60;
var nTarget2 = 120;
function beginAnimate() {
renderer.animate( render );
}
init();
beginAnimate();
return space;
}