-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
249 lines (182 loc) · 7.11 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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Three.js VR Boilerplate</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
</head>
<body>
<div id="info">
VR Boilerplate
</div>
<script src="js/vr/HelioWebXRPolyfill.js"></script>
<script type="module">
import * as THREE from './build/three.module.js';
import { BoxLineGeometry } from './jsm/geometries/BoxLineGeometry.js';
import { VRButton } from './jsm/webxr/VRButton.js';
import { XRControllerModelFactory } from './jsm/webxr/XRControllerModelFactory.js';
var clock = new THREE.Clock();
var camera, scene, raycaster, renderer;
var room; // Contains the main objects in the scene.
var controller, controllerGrip, tempMatrix = new THREE.Matrix4();
var intersectedObject, intersectedPosition; // Contains the object and intersected position of where the user is pointing.
var loader; // Texture loader
var floor;
var dolly; // The dolly contains the camera and the controller. Move the dolly to move the camera and ontroller.
init();
animate();
function init() {
var container = document.createElement( 'div' );
document.body.appendChild( container );
scene = new THREE.Scene();
scene.background = new THREE.Color( 0x505050 );
camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 100 );
dolly = new THREE.Group();
dolly.position.set( 0, 2, 0 );
dolly.add( camera );
scene.add( dolly );
room = new THREE.Group();
scene.add( room );
addLights();
loader = new THREE.TextureLoader();
// Add some dummy objects to view
addFloatingCubes();
addFloor();
raycaster = new THREE.Raycaster();
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.outputEncoding = THREE.sRGBEncoding;
renderer.xr.enabled = true;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
container.appendChild( renderer.domElement );
function onSelectStart() {
// Add code for when user presses their controller
}
function onSelectEnd() {
// Add code for when user releases the button on their controller
}
controller = renderer.xr.getController( 0 );
controller.addEventListener( 'selectstart', onSelectStart );
controller.addEventListener( 'selectend', onSelectEnd );
controller.addEventListener( 'connected', function ( event ) {
this.add( buildController( event.data ) );
} );
controller.addEventListener( 'disconnected', function () {
this.remove( this.children[ 0 ] );
} );
//controller.position.y += 5;
dolly.add( controller );
var controllerModelFactory = new XRControllerModelFactory();
controllerGrip = renderer.xr.getControllerGrip( 0 );
controllerGrip.add( controllerModelFactory.createControllerModel( controllerGrip ) );
dolly.add( controllerGrip );
window.addEventListener( 'resize', onWindowResize, false );
document.body.appendChild( VRButton.createButton( renderer ) );
}
function buildController( data ) {
// Create laser pointer line
switch ( data.targetRayMode ) {
case 'tracked-pointer':
var geometry = new THREE.BufferGeometry();
geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 0, 0, - 1 ], 3 ) );
geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( [ 0.5, 0.5, 0.5, 0, 0, 0 ], 3 ) );
var material = new THREE.LineBasicMaterial( { vertexColors: true, blending: THREE.AdditiveBlending } );
return new THREE.Line( geometry, material );
case 'gaze':
var geometry = new THREE.RingBufferGeometry( 0.02, 0.04, 32 ).translate( 0, 0, - 1 );
var material = new THREE.MeshBasicMaterial( { opacity: 0.5, transparent: true } );
return new THREE.Mesh( geometry, material );
}
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function addLights() {
scene.add( new THREE.HemisphereLight( 0x606060, 0x404040 ) );
var light = new THREE.DirectionalLight( 0x00ff00, 1, 100 );
light.position.set( 0, 10, 0 );
light.target.position.set( 0, 0, 0 );
light.castShadow = true;
scene.add( light );
}
function animate() {
renderer.setAnimationLoop( render );
}
function render() {
var delta = clock.getDelta();
// find what user is pointing at
tempMatrix.identity().extractRotation( controller.matrixWorld );
raycaster.ray.origin.setFromMatrixPosition( controller.matrixWorld );
raycaster.ray.direction.set( 0, 0, - 1 ).applyMatrix4( tempMatrix );
var intersects = raycaster.intersectObjects( room.children );
if ( intersects.length > 0 ) {
intersectedObject = intersects[0].object;
intersectedPosition = intersects[0].point;
// Rotate the object to show it is selected
intersectedObject.rotation.y += .1;
} else {
intersectedObject = undefined;
}
// Test moving the camera
//dolly.position.x += .01;
renderer.render( scene, camera );
}
function addFloatingCubes() {
var geometry = new THREE.BoxBufferGeometry( 0.15, 0.15, 0.15 );
loader.load('textures/antattack/antattack.png', function ( texture ) {
// Stop texture being blurred
texture.magFilter = THREE.NearestFilter;
texture.minFilter = THREE.NearestFilter;
// Basic material
//var material = new THREE.MeshBasicMaterial({map: texture});
// Light aware material
var material = new THREE.MeshLambertMaterial ({
map: texture,
combine: THREE.MixOperation,
reflectivity: .5
});
for ( var i = 0; i < 50; i ++ ) {
var object = new THREE.Mesh(geometry, material);
object.castShadow = true;
object.receiveShadow = true;
object.position.x = Math.random() * 4 - 2;
object.position.y = Math.random() * 4;
object.position.z = Math.random() * 4 - 2;
object.rotation.x = Math.random() * 2 * Math.PI;
object.rotation.y = Math.random() * 2 * Math.PI;
object.rotation.z = Math.random() * 2 * Math.PI;
room.add( object );
}
});
}
function addFloor() {
loader.load(
'textures/antattack/antattack.png',
texture => {
texture.wrapS = THREE.NearestFilter;
texture.wrapT = THREE.NearestFilter;
texture.magFilter = THREE.NearestFilter;
texture.minFilter = THREE.NearestFilter;
var woodMaterial = new THREE.MeshPhongMaterial({
map: texture
});
floor = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30, 32),
woodMaterial
);
floor.rotation.x = -Math.PI / 2;
floor.position.x = 0;
floor.position.y = 0;
floor.position.z = -10;
floor.receiveShadow = true;
scene.add(floor);
}
);
}
</script>
</body>
</html>