forked from galacean/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEngine.ts
736 lines (648 loc) · 22.6 KB
/
Engine.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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
import { IPhysics, IPhysicsManager, IShaderLab } from "@galacean/engine-design";
import { Color } from "@galacean/engine-math/src/Color";
import { SpriteMaskInteraction } from "./2d";
import { Font } from "./2d/text/Font";
import { Camera } from "./Camera";
import { Canvas } from "./Canvas";
import { EngineSettings } from "./EngineSettings";
import { Entity } from "./Entity";
import { ClassPool } from "./RenderPipeline/ClassPool";
import { RenderContext } from "./RenderPipeline/RenderContext";
import { RenderData } from "./RenderPipeline/RenderData";
import { RenderElement } from "./RenderPipeline/RenderElement";
import { SpriteMaskManager } from "./RenderPipeline/SpriteMaskManager";
import { SpriteMaskRenderData } from "./RenderPipeline/SpriteMaskRenderData";
import { SpriteRenderData } from "./RenderPipeline/SpriteRenderData";
import { TextRenderData } from "./RenderPipeline/TextRenderData";
import { Scene } from "./Scene";
import { SceneManager } from "./SceneManager";
import { ContentRestorer } from "./asset/ContentRestorer";
import { ResourceManager } from "./asset/ResourceManager";
import { EventDispatcher, Logger, Time } from "./base";
import { GLCapabilityType } from "./base/Constant";
import { ColorSpace } from "./enums/ColorSpace";
import { InputManager } from "./input";
import { Material } from "./material/Material";
import { ParticleBufferUtils } from "./particle/ParticleBufferUtils";
import { PhysicsScene } from "./physics/PhysicsScene";
import { ColliderShape } from "./physics/shape/ColliderShape";
import { IHardwareRenderer } from "./renderingHardwareInterface";
import { CompareFunction } from "./shader";
import { Shader } from "./shader/Shader";
import { ShaderMacro } from "./shader/ShaderMacro";
import { ShaderMacroCollection } from "./shader/ShaderMacroCollection";
import { ShaderPass } from "./shader/ShaderPass";
import { ShaderPool } from "./shader/ShaderPool";
import { ShaderProgramPool } from "./shader/ShaderProgramPool";
import { BlendFactor } from "./shader/enums/BlendFactor";
import { BlendOperation } from "./shader/enums/BlendOperation";
import { ColorWriteMask } from "./shader/enums/ColorWriteMask";
import { CullMode } from "./shader/enums/CullMode";
import { RenderQueueType } from "./shader/enums/RenderQueueType";
import { RenderState } from "./shader/state/RenderState";
import { Texture2D, Texture2DArray, TextureCube, TextureCubeFace, TextureFormat } from "./texture";
ShaderPool.init();
/**
* Engine.
*/
export class Engine extends EventDispatcher {
/** @internal */
static _gammaMacro: ShaderMacro = ShaderMacro.getByName("ENGINE_IS_COLORSPACE_GAMMA");
/** @internal */
static _noDepthTextureMacro: ShaderMacro = ShaderMacro.getByName("ENGINE_NO_DEPTH_TEXTURE");
/** @internal Conversion of space units to pixel units for 2D. */
static _pixelsPerUnit: number = 100;
/** Input manager of Engine. */
readonly inputManager: InputManager;
/** @internal */
_particleBufferUtils: ParticleBufferUtils;
/** @internal */
_physicsInitialized: boolean = false;
/** @internal */
_physicalObjectsMap: Record<number, ColliderShape> = {};
/** @internal */
_nativePhysicsManager: IPhysicsManager;
/* @internal */
_hardwareRenderer: IHardwareRenderer;
/* @internal */
_lastRenderState: RenderState = new RenderState();
/* @internal */
_renderElementPool: ClassPool<RenderElement> = new ClassPool(RenderElement);
/* @internal */
_renderDataPool: ClassPool<RenderData> = new ClassPool(RenderData);
/* @internal */
_spriteRenderDataPool: ClassPool<SpriteRenderData> = new ClassPool(SpriteRenderData);
/* @internal */
_spriteMaskRenderDataPool: ClassPool<SpriteMaskRenderData> = new ClassPool(SpriteMaskRenderData);
/* @internal */
_textRenderDataPool: ClassPool<TextRenderData> = new ClassPool(TextRenderData);
/* @internal */
_spriteDefaultMaterial: Material;
/** @internal */
_spriteDefaultMaterials: Material[] = [];
/* @internal */
_spriteMaskDefaultMaterial: Material;
/* @internal */
_textDefaultFont: Font;
/* @internal */
_renderContext: RenderContext = new RenderContext();
/* @internal */
_whiteTexture2D: Texture2D;
/* @internal */
_magentaTexture2D: Texture2D;
/* @internal */
_magentaTextureCube: TextureCube;
/* @internal */
_magentaTexture2DArray: Texture2DArray;
/* @internal */
_meshMagentaMaterial: Material;
/* @internal */
_particleMagentaMaterial: Material;
/* @internal */
_depthTexture2D: Texture2D;
/* @internal */
_renderCount: number = 0;
/* @internal */
_shaderProgramPools: ShaderProgramPool[] = [];
/** @internal */
_spriteMaskManager: SpriteMaskManager;
/** @internal */
_canSpriteBatch: boolean = true;
/** @internal */
_fontMap: Record<string, Font> = {};
/** @internal @todo: temporary solution */
_macroCollection: ShaderMacroCollection = new ShaderMacroCollection();
/** @internal */
protected _canvas: Canvas;
private _settings: EngineSettings = {};
private _resourceManager: ResourceManager = new ResourceManager(this);
private _sceneManager: SceneManager = new SceneManager(this);
private _vSyncCount: number = 1;
private _targetFrameRate: number = 60;
private _time: Time = new Time();
private _isPaused: boolean = true;
private _requestId: number;
private _timeoutId: number;
private _vSyncCounter: number = 1;
private _targetFrameInterval: number = 1000 / 60;
private _destroyed: boolean = false;
private _frameInProcess: boolean = false;
private _waitingDestroy: boolean = false;
private _isDeviceLost: boolean = false;
private _waitingGC: boolean = false;
private _animate = () => {
if (this._vSyncCount) {
this._requestId = requestAnimationFrame(this._animate);
if (this._vSyncCounter++ % this._vSyncCount === 0) {
this.update();
this._vSyncCounter = 1;
}
} else {
this._timeoutId = window.setTimeout(this._animate, this._targetFrameInterval);
this.update();
}
};
/**
* Settings of Engine.
*/
get settings(): EngineSettings {
return this._settings;
}
/**
* The canvas to use for rendering.
*/
get canvas(): Canvas {
return this._canvas;
}
/**
* The resource manager.
*/
get resourceManager(): ResourceManager {
return this._resourceManager;
}
/**
* The scene manager.
*/
get sceneManager(): SceneManager {
return this._sceneManager;
}
/**
* The time information of the engine.
*/
get time(): Time {
return this._time;
}
/**
* Whether the engine is paused.
*/
get isPaused(): boolean {
return this._isPaused;
}
/**
* The number of vertical synchronization means the number of vertical blanking for one frame.
* @remarks 0 means that the vertical synchronization is turned off.
*/
get vSyncCount(): number {
return this._vSyncCount;
}
set vSyncCount(value: number) {
this._vSyncCount = Math.max(0, Math.floor(value));
}
/**
* Set the target frame rate you want to achieve.
* @remarks
* It only takes effect when vSyncCount = 0 (ie, vertical synchronization is turned off).
* The larger the value, the higher the target frame rate, Number.POSITIVE_INFINITY represents the infinite target frame rate.
*/
get targetFrameRate(): number {
return this._targetFrameRate;
}
set targetFrameRate(value: number) {
value = Math.max(0.000001, value);
this._targetFrameRate = value;
this._targetFrameInterval = 1000 / value;
}
/**
* Indicates whether the engine is destroyed.
*/
get destroyed(): boolean {
return this._destroyed;
}
protected constructor(canvas: Canvas, hardwareRenderer: IHardwareRenderer, configuration: EngineConfiguration) {
super();
this._hardwareRenderer = hardwareRenderer;
this._hardwareRenderer.init(canvas, this._onDeviceLost.bind(this), this._onDeviceRestored.bind(this));
this._canvas = canvas;
this._spriteMaskManager = new SpriteMaskManager(this);
const { _spriteDefaultMaterials: spriteDefaultMaterials } = this;
this._spriteDefaultMaterial = spriteDefaultMaterials[SpriteMaskInteraction.None] = this._createSpriteMaterial(
SpriteMaskInteraction.None
);
spriteDefaultMaterials[SpriteMaskInteraction.VisibleInsideMask] = this._createSpriteMaterial(
SpriteMaskInteraction.VisibleInsideMask
);
spriteDefaultMaterials[SpriteMaskInteraction.VisibleOutsideMask] = this._createSpriteMaterial(
SpriteMaskInteraction.VisibleOutsideMask
);
this._spriteMaskDefaultMaterial = this._createSpriteMaskMaterial();
this._textDefaultFont = Font.createFromOS(this, "Arial");
this._textDefaultFont.isGCIgnored = true;
this.inputManager = new InputManager(this);
this._initMagentaTextures(hardwareRenderer);
if (!hardwareRenderer.canIUse(GLCapabilityType.depthTexture)) {
this._macroCollection.enable(Engine._noDepthTextureMacro);
} else {
const depthTexture2D = new Texture2D(this, 1, 1, TextureFormat.Depth16, false);
depthTexture2D.isGCIgnored = true;
this._depthTexture2D = depthTexture2D;
}
const meshMagentaMaterial = new Material(this, Shader.find("unlit"));
meshMagentaMaterial.isGCIgnored = true;
meshMagentaMaterial.shaderData.setColor("material_BaseColor", new Color(1.0, 0.0, 1.01, 1.0));
this._meshMagentaMaterial = meshMagentaMaterial;
const particleMagentaMaterial = new Material(this, Shader.find("particle-shader"));
particleMagentaMaterial.isGCIgnored = true;
particleMagentaMaterial.shaderData.setColor("material_BaseColor", new Color(1.0, 0.0, 1.01, 1.0));
this._particleMagentaMaterial = particleMagentaMaterial;
const innerSettings = this._settings;
const colorSpace = configuration.colorSpace || ColorSpace.Linear;
colorSpace === ColorSpace.Gamma && this._macroCollection.enable(Engine._gammaMacro);
innerSettings.colorSpace = colorSpace;
this._particleBufferUtils = new ParticleBufferUtils(this);
}
/**
* Create an entity.
* @param name - The name of the entity
* @returns Entity
*/
createEntity(name?: string): Entity {
return new Entity(this, name);
}
/**
* Pause the engine.
*/
pause(): void {
this._isPaused = true;
cancelAnimationFrame(this._requestId);
clearTimeout(this._timeoutId);
}
/**
* Resume the engine.
*/
resume(): void {
if (!this._isPaused) return;
this._isPaused = false;
this.time._reset();
this._requestId = requestAnimationFrame(this._animate);
}
/**
* Update the engine loop manually. If you call engine.run(), you generally don't need to call this function.
*/
update(): void {
const time = this._time;
time._update();
const deltaTime = time.deltaTime;
this._frameInProcess = true;
this._renderElementPool.resetPool();
this._renderDataPool.resetPool();
this._spriteRenderDataPool.resetPool();
this._spriteMaskRenderDataPool.resetPool();
this._textRenderDataPool.resetPool();
const { inputManager, _physicsInitialized: physicsInitialized } = this;
inputManager._update();
const scenes = this._sceneManager._scenes.getLoopArray();
const sceneCount = scenes.length;
// Sort cameras and fire script `onStart`
for (let i = 0; i < sceneCount; i++) {
const scene = scenes[i];
if (!scene.isActive || scene.destroyed) continue;
const componentsManager = scene._componentsManager;
componentsManager.sortCameras();
componentsManager.callScriptOnStart();
}
// Update physics and fire `onPhysicsUpdate`
if (physicsInitialized) {
for (let i = 0; i < sceneCount; i++) {
const scene = scenes[i];
if (!scene.isActive || scene.destroyed) continue;
scene.physics._update(deltaTime);
}
}
// Fire `onPointerXX`
physicsInitialized && inputManager._firePointerScript(scenes);
// Fire `onUpdate`
for (let i = 0; i < sceneCount; i++) {
const scene = scenes[i];
if (!scene.isActive || scene.destroyed) continue;
scene._componentsManager.callScriptOnUpdate(deltaTime);
}
// Update `Animator` logic
for (let i = 0; i < sceneCount; i++) {
const scene = scenes[i];
if (!scene.isActive || scene.destroyed) continue;
scene._componentsManager.callAnimationUpdate(deltaTime);
}
// Fire `onLateUpdate`
for (let i = 0; i < sceneCount; i++) {
const scene = scenes[i];
if (!scene.isActive || scene.destroyed) continue;
scene._componentsManager.callScriptOnLateUpdate(deltaTime);
}
// Render scene and fire `onBeginRender` and `onEndRender`
if (!this._isDeviceLost) {
this._render(scenes);
}
if (this._waitingDestroy) {
this._destroy();
} else {
// Handling invalid scripts and fire `onDestroy`
for (let i = 0; i < sceneCount; i++) {
const scene = scenes[i];
if (!scene.isActive || scene.destroyed) continue;
scene._componentsManager.handlingInvalidScripts();
}
}
if (this._waitingGC) {
this._gc();
this._waitingGC = false;
}
this._frameInProcess = false;
}
/**
* Execution engine loop.
*/
run(): void {
this.resume();
this.dispatch("run", this);
}
/**
* Force lose graphic device.
* @remarks Used to simulate the phenomenon after the real loss of device.
*/
forceLoseDevice(): void {
this._hardwareRenderer.forceLoseDevice();
}
/**
* Force restore graphic device.
* @remarks Used to simulate the phenomenon after the real restore of device.
*/
forceRestoreDevice(): void {
this._hardwareRenderer.forceRestoreDevice();
}
private _destroy(): void {
this._sceneManager._destroyAllScene();
this._resourceManager._destroy();
this._textDefaultFont = null;
this._fontMap = null;
this.inputManager._destroy();
this.dispatch("shutdown", this);
// Cancel animation
this.pause();
this._spriteMaskManager.destroy();
this._hardwareRenderer.destroy();
this.removeAllEventListeners();
this._animate = null;
this._sceneManager = null;
this._resourceManager = null;
this._canvas = null;
this._time = null;
this._waitingDestroy = false;
this._destroyed = true;
}
/**
* Destroy engine.
* @remarks If call during frame execution will delay until the end of the frame
*/
destroy(): void {
if (this._destroyed) {
return;
}
if (this._frameInProcess) {
this._waitingDestroy = true;
} else {
this._destroy();
}
}
/**
* @internal
*/
_getShaderProgramPool(shaderPass: ShaderPass): ShaderProgramPool {
const index = shaderPass._shaderPassId;
const shaderProgramPools = this._shaderProgramPools;
let pool = shaderProgramPools[index];
if (!pool) {
const length = index + 1;
if (length > shaderProgramPools.length) {
shaderProgramPools.length = length;
}
shaderProgramPools[index] = pool = new ShaderProgramPool();
}
return pool;
}
/**
* @internal
*/
_render(scenes: ReadonlyArray<Scene>): void {
// Update `Renderer` logic and shader data
const deltaTime = this.time.deltaTime;
for (let i = 0, n = scenes.length; i < n; i++) {
const scene = scenes[i];
if (!scene.isActive || scene.destroyed) continue;
scene._componentsManager.callRendererOnUpdate(deltaTime);
scene._updateShaderData();
}
// Fire script `onBeginRender` and `onEndRender`
for (let i = 0, n = scenes.length; i < n; i++) {
const scene = scenes[i];
if (!scene.isActive || scene.destroyed) continue;
const cameras = scene._componentsManager._activeCameras;
if (cameras.length === 0) {
Logger.debug("No active camera in scene.");
continue;
}
cameras.forEach(
(camera: Camera) => {
const componentsManager = scene._componentsManager;
componentsManager.callCameraOnBeginRender(camera);
camera.render();
componentsManager.callCameraOnEndRender(camera);
// Temp solution for webgl implement bug
if (this._hardwareRenderer._options._forceFlush) {
this._hardwareRenderer.flush();
}
},
(camera: Camera, index: number) => {
camera._cameraIndex = index;
}
);
}
}
/**
* @internal
* Standalone for CanvasRenderer plugin.
*/
_initMagentaTextures(hardwareRenderer: IHardwareRenderer) {
const whitePixel = new Uint8Array([255, 255, 255, 255]);
const whiteTexture2D = new Texture2D(this, 1, 1, TextureFormat.R8G8B8A8, false);
whiteTexture2D.setPixelBuffer(whitePixel);
whiteTexture2D.isGCIgnored = true;
const magentaPixel = new Uint8Array([255, 0, 255, 255]);
const magentaTexture2D = new Texture2D(this, 1, 1, TextureFormat.R8G8B8A8, false);
magentaTexture2D.setPixelBuffer(magentaPixel);
magentaTexture2D.isGCIgnored = true;
this.resourceManager.addContentRestorer(
new (class extends ContentRestorer<Texture2D> {
constructor() {
super(magentaTexture2D);
}
restoreContent() {
this.resource.setPixelBuffer(magentaPixel);
}
})()
);
const magentaTextureCube = new TextureCube(this, 1, TextureFormat.R8G8B8A8, false);
for (let i = 0; i < 6; i++) {
magentaTextureCube.setPixelBuffer(TextureCubeFace.PositiveX + i, magentaPixel);
}
magentaTextureCube.isGCIgnored = true;
this.resourceManager.addContentRestorer(
new (class extends ContentRestorer<TextureCube> {
constructor() {
super(magentaTextureCube);
}
restoreContent() {
for (let i = 0; i < 6; i++) {
this.resource.setPixelBuffer(TextureCubeFace.PositiveX + i, magentaPixel);
}
}
})()
);
this._whiteTexture2D = whiteTexture2D;
this._magentaTexture2D = magentaTexture2D;
this._magentaTextureCube = magentaTextureCube;
if (hardwareRenderer.isWebGL2) {
const magentaTexture2DArray = new Texture2DArray(this, 1, 1, 1, TextureFormat.R8G8B8A8, false);
magentaTexture2DArray.setPixelBuffer(0, magentaPixel);
magentaTexture2DArray.isGCIgnored = true;
this.resourceManager.addContentRestorer(
new (class extends ContentRestorer<Texture2DArray> {
constructor() {
super(magentaTexture2DArray);
}
restoreContent() {
this.resource.setPixelBuffer(0, magentaPixel);
}
})()
);
this._magentaTexture2DArray = magentaTexture2DArray;
}
}
/**
* @internal
*/
_pendingGC() {
if (this._frameInProcess) {
this._waitingGC = true;
} else {
this._gc();
}
}
/**
* @internal
*/
protected _initialize(configuration: EngineConfiguration): Promise<Engine> {
const { shaderLab, physics } = configuration;
if (shaderLab) {
Shader._shaderLab = shaderLab;
}
const initializePromises = new Array<Promise<any>>();
if (physics) {
initializePromises.push(
physics.initialize().then(() => {
PhysicsScene._nativePhysics = physics;
this._nativePhysicsManager = physics.createPhysicsManager();
this._physicsInitialized = true;
return this;
})
);
}
const loaders = ResourceManager._loaders;
for (let key in loaders) {
const loader = loaders[key];
if (loader.initialize) initializePromises.push(loader.initialize(this, configuration));
}
return Promise.all(initializePromises).then(() => this);
}
private _createSpriteMaterial(maskInteraction: SpriteMaskInteraction): Material {
const material = new Material(this, Shader.find("Sprite"));
const renderState = material.renderState;
const target = renderState.blendState.targetBlendState;
target.enabled = true;
target.sourceColorBlendFactor = BlendFactor.SourceAlpha;
target.destinationColorBlendFactor = BlendFactor.OneMinusSourceAlpha;
target.sourceAlphaBlendFactor = BlendFactor.One;
target.destinationAlphaBlendFactor = BlendFactor.OneMinusSourceAlpha;
target.colorBlendOperation = target.alphaBlendOperation = BlendOperation.Add;
if (maskInteraction !== SpriteMaskInteraction.None) {
const stencilState = renderState.stencilState;
stencilState.enabled = true;
stencilState.writeMask = 0x00;
stencilState.referenceValue = 1;
const compare =
maskInteraction === SpriteMaskInteraction.VisibleInsideMask
? CompareFunction.LessEqual
: CompareFunction.Greater;
stencilState.compareFunctionFront = compare;
stencilState.compareFunctionBack = compare;
}
renderState.depthState.writeEnabled = false;
renderState.rasterState.cullMode = CullMode.Off;
renderState.renderQueueType = RenderQueueType.Transparent;
material.isGCIgnored = true;
return material;
}
private _createSpriteMaskMaterial(): Material {
const material = new Material(this, Shader.find("SpriteMask"));
const renderState = material.renderState;
renderState.blendState.targetBlendState.colorWriteMask = ColorWriteMask.None;
renderState.rasterState.cullMode = CullMode.Off;
renderState.stencilState.enabled = true;
renderState.depthState.enabled = false;
material.isGCIgnored = true;
return material;
}
private _onDeviceLost(): void {
this._isDeviceLost = true;
// Lose graphic resources
this.resourceManager._lostGraphicResources();
console.log("Device lost.");
this.dispatch("devicelost", this);
}
private _onDeviceRestored(): void {
this._hardwareRenderer.resetState();
this._lastRenderState = new RenderState();
// Clear shader pools
this._shaderProgramPools.length = 0;
const { resourceManager } = this;
// Restore graphic resources
resourceManager._restoreGraphicResources();
console.log("Graphic resource restored.");
// Restore resources content
this._particleBufferUtils.setBufferData();
resourceManager
._restoreResourcesContent()
.then(() => {
console.log("Graphic resource content restored.\n\n" + "Device restored.");
this.dispatch("devicerestored", this);
this._isDeviceLost = false;
})
.catch((error) => {
console.error(error);
});
}
private _gc(): void {
this._renderElementPool.garbageCollection();
this._renderDataPool.garbageCollection();
this._spriteRenderDataPool.garbageCollection();
this._spriteMaskRenderDataPool.garbageCollection();
this._textRenderDataPool.garbageCollection();
}
/**
* @deprecated
* The first scene physics manager.
*/
get physicsManager() {
return this.sceneManager.scenes[0]?.physics;
}
}
/**
* Engine configuration.
*/
export interface EngineConfiguration {
/** Physics. */
physics?: IPhysics;
/** Color space. */
colorSpace?: ColorSpace;
/** Shader lab */
shaderLab?: IShaderLab;
}