-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.html
154 lines (125 loc) · 4.88 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
<!-- This project uses codes from threejs examples (https://threejs.org/), WebVR (https://webvr.info/), and mrdoob / http://mrdoob.com. -->
<!DOCTYPE html>
<html lang="en">
<head>
<title>PennVR Head Gazer</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<style>
body {
font-family: Monospace;
background-color: #101010;
color: #fff;
margin: 0px;
overflow: hidden;
}
a {
color: #f00;
}
</style>
</head>
<body>
<script src="js/threejs/three.min.js"></script>
<script src="js/controls/PointerLockControl.js"></script>
<div id="blocker">
<div id="instructions">
<span style="font-size:40px">Click to play</span>
<br />
MOUSE = Look around
</div>
</div>
<script>
var floor;
var camera, scene, renderer;
var geometry, material, mesh;
var controls;
var cubes = [];
var raycaster;
var blocker = document.getElementById( 'blocker' );
var instructions = document.getElementById( 'instructions' );
// http://www.html5rocks.com/en/tutorials/pointerlock/intro/
var havePointerLock = 'pointerLockElement' in document || 'mozPointerLockElement' in document || 'webkitPointerLockElement' in document;
if ( havePointerLock ) {
var element = document.body;
var pointerlockchange = function ( event ) {
if ( document.pointerLockElement === element || document.mozPointerLockElement === element || document.webkitPointerLockElement === element ) {
controls.enabled = true;
blocker.style.display = 'none';
} else {
controls.enabled = false;
blocker.style.display = '-webkit-box';
blocker.style.display = '-moz-box';
blocker.style.display = 'box';
instructions.style.display = '';
}
};
var pointerlockerror = function ( event ) {
instructions.style.display = '';
};
// Hook pointer lock state change events
document.addEventListener( 'pointerlockchange', pointerlockchange, false );
document.addEventListener( 'mozpointerlockchange', pointerlockchange, false );
document.addEventListener( 'webkitpointerlockchange', pointerlockchange, false );
document.addEventListener( 'pointerlockerror', pointerlockerror, false );
document.addEventListener( 'mozpointerlockerror', pointerlockerror, false );
document.addEventListener( 'webkitpointerlockerror', pointerlockerror, false );
instructions.style.display = 'none';
// Ask the browser to lock the pointer
element.requestPointerLock = element.requestPointerLock || element.mozRequestPointerLock || element.webkitRequestPointerLock;
element.requestPointerLock();
instructions.addEventListener( 'click', function ( event ) {
instructions.style.display = 'none';
// Ask the browser to lock the pointer
element.requestPointerLock = element.requestPointerLock || element.mozRequestPointerLock || element.webkitRequestPointerLock;
element.requestPointerLock();
}, false );
} else {
instructions.innerHTML = 'Your browser doesn\'t seem to support Pointer Lock API';
}
init();
animate();
function init() {
// === LOOK: Setup camera
camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 1000 );
scene = new THREE.Scene();
// === LOOK: Setup light
var light = new THREE.HemisphereLight( 0xeeeeff, 0x777788, 0.75 );
light.position.set( 0.5, 1, 0.75 );
scene.add( light );
// === LOOK: Get pointer lock controls
controls = new THREE.PointerLockControls( camera );
scene.add( controls.getObject() );
// === TODO: Create ray caster
// === LOOK: Setup floor
geometry = new THREE.PlaneGeometry( 2000, 2000, 100, 100 );
geometry.rotateX( - Math.PI / 2 );
material = new THREE.MeshBasicMaterial( { color: 0xeeeeee } );
floor = new THREE.Mesh( geometry, material );
scene.add( floor );
// === LOOK: Setup cubes
geometry = new THREE.BoxGeometry( 20, 20, 20 );
for ( var i = 0; i < 500; i ++ ) {
material = new THREE.MeshPhongMaterial( { color: 0x11eeee, specular: 0xffffff } );
var mesh = new THREE.Mesh( geometry, material );
mesh.position.x = Math.floor( Math.random() * 20 - 10 ) * 20;
mesh.position.y = Math.floor( Math.random() * 20 ) * 20 + 10;
mesh.position.z = Math.floor( Math.random() * 20 - 10 ) * 20;
scene.add( mesh );
cubes.push( mesh );
}
// === Setup renderer
renderer = new THREE.WebGLRenderer();
renderer.setClearColor( 0xffffff );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
selecting = false;
}
function animate() {
requestAnimationFrame( animate );
// TODO: Ray caster
renderer.render( scene, camera );
}
</script>
</body>
</html>