forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi-app.mjs
261 lines (231 loc) · 8.53 KB
/
multi-app.mjs
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
250
251
252
253
254
255
256
257
258
259
260
261
import * as pc from 'playcanvas';
/**
* @param {import('../../app/example.mjs').ControlOptions} options - The options.
* @returns {JSX.Element} The returned JSX Element.
*/
function controls({ observer, ReactPCUI, React, jsx, fragment }) {
const { BindingTwoWay, Panel, Label, Button } = ReactPCUI;
return fragment(
jsx(Panel, { headerText: 'WebGPU' },
jsx(Button, {
text: 'Add',
onClick: () => observer.emit('add:webgpu')
}),
jsx(Button, {
text: 'Remove',
onClick: () => observer.emit('remove:webgpu')
}),
jsx(Label, {
binding: new BindingTwoWay(),
link: { observer, path: 'webgpu' },
value: observer.get('webgpu')
})
),
jsx(Panel, { headerText: 'WebGL 2' },
jsx(Button, {
text: 'Add',
onClick: () => observer.emit('add:webgl2')
}),
jsx(Button, {
text: 'Remove',
onClick: () => observer.emit('remove:webgl2')
}),
jsx(Label, {
binding: new BindingTwoWay(),
link: { observer, path: 'webgl2' },
value: observer.get('webgl2')
})
),
jsx(Panel, { headerText: 'WebGL 1' },
jsx(Button, {
text: 'Add',
onClick: () => observer.emit('add:webgl1')
}),
jsx(Button, {
text: 'Remove',
onClick: () => observer.emit('remove:webgl1')
}),
jsx(Label, {
binding: new BindingTwoWay(),
link: { observer, path: 'webgl1' },
value: observer.get('webgl1')
})
),
jsx(Panel, { headerText: 'Null' },
jsx(Button, {
text: 'Add',
onClick: () => observer.emit('add:null')
}),
jsx(Button, {
text: 'Remove',
onClick: () => observer.emit('remove:null')
}),
jsx(Label, {
binding: new BindingTwoWay(),
link: { observer, path: 'null' },
value: observer.get('null')
})
)
);
}
/**
* @param {import('../../options.mjs').ExampleOptions} options - The example options.
* @returns {Promise<null>} The example application.
*/
async function example({ glslangPath, twgslPath, data }) {
/**
* @param {string} deviceType - The device type.
* @returns {Promise<pc.AppBase>} The example application.
*/
const createApp = async function(deviceType) {
const assets = {
font: new pc.Asset('font', 'font', { url: assetPath + 'fonts/courier.json' })
};
let canvas = document.createElement('canvas');
canvas.id = `app-${Math.random().toString(36).substring(7)}`; // generate a random id
document.getElementById('appInner')?.appendChild(canvas);
// Don't use createGraphicsDevice as it automatically falls back, which we don't want
let device;
if (deviceType === 'webgpu') {
device = new pc.WebgpuGraphicsDevice(canvas, {});
try {
await device.initWebGpu(glslangPath + 'glslang.js', twgslPath + 'twgsl.js');
} catch (err) {
throw err;
}
} else if (deviceType === 'webgl1' || deviceType === 'webgl2') {
device = new pc.WebglGraphicsDevice(canvas, {
preferWebGl2: deviceType === 'webgl2'
});
} else {
device = new pc.NullGraphicsDevice(canvas, {});
}
const createOptions = new pc.AppOptions();
createOptions.graphicsDevice = device;
createOptions.componentSystems = [
pc.RenderComponentSystem,
pc.CameraComponentSystem,
pc.LightComponentSystem,
pc.ScreenComponentSystem,
pc.ElementComponentSystem
];
createOptions.resourceHandlers = [
// @ts-ignore
pc.TextureHandler,
// @ts-ignore
pc.FontHandler
];
const app = new pc.AppBase(canvas);
app.init(createOptions);
app.start();
const assetListLoader = new pc.AssetListLoader(Object.values(assets), app.assets);
assetListLoader.load(() => {
app.setCanvasFillMode(pc.FILLMODE_NONE);
app.setCanvasResolution(pc.RESOLUTION_AUTO);
// Ensure canvas is resized when window changes size
const resize = () => app.resizeCanvas();
window.addEventListener('resize', resize);
app.on('destroy', () => {
window.removeEventListener('resize', resize);
});
// create box entity
const box = new pc.Entity('cube');
box.addComponent('render', {
type: 'box'
});
app.root.addChild(box);
// create camera entity
const clearValue = 0.3 + Math.random() * 0.3;
const camera = new pc.Entity('camera');
camera.addComponent('camera', {
clearColor: new pc.Color(clearValue, clearValue, clearValue)
});
app.root.addChild(camera);
camera.setPosition(0, -0.4, 3);
// create directional light entity
const light = new pc.Entity('light');
light.addComponent('light');
app.root.addChild(light);
light.setEulerAngles(45, 0, 0);
// Create a 2D screen
const screen = new pc.Entity();
screen.addComponent("screen", {
referenceResolution: new pc.Vec2(1280, 720),
scaleBlend: 0.5,
scaleMode: pc.SCALEMODE_BLEND,
screenSpace: true
});
app.root.addChild(screen);
// device type as text
const text = app.graphicsDevice.isWebGL1 ? 'WebGL 1' : app.graphicsDevice.isWebGL2 ? 'WebGL 2' : 'WebGPU';
// Text with outline to identify the platform
const textOutline = new pc.Entity();
textOutline.setLocalPosition(0, -100, 0);
textOutline.addComponent("element", {
pivot: new pc.Vec2(0.5, 0.5),
anchor: new pc.Vec4(0.5, -0.2, 0.5, 0.5),
fontAsset: assets.font.id,
fontSize: 130,
text: text,
color: new pc.Color(1, 0.9, 0.9),
outlineColor: new pc.Color(0, 0, 0),
outlineThickness: 1,
type: pc.ELEMENTTYPE_TEXT
});
screen.addChild(textOutline);
// rotate the box according to the delta time since the last frame
app.on('update', (/** @type {number} */ dt) => box.rotate(10 * dt, 20 * dt, 30 * dt));
});
return app;
};
const apps = {
webgpu: [],
webgl2: [],
webgl1: [],
null: []
};
// Add event listers for adding and removing apps
for (const deviceType in apps) {
data.set(deviceType, 0);
data.on(`add:${deviceType}`, () => {
createApp(deviceType).then((app) => {
apps[deviceType].push(app);
data.set(deviceType, apps[deviceType].length);
}).catch((err) => {
console.error(err);
});
});
data.on(`remove:${deviceType}`, () => {
const app = apps[deviceType].pop();
if (app && app.graphicsDevice) {
const canvas = app.graphicsDevice.canvas;
app.destroy();
canvas.remove();
data.set(deviceType, apps[deviceType].length);
}
});
}
// Make sure to remove all apps when the example is destroyed or hot reloaded
const removeAll = () => {
for (const deviceType in apps) {
while (apps[deviceType].length) {
data.emit(`remove:${deviceType}`);
}
}
};
window.addEventListener('destroy', removeAll);
window.addEventListener('hotReload', removeAll);
// Start with a webgl2 and webgpu app
data.emit('add:webgl2');
data.emit('add:webgpu');
return null;
}
class MultiApplicationExample {
static CATEGORY = 'Misc';
static NO_CANVAS = true;
static NO_MINISTATS = true;
static NO_DEVICE_SELECTOR = true;
static controls = controls;
static example = example;
}
export { MultiApplicationExample };