-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__start__.ts
162 lines (133 loc) · 5.49 KB
/
__start__.ts
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
import * as pc from "playcanvas";
import {loadModules} from "./__modules__";
export function startApplication(options) {
const CANVAS_ID = 'application-canvas';
function createCanvas() {
canvas = document.createElement('canvas');
canvas.setAttribute('id', CANVAS_ID);
canvas.setAttribute('tabindex', 0);
// canvas.style.visibility = 'hidden';
// Disable I-bar cursor on click+drag
canvas.onselectstart = function () { return false; };
document.body.appendChild(canvas);
return canvas;
};
function createInputDevices(canvas) {
let devices = {
elementInput: new pc.ElementInput(canvas, {
useMouse: options.INPUT_SETTINGS.useMouse,
useTouch: options.INPUT_SETTINGS.useTouch
}),
keyboard: options.INPUT_SETTINGS.useKeyboard ? new pc.Keyboard(window) : undefined,
mouse: options.INPUT_SETTINGS.useMouse ? new pc.Mouse(canvas) : undefined,
gamepads: options.INPUT_SETTINGS.useGamepads ? new pc.GamePads() : undefined,
touch: options.INPUT_SETTINGS.useTouch && pc.platform.touch ? new pc.TouchDevice(canvas) : undefined
};
return devices;
};
function configureCss(fillMode, width, height) {
// Configure resolution and resize event
if (canvas.classList) {
canvas.classList.add('fill-mode-' + fillMode);
}
// css media query for aspect ratio changes
let css = "@media screen and (min-aspect-ratio: " + width + "/" + height + ") {";
css += " #application-canvas.fill-mode-KEEP_ASPECT {";
css += " width: auto;";
css += " height: 100%;";
css += " margin: 0 auto;";
css += " }";
css += "}";
// append css to style
if (document.head.querySelector) {
document.head.querySelector('style')!.innerHTML += css;
}
};
function reflow() {
app.resizeCanvas(canvas.width, canvas.height);
canvas.style.width = '';
canvas.style.height = '';
let fillMode = app._fillMode;
if (fillMode == pc.FILLMODE_NONE || fillMode == pc.FILLMODE_KEEP_ASPECT) {
if ((fillMode == pc.FILLMODE_NONE && canvas.clientHeight < window.innerHeight) || (canvas.clientWidth / canvas.clientHeight >= window.innerWidth / window.innerHeight)) {
canvas.style.marginTop = Math.floor((window.innerHeight - canvas.clientHeight) / 2) + 'px';
} else {
canvas.style.marginTop = '';
}
}
};
function displayError(html) {
let div = document.createElement('div');
div.innerHTML = [
'<table style="background-color: #8CE; width: 100%; height: 100%;">',
' <tr>',
' <td align="center">',
' <div style="display: table-cell; vertical-align: middle;">',
' <div style="">' + html + '</div>',
' </div>',
' </td>',
' </tr>',
'</table>'
].join('\n');
document.body.appendChild(div);
};
let app;
let canvas = createCanvas();
let devices = createInputDevices(canvas);
try {
app = new pc.Application(canvas, {
elementInput: devices.elementInput,
keyboard: devices.keyboard,
mouse: devices.mouse,
gamepads: devices.gamepads,
touch: devices.touch,
graphicsDeviceOptions: options.CONTEXT_OPTIONS,
assetPrefix: options.ASSET_PREFIX || "",
scriptPrefix: options.SCRIPT_PREFIX || "",
scriptsOrder: options.SCRIPTS || []
});
} catch (e) {
if (e.name == "UnsupportedBrowserError") {
displayError('This page requires a browser that supports WebGL.<br/>' +
'<a href="http://get.webgl.org">Click here to find out more.</a>');
} else if (e.name == "ContextCreationError") {
displayError("It doesn't appear your computer can support WebGL.<br/>" +
'<a href="http://get.webgl.org/troubleshooting/">Click here for more information.</a>');
} else {
displayError('Could not initialize application. Error: ' + e);
}
return;
}
function configure() {
app.configure(options.CONFIG_FILENAME, function (err) {
if (err) {
console.error(err);
}
configureCss(app._fillMode, app._width, app._height);
// do the first reflow after a timeout because of
// iOS showing a squished iframe sometimes
setTimeout(function () {
reflow();
window.addEventListener('resize', reflow, false);
window.addEventListener('orientationchange', reflow, false);
app.preload(function (err) {
if (err) {
console.error(err);
}
app.loadScene(options.SCENE_PATH, function (err, scene) {
if (err) {
console.error(err);
}
app.start();
});
});
});
});
};
if (options.PRELOAD_MODULES.length > 0) {
loadModules(options.PRELOAD_MODULES, options.ASSET_PREFIX, configure);
} else {
configure();
}
return app as pc.Application;
}