-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
index.js
422 lines (338 loc) · 12.4 KB
/
index.js
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
import { Texture, BaseTexture, RenderTexture } from '@pixi/core';
import { Sprite } from '@pixi/sprite';
import { DisplayObject } from '@pixi/display';
import { Matrix } from '@pixi/math';
import { uid } from '@pixi/utils';
import { settings } from '@pixi/settings';
const _tempMatrix = new Matrix();
DisplayObject.prototype._cacheAsBitmap = false;
DisplayObject.prototype._cacheData = false;
// figured theres no point adding ALL the extra variables to prototype.
// this model can hold the information needed. This can also be generated on demand as
// most objects are not cached as bitmaps.
/**
* @class
* @ignore
*/
class CacheData
{
constructor()
{
this.textureCacheId = null;
this.originalRender = null;
this.originalRenderCanvas = null;
this.originalCalculateBounds = null;
this.originalGetLocalBounds = null;
this.originalUpdateTransform = null;
this.originalHitTest = null;
this.originalDestroy = null;
this.originalMask = null;
this.originalFilterArea = null;
this.sprite = null;
}
}
Object.defineProperties(DisplayObject.prototype, {
/**
* Set this to true if you want this display object to be cached as a bitmap.
* This basically takes a snap shot of the display object as it is at that moment. It can
* provide a performance benefit for complex static displayObjects.
* To remove simply set this property to `false`
*
* IMPORTANT GOTCHA - Make sure that all your textures are preloaded BEFORE setting this property to true
* as it will take a snapshot of what is currently there. If the textures have not loaded then they will not appear.
*
* @member {boolean}
* @memberof PIXI.DisplayObject#
*/
cacheAsBitmap: {
get()
{
return this._cacheAsBitmap;
},
set(value)
{
if (this._cacheAsBitmap === value)
{
return;
}
this._cacheAsBitmap = value;
let data;
if (value)
{
if (!this._cacheData)
{
this._cacheData = new CacheData();
}
data = this._cacheData;
data.originalRender = this.render;
data.originalRenderCanvas = this.renderCanvas;
data.originalUpdateTransform = this.updateTransform;
data.originalCalculateBounds = this.calculateBounds;
data.originalGetLocalBounds = this.getLocalBounds;
data.originalDestroy = this.destroy;
data.originalContainsPoint = this.containsPoint;
data.originalMask = this._mask;
data.originalFilterArea = this.filterArea;
this.render = this._renderCached;
this.renderCanvas = this._renderCachedCanvas;
this.destroy = this._cacheAsBitmapDestroy;
}
else
{
data = this._cacheData;
if (data.sprite)
{
this._destroyCachedDisplayObject();
}
this.render = data.originalRender;
this.renderCanvas = data.originalRenderCanvas;
this.calculateBounds = data.originalCalculateBounds;
this.getLocalBounds = data.originalGetLocalBounds;
this.destroy = data.originalDestroy;
this.updateTransform = data.originalUpdateTransform;
this.containsPoint = data.originalContainsPoint;
this._mask = data.originalMask;
this.filterArea = data.originalFilterArea;
}
},
},
});
/**
* Renders a cached version of the sprite with WebGL
*
* @private
* @function _renderCached
* @memberof PIXI.DisplayObject#
* @param {PIXI.Renderer} renderer - the WebGL renderer
*/
DisplayObject.prototype._renderCached = function _renderCached(renderer)
{
if (!this.visible || this.worldAlpha <= 0 || !this.renderable)
{
return;
}
this._initCachedDisplayObject(renderer);
this._cacheData.sprite.transform._worldID = this.transform._worldID;
this._cacheData.sprite.worldAlpha = this.worldAlpha;
this._cacheData.sprite._render(renderer);
};
/**
* Prepares the WebGL renderer to cache the sprite
*
* @private
* @function _initCachedDisplayObject
* @memberof PIXI.DisplayObject#
* @param {PIXI.Renderer} renderer - the WebGL renderer
*/
DisplayObject.prototype._initCachedDisplayObject = function _initCachedDisplayObject(renderer)
{
if (this._cacheData && this._cacheData.sprite)
{
return;
}
// make sure alpha is set to 1 otherwise it will get rendered as invisible!
const cacheAlpha = this.alpha;
this.alpha = 1;
// first we flush anything left in the renderer (otherwise it would get rendered to the cached texture)
renderer.batch.flush();
// this.filters= [];
// next we find the dimensions of the untransformed object
// this function also calls updatetransform on all its children as part of the measuring.
// This means we don't need to update the transform again in this function
// TODO pass an object to clone too? saves having to create a new one each time!
const bounds = this.getLocalBounds().clone();
// add some padding!
if (this.filters)
{
const padding = this.filters[0].padding;
bounds.pad(padding);
}
bounds.ceil(settings.RESOLUTION);
// for now we cache the current renderTarget that the WebGL renderer is currently using.
// this could be more elegant..
const cachedRenderTarget = renderer._activeRenderTarget;
// We also store the filter stack - I will definitely look to change how this works a little later down the line.
// const stack = renderer.filterManager.filterStack;
// this renderTexture will be used to store the cached DisplayObject
const renderTexture = RenderTexture.create(bounds.width, bounds.height);
const textureCacheId = `cacheAsBitmap_${uid()}`;
this._cacheData.textureCacheId = textureCacheId;
BaseTexture.addToCache(renderTexture.baseTexture, textureCacheId);
Texture.addToCache(renderTexture, textureCacheId);
// need to set //
const m = _tempMatrix;
m.tx = -bounds.x;
m.ty = -bounds.y;
// reset
this.transform.worldTransform.identity();
// set all properties to there original so we can render to a texture
this.render = this._cacheData.originalRender;
renderer.render(this, renderTexture, true, m, true);
// now restore the state be setting the new properties
renderer.renderTexture.bind(cachedRenderTarget);
// renderer.filterManager.filterStack = stack;
this.render = this._renderCached;
// the rest is the same as for Canvas
this.updateTransform = this.displayObjectUpdateTransform;
this.calculateBounds = this._calculateCachedBounds;
this.getLocalBounds = this._getCachedLocalBounds;
this._mask = null;
this.filterArea = null;
// create our cached sprite
const cachedSprite = new Sprite(renderTexture);
cachedSprite.transform.worldTransform = this.transform.worldTransform;
cachedSprite.anchor.x = -(bounds.x / bounds.width);
cachedSprite.anchor.y = -(bounds.y / bounds.height);
cachedSprite.alpha = cacheAlpha;
cachedSprite._bounds = this._bounds;
this._cacheData.sprite = cachedSprite;
this.transform._parentID = -1;
// restore the transform of the cached sprite to avoid the nasty flicker..
if (!this.parent)
{
this.parent = renderer._tempDisplayObjectParent;
this.updateTransform();
this.parent = null;
}
else
{
this.updateTransform();
}
// map the hit test..
this.containsPoint = cachedSprite.containsPoint.bind(cachedSprite);
};
/**
* Renders a cached version of the sprite with canvas
*
* @private
* @function _renderCachedCanvas
* @memberof PIXI.DisplayObject#
* @param {PIXI.Renderer} renderer - the WebGL renderer
*/
DisplayObject.prototype._renderCachedCanvas = function _renderCachedCanvas(renderer)
{
if (!this.visible || this.worldAlpha <= 0 || !this.renderable)
{
return;
}
this._initCachedDisplayObjectCanvas(renderer);
this._cacheData.sprite.worldAlpha = this.worldAlpha;
this._cacheData.sprite._renderCanvas(renderer);
};
// TODO this can be the same as the WebGL version.. will need to do a little tweaking first though..
/**
* Prepares the Canvas renderer to cache the sprite
*
* @private
* @function _initCachedDisplayObjectCanvas
* @memberof PIXI.DisplayObject#
* @param {PIXI.Renderer} renderer - the WebGL renderer
*/
DisplayObject.prototype._initCachedDisplayObjectCanvas = function _initCachedDisplayObjectCanvas(renderer)
{
if (this._cacheData && this._cacheData.sprite)
{
return;
}
// get bounds actually transforms the object for us already!
const bounds = this.getLocalBounds();
const cacheAlpha = this.alpha;
this.alpha = 1;
const cachedRenderTarget = renderer.context;
bounds.ceil(settings.RESOLUTION);
const renderTexture = RenderTexture.create(bounds.width, bounds.height);
const textureCacheId = `cacheAsBitmap_${uid()}`;
this._cacheData.textureCacheId = textureCacheId;
BaseTexture.addToCache(renderTexture.baseTexture, textureCacheId);
Texture.addToCache(renderTexture, textureCacheId);
// need to set //
const m = _tempMatrix;
this.transform.localTransform.copyTo(m);
m.invert();
m.tx -= bounds.x;
m.ty -= bounds.y;
// m.append(this.transform.worldTransform.)
// set all properties to there original so we can render to a texture
this.renderCanvas = this._cacheData.originalRenderCanvas;
// renderTexture.render(this, m, true);
renderer.render(this, renderTexture, true, m, false);
// now restore the state be setting the new properties
renderer.context = cachedRenderTarget;
this.renderCanvas = this._renderCachedCanvas;
// the rest is the same as for WebGL
this.updateTransform = this.displayObjectUpdateTransform;
this.calculateBounds = this._calculateCachedBounds;
this.getLocalBounds = this._getCachedLocalBounds;
this._mask = null;
this.filterArea = null;
// create our cached sprite
const cachedSprite = new Sprite(renderTexture);
cachedSprite.transform.worldTransform = this.transform.worldTransform;
cachedSprite.anchor.x = -(bounds.x / bounds.width);
cachedSprite.anchor.y = -(bounds.y / bounds.height);
cachedSprite.alpha = cacheAlpha;
cachedSprite._bounds = this._bounds;
this._cacheData.sprite = cachedSprite;
this.transform._parentID = -1;
// restore the transform of the cached sprite to avoid the nasty flicker..
if (!this.parent)
{
this.parent = renderer._tempDisplayObjectParent;
this.updateTransform();
this.parent = null;
}
else
{
this.updateTransform();
}
// map the hit test..
this.containsPoint = cachedSprite.containsPoint.bind(cachedSprite);
};
/**
* Calculates the bounds of the cached sprite
*
* @private
*/
DisplayObject.prototype._calculateCachedBounds = function _calculateCachedBounds()
{
this._bounds.clear();
this._cacheData.sprite.transform._worldID = this.transform._worldID;
this._cacheData.sprite._calculateBounds();
this._lastBoundsID = this._boundsID;
};
/**
* Gets the bounds of the cached sprite.
*
* @private
* @return {Rectangle} The local bounds.
*/
DisplayObject.prototype._getCachedLocalBounds = function _getCachedLocalBounds()
{
return this._cacheData.sprite.getLocalBounds();
};
/**
* Destroys the cached sprite.
*
* @private
*/
DisplayObject.prototype._destroyCachedDisplayObject = function _destroyCachedDisplayObject()
{
this._cacheData.sprite._texture.destroy(true);
this._cacheData.sprite = null;
BaseTexture.removeFromCache(this._cacheData.textureCacheId);
Texture.removeFromCache(this._cacheData.textureCacheId);
this._cacheData.textureCacheId = null;
};
/**
* Destroys the cached object.
*
* @private
* @param {object|boolean} [options] - Options parameter. A boolean will act as if all options
* have been set to that value.
* Used when destroying containers, see the Container.destroy method.
*/
DisplayObject.prototype._cacheAsBitmapDestroy = function _cacheAsBitmapDestroy(options)
{
this.cacheAsBitmap = false;
this.destroy(options);
};