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
/****************************************************************************
 Copyright (c) 2017-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 Armature = require('./ArmatureDisplay');
const renderEngine = require('../../cocos2d/core/renderer/render-engine');
const RenderFlow = require('../../cocos2d/core/renderer/render-flow');
const gfx = renderEngine.gfx;
const math = require('../../cocos2d/core/renderer/render-engine').math;
const NEED_NONE = 0x00;
const NEED_COLOR = 0x01;
const NEED_BATCH = 0x10;
const NEED_COLOR_BATCH = 0x11;
 
let _boneColor = cc.color(255, 0, 0, 255);
let _slotColor = cc.color(0, 0, 255, 255);
 
let _nodeR, _nodeG, _nodeB, _nodeA,
    _premultipliedAlpha,
    _mustFlush, _buffer, _node,
    _renderer, _comp,
    _vfOffset, _indexOffset, _vertexOffset,
    _vertexCount, _indexCount,
    _x, _y, _c, _r, _g, _b, _a, _handleVal,
    _m00, _m04, _m12,
    _m01, _m05, _m13;
 
function _getSlotMaterial (tex, blendMode) {
    if(!tex)return null;
 
    let src, dst;
    switch (blendMode) {
        case 1://additive
            src = _premultipliedAlpha ? cc.macro.ONE : cc.macro.SRC_ALPHA;
            dst = cc.macro.ONE;
            break;
        case 10://multiply
            src = cc.macro.DST_COLOR;
            dst = cc.macro.ONE_MINUS_SRC_ALPHA;
            break;
        case 12://screen
            src = cc.macro.ONE;
            dst = cc.macro.ONE_MINUS_SRC_COLOR;
            break;
        case 0://normal
        default:
            src = _premultipliedAlpha ? cc.macro.ONE : cc.macro.SRC_ALPHA;
            dst = cc.macro.ONE_MINUS_SRC_ALPHA;
            break;
    }
 
    let useModel = !_comp.enableBatch;
    // Add useModel flag due to if pre same db useModel but next db no useModel,
    // then next db will multiply model matrix more than once.
    let key = tex.url + src + dst + useModel;
    let baseMaterial = _comp._material;
    if (!baseMaterial) return null;
    let materialCache = _comp._materialCache;
    let material = materialCache[key];
    if (!material) {
 
        var baseKey = baseMaterial._hash;
        if (!materialCache[baseKey]) {
            material = baseMaterial;
        } else {
            material = baseMaterial.clone();
        }
 
        material.useModel = useModel;
        // update texture
        material.texture = tex;
        material.useColor = false;
 
        // update blend function
        let pass = material._mainTech.passes[0];
        pass.setBlend(
            gfx.BLEND_FUNC_ADD,
            src, dst,
            gfx.BLEND_FUNC_ADD,
            src, dst
        );
        materialCache[key] = material;
        material.updateHash(key);
    }
    else if (material.texture !== tex) {
        material.texture = tex;
        material.updateHash(key);
    }
    return material;
}
 
function _handleColor (color) {
    _r = color.r * _nodeR;
    _g = color.g * _nodeG;
    _b = color.b * _nodeB;
    _a = color.a * _nodeA;
    _c = ((_a<<24) >>> 0) + (_b<<16) + (_g<<8) + _r;
}
 
let armatureAssembler = {
    updateRenderData (comp, batchData) {},
 
    realTimeTraverse (armature, parentMat) {
        let slots = armature._slots;
        let vbuf, ibuf, uintbuf;
        let material;
        let vertices, indices;
        let slotColor;
        let slot;
        let slotMat;
        let offsetInfo;
 
        for (let i = 0, l = slots.length; i < l; i++) {
            slot = slots[i];
            if (!slot._visible || !slot._displayData) continue;
 
            if (parentMat) {
                slot._mulMat(slot._worldMatrix, parentMat, slot._matrix);
            } else {
                math.mat4.copy(slot._worldMatrix, slot._matrix);
            }
 
            if (slot.childArmature) {
                this.realTimeTraverse(slot.childArmature, slot._worldMatrix);
                continue;
            }
 
            material = _getSlotMaterial(slot.getTexture(), slot._blendMode);
            if (!material) {
                continue;
            }
 
            if (_mustFlush || material._hash !== _renderer.material._hash) {
                _mustFlush = false;
                _renderer._flush();
                _renderer.node = _node;
                _renderer.material = material;
            }
 
            slotColor = slot._color;
            _handleColor(slotColor);
            slotMat = slot._worldMatrix;
 
            vertices = slot._localVertices;
            _vertexCount = vertices.length >> 2;
 
            indices = slot._indices;
            _indexCount = indices.length;
            
            offsetInfo = _buffer.request(_vertexCount, _indexCount);
            _indexOffset = offsetInfo.indiceOffset;
            _vfOffset = offsetInfo.byteOffset >> 2;
            _vertexOffset = offsetInfo.vertexOffset;
            vbuf = _buffer._vData;
            ibuf = _buffer._iData;
            uintbuf = _buffer._uintVData;
 
            _m00 = slotMat.m00;
            _m04 = slotMat.m04;
            _m12 = slotMat.m12;
            _m01 = slotMat.m01;
            _m05 = slotMat.m05;
            _m13 = slotMat.m13;
 
            for (let vi = 0, vl = vertices.length; vi < vl;) {
                _x = vertices[vi++]; 
                _y = vertices[vi++];
 
                vbuf[_vfOffset++] = _x * _m00 + _y * _m04 + _m12; // x
                vbuf[_vfOffset++] = _x * _m01 + _y * _m05 + _m13; // y
 
                vbuf[_vfOffset++] = vertices[vi++]; // u
                vbuf[_vfOffset++] = vertices[vi++]; // v
                uintbuf[_vfOffset++] = _c; // color
            }
 
            for (let ii = 0, il = indices.length; ii < il; ii ++) {
                ibuf[_indexOffset++] = _vertexOffset + indices[ii];
            }
        }
    },
 
    cacheTraverse (frame, parentMat) {
        if (!frame) return;
        let segments = frame.segments;
        if (segments.length == 0) return;
 
        let vbuf, ibuf, uintbuf;
        let material;
        let offsetInfo;
        let vertices = frame.vertices;
        let indices = frame.indices;
        let uintVert = frame.uintVert;
        
        let frameVFOffset = 0, frameIndexOffset = 0, segVFCount = 0;
        if (parentMat) {
            _m00 = parentMat.m00;
            _m04 = parentMat.m04;
            _m12 = parentMat.m12;
            _m01 = parentMat.m01;
            _m05 = parentMat.m05;
            _m13 = parentMat.m13;
        }
 
        let colorOffset = 0;
        let colors = frame.colors;
        let nowColor = colors[colorOffset++];
        let maxVFOffset = nowColor.vfOffset;
        _handleColor(nowColor);
 
        for (let i = 0, n = segments.length; i < n; i++) {
            let segInfo = segments[i];
            material = _getSlotMaterial(segInfo.tex, segInfo.blendMode);
            if (_mustFlush || material._hash !== _renderer.material._hash) {
                _mustFlush = false;
                _renderer._flush();
                _renderer.node = _node;
                _renderer.material = material;
            }
 
            _vertexCount = segInfo.vertexCount;
            _indexCount = segInfo.indexCount;
            
            offsetInfo = _buffer.request(_vertexCount, _indexCount);
            _indexOffset = offsetInfo.indiceOffset;
            _vertexOffset = offsetInfo.vertexOffset;
            _vfOffset = offsetInfo.byteOffset >> 2;
            vbuf = _buffer._vData;
            ibuf = _buffer._iData;
            uintbuf = _buffer._uintVData;
 
            for (let ii = _indexOffset, il = _indexOffset + _indexCount; ii < il; ii++) {
                ibuf[ii] = _vertexOffset + indices[frameIndexOffset++];
            }
 
            segVFCount = segInfo.vfCount;
            switch (_handleVal) {
                case NEED_COLOR:
                case NEED_NONE:
                    vbuf.set(vertices.subarray(frameVFOffset, frameVFOffset + segVFCount), _vfOffset);
                    frameVFOffset += segVFCount;
                break;
                case NEED_BATCH:
                case NEED_COLOR_BATCH:
                    for (let ii = _vfOffset, il = _vfOffset + segVFCount; ii < il;) {
                        _x = vertices[frameVFOffset++];
                        _y = vertices[frameVFOffset++];
                        vbuf[ii++] = _x * _m00 + _y * _m04 + _m12; // x
                        vbuf[ii++] = _x * _m01 + _y * _m05 + _m13; // y
                        vbuf[ii++] = vertices[frameVFOffset++]; // u
                        vbuf[ii++] = vertices[frameVFOffset++]; // v
                        uintbuf[ii++] = uintVert[frameVFOffset++];
                    }
                break;
            }
 
            if ( !(_handleVal & NEED_COLOR) ) continue;
 
            // handle color
            let frameColorOffset = frameVFOffset - segVFCount;
            for (let ii = _vfOffset + 4, il = _vfOffset + 4 + segVFCount; ii < il; ii+=5, frameColorOffset += 5) {
                if (frameColorOffset >= maxVFOffset) {
                    nowColor = colors[colorOffset++];
                    _handleColor(nowColor);
                    maxVFOffset = nowColor.vfOffset;
                }
                uintbuf[ii] = _c;
            }
        }
    },
 
    fillBuffers (comp, renderer) {
        comp.node._renderFlag |= RenderFlow.FLAG_UPDATE_RENDER_DATA;
        
        // Init temp var.
        _mustFlush = true;
        _premultipliedAlpha = comp.premultipliedAlpha;
        _node = comp.node;
        _buffer = renderer._meshBuffer;
        _renderer = renderer;
        _comp = comp;
        _handleVal = 0;
 
        let nodeColor = _node._color;
        _nodeR = nodeColor.r / 255;
        _nodeG = nodeColor.g / 255;
        _nodeB = nodeColor.b / 255;
        _nodeA = nodeColor.a / 255;
        if (nodeColor._val !== 0xffffffff) {
            _handleVal |= NEED_COLOR;
        }
 
        let worldMat = undefined;
        if (_comp.enableBatch) {
            worldMat = _node._worldMatrix;
            _mustFlush = false;
            _handleVal |= NEED_BATCH;
        }
 
        if (comp.isAnimationCached()) {
            // Traverse input assembler.
            this.cacheTraverse(comp._curFrame, worldMat);
        } else {
            // Traverse all armature.
            let armature = comp._armature;
            if (!armature) return;
 
            this.realTimeTraverse(armature, worldMat);
 
            let graphics = comp._debugDraw;
            if (comp.debugBones && graphics) {
                graphics.clear();
 
                graphics.lineWidth = 5;
                graphics.strokeColor = _boneColor;
                graphics.fillColor = _slotColor; // Root bone color is same as slot color.
 
                let bones = armature.getBones();
                for (let i = 0, l = bones.length; i < l; i++) {
                    let bone =  bones[i];
                    let boneLength = Math.max(bone.boneData.length, 5);
                    let startX = bone.globalTransformMatrix.tx;
                    let startY = -bone.globalTransformMatrix.ty;
                    let endX = startX + bone.globalTransformMatrix.a * boneLength;
                    let endY = startY - bone.globalTransformMatrix.b * boneLength;
 
                    graphics.moveTo(startX, startY);
                    graphics.lineTo(endX, endY);
                    graphics.stroke();
                }
            }
        }
        
        // Clear temp var.
        _node = undefined;
        _buffer = undefined;
        _renderer = undefined;
        _comp = undefined;
    }
};
 
module.exports = Armature._assembler = armatureAssembler;