BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / scene.js

scene.js

scene.js 9.0 KiB Download Raw
1class Scene
2{
3 static idCount = 0;
4 id = null;
5 activeObjects = [];
6 position = null;
7 size = null;
8 real_position = null;
9 px = 0; py = 0;
10 real_size = null;
11 cameraPosition = null;
12 parentScene = null;
13 renderBackground = true;
14 quadTree = null;
15
16 pendingRemoval = [];
17
18 g_primary = false;
19
20 static DisplayModes = { absolute: 0, relativeToParent: 1 };
21
22 displayMode = Scene.DisplayModes.absolute;
23
24 cv_canvas = null;
25 glContext = null;
26 destroyed = false;
27
28 // Legacy: posts applied to the "default" layer when that layer has none of its own.
29 postShaders = [];
30 postTime = 0;
31
32 // Ordered list of RenderLayer. Index 0 is drawn first (back).
33 renderLayers = null;
34
35 game = null;
36
37 get primary ()
38 {
39 return this.g_primary;
40 }
41
42 constructor (x, y, width, height, displayMode = 0)
43 {
44 Scene.idCount++;
45 this.id = 'canvas_' + Scene.idCount;
46
47 this.position = new vector(x, y);
48 this.size = new vector(width, height);
49 this.real_position = new vector(x, y);
50 this.real_size = new vector(width, height);
51 this.cameraPosition = new vector(0, 0);
52 this.displayMode = displayMode;
53
54
55 const body = document.body;
56
57 body.insertAdjacentHTML('beforeend', '<canvas id="' + this.id + '" style="position: absolute; left: ' + x + 'px; top: ' + y + 'px;" width="' + width + '" height="' + height + '"></canvas>');
58
59 this.cv_canvas = document.getElementById(this.id);
60 this.glContext = new GLContext(this.cv_canvas);
61 this.postShaders = [];
62 this.postTime = 0;
63 this.renderLayers = [new RenderLayer(RenderLayer.DEFAULT_NAME)];
64
65 this.g_fixSize();
66 this.quadTree = new QuadTree(this.real_size.x, this.real_size.y);
67 }
68
69 /**
70 * Replace the layer stack. Order in the array is draw order (first = back).
71 * Each entry: RenderLayer, or { name, postShaders?, composite? }.
72 */
73 setLayers (layerConfigs)
74 {
75 if (!layerConfigs || !layerConfigs.length)
76 {
77 this.renderLayers = [new RenderLayer(RenderLayer.DEFAULT_NAME)];
78 return this.renderLayers;
79 }
80
81 const layers = [];
82 for (let i = 0; i < layerConfigs.length; i++)
83 {
84 const cfg = layerConfigs[i];
85 if (!cfg)
86 { continue; }
87
88 if (cfg instanceof RenderLayer)
89 { layers.push(cfg); }
90 else
91 { layers.push(new RenderLayer(cfg.name, cfg)); }
92 }
93
94 if (!layers.length)
95 { layers.push(new RenderLayer(RenderLayer.DEFAULT_NAME)); }
96
97 this.renderLayers = layers;
98 return this.renderLayers;
99 }
100
101 getLayer (name)
102 {
103 const layers = this.renderLayers || [];
104 for (let i = 0; i < layers.length; i++)
105 {
106 if (layers[i].name === name)
107 { return layers[i]; }
108 }
109
110 return null;
111 }
112
113 setPrimary ()
114 {
115 this.game.setPrimaryScene(this);
116 }
117
118 onLoad ()
119 {
120
121 }
122
123 loadChildScene (scene)
124 {
125 scene.parentScene = this;
126 this.game.loadScene(scene);
127 }
128
129 destroy ()
130 {
131 if (this.destroyed)
132 { return; }
133
134 for (var i = 0; i < this.activeObjects.length; i++)
135 {
136 this.activeObjects[i].destroy();
137 }
138
139 this.destroyed = true;
140
141 if (this.game)
142 { this.game.removeScene(this); }
143
144 this.removeCanvas();
145
146 if (this.primary)
147 {
148 if (this.parentScene && !this.parentScene.destroyed)
149 {
150 this.parentScene.setPrimary();
151 }
152 else if (this.game)
153 {
154 let promoted = null;
155 for (let i = 0; i < this.game.activeScenes.length; i++)
156 {
157 const candidate = this.game.activeScenes[i];
158 if (candidate !== this && !candidate.destroyed)
159 {
160 promoted = candidate;
161 break;
162 }
163 }
164
165 if (promoted)
166 { this.game.setPrimaryScene(promoted); }
167 else
168 { this.g_primary = false; }
169 }
170 else
171 {
172 this.g_primary = false;
173 }
174 }
175 }
176
177 removeCanvas ()
178 {
179 var elem = this.cv_canvas || document.getElementById(this.id);
180 if (elem && elem.parentNode)
181 {
182 elem.parentNode.removeChild(elem);
183 }
184
185 this.cv_canvas = null;
186 this.glContext = null;
187 }
188
189 backgroundColor =
190 {
191 r: 255,
192 g: 255,
193 b: 255
194 };
195
196 get canvas ()
197 {
198 return this.cv_canvas;
199 }
200
201 get context ()
202 {
203 return this.glContext ? this.glContext.gl : null;
204 }
205
206 addObject (obj)
207 {
208 if (this.destroyed)
209 { return; }
210
211 this.activeObjects.push(obj);
212 obj.scene = this;
213 obj.initQuadtree(this.quadTree);
214 obj.checkQuadtree();
215
216 obj.addedToScene && obj.addedToScene();
217
218 return obj;
219 }
220
221 removeObject (obj)
222 {
223 if (this.activeObjects.indexOf(obj) >= 0)
224 {
225 if (!this.pendingRemoval.contains(obj))
226 { this.pendingRemoval.push(obj); }
227 }
228
229 if (this.quadTree === obj.quadTree)
230 { this.quadTree.remove(obj); }
231 }
232
233 update (deltaTime)
234 {
235 if (this.destroyed)
236 { return; }
237
238 this.postTime += deltaTime;
239
240 for (let i = 0; i < this.activeObjects.length; i++)
241 {
242 this.activeObjects[i].update(deltaTime);
243 }
244
245 if (this.pendingRemoval.length)
246 {
247 for (let i = 0; i < this.pendingRemoval.length; i++)
248 {
249 const obj = this.pendingRemoval[i];
250 const index = this.activeObjects.indexOf(obj);
251 if (index >= 0 && obj.destroyed)
252 { this.activeObjects.removeAt(index); }
253 }
254
255 this.pendingRemoval.length = 0;
256 }
257
258 this.quadTree.update(deltaTime);
259 }
260
261 g_fixSize ()
262 {
263 if (this.displayMode == Scene.DisplayModes.relativeToParent)
264 {
265 if (this.parentScene)
266 {
267 this.real_position.x = this.parentScene.real_position.x + (this.position.x * this.parentScene.real_size.x);
268 this.real_position.y = this.parentScene.real_position.y + (this.position.y * this.parentScene.real_size.y);
269
270 this.real_size.x = this.parentScene.real_size.x * this.size.x;
271 this.real_size.y = this.parentScene.real_size.y * this.size.y;
272 }
273 else
274 {
275 this.real_position.x = window.innerWidth * this.position.x;
276 this.real_position.y = window.innerHeight * this.position.y;
277
278 this.real_size.x = window.innerWidth * this.size.x;
279 this.real_size.y = window.innerHeight * this.size.y;
280 }
281 }
282 else if (this.displayMode == Scene.DisplayModes.absolute)
283 {
284 this.real_position.x = this.position.x;
285 this.real_position.y = this.position.y;
286
287 this.real_size.x = this.size.x;
288 this.real_size.y = this.size.y;
289 }
290
291 const canvasW = Math.max(1, Math.round(this.real_size.x));
292 const canvasH = Math.max(1, Math.round(this.real_size.y));
293 let resized = false;
294
295 if (this.canvas.width != canvasW)
296 {
297 this.canvas.width = canvasW;
298 resized = true;
299 }
300
301 if (this.canvas.height != canvasH)
302 {
303 this.canvas.height = canvasH;
304 resized = true;
305 }
306
307 // Changing canvas dimensions resets WebGL — rebuild helper/buffers/programs.
308 if (resized || !this.glContext)
309 { this.glContext = new GLContext(this.cv_canvas); }
310 else
311 { this.glContext.setSize(this.canvas.width, this.canvas.height); }
312
313 if (this.px != this.real_position.x)
314 {
315 this.px = this.real_position.x;
316 this.canvas.style.left = this.real_position.x + "px";
317 }
318
319 if (this.py != this.real_position.y)
320 {
321 this.py = this.real_position.y;
322 this.canvas.style.top = this.real_position.y + "px";
323 }
324 }
325
326 normalizePostShaderList (list)
327 {
328 if (!list || !list.length)
329 { return []; }
330
331 const passes = [];
332 for (let i = 0; i < list.length; i++)
333 {
334 const entry = list[i];
335 if (!entry)
336 { continue; }
337
338 if (entry instanceof Shader || (entry.vertSource && entry.fragSource && entry.getProgramInfo))
339 {
340 passes.push({ shader: entry, uniforms: null });
341 }
342 else if (entry.shader)
343 {
344 passes.push({ shader: entry.shader, uniforms: entry.uniforms || null });
345 }
346 }
347
348 return passes;
349 }
350
351 normalizePostShaders ()
352 {
353 return this.normalizePostShaderList(this.postShaders);
354 }
355
356 getPostPassesForLayer (layer)
357 {
358 const fromLayer = this.normalizePostShaderList(layer.postShaders);
359 if (fromLayer.length)
360 { return fromLayer; }
361
362 // Back-compat: scene.postShaders apply to the default layer only.
363 if (layer.name === RenderLayer.DEFAULT_NAME)
364 { return this.normalizePostShaders(); }
365
366 return [];
367 }
368
369 draw ()
370 {
371 if (this.destroyed || !this.glContext)
372 { return; }
373
374 this.g_fixSize();
375
376 const gl = this.glContext;
377 const layers = this.renderLayers && this.renderLayers.length
378 ? this.renderLayers
379 : [new RenderLayer(RenderLayer.DEFAULT_NAME)];
380
381 // Canvas clear (background behind all layers).
382 gl.endOffscreenPass();
383 if (this.renderBackground)
384 {
385 gl.clear(
386 this.backgroundColor.r,
387 this.backgroundColor.g,
388 this.backgroundColor.b,
389 1
390 );
391 }
392 else
393 {
394 gl.clearTransparent();
395 }
396
397 const sortedActiveObjects = this.activeObjects.slice().sort((a, b) => a.layer - b.layer);
398 const builtins = {
399 u_resolution: [gl._lastWidth, gl._lastHeight],
400 u_time: this.postTime
401 };
402
403 for (let li = 0; li < layers.length; li++)
404 {
405 const renderLayer = layers[li];
406 const layerName = renderLayer.name;
407
408 gl.beginOffscreenPass();
409 gl.clearTransparent();
410
411 for (let i = 0; i < sortedActiveObjects.length; i++)
412 {
413 const obj = sortedActiveObjects[i];
414 if (obj.destroyed || !obj.renderer || !obj.renderer.visible)
415 { continue; }
416
417 if (obj.renderer.layerName !== layerName)
418 { continue; }
419
420 obj.draw(this.context);
421 }
422
423 const postPasses = this.getPostPassesForLayer(renderLayer);
424 let texture = gl.colorTextures[0];
425
426 if (postPasses.length)
427 {
428 texture = gl.applyPostShaders(postPasses, builtins, { toCanvas: false });
429 }
430 else
431 {
432 gl.endOffscreenPass();
433 }
434
435 gl.blitToCanvas(texture, renderLayer.composite || "alpha");
436 }
437 }
438}