@eva/plugin-renderer 2.1.0-beta.1 → 2.1.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,762 +0,0 @@
|
|
|
1
|
-
import { OBSERVER_TYPE, decorators, System, LOAD_SCENE_MODE, resource } from '@eva/eva.js';
|
|
2
|
-
import { Container, Application } from '@eva/renderer-adapter';
|
|
3
|
-
import isEqual from 'lodash-es/isEqual';
|
|
4
|
-
import EventEmitter from 'eventemitter3';
|
|
5
|
-
import { Ticker, setKTXTranscoderPath, extensions, loadKTX2, resolveCompressedTextureUrl, detectCompressed } from 'pixi.js';
|
|
6
|
-
|
|
7
|
-
/******************************************************************************
|
|
8
|
-
Copyright (c) Microsoft Corporation.
|
|
9
|
-
|
|
10
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
11
|
-
purpose with or without fee is hereby granted.
|
|
12
|
-
|
|
13
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
14
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
15
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
16
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
17
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
18
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
19
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
20
|
-
***************************************************************************** */
|
|
21
|
-
|
|
22
|
-
function __decorate(decorators, target, key, desc) {
|
|
23
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
24
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
25
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
26
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function __metadata(metadataKey, metadataValue) {
|
|
30
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function __awaiter(thisArg, _arguments, P, generator) {
|
|
34
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
35
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
36
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
37
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
38
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
39
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
44
|
-
var e = new Error(message);
|
|
45
|
-
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* 渲染管理器类
|
|
50
|
-
*
|
|
51
|
-
* RendererManager 负责管理和协调所有渲染器。
|
|
52
|
-
* 它将组件变化事件分发给相应的渲染器,
|
|
53
|
-
* 并在每帧调用渲染器的更新方法。
|
|
54
|
-
*
|
|
55
|
-
* @example
|
|
56
|
-
* ```typescript
|
|
57
|
-
* const rendererManager = new RendererManager({
|
|
58
|
-
* game,
|
|
59
|
-
* rendererSystem
|
|
60
|
-
* });
|
|
61
|
-
*
|
|
62
|
-
* rendererManager.register(
|
|
63
|
-
* new SpriteRenderer(),
|
|
64
|
-
* new TextRenderer()
|
|
65
|
-
* );
|
|
66
|
-
* ```
|
|
67
|
-
*/
|
|
68
|
-
class RendererManager {
|
|
69
|
-
/**
|
|
70
|
-
* 构造渲染管理器
|
|
71
|
-
* @param game - 游戏实例
|
|
72
|
-
* @param rendererSystem - 渲染系统实例
|
|
73
|
-
*/
|
|
74
|
-
constructor({ game, rendererSystem }) {
|
|
75
|
-
/** 注册的渲染器列表 */
|
|
76
|
-
this.renderers = [];
|
|
77
|
-
this.game = game;
|
|
78
|
-
this.rendererSystem = rendererSystem;
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* 注册渲染器
|
|
82
|
-
*
|
|
83
|
-
* 将渲染器添加到管理器,并为其设置必要的引用。
|
|
84
|
-
*
|
|
85
|
-
* @param renderers - 要注册的渲染器列表
|
|
86
|
-
*/
|
|
87
|
-
register(...renderers) {
|
|
88
|
-
for (const renderer of renderers) {
|
|
89
|
-
renderer.game = this.game;
|
|
90
|
-
renderer.rendererManager = this.rendererSystem.rendererManager;
|
|
91
|
-
renderer.containerManager = this.rendererSystem.containerManager;
|
|
92
|
-
this.renderers.push(renderer);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
componentChanged(changes) {
|
|
96
|
-
for (const changed of changes) {
|
|
97
|
-
for (const renderer of this.renderers) {
|
|
98
|
-
const props = renderer.observerInfo[changed.componentName];
|
|
99
|
-
if (props) {
|
|
100
|
-
if ([OBSERVER_TYPE.ADD, OBSERVER_TYPE.REMOVE].indexOf(changed.type) > -1) {
|
|
101
|
-
try {
|
|
102
|
-
renderer.componentChanged && renderer.componentChanged(changed);
|
|
103
|
-
}
|
|
104
|
-
catch (e) {
|
|
105
|
-
console.error(`gameObject: ${changed.gameObject.name}, ${changed.componentName} is error.`, changed, e);
|
|
106
|
-
}
|
|
107
|
-
continue;
|
|
108
|
-
}
|
|
109
|
-
const index = props.findIndex(prop => {
|
|
110
|
-
return isEqual(prop, changed.prop);
|
|
111
|
-
});
|
|
112
|
-
if (index > -1) {
|
|
113
|
-
try {
|
|
114
|
-
renderer.componentChanged && renderer.componentChanged(changed);
|
|
115
|
-
}
|
|
116
|
-
catch (e) {
|
|
117
|
-
console.error(`gameObject: ${changed.gameObject && changed.gameObject.name}, ${changed.componentName} is componentChanged error.`, changed, e);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
update(gameObject) {
|
|
125
|
-
for (const component of gameObject.components) {
|
|
126
|
-
for (const renderer of this.renderers) {
|
|
127
|
-
const cache = [];
|
|
128
|
-
const props = renderer.observerInfo[component.name];
|
|
129
|
-
if (props && cache.indexOf(gameObject) === -1) {
|
|
130
|
-
cache.push(gameObject);
|
|
131
|
-
try {
|
|
132
|
-
renderer.rendererUpdate && renderer.rendererUpdate(gameObject);
|
|
133
|
-
}
|
|
134
|
-
catch (e) {
|
|
135
|
-
console.info(`gameObject: ${gameObject.name}, ${component.name} is update error`, e);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
var RendererManager$1 = RendererManager;
|
|
143
|
-
|
|
144
|
-
const toRenderBounds = (bounds, coordinateSpace, source) => {
|
|
145
|
-
const x = Number.isFinite(bounds.x) ? bounds.x : 0;
|
|
146
|
-
const y = Number.isFinite(bounds.y) ? bounds.y : 0;
|
|
147
|
-
const width = Number.isFinite(bounds.width) ? bounds.width : 0;
|
|
148
|
-
const height = Number.isFinite(bounds.height) ? bounds.height : 0;
|
|
149
|
-
return {
|
|
150
|
-
x,
|
|
151
|
-
y,
|
|
152
|
-
width,
|
|
153
|
-
height,
|
|
154
|
-
right: x + width,
|
|
155
|
-
bottom: y + height,
|
|
156
|
-
coordinateSpace,
|
|
157
|
-
source,
|
|
158
|
-
};
|
|
159
|
-
};
|
|
160
|
-
class ContainerManager {
|
|
161
|
-
constructor() {
|
|
162
|
-
this.containerMap = {};
|
|
163
|
-
}
|
|
164
|
-
addContainer({ name, container, gameObject }) {
|
|
165
|
-
this.containerMap[name] = container;
|
|
166
|
-
container.gName = gameObject.name || name;
|
|
167
|
-
}
|
|
168
|
-
getContainer(name) {
|
|
169
|
-
return this.containerMap[name];
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* Return actual rendered bounds for a GameObject.
|
|
173
|
-
*
|
|
174
|
-
* The primary path uses PixiJS Container#getBounds(), so renderers such as
|
|
175
|
-
* Text/Img/Graphics/Sprite/Spine report the real display size after loading
|
|
176
|
-
* and style application. If the display object is not ready yet, callers can
|
|
177
|
-
* fall back to Transform.size in the same Eva design/canvas coordinate space.
|
|
178
|
-
*/
|
|
179
|
-
getBounds(gameObject, options = {}) {
|
|
180
|
-
if (!gameObject || gameObject.destroyed)
|
|
181
|
-
return null;
|
|
182
|
-
const coordinateSpace = options.coordinateSpace || 'world';
|
|
183
|
-
const fallbackToTransform = options.fallbackToTransform !== false;
|
|
184
|
-
const container = this.getContainer(gameObject.id);
|
|
185
|
-
if (container === null || container === void 0 ? void 0 : container.getBounds) {
|
|
186
|
-
try {
|
|
187
|
-
const bounds = container.getBounds();
|
|
188
|
-
if (bounds && Number.isFinite(bounds.width) && Number.isFinite(bounds.height) && (bounds.width !== 0 || bounds.height !== 0)) {
|
|
189
|
-
return toRenderBounds(bounds, coordinateSpace, 'pixi');
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
catch (error) {
|
|
193
|
-
// Pixi can throw while async resources are still being attached; fallback below keeps editor tools stable.
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
if (!fallbackToTransform)
|
|
197
|
-
return null;
|
|
198
|
-
return this.getTransformBounds(gameObject, coordinateSpace);
|
|
199
|
-
}
|
|
200
|
-
getTransformBounds(gameObject, coordinateSpace) {
|
|
201
|
-
var _a, _b, _c, _d, _e, _f;
|
|
202
|
-
const transform = gameObject.transform;
|
|
203
|
-
const container = this.getContainer(gameObject.id);
|
|
204
|
-
let x = (_a = container === null || container === void 0 ? void 0 : container.worldTransform) === null || _a === void 0 ? void 0 : _a.tx;
|
|
205
|
-
let y = (_b = container === null || container === void 0 ? void 0 : container.worldTransform) === null || _b === void 0 ? void 0 : _b.ty;
|
|
206
|
-
if (!Number.isFinite(x) || !Number.isFinite(y)) {
|
|
207
|
-
const position = this.resolveFallbackWorldPosition(transform);
|
|
208
|
-
x = position.x;
|
|
209
|
-
y = position.y;
|
|
210
|
-
}
|
|
211
|
-
const scaleX = Number.isFinite((_c = transform.scale) === null || _c === void 0 ? void 0 : _c.x) ? transform.scale.x : 1;
|
|
212
|
-
const scaleY = Number.isFinite((_d = transform.scale) === null || _d === void 0 ? void 0 : _d.y) ? transform.scale.y : 1;
|
|
213
|
-
const width = Math.abs((((_e = transform.size) === null || _e === void 0 ? void 0 : _e.width) || 0) * scaleX);
|
|
214
|
-
const height = Math.abs((((_f = transform.size) === null || _f === void 0 ? void 0 : _f.height) || 0) * scaleY);
|
|
215
|
-
return toRenderBounds({ x, y, width, height }, coordinateSpace, 'transform');
|
|
216
|
-
}
|
|
217
|
-
resolveFallbackWorldPosition(transform) {
|
|
218
|
-
var _a, _b, _c, _d;
|
|
219
|
-
if (!transform)
|
|
220
|
-
return { x: 0, y: 0 };
|
|
221
|
-
const parent = transform.parent;
|
|
222
|
-
const parentPosition = parent ? this.resolveFallbackWorldPosition(parent) : { x: 0, y: 0 };
|
|
223
|
-
const parentSize = (parent === null || parent === void 0 ? void 0 : parent.size) || { width: 0, height: 0 };
|
|
224
|
-
return {
|
|
225
|
-
x: parentPosition.x + (((_a = transform.position) === null || _a === void 0 ? void 0 : _a.x) || 0) + parentSize.width * (((_b = transform.anchor) === null || _b === void 0 ? void 0 : _b.x) || 0),
|
|
226
|
-
y: parentPosition.y + (((_c = transform.position) === null || _c === void 0 ? void 0 : _c.y) || 0) + parentSize.height * (((_d = transform.anchor) === null || _d === void 0 ? void 0 : _d.y) || 0),
|
|
227
|
-
};
|
|
228
|
-
}
|
|
229
|
-
removeContainer(name) {
|
|
230
|
-
const container = this.containerMap[name];
|
|
231
|
-
if (container) {
|
|
232
|
-
container.destroy({ children: true });
|
|
233
|
-
}
|
|
234
|
-
delete this.containerMap[name];
|
|
235
|
-
}
|
|
236
|
-
updateTransform({ name, transform }) {
|
|
237
|
-
const container = this.containerMap[name];
|
|
238
|
-
if (!container || !transform)
|
|
239
|
-
return;
|
|
240
|
-
const { anchor, origin, position, rotation, scale, size, skew } = transform;
|
|
241
|
-
container.rotation = rotation;
|
|
242
|
-
// @ts-ignore
|
|
243
|
-
container.scale = scale;
|
|
244
|
-
container.pivot.x = size.width * origin.x;
|
|
245
|
-
container.pivot.y = size.height * origin.y;
|
|
246
|
-
// @ts-ignore
|
|
247
|
-
container.skew = skew;
|
|
248
|
-
let x = position.x;
|
|
249
|
-
let y = position.y;
|
|
250
|
-
if (transform.parent) {
|
|
251
|
-
const parent = transform.parent;
|
|
252
|
-
x = x + parent.size.width * anchor.x;
|
|
253
|
-
y = y + parent.size.height * anchor.y;
|
|
254
|
-
}
|
|
255
|
-
// @ts-ignore
|
|
256
|
-
container.position = { x, y };
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
let Transform = class Transform extends EventEmitter {
|
|
261
|
-
constructor({ system, containerManager }) {
|
|
262
|
-
super();
|
|
263
|
-
this.name = 'Transform';
|
|
264
|
-
this.waitRemoveIds = [];
|
|
265
|
-
this.waitChangeScenes = [];
|
|
266
|
-
this.containerManager = containerManager;
|
|
267
|
-
this.init(system);
|
|
268
|
-
}
|
|
269
|
-
init(system) {
|
|
270
|
-
this.system = system;
|
|
271
|
-
this.on('changeScene', ({ scene, mode, application }) => {
|
|
272
|
-
// switch (mode) {
|
|
273
|
-
// case LOAD_SCENE_MODE.SINGLE:
|
|
274
|
-
this.waitChangeScenes.push({ scene, mode, application });
|
|
275
|
-
// break;
|
|
276
|
-
// case LOAD_SCENE_MODE.MULTI_CANVAS:
|
|
277
|
-
// }
|
|
278
|
-
});
|
|
279
|
-
}
|
|
280
|
-
update() {
|
|
281
|
-
for (const id of this.waitRemoveIds) {
|
|
282
|
-
this.containerManager.removeContainer(id);
|
|
283
|
-
}
|
|
284
|
-
this.waitRemoveIds = [];
|
|
285
|
-
for (const sceneInfo of this.waitChangeScenes) {
|
|
286
|
-
// set scene
|
|
287
|
-
const container = this.containerManager.getContainer(sceneInfo.scene.id);
|
|
288
|
-
if (container) {
|
|
289
|
-
sceneInfo.application.stage.removeChildren();
|
|
290
|
-
sceneInfo.application.stage.addChild(container);
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
this.waitChangeScenes = [];
|
|
294
|
-
}
|
|
295
|
-
componentChanged(changed) {
|
|
296
|
-
if (changed.type === OBSERVER_TYPE.ADD) {
|
|
297
|
-
this.addContainer(changed);
|
|
298
|
-
}
|
|
299
|
-
else if (changed.type === OBSERVER_TYPE.CHANGE) {
|
|
300
|
-
this.change(changed);
|
|
301
|
-
}
|
|
302
|
-
else if (changed.type === OBSERVER_TYPE.REMOVE) {
|
|
303
|
-
this.waitRemoveIds.push(changed.gameObject.id);
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
addContainer(changed) {
|
|
307
|
-
const container = new Container();
|
|
308
|
-
container.label = changed.gameObject.name;
|
|
309
|
-
this.containerManager.addContainer({
|
|
310
|
-
name: changed.gameObject.id,
|
|
311
|
-
container,
|
|
312
|
-
gameObject: changed.gameObject,
|
|
313
|
-
});
|
|
314
|
-
const transform = changed.component;
|
|
315
|
-
Object.defineProperty(transform, 'worldTransform', {
|
|
316
|
-
get() {
|
|
317
|
-
return container.renderGroup || container.parentRenderGroup ? container.worldTransform : void 0;
|
|
318
|
-
},
|
|
319
|
-
});
|
|
320
|
-
}
|
|
321
|
-
change(changed) {
|
|
322
|
-
const transform = changed.component;
|
|
323
|
-
if (transform.parent) {
|
|
324
|
-
const parentContainer = this.containerManager.getContainer(transform.parent.gameObject.id);
|
|
325
|
-
parentContainer.addChild(this.containerManager.getContainer(changed.gameObject.id));
|
|
326
|
-
const render = changed.gameObject.transform.parent &&
|
|
327
|
-
changed.gameObject.transform.parent.gameObject.getComponent('Render');
|
|
328
|
-
if (render) {
|
|
329
|
-
render.sortDirty = true;
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
else {
|
|
333
|
-
const container = this.containerManager.getContainer(changed.gameObject.id);
|
|
334
|
-
container.parent && container.parent.removeChild(container);
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
destroy() {
|
|
338
|
-
this.removeAllListeners();
|
|
339
|
-
this.waitRemoveIds = null;
|
|
340
|
-
this.waitChangeScenes = null;
|
|
341
|
-
this.system = null;
|
|
342
|
-
this.containerManager = null;
|
|
343
|
-
}
|
|
344
|
-
};
|
|
345
|
-
Transform = __decorate([
|
|
346
|
-
decorators.componentObserver({
|
|
347
|
-
Transform: ['_parent'],
|
|
348
|
-
}),
|
|
349
|
-
__metadata("design:paramtypes", [Object])
|
|
350
|
-
], Transform);
|
|
351
|
-
var Transform$1 = Transform;
|
|
352
|
-
|
|
353
|
-
var RENDERER_TYPE;
|
|
354
|
-
(function (RENDERER_TYPE) {
|
|
355
|
-
RENDERER_TYPE[RENDERER_TYPE["UNKNOWN"] = 0] = "UNKNOWN";
|
|
356
|
-
RENDERER_TYPE[RENDERER_TYPE["WEBGL"] = 1] = "WEBGL";
|
|
357
|
-
RENDERER_TYPE[RENDERER_TYPE["CANVAS"] = 2] = "CANVAS";
|
|
358
|
-
})(RENDERER_TYPE || (RENDERER_TYPE = {}));
|
|
359
|
-
const disableScroll = renderer => {
|
|
360
|
-
renderer.events.autoPreventDefault = true;
|
|
361
|
-
renderer.canvas.style.touchAction = 'none';
|
|
362
|
-
};
|
|
363
|
-
const enableScroll = renderer => {
|
|
364
|
-
renderer.events.autoPreventDefault = false;
|
|
365
|
-
renderer.canvas.style.touchAction = 'auto';
|
|
366
|
-
};
|
|
367
|
-
function isRendererGameLike(value) {
|
|
368
|
-
if (!value || typeof value !== 'object')
|
|
369
|
-
return false;
|
|
370
|
-
const candidate = value;
|
|
371
|
-
return Boolean(candidate.scene || candidate.gameObjects || typeof candidate.on === 'function');
|
|
372
|
-
}
|
|
373
|
-
let Renderer$1 = class Renderer extends System {
|
|
374
|
-
constructor() {
|
|
375
|
-
super(...arguments);
|
|
376
|
-
this.multiApps = [];
|
|
377
|
-
this.destroyed = false;
|
|
378
|
-
}
|
|
379
|
-
init(params = {}) {
|
|
380
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
381
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
382
|
-
this.destroyed = false;
|
|
383
|
-
const gameLike = !this.game && isRendererGameLike(params) ? params : undefined;
|
|
384
|
-
if (gameLike) {
|
|
385
|
-
this.game = gameLike;
|
|
386
|
-
}
|
|
387
|
-
const rendererParams = gameLike ? this.__systemDefaultParams || {} : params;
|
|
388
|
-
this.params = rendererParams;
|
|
389
|
-
this.application = yield this.createApplication(rendererParams);
|
|
390
|
-
this.containerManager = new ContainerManager();
|
|
391
|
-
this.rendererManager = new RendererManager$1({
|
|
392
|
-
game: this.game,
|
|
393
|
-
rendererSystem: this,
|
|
394
|
-
});
|
|
395
|
-
if (this.game) {
|
|
396
|
-
this.game.canvas = this.application.canvas;
|
|
397
|
-
}
|
|
398
|
-
this.transform = new Transform$1({
|
|
399
|
-
system: this,
|
|
400
|
-
containerManager: this.containerManager,
|
|
401
|
-
});
|
|
402
|
-
(_b = (_a = this.game) === null || _a === void 0 ? void 0 : _a.on) === null || _b === void 0 ? void 0 : _b.call(_a, 'sceneChanged', ({ scene, mode, params }) => __awaiter(this, void 0, void 0, function* () {
|
|
403
|
-
let application;
|
|
404
|
-
switch (mode) {
|
|
405
|
-
case LOAD_SCENE_MODE.SINGLE:
|
|
406
|
-
application = this.application;
|
|
407
|
-
break;
|
|
408
|
-
case LOAD_SCENE_MODE.MULTI_CANVAS:
|
|
409
|
-
application = yield this.createMultiApplication({ params });
|
|
410
|
-
break;
|
|
411
|
-
}
|
|
412
|
-
scene.canvas = application.canvas;
|
|
413
|
-
this.transform.emit('changeScene', {
|
|
414
|
-
scene,
|
|
415
|
-
mode,
|
|
416
|
-
application,
|
|
417
|
-
});
|
|
418
|
-
}));
|
|
419
|
-
(_d = (_c = this.game) === null || _c === void 0 ? void 0 : _c.on) === null || _d === void 0 ? void 0 : _d.call(_c, 'pauseScene', ({ scene }) => {
|
|
420
|
-
this.onPauseScene(scene);
|
|
421
|
-
});
|
|
422
|
-
(_f = (_e = this.game) === null || _e === void 0 ? void 0 : _e.on) === null || _f === void 0 ? void 0 : _f.call(_e, 'startScene', ({ scene }) => {
|
|
423
|
-
this.onStartScene(scene);
|
|
424
|
-
});
|
|
425
|
-
(_h = (_g = this.game) === null || _g === void 0 ? void 0 : _g.on) === null || _h === void 0 ? void 0 : _h.call(_g, 'sceneDestroyed', ({ scene }) => __awaiter(this, void 0, void 0, function* () {
|
|
426
|
-
const index = this.multiApps.findIndex(app => app.canvas === scene.canvas);
|
|
427
|
-
if (index > -1) {
|
|
428
|
-
const app = this.multiApps.splice(index, 1)[0];
|
|
429
|
-
app.destroy();
|
|
430
|
-
scene.destroy();
|
|
431
|
-
}
|
|
432
|
-
}));
|
|
433
|
-
});
|
|
434
|
-
}
|
|
435
|
-
registerObserver(observerInfo) {
|
|
436
|
-
// @ts-ignore
|
|
437
|
-
const thisObserverInfo = this.constructor.observerInfo;
|
|
438
|
-
for (const key in observerInfo) {
|
|
439
|
-
if (!thisObserverInfo[key]) {
|
|
440
|
-
thisObserverInfo[key] = [];
|
|
441
|
-
}
|
|
442
|
-
//@ts-ignore
|
|
443
|
-
thisObserverInfo[key].push(...observerInfo[key]);
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
createMultiApplication({ params }) {
|
|
447
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
448
|
-
const app = yield this.createApplication(params);
|
|
449
|
-
// @ts-ignore
|
|
450
|
-
this.multiApps.push(app);
|
|
451
|
-
return app;
|
|
452
|
-
});
|
|
453
|
-
}
|
|
454
|
-
createApplication(params) {
|
|
455
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
456
|
-
const app = new Application();
|
|
457
|
-
if (params.debugMode) {
|
|
458
|
-
globalThis.__PIXI_APP__ = app;
|
|
459
|
-
}
|
|
460
|
-
yield app.init(Object.assign(Object.assign({ sharedTicker: true }, params), { hello: true }));
|
|
461
|
-
if (params.enableScroll !== undefined) {
|
|
462
|
-
params.enableScroll ? enableScroll(app.renderer) : disableScroll(app.renderer);
|
|
463
|
-
}
|
|
464
|
-
Ticker.shared.stop();
|
|
465
|
-
Ticker.shared.autoStart = false;
|
|
466
|
-
return app;
|
|
467
|
-
});
|
|
468
|
-
}
|
|
469
|
-
update() {
|
|
470
|
-
if (this.destroyed || !this.game || !this.containerManager || !this.rendererManager)
|
|
471
|
-
return;
|
|
472
|
-
const changes = this.componentObserver.clear();
|
|
473
|
-
for (const changed of changes) {
|
|
474
|
-
this.transform.componentChanged(changed);
|
|
475
|
-
}
|
|
476
|
-
for (const gameObject of this.game.gameObjects) {
|
|
477
|
-
this.containerManager.updateTransform({
|
|
478
|
-
name: gameObject.id,
|
|
479
|
-
transform: gameObject.transform,
|
|
480
|
-
});
|
|
481
|
-
this.rendererManager.update(gameObject);
|
|
482
|
-
}
|
|
483
|
-
}
|
|
484
|
-
lateUpdate(e) {
|
|
485
|
-
if (this.destroyed || !this.transform || !this.application)
|
|
486
|
-
return;
|
|
487
|
-
this.transform.update();
|
|
488
|
-
this.application.ticker.update(e.time);
|
|
489
|
-
}
|
|
490
|
-
onDestroy() {
|
|
491
|
-
var _a, _b, _c, _d, _e, _f, _g;
|
|
492
|
-
if (this.destroyed)
|
|
493
|
-
return;
|
|
494
|
-
this.destroyed = true;
|
|
495
|
-
(_c = (_b = (_a = this.application) === null || _a === void 0 ? void 0 : _a.ticker) === null || _b === void 0 ? void 0 : _b.stop) === null || _c === void 0 ? void 0 : _c.call(_b);
|
|
496
|
-
(_d = this.application) === null || _d === void 0 ? void 0 : _d.destroy(false, { children: true, context: true });
|
|
497
|
-
for (const app of this.multiApps) {
|
|
498
|
-
(_f = (_e = app === null || app === void 0 ? void 0 : app.ticker) === null || _e === void 0 ? void 0 : _e.stop) === null || _f === void 0 ? void 0 : _f.call(_e);
|
|
499
|
-
app && app.destroy(false, { children: true, context: true });
|
|
500
|
-
}
|
|
501
|
-
(_g = this.transform) === null || _g === void 0 ? void 0 : _g.destroy();
|
|
502
|
-
this.transform = null;
|
|
503
|
-
this.params = null;
|
|
504
|
-
this.rendererManager = null;
|
|
505
|
-
this.containerManager = null;
|
|
506
|
-
this.application = null;
|
|
507
|
-
this.game = null;
|
|
508
|
-
this.multiApps = null;
|
|
509
|
-
}
|
|
510
|
-
resize(width, height, resolution) {
|
|
511
|
-
this.params.width = width;
|
|
512
|
-
this.params.height = height;
|
|
513
|
-
if (resolution != null) {
|
|
514
|
-
this.params.resolution = this.clampResolution(resolution);
|
|
515
|
-
}
|
|
516
|
-
// @ts-ignore
|
|
517
|
-
this.application.renderer.resize(width, height, this.params.resolution);
|
|
518
|
-
if (resolution != null) {
|
|
519
|
-
this.syncDisplayObjectResolution(this.params.resolution);
|
|
520
|
-
}
|
|
521
|
-
}
|
|
522
|
-
/**
|
|
523
|
-
* 更新 Pixi renderer backing-store resolution,不改变 Eva 设计坐标系。
|
|
524
|
-
*
|
|
525
|
-
* 预览画布可用该入口在 zoom/devicePixelRatio 变化时提高清晰度;
|
|
526
|
-
* getBounds/Transform/worldTransform 仍返回设计逻辑坐标,不乘 resolution。
|
|
527
|
-
*/
|
|
528
|
-
resizeRenderer(options) {
|
|
529
|
-
var _a, _b, _c, _d, _e, _f;
|
|
530
|
-
const width = (_b = (_a = options.width) !== null && _a !== void 0 ? _a : this.params.width) !== null && _b !== void 0 ? _b : this.application.renderer.width;
|
|
531
|
-
const height = (_d = (_c = options.height) !== null && _c !== void 0 ? _c : this.params.height) !== null && _d !== void 0 ? _d : this.application.renderer.height;
|
|
532
|
-
const resolution = options.resolution == null
|
|
533
|
-
? (_f = (_e = this.params.resolution) !== null && _e !== void 0 ? _e : this.application.renderer.resolution) !== null && _f !== void 0 ? _f : 1
|
|
534
|
-
: this.clampResolution(options.resolution, options.maxResolution);
|
|
535
|
-
this.params.width = width;
|
|
536
|
-
this.params.height = height;
|
|
537
|
-
this.params.resolution = resolution;
|
|
538
|
-
// @ts-ignore Pixi v8 accepts resolution as the third resize argument.
|
|
539
|
-
this.application.renderer.resize(width, height, resolution);
|
|
540
|
-
const displayResolutionSyncCount = this.syncDisplayObjectResolution(resolution);
|
|
541
|
-
return { width, height, resolution, displayResolutionSyncCount };
|
|
542
|
-
}
|
|
543
|
-
setResolution(resolution, options = {}) {
|
|
544
|
-
return this.resizeRenderer(Object.assign(Object.assign({}, options), { resolution }));
|
|
545
|
-
}
|
|
546
|
-
clampResolution(resolution, maxResolution = 3) {
|
|
547
|
-
const normalized = Number.isFinite(resolution) && resolution > 0 ? resolution : 1;
|
|
548
|
-
return Math.min(normalized, maxResolution);
|
|
549
|
-
}
|
|
550
|
-
/**
|
|
551
|
-
* Keep renderer-owned display objects that expose a resolution property in
|
|
552
|
-
* sync with the backing-store resolution. This is intentionally display-tree
|
|
553
|
-
* based rather than component-type based, so Text/HTMLText and future Pixi
|
|
554
|
-
* objects with resolution support all follow the same renderer contract.
|
|
555
|
-
*/
|
|
556
|
-
syncDisplayObjectResolution(resolution) {
|
|
557
|
-
var _a;
|
|
558
|
-
const roots = ((_a = this.containerManager) === null || _a === void 0 ? void 0 : _a.containerMap) ? Object.values(this.containerManager.containerMap) : [];
|
|
559
|
-
const visited = typeof WeakSet !== 'undefined' ? new WeakSet() : undefined;
|
|
560
|
-
let syncedCount = 0;
|
|
561
|
-
const visit = (displayObject) => {
|
|
562
|
-
if (!displayObject || typeof displayObject !== 'object')
|
|
563
|
-
return;
|
|
564
|
-
if (visited === null || visited === void 0 ? void 0 : visited.has(displayObject))
|
|
565
|
-
return;
|
|
566
|
-
visited === null || visited === void 0 ? void 0 : visited.add(displayObject);
|
|
567
|
-
if ('resolution' in displayObject) {
|
|
568
|
-
const currentResolution = displayObject.resolution;
|
|
569
|
-
if ((typeof currentResolution === 'number' || currentResolution === null) &&
|
|
570
|
-
Math.abs((currentResolution !== null && currentResolution !== void 0 ? currentResolution : 1) - resolution) > 0.001) {
|
|
571
|
-
try {
|
|
572
|
-
displayObject.resolution = resolution;
|
|
573
|
-
if (typeof displayObject.resolution === 'number' &&
|
|
574
|
-
Math.abs(displayObject.resolution - resolution) <= 0.001) {
|
|
575
|
-
syncedCount += 1;
|
|
576
|
-
}
|
|
577
|
-
}
|
|
578
|
-
catch (_) {
|
|
579
|
-
// Some Pixi objects expose a readonly or unsupported resolution setter.
|
|
580
|
-
}
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
if (Array.isArray(displayObject.children)) {
|
|
584
|
-
for (const child of displayObject.children) {
|
|
585
|
-
visit(child);
|
|
586
|
-
}
|
|
587
|
-
}
|
|
588
|
-
};
|
|
589
|
-
for (const root of roots) {
|
|
590
|
-
visit(root);
|
|
591
|
-
}
|
|
592
|
-
return syncedCount;
|
|
593
|
-
}
|
|
594
|
-
/**
|
|
595
|
-
* 获取 GameObject 的真实渲染 bounds。
|
|
596
|
-
*
|
|
597
|
-
* 默认返回 PixiJS display object 的 world bounds。Eva 的 Pixi world 坐标
|
|
598
|
-
* 与 canvas/design 逻辑坐标一致;如果画布被 CSS 缩放,调用方再按
|
|
599
|
-
* canvas.getBoundingClientRect() / renderer width 做屏幕坐标换算。
|
|
600
|
-
*
|
|
601
|
-
* 当渲染对象尚未挂载或资源未加载完成时,会回退到 Transform.size。
|
|
602
|
-
*/
|
|
603
|
-
getBounds(gameObject, options) {
|
|
604
|
-
var _a;
|
|
605
|
-
return ((_a = this.containerManager) === null || _a === void 0 ? void 0 : _a.getBounds(gameObject, options)) || null;
|
|
606
|
-
}
|
|
607
|
-
getApplicationByScene(scene) {
|
|
608
|
-
const index = this.multiApps.findIndex(app => app.canvas === scene.canvas);
|
|
609
|
-
if (index > -1) {
|
|
610
|
-
const application = this.multiApps[index];
|
|
611
|
-
return application;
|
|
612
|
-
}
|
|
613
|
-
else {
|
|
614
|
-
console.warn('application not found');
|
|
615
|
-
}
|
|
616
|
-
}
|
|
617
|
-
onPauseScene(scene) {
|
|
618
|
-
const app = this.getApplicationByScene(scene);
|
|
619
|
-
if (app) {
|
|
620
|
-
app.stop();
|
|
621
|
-
}
|
|
622
|
-
}
|
|
623
|
-
onStartScene(scene) {
|
|
624
|
-
const app = this.getApplicationByScene(scene);
|
|
625
|
-
if (app) {
|
|
626
|
-
app.start();
|
|
627
|
-
}
|
|
628
|
-
}
|
|
629
|
-
resizeByScene(scene, width, height, resolution) {
|
|
630
|
-
const app = this.getApplicationByScene(scene);
|
|
631
|
-
if (app) {
|
|
632
|
-
// @ts-ignore
|
|
633
|
-
app.renderer.resize(width, height, resolution);
|
|
634
|
-
}
|
|
635
|
-
}
|
|
636
|
-
};
|
|
637
|
-
Renderer$1.systemName = 'Renderer';
|
|
638
|
-
Renderer$1 = __decorate([
|
|
639
|
-
decorators.componentObserver({
|
|
640
|
-
Transform: ['_parent'],
|
|
641
|
-
})
|
|
642
|
-
], Renderer$1);
|
|
643
|
-
var Renderer$2 = Renderer$1;
|
|
644
|
-
|
|
645
|
-
let result = undefined;
|
|
646
|
-
function getSuportCompressedTextureFormats() {
|
|
647
|
-
if (result)
|
|
648
|
-
return result;
|
|
649
|
-
const canvas = document.createElement('canvas');
|
|
650
|
-
const gl = canvas.getContext('webgl2');
|
|
651
|
-
if (!gl) {
|
|
652
|
-
// #if _DEBUG
|
|
653
|
-
console.warn('WebGL not available for compressed textures. Silently failing.');
|
|
654
|
-
// #endif
|
|
655
|
-
return {
|
|
656
|
-
s3tc: false,
|
|
657
|
-
etc: false,
|
|
658
|
-
etc1: false,
|
|
659
|
-
pvrtc: false,
|
|
660
|
-
atc: false,
|
|
661
|
-
astc: false,
|
|
662
|
-
};
|
|
663
|
-
}
|
|
664
|
-
result = {
|
|
665
|
-
s3tc: !!gl.getExtension('WEBGL_compressed_texture_s3tc'),
|
|
666
|
-
etc: !!gl.getExtension('WEBGL_compressed_texture_etc'),
|
|
667
|
-
etc1: !!gl.getExtension('WEBGL_compressed_texture_etc1'),
|
|
668
|
-
pvrtc: !!gl.getExtension('WEBGL_compressed_texture_pvrtc') || !!gl.getExtension('WEBKIT_WEBGL_compressed_texture_pvrtc'),
|
|
669
|
-
atc: !!gl.getExtension('WEBGL_compressed_texture_atc'),
|
|
670
|
-
astc: !!gl.getExtension('WEBGL_compressed_texture_astc'),
|
|
671
|
-
};
|
|
672
|
-
try {
|
|
673
|
-
console.log('Eva.js Supported Compressed Texture Format List: ' +
|
|
674
|
-
Object.keys(result)
|
|
675
|
-
.filter(type => result[type])
|
|
676
|
-
.join(', '));
|
|
677
|
-
}
|
|
678
|
-
catch (e) { }
|
|
679
|
-
return result;
|
|
680
|
-
}
|
|
681
|
-
|
|
682
|
-
function addPreProcessResourceHandler(resource) {
|
|
683
|
-
resource.addPreProcessResourceHandler(function normalizeResource(resource) {
|
|
684
|
-
var _a, _b, _c;
|
|
685
|
-
let textures = (_b = (_a = resource.src) === null || _a === void 0 ? void 0 : _a.image) === null || _b === void 0 ? void 0 : _b.texture;
|
|
686
|
-
if (!textures)
|
|
687
|
-
return;
|
|
688
|
-
if (!Array.isArray(textures)) {
|
|
689
|
-
textures = [textures];
|
|
690
|
-
}
|
|
691
|
-
const formats = (_c = getSuportCompressedTextureFormats()) !== null && _c !== void 0 ? _c : {};
|
|
692
|
-
let target = textures.find(texture => formats[texture.type]);
|
|
693
|
-
if (target) {
|
|
694
|
-
Object.assign(resource.src.image, target);
|
|
695
|
-
}
|
|
696
|
-
});
|
|
697
|
-
}
|
|
698
|
-
|
|
699
|
-
function registerKtx2CompressedTexture(params) {
|
|
700
|
-
setKTXTranscoderPath(params);
|
|
701
|
-
extensions.add(loadKTX2);
|
|
702
|
-
extensions.add(resolveCompressedTextureUrl);
|
|
703
|
-
extensions.add(detectCompressed);
|
|
704
|
-
addPreProcessResourceHandler(resource);
|
|
705
|
-
}
|
|
706
|
-
|
|
707
|
-
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
708
|
-
class Renderer extends System {
|
|
709
|
-
constructor(params) {
|
|
710
|
-
super(params);
|
|
711
|
-
this.asyncIdMap = {};
|
|
712
|
-
// @ts-ignore
|
|
713
|
-
this.observerInfo = this.constructor.observerInfo;
|
|
714
|
-
}
|
|
715
|
-
// init(arg?: any): void;
|
|
716
|
-
/**
|
|
717
|
-
* 当监听的属性变化时候调用
|
|
718
|
-
*
|
|
719
|
-
* called while the observed component props change.
|
|
720
|
-
*/
|
|
721
|
-
componentChanged(_changed) { }
|
|
722
|
-
/**
|
|
723
|
-
* 每帧调用
|
|
724
|
-
*
|
|
725
|
-
* called by every loop
|
|
726
|
-
* @param _gameObject - gameObject
|
|
727
|
-
*/
|
|
728
|
-
rendererUpdate(_gameObject) { }
|
|
729
|
-
// @ts-ignore
|
|
730
|
-
update(e) {
|
|
731
|
-
const changes = this.componentObserver.clear();
|
|
732
|
-
for (const changed of changes) {
|
|
733
|
-
this.componentChanged(changed);
|
|
734
|
-
}
|
|
735
|
-
}
|
|
736
|
-
increaseAsyncId(id) {
|
|
737
|
-
this.asyncIdMap[id] = (this.asyncIdMap[id] || 0) + 1;
|
|
738
|
-
return this.asyncIdMap[id];
|
|
739
|
-
}
|
|
740
|
-
validateAsyncId(id, asyncId) {
|
|
741
|
-
return this.asyncIdMap[id] === asyncId;
|
|
742
|
-
}
|
|
743
|
-
}
|
|
744
|
-
|
|
745
|
-
// import { BaseTexture, utils } from 'pixi.js';
|
|
746
|
-
const mixinPIXI = () => {
|
|
747
|
-
// BaseTexture.prototype.destroy = function () {
|
|
748
|
-
// if (this.imageUrl) {
|
|
749
|
-
// delete utils.TextureCache[this.imageUrl];
|
|
750
|
-
// this.imageUrl = null;
|
|
751
|
-
// }
|
|
752
|
-
// this.source = null;
|
|
753
|
-
// this.dispose();
|
|
754
|
-
// BaseTexture.removeFromCache(this);
|
|
755
|
-
// this.textureCacheIds = null;
|
|
756
|
-
// this._destroyed = true;
|
|
757
|
-
// };
|
|
758
|
-
};
|
|
759
|
-
|
|
760
|
-
mixinPIXI();
|
|
761
|
-
|
|
762
|
-
export { ContainerManager, RENDERER_TYPE, Renderer, RendererManager$1 as RendererManager, Renderer$2 as RendererSystem, registerKtx2CompressedTexture };
|