-
Notifications
You must be signed in to change notification settings - Fork 1
/
game-threejs-template.html
236 lines (194 loc) · 8.05 KB
/
game-threejs-template.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
<!DOCTYPE html>
<html>
<head>
<!-- This template provides a starting point for implementing a game that uses three.js for rendering -->
<title>Gameutils.js game</title>
<meta charset="UTF-8">
<!-- output -->
<script src="src/lib/howler.core.js"></script>
<script src="src/lib/tween.min.js"></script>
<script src="src/lib/three.min.js"></script>
<script src="src/lib/threejs/SkyShader.js"></script>
<script src="src/lib/threejs/CopyShader.js"></script>
<script src="src/lib/threejs/EffectComposer.js"></script>
<script src="src/lib/threejs/GLTFLoader.js"></script>
<script src="src/lib/threejs/MaskPass.js"></script>
<script src="src/lib/threejs/RenderPass.js"></script>
<script src="src/lib/threejs/ShaderPass.js"></script>
<script src="src/lib/threejs/SMAAShader.js"></script>
<script src="src/lib/threejs/SMAAPass.js"></script>
<script src="src/lib/threejs/SSAOShader.js"></script>
<!-- dev mode helpers -->
<script src="src/lib/dat.gui.js"></script>
<script src="src/lib/FileSaver.js"></script>
<!-- input -->
<script src="src/lib/mousetrap.js"></script>
<script src="src/lib/mousetrap-global-bind.js"></script>
<script type="module">
import { querystringUtil } from "./src/gjs/utiljs.js";
import { Audio } from "./src/gjs/legacy/audio.js";
import { LoadingBar } from "./src/gjs/loadingbar.js";
import { CanvasResizer } from "./src/gjs/canvasresizer.js";
import { commonUI } from "./src/gjs/commonui.js";
import { GameParameters } from "./src/gjs/gameparameters.js";
import { Gamepad } from "./src/gjs/gamepad.js";
import { InputMapper } from "./src/gjs/inputmapper.js";
import { startMainLoop } from "./src/gjs/mainloop.js";
import { utilTHREE } from "./src/gjs/threejs/utilthree.js";
import { seedrandom, random } from "./src/lib/seedrandom.js";
var Game = function(resizer, renderer, loadingBar) {
this.renderer = renderer;
this.renderer.setClearColor( 0xffffff, 1);
this.loadingBar = loadingBar;
this.scene = new THREE.Scene();
this.camera = new THREE.PerspectiveCamera( 75, resizer.canvas.width / resizer.canvas.height, 1, 10000 );
this.camera.position.z = 25;
this.camera.position.y = 3;
this.pointerRaycaster = new THREE.Raycaster();
this.time = 0;
this.initializedAfterLoad = false;
var numPlayers = 1;
this.input = new InputMapper(this, numPlayers);
// Example usage of InputMapper
//this.input.addListener(Gamepad.BUTTONS.UP_OR_ANALOG_UP, ['up', 'w'], this.upPress, this.upRelease);
if (DEV_MODE) {
this.gridHelpers = new THREE.Object3D();
this.scene.add(this.gridHelpers);
var axesHelper = new THREE.AxesHelper(5);
this.gridHelpers.add(axesHelper);
var gridHelper = new THREE.GridHelper(20, 20);
this.gridHelpers.add(gridHelper);
this.input.addListener(undefined, ['0'], this.devModeTakeScreenshot);
}
this.takeScreenshot = false;
};
Game.prototype.loadedInit = function() {
// Called after all the assets like 3D models and fonts have been loaded.
// Test geometry
var geometry = new THREE.BoxGeometry( 5, 5, 5 );
var material = new THREE.MeshBasicMaterial( { color: 0xff0000, wireframe: true } );
this.mesh = new THREE.Mesh( geometry, material );
this.scene.add(this.mesh);
};
Game.prototype.render = function() {
if (DEV_MODE) {
this.gridHelpers.visible = Game.parameters.get('gridHelpersVisible');
}
this.renderer.render(this.scene, this.camera);
var that = this;
if (this.takeScreenshot) {
this.renderer.domElement.toBlob(function(blob) {
saveAs(blob, 'screenshot.png');
that.takeScreenshot = false;
});
this.takeScreenshot = false;
}
return this.renderer;
};
Game.prototype.update = function(deltaTime) {
this.time += deltaTime;
this.input.update();
// Update your level here
if (this.mesh) {
this.mesh.rotation.x += 0.01;
this.mesh.rotation.y += 0.02;
var intersects = this.pointerRaycaster.intersectObject(this.mesh, true);
this.mesh.material.wireframe = (intersects.length === 0);
}
Audio.muteAll(Game.parameters.get('muteAudio'));
// Call initialization function after all model assets have been loaded.
if (this.loadingBar.finished() && !this.initializedAfterLoad) {
this.loadedInit();
this.initializedAfterLoad = true;
}
};
/**
* Mouse/touch handler for pressing down a mouse button or touch.
* @param {Object} event With following keys:
* currentPosition: Vec2 with current pointer coordinates in normalized device coordinates.
* lastDown: Vec2 with coordinates of the latest press event in normalized device coordinates.
* isDown: Boolean telling if the pointer is down.
* index: Integer index of the pointer being tracked.
*/
Game.prototype.canvasPress = function(event) {
};
/**
* Mouse/touch handler for releasing a mouse button or touch.
* @param {Object} event With following keys:
* currentPosition: Vec2 with current pointer coordinates in normalized device coordinates.
* lastDown: Vec2 with coordinates of the latest press event in normalized device coordinates.
* isDown: Boolean telling if the pointer is down.
* index: Integer index of the pointer being tracked.
*/
Game.prototype.canvasRelease = function(event) {
};
/**
* Mouse/touch handler when a pointer is being moved.
* @param {Object} event With following keys:
* currentPosition: Vec2 with current pointer coordinates in normalized device coordinates.
* lastDown: Vec2 with coordinates of the latest press event in normalized device coordinates.
* isDown: Boolean telling if the pointer is down.
* index: Integer index of the pointer being tracked.
*/
Game.prototype.canvasMove = function(event) {
var positionAsVec3 = new THREE.Vector3(event.currentPosition.x, event.currentPosition.y, 0);
this.pointerRaycaster.setFromCamera(positionAsVec3, this.camera);
};
/**
* Set the takeScreenshot flag so that a screenshot is taken on the next frame.
*/
Game.prototype.devModeTakeScreenshot = function() {
this.takeScreenshot = true;
};
// Parameters added here can be tuned at run time when in developer mode
Game.parameters = new GameParameters({
'gridHelpersVisible': {initial: false},
'muteAudio': {initial: false}
});
var DEV_MODE = querystringUtil.get('devMode') !== undefined;
window['start'] = function() {
var DEBUG_MAIN_LOOP = DEV_MODE && true; // Set to true to allow fast-forwarding main loop with 'f'
Game.parameters.set('muteAudio', (DEV_MODE && true)); // Set to true if sounds annoy developers
seedrandom();
var game;
var renderer = new THREE.WebGLRenderer();
var canvasWrapper = document.createElement('div');
canvasWrapper.appendChild(renderer.domElement);
commonUI.createUI({
parent: canvasWrapper,
fullscreenElement: document.body,
twitterAccount: 'Oletus',
fillStyle: '#000000',
opacity: 0.5,
scale: 1
});
var resizer = new CanvasResizer({
mode: CanvasResizer.Mode.FIXED_ASPECT_RATIO,
canvas: renderer.domElement,
wrapperElement: canvasWrapper,
width: 16,
height: 9,
setCanvasSizeCallback: function(width, height) {
renderer.setSize(width, height);
if (game !== undefined) {
game.camera.aspect = width / height;
game.camera.updateProjectionMatrix();
}
}
});
var loadingBar = new LoadingBar();
game = new Game(resizer, renderer, loadingBar);
// Create event handlers for mouse and touch based input that will call on the canvas* members of game.
resizer.createPointerEventListener(game, true,
CanvasResizer.EventCoordinateSystem.WEBGL_NORMALIZED_DEVICE_COORDINATES);
// Initialize after CanvasResizer so it is always drawn on top
if (DEV_MODE) {
Game.parameters.initGUI();
}
startMainLoop([resizer, game, loadingBar, resizer.pixelator()], {debugMode: DEBUG_MAIN_LOOP});
};
</script>
</head>
<body onload="window.start()" style="background: black; height: 100%">
</body>
</html>