-
Notifications
You must be signed in to change notification settings - Fork 4
/
example-car.html
178 lines (146 loc) · 4.35 KB
/
example-car.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
<!DOCTYPE html>
<html>
<head>
<title>DIBox -- Car Style Example</title>
<style>
html, body {
height: 100%;
margin: 0;
overflow: hidden;
}
#info {
text-align: center;
position: absolute;
width: 100%;
font-family: monospace;
}
button {
position: absolute;
right: 20px;
top: 20px;
}
#vis {
position: absolute;
top: 20px;
left: 20px;
font-family: sans-serif;
}
#vis.hidden {
display: none;
}
#steering {
margin: 20px 0;
border: solid 1px gray;
position: relative;
width: 100px;
height: 17px;
padding-right: 3px;
}
#steering span {
position: absolute;
top: 0;
left: 50%;
}
.input-active {
color: green;
}
</style>
<script src="Three.js"></script>
<script src="require.js"></script>
<script src="game-shim.js"></script>
</head>
<body>
<div id="info">
DIBox -- Car Style Example<br>accel/brake: W/S | steering: mouse | boost: mouse button | toggle vis: T
</div>
<button
onclick="document.documentElement.requestFullscreen();GameShim.supports.pointerLock && document.documentElement.requestPointerLock();">
Fullscreen
</button>
<div id="vis">
<div id="accel">Accelerate</div>
<div id="brake">Brake</div>
<div id="boost">Boost</div>
<div id="steering"><span>|</span></div>
</div>
<script type="text/javascript">
require([
// get DIBox:
'../build/dibox',
// and the bindings file:
'bindings-car'
], function (DIBox, bindings) {
// setup some vars:
var camera, scene, renderer;
var SCREEN_HEIGHT = window.innerHeight;
var SCREEN_WIDTH = window.innerWidth;
var speed = 0,
steering = 0;
var visualisationNodes;
// tell DIBox about the bindings config
DIBox.setBindings(bindings);
// setup the WebGL world:
setupWorld();
// and go!
render();
function render () {
requestAnimationFrame(render);
visualizeInput();
// read current user input state from DIBox's `input` object:
speed += (DIBox.input.accelerate - DIBox.input.brake) / 50;
speed = THREE.Math.clamp(speed, -0.5, 1);
if (DIBox.input.boost) {
speed *= 1.5;
}
camera.translateZ(-speed);
camera.rotation.y += DIBox.input.steering / 100 * speed;
if (DIBox.input.toggleSomething) {
visualisationNodes.wrapper.classList.toggle('hidden');
DIBox.input.toggleSomething = false;
}
renderer.render(scene, camera);
}
function visualizeInput () {
if (!visualisationNodes) {
visualisationNodes = {
wrapper: document.getElementById('vis'),
steering: document.getElementById('steering').firstChild,
accelerate: document.getElementById('accel'),
brake: document.getElementById('brake'),
boost: document.getElementById('boost')
};
}
visualisationNodes.steering.style.left = ( DIBox.input.steering * -50 + 50 ) + 'px';
visualisationNodes.accelerate.className = DIBox.input.accelerate ? 'input-active' : '';
visualisationNodes.brake.className = DIBox.input.brake ? 'input-active' : '';
visualisationNodes.boost.className = DIBox.input.boost ? 'input-active' : '';
}
function setupWorld () {
/* Scene & Camera */
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(25, SCREEN_WIDTH / SCREEN_HEIGHT);
camera.position.y = 10;
scene.add(camera);
/* Renderer */
renderer = new THREE.WebGLRenderer({ antialias: true, preserveDrawingBuffer: true });
renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
document.body.appendChild(renderer.domElement);
/* Lights */
var ambientLight = new THREE.AmbientLight(0x606060);
scene.add(ambientLight);
/* Ground */
var plane = new THREE.Mesh(new THREE.PlaneGeometry(1000, 1000, 10, 10), new THREE.MeshBasicMaterial({ color: 0x808080, wireframe: true }));
scene.add(plane);
window.addEventListener('resize', onResize, false);
}
function onResize () {
SCREEN_HEIGHT = window.innerHeight;
SCREEN_WIDTH = window.innerWidth;
renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
camera.updateProjectionMatrix();
}
});
</script>
</body>
</html>