difenduandada
2024-12-31 34abe6963b344c882358274957f4b992456fee40
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
/****************************************************************************
 Copyright (c) 2018 Xiamen Yaji Software Co., Ltd.
 
 https://www.cocos.com/
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated engine source code (the "Software"), a limited,
 worldwide, royalty-free, non-assignable, revocable and non-exclusive license
 to use Cocos Creator solely to develop games on your target platforms. You shall
 not use Cocos Creator software for developing other software or tools that's
 used for developing games. You are not granted to publish, distribute,
 sublicense, and/or sell copies of Cocos Creator.
 
 The software or tools in this License Agreement are licensed, not sold.
 Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/
const MaxCacheTime = 30;
 
let _vertices = [];
let _indices = [];
let _vertexOffset = 0;
let _indexOffset = 0;
let _vfOffset = 0;
let _frameTime = 1 / 60;
let _preTexUrl = null;
let _preBlendMode = null;
let _segVCount = 0;
let _segICount = 0;
let _segOffset = 0;
let _colorOffset = 0;
let _preColor = null;
let _x, _y;
 
//Cache all frames in an animation
let AnimationCache = cc.Class({
    ctor () {
        this.frames = [];
        this.totalTime = 0;
 
        this._animationName = null;
        this._tempSegments = null;
        this._tempColors = null;
    },
 
    init (animationName) {
        this._animationName = animationName;
    },
 
    // Clear texture quote.
    clear () {
        for (let i = 0, n = this.frames.length; i < n; i++) {
            let frame = this.frames[i];
            frame.segments.length = 0;
        }
    },
 
    update (armature) {
        if (!this._animationName) {
            cc.error("AnimationCache:update animationName is empty");
            return;
        }
        let animation = armature.animation;
        animation.play(this._animationName, 1);
        let frameIdx = 0;
        this.totalTime = 0;
        do {
            // Solid update frame rate 1/60.
            armature.advanceTime(_frameTime);
            this._updateFrame(armature, frameIdx);
            frameIdx++;
            this.totalTime += _frameTime;
        } while (!animation.isCompleted && this.totalTime < MaxCacheTime);
        // Update frame length.
        this.frames.length = frameIdx;
    },
 
    _updateFrame (armature, index) {
        _vfOffset = 0;
        _indexOffset = 0;
        _vertexOffset = 0;
        _preTexUrl = null;
        _preBlendMode = null;
        _segVCount = 0;
        _segICount = 0;
        _segOffset = 0;
        _colorOffset = 0;
        _preColor = null;
 
        this.frames[index] = this.frames[index] || {
            segments : [],
            colors : [],
            vertices : null,
            uintVert : null,
            indices : null,
        };
        let frame = this.frames[index];
 
        let segments = this._tempSegments = frame.segments;
        let colors = this._tempColors = frame.colors;
        this._traverseArmature(armature);
        // At last must handle pre color and segment.
        // Because vertex count will right at the end.
        // Handle pre color.
        if (_colorOffset > 0) {
            colors[_colorOffset - 1].vfOffset = _vfOffset;
        }
        colors.length = _colorOffset;
        // Handle pre segment
        let preSegOffset = _segOffset - 1;
        if (preSegOffset >= 0) {
            if (_segICount > 0) {
                let preSegInfo = segments[preSegOffset];
                preSegInfo.indexCount = _segICount;
                preSegInfo.vfCount = _segVCount * 5;
                preSegInfo.vertexCount = _segVCount;
                segments.length = _segOffset;
            } else {
                segments.length = _segOffset - 1;
            }
        }
 
        // Discard all segments.
        if (segments.length === 0) return;
 
        // Fill vertices
        let vertices = frame.vertices || new Float32Array(_vfOffset);
        let uintVert = frame.uintVert || new Uint32Array(vertices.buffer);
        for (let i = 0, j = 0; i < _vfOffset;) {
            vertices[i++] = _vertices[j++]; // x
            vertices[i++] = _vertices[j++]; // y
            vertices[i++] = _vertices[j++]; // u
            vertices[i++] = _vertices[j++]; // v
            uintVert[i++] = _vertices[j++]; // color
        }
 
        // Fill indices
        let indices = frame.indices || new Uint16Array(_indexOffset);
        for (let i = 0; i < _indexOffset; i++) {
            indices[i] = _indices[i];
        }
 
        frame.vertices = vertices;
        frame.uintVert = uintVert;
        frame.indices = indices;
    },
 
    _traverseArmature (armature) {
        let colors = this._tempColors;
        let segments = this._tempSegments;
        let gVertices = _vertices;
        let gIndices = _indices;
        let slotVertices, slotIndices;
        let slots = armature._slots, slot, slotMatrix, slotColor, colorVal;
        let texture;
        let preSegOffset, preSegInfo;
 
        for (let i = 0, l = slots.length; i < l; i++) {
            slot = slots[i];
            if (!slot._visible || !slot._displayData) continue;
 
            slot.updateWorldMatrix();
 
            if (slot.childArmature) {
                this._traverseArmature(slot.childArmature);
                continue;
            }
 
            texture = slot.getTexture();
            if (!texture) continue;
 
            slotColor = slot._color;
 
            if (_preTexUrl !== texture.url || _preBlendMode !== slot._blendMode) {
                _preTexUrl = texture.url;
                _preBlendMode = slot._blendMode;
                // Handle pre segment.
                preSegOffset = _segOffset - 1;
                if (preSegOffset >= 0) {
                    if (_segICount > 0) {
                        preSegInfo = segments[preSegOffset];
                        preSegInfo.indexCount = _segICount;
                        preSegInfo.vertexCount = _segVCount;
                        preSegInfo.vfCount = _segVCount * 5;
                    } else {
                        // Discard pre segment.
                        _segOffset--;
                    }
                }
                // Handle now segment.
                segments[_segOffset] = {
                    tex : texture,
                    blendMode : slot._blendMode,
                    indexCount : 0,
                    vertexCount : 0,
                    vfCount : 0
                };
                _segOffset++;
                _segICount = 0;
                _segVCount = 0;
            }
 
            colorVal = ((slotColor.a<<24) >>> 0) + (slotColor.b<<16) + (slotColor.g<<8) + slotColor.r;
 
            if (_preColor !== colorVal) {
                _preColor = colorVal;
                if (_colorOffset > 0) {
                    colors[_colorOffset - 1].vfOffset = _vfOffset;
                }
                colors[_colorOffset++] = {
                    r : slotColor.r,
                    g : slotColor.g,
                    b : slotColor.b,
                    a : slotColor.a,
                    vfOffset : 0
                }
            }
 
            slotVertices = slot._localVertices;
            slotIndices = slot._indices;
 
            slotMatrix = slot._worldMatrix;
 
            for (let j = 0, vl = slotVertices.length; j < vl;) {
                _x = slotVertices[j++];
                _y = slotVertices[j++];
                gVertices[_vfOffset++] = _x * slotMatrix.m00 + _y * slotMatrix.m04 + slotMatrix.m12;
                gVertices[_vfOffset++] = _x * slotMatrix.m01 + _y * slotMatrix.m05 + slotMatrix.m13;
                gVertices[_vfOffset++] = slotVertices[j++];
                gVertices[_vfOffset++] = slotVertices[j++];
                gVertices[_vfOffset++] = colorVal;
            }
            
            // This place must use segment vertex count to calculate vertex offset.
            // Assembler will calculate vertex offset again for different segment.
            for (let ii = 0, il = slotIndices.length; ii < il; ii ++) {
                gIndices[_indexOffset++] = _segVCount + slotIndices[ii];
            }
 
            _vertexOffset = _vfOffset / 5;
            _segICount += slotIndices.length;
            _segVCount += slotVertices.length / 4;
        }
    },
});
 
let ArmatureCache = cc.Class({
    ctor () {
        this._animationPool = {};
        this._armatureCache = {};
    },
 
    // If cache is private, cache will be destroy when dragonbones node destroy.
    dispose () {
        for (var key in this._armatureCache) {
            var armatureInfo = this._armatureCache[key];
            if (armatureInfo) {
                let armature = armatureInfo.armature;
                armature && armature.dispose();
            }
        }
        this._armatureCache = null;
        this._animationPool = null;
    },
 
    _removeArmature (armatureKey) {
        var armatureInfo = this._armatureCache[armatureKey];
        let animationsCache = armatureInfo.animationsCache;
        for (var aniKey in animationsCache) {
            // Clear cache texture, and put cache into pool.
            // No need to create TypedArray next time.
            let animationCache = animationsCache[aniKey];
            if (!animationCache) continue;
            this._animationPool[armatureKey + "#" + aniKey] = animationCache;
            animationCache.clear();
        }
 
        let armature = armatureInfo.armature;
        armature && armature.dispose();
        delete this._armatureCache[armatureKey];
    },
 
    // When db assets be destroy, remove armature from db cache.
    resetArmature (uuid) {
        for (var armatureKey in this._armatureCache) {
            if (armatureKey.indexOf(uuid) == -1) continue;
            this._removeArmature(armatureKey);
        }
    },
 
    getArmatureCache (armatureName, armatureKey, atlasUUID) {
        let armatureInfo = this._armatureCache[armatureKey];
        let armature;
        if (!armatureInfo) {
            let factory = dragonBones.CCFactory.getInstance();
            let proxy = factory.buildArmatureDisplay(armatureName, armatureKey, "", atlasUUID);
            if (!proxy || !proxy._armature) return;
            armature = proxy._armature;
            // If armature has child armature, can not be cache, because it's
            // animation data can not be precompute.
            if (!ArmatureCache.canCache(armature)) {
                armature.dispose();
                return;
            }
 
            this._armatureCache[armatureKey] = {
                armature : armature,
                // Cache all kinds of animation frame.
                // When armature is dispose, clear all animation cache.
                animationsCache : {},
            };
        } else {
            armature = armatureInfo.armature;
        }
        return armature;
    },
 
    getAnimationCache (armatureKey, animationName) {
        let armatureInfo = this._armatureCache[armatureKey];
        if (!armatureInfo) return null;
 
        let animationsCache = armatureInfo.animationsCache;
        return animationsCache[animationName];
    },
 
    updateAnimationCache (armatureKey, animationName) {
        let armatureInfo = this._armatureCache[armatureKey];
        let armature = armatureInfo && armatureInfo.armature;
        if (!armature) return null;
        let animation = armature.animation;
        let hasAni = animation.hasAnimation(animationName);
        if (!hasAni) return null;
 
        let animationsCache = armatureInfo.animationsCache;
        let animationCache = animationsCache[animationName];
        if (!animationCache) {
            // If cache exist in pool, then just use it.
            let poolKey = armatureKey + "#" + animationName;
            animationCache = this._animationPool[poolKey];
            if (animationCache) {
                delete this._animationPool[poolKey];
            } else {
                animationCache = new AnimationCache();
                animationCache.init(animationName);
            }
            animationsCache[animationName] = animationCache;
        }
        animationCache.update(armature);
        if (animationCache.totalTime >= MaxCacheTime) {
            cc.warn("Animation cache is overflow, maybe animation's frame is infinite, please change armature render mode to REALTIME, dragonbones uuid is [%s], animation name is [%s]", armatureKey, animationName);
        }
        return animationCache;
    }
});
 
ArmatureCache.sharedCache = new ArmatureCache();
ArmatureCache.canCache = function (armature) {
    let slots = armature._slots;
    for (let i = 0, l = slots.length; i < l; i++) {
        let slot = slots[i];
        if (slot.childArmature) {
            return false;
        }
    }
    return true;
},
 
module.exports = ArmatureCache;