-
Notifications
You must be signed in to change notification settings - Fork 1
/
VRController.html
executable file
·261 lines (172 loc) · 7.63 KB
/
VRController.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>THREE.VRController demo</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<!-- Origin Trial Token, feature = WebVR (For Chrome M59+), origin = https://croquet.github.io, expires = 2017-08-18 -->
<meta http-equiv="origin-trial" data-feature="WebVR (For Chrome M59+)" data-expires="2017-08-18" content="AgGCVIGBMf2mGdyHZ+OqfhUhfnWU5QqQhx76L68BzK09+hWC2VIjTnXYBuqSwiZEJyefN3DE7PXZORm5QUpdNAwAAABTeyJvcmlnaW4iOiJodHRwczovL2Nyb3F1ZXQuZ2l0aHViLmlvOjQ0MyIsImZlYXR1cmUiOiJXZWJWUjEuMSIsImV4cGlyeSI6MTUwMzA5NjIwMH0=">
<style>
body { margin: 0; overflow: hidden; }
</style>
<script src="https://cdn.rawgit.com/mrdoob/three.js/r85/build/three.js"></script>
<script src="https://cdn.rawgit.com/mrdoob/three.js/r85/examples/js/controls/VRControls.js"></script>
<script src="https://cdn.rawgit.com/mrdoob/three.js/r85/examples/js/effects/VREffect.js"></script>
<script src="https://cdn.rawgit.com/mrdoob/three.js/r85/examples/js/vr/WebVR.js"></script>
<script src="https://cdn.rawgit.com/dataarts/dat.guiVR/master/build/datguivr.js"></script>
<script src="VRController.js"></script>
</head>
<body>
<script>
// Hi! I think you’re going to like this VR stuff :)
// This index.html file created by Jeff Nusz / http://custom-logic.com
// refactored and commented by Stewart Smith / http://stewartsmith.io
// because this stuff is fun and you can learn it with us!
// The real heart of this is VRController.js -- check it out!
// Does this browser support the WebVR API?
// Here’s how to download and configure one that does:
// https://webvr.rocks
if( WEBVR.isAvailable() === false ) document.body.appendChild( WEBVR.getMessage() )
// Regardless, let’s kick off the Three.js basics here:
var camera, scene, renderer, effect, controls
// We need a renderer, and our VREffect ... effect.
renderer = new THREE.WebGLRenderer({ antialias: true })
renderer.setClearColor( 0x101010 )
renderer.setPixelRatio( window.devicePixelRatio )
renderer.setSize( window.innerWidth, window.innerHeight )
document.body.appendChild( renderer.domElement )
effect = new THREE.VREffect( renderer )
// Camera, VRControls to move the camera as your headset moves,
// (NOTE that VRControls and VRController are NOT related!!),
// and a Scene to render it to.
camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 )
controls = new THREE.VRControls( camera )
controls.standing = true
scene = new THREE.Scene()
// This button is important. It toggles between normal in-browser view
// and the brand new WebVR in-your-goggles view!
WEBVR.getVRDisplay( function( display ){
document.body.appendChild( WEBVR.getButton( display, renderer.domElement ))
})
// Should things change in the future we’ve got you covered:
function onWindowResize(){
camera.aspect = window.innerWidth / window.innerHeight
camera.updateProjectionMatrix()
effect.setSize( window.innerWidth, window.innerHeight )
}
window.addEventListener( 'resize', onWindowResize, false )
// Ok, we’ve got the basic Three.js overhead out of the way.
// Now let’s add some stuff to our empty scene.
var torus = new THREE.Mesh(
new THREE.TorusKnotGeometry( 0.4, 0.15, 150, 20 ),
new THREE.MeshStandardMaterial({ roughness: 0.01, metalness: 0.2 })
)
torus.position.set( -0.25, 1.4, -1.5 )
scene.add( torus )
var floor = new THREE.Mesh(
new THREE.BoxGeometry( 6, 0.2, 6, 6, 1, 6 ),
new THREE.MeshStandardMaterial({
roughness: 1.0,
metalness: 0.0,
color: 0x909090,
wireframe: true
})
)
floor.position.y = -0.1
scene.add( floor )
var light = new THREE.DirectionalLight()
light.position.set( 1, 1.5, -0.5 )
scene.add( light )
scene.add( new THREE.HemisphereLight( 0x909090, 0x404040 ))
// DAT GUI for WebVR is just one of the coolest things ever.
// Huge, huge thanks to Jeff Nusz / http://custom-logic.com
// and Michael Chang / http://minmax.design for making this!!
// https://github.com/dataarts/dat.guiVR
dat.GUIVR.enableMouse( camera )
var gui = dat.GUIVR.create( 'Settings' )
gui.position.x = 0.15
gui.position.z = -1.00
gui.position.y = 1.5
gui.rotation.y = -Math.PI / 4
scene.add( gui )
gui.add( torus.position, 'x', -1, 1 ).step( 0.001 ).name( 'Position X' )
gui.add( torus.position, 'y', -1, 1 ).step( 0.001 ).name( 'Position Y' )
gui.add( torus.rotation, 'y', -Math.PI, Math.PI ).step( 0.001 ).name( 'Rotation' ).listen()
// Check this out: When THREE.VRController finds a new controller
// it will emit a custom “vr controller connected” event on the
// global window object. It uses this to pass you the controller
// instance and from there you do what you want with it.
window.addEventListener( 'vr controller connected', function( event ){
// Here it is, your VR controller instance.
// It’s really a THREE.Object3D so you can just add it to your scene:
var controller = event.detail
scene.add( controller )
// HEY HEY HEY! This is important. You need to make sure you do this.
// For 6DOF (room-scale) controllers we need to set the standingMatrix
// otherwise you’ll wonder why your controller appears on the floor
// instead of in your hands!
controller.standingMatrix = window.controls.getStandingMatrix()
// And for 3DOF (seated) controllers you need to set the controller.head
// to reference your camera. That way we can make an educated guess where
// your hand ought to appear based on the camera’s rotation.
controller.head = window.camera
// Right now your controller has no visual.
// It’s just an empty THREE.Object3D.
// Let’s fix that!
var
meshColorOff = 0xFF4040,
meshColorOn = 0xFFFF00,
controllerMaterial = new THREE.MeshStandardMaterial({
color: meshColorOff,
shading: THREE.FlatShading
}),
controllerMesh = new THREE.Mesh(
new THREE.CylinderGeometry( 0.005, 0.05, 0.1, 6 ),
controllerMaterial
),
handleMesh = new THREE.Mesh(
new THREE.BoxGeometry( 0.03, 0.1, 0.03 ),
controllerMaterial
)
controllerMesh.rotation.x = -Math.PI / 2
handleMesh.position.y = -0.05
controllerMesh.add( handleMesh )
controller.userData.mesh = controllerMesh// So we can change the color later.
controller.add( controllerMesh )
// Allow this controller to interact with DAT GUI.
var guiInputHelper = dat.GUIVR.addInputObject( controller )
scene.add( guiInputHelper )
// Button events. How easy is this?!
// We’ll just use the “primary” button -- whatever that might be ;)
// Check out the THREE.VRController.supported{} object to see
// all the named buttons we’ve already mapped for you!
controller.addEventListener( 'primary press began', function( event ){
event.target.userData.mesh.material.color.setHex( meshColorOn )
guiInputHelper.pressed( true )
})
controller.addEventListener( 'primary press ended', function( event ){
event.target.userData.mesh.material.color.setHex( meshColorOff )
guiInputHelper.pressed( false )
})
// Daddy, what happens when we die?
controller.addEventListener( 'disconnected', function( event ){
controller.parent.remove( controller )
})
})
function update(){
// Here’s VRController’s UPDATE goods right here:
// This one command in your animation loop is going to handle
// all the VR controller business you need to get done!
THREE.VRController.update()
// And here’s your standard update and render stuff.
// Nothing new to see here.
effect.requestAnimationFrame( update )
torus.rotation.y += 0.002
if( torus.rotation.y > Math.PI ) torus.rotation.y -= ( Math.PI * 2 )// Keep DAT GUI display tidy!
controls.update()
effect.render( scene, camera )
}
update()
</script>
</body>
</html>