From b96ffd8f091303584ab2bc6dbe089fa76252a426 Mon Sep 17 00:00:00 2001 From: zhakesi Date: Mon, 20 Jun 2022 14:31:50 +0800 Subject: [PATCH] optimize function fillVertices in spine module (#11251) * optimize function fillVertices in spine module * use multiplication instead of division Co-authored-by: zhakesi --- cocos/spine/assembler/simple.ts | 149 +++++++++++++++++++++++--------- 1 file changed, 107 insertions(+), 42 deletions(-) diff --git a/cocos/spine/assembler/simple.ts b/cocos/spine/assembler/simple.ts index d6d01ab854..951c115e5b 100644 --- a/cocos/spine/assembler/simple.ts +++ b/cocos/spine/assembler/simple.ts @@ -108,6 +108,7 @@ let _needColor: boolean; let _vertexEffect: spine.VertexEffect | null = null; let _currentMaterial: MaterialInstance | null = null; let _currentTexture: Texture2D | null = null; +const _inv255 = 1.0 / 255.0; function _getSlotMaterial (blendMode: spine.BlendMode) { let src: BlendFactor; @@ -137,7 +138,7 @@ function _getSlotMaterial (blendMode: spine.BlendMode) { function _handleColor (color: FrameColor) { // temp rgb has multiply 255, so need divide 255; _fa = color.fa * _nodeA; - _multiplier = _premultipliedAlpha ? _fa / 255 : 1; + _multiplier = _premultipliedAlpha ? _fa * _inv255 : 1; _r = _nodeR * _multiplier; _g = _nodeG * _multiplier; _b = _nodeB * _multiplier; @@ -145,27 +146,27 @@ function _handleColor (color: FrameColor) { _fr = color.fr * _r; _fg = color.fg * _g; _fb = color.fb * _b; - _finalColor32[0] = _fr / 255.0; - _finalColor32[1] = _fg / 255.0; - _finalColor32[2] = _fb / 255.0; - _finalColor32[3] = _fa / 255.0; + _finalColor32[0] = _fr * _inv255; + _finalColor32[1] = _fg * _inv255; + _finalColor32[2] = _fb * _inv255; + _finalColor32[3] = _fa * _inv255; _dr = color.dr * _r; _dg = color.dg * _g; _db = color.db * _b; _da = _premultipliedAlpha ? 255 : 0; - _darkColor32[0] = _dr / 255.0; - _darkColor32[1] = _dg / 255.0; - _darkColor32[2] = _db / 255.0; - _darkColor32[3] = _da / 255.0; + _darkColor32[0] = _dr * _inv255; + _darkColor32[1] = _dg * _inv255; + _darkColor32[2] = _db * _inv255; + _darkColor32[3] = _da * _inv255; } const _tmpColor4 = new Float32Array(4); function _spineColorToFloat32Array4 (spineColor: spine.Color) { - _tmpColor4[0] = spineColor.r / 255.0; - _tmpColor4[1] = spineColor.g / 255.0; - _tmpColor4[2] = spineColor.b / 255.0; - _tmpColor4[3] = spineColor.a / 255.0; + _tmpColor4[0] = spineColor.r * _inv255; + _tmpColor4[1] = spineColor.g * _inv255; + _tmpColor4[2] = spineColor.b * _inv255; + _tmpColor4[3] = spineColor.a * _inv255; return _tmpColor4; } @@ -262,9 +263,9 @@ function updateComponentRenderData (comp: Skeleton, batcher: Batcher2D) { if (!comp._skeleton) return; const nodeColor = comp.color; - _nodeR = nodeColor.r / 255; - _nodeG = nodeColor.g / 255; - _nodeB = nodeColor.b / 255; + _nodeR = nodeColor.r * _inv255; + _nodeG = nodeColor.g * _inv255; + _nodeB = nodeColor.b * _inv255; _nodeA = comp.node._uiProps.opacity; _useTint = comp.useTint || comp.isAnimationCached(); @@ -366,7 +367,91 @@ function fillVertices (skeletonColor: spine.Color, } _darkColor.a = _premultipliedAlpha ? 255 : 0; - if (!clipper.isClipping()) { + if (_useTint) { + if (!clipper.isClipping()) { + if (_vertexEffect) { + for (let v = _vertexFloatOffset, n = _vertexFloatOffset + _vertexFloatCount; v < n; v += _perVertexSize) { + _tempPos.x = _vbuf[v]; + _tempPos.y = _vbuf[v + 1]; + _tempUv.x = _vbuf[v + 3]; + _tempUv.y = _vbuf[v + 4]; + _vertexEffect.transform(_tempPos, _tempUv, _finalColor, _darkColor); + + _vbuf[v] = _tempPos.x; // x + _vbuf[v + 1] = _tempPos.y; // y + _vbuf[v + 3] = _tempUv.x; // u + _vbuf[v + 4] = _tempUv.y; // v + + _vbuf.set(_spineColorToFloat32Array4(_finalColor), v + 5); + _vbuf.set(_spineColorToFloat32Array4(_darkColor), v + 9); // dark color + } + } else { + _finalColor32.set(_spineColorToFloat32Array4(_finalColor)); + _darkColor32.set(_spineColorToFloat32Array4(_darkColor)); + + for (let v = _vertexFloatOffset, n = _vertexFloatOffset + _vertexFloatCount; v < n; v += _perVertexSize) { + _vbuf.set(_finalColor32, v + 5); // light color + _vbuf.set(_darkColor32, v + 9); // dark color + } + } + } else { + _perClipVertexSize = _useTint ? 12 : 8; // const + const vertices = _vbuf.subarray(_vertexFloatOffset); + const uvs = _vbuf.subarray(_vertexFloatOffset + 3); + + clipper.clipTriangles(vertices, _vertexFloatCount, + _ibuf.subarray(_indexOffset), _indexCount, uvs, _finalColor, _darkColor, _useTint, + _perVertexSize); + const clippedVertices = clipper.clippedVertices; + const clippedTriangles = clipper.clippedTriangles; + + // Update vertex and index count, reallocate vertex buffer chunk if needed + updateChunkForClip(clippedVertices, clippedTriangles); + + // fill indices + if (clippedTriangles.length > 0) { + _ibuf.set(clippedTriangles, _indexOffset); + } + + // fill vertices contain x y u v light color dark color + if (_vertexEffect) { + for (let v = 0, n = clippedVertices.length, offset = _vertexFloatOffset; v < n; v += _perClipVertexSize, offset += _perVertexSize) { + _tempPos.x = clippedVertices[v]; + _tempPos.y = clippedVertices[v + 1]; + _finalColor.set(clippedVertices[v + 2], clippedVertices[v + 3], clippedVertices[v + 4], clippedVertices[v + 5]); + _tempUv.x = clippedVertices[v + 6]; + _tempUv.y = clippedVertices[v + 7]; + _darkColor.set(clippedVertices[v + 8], clippedVertices[v + 9], clippedVertices[v + 10], clippedVertices[v + 11]); + _vertexEffect.transform(_tempPos, _tempUv, _finalColor, _darkColor); + + _vbuf[offset] = _tempPos.x; // x + _vbuf[offset + 1] = _tempPos.y; // y + _vbuf[offset + 3] = _tempUv.x; // u + _vbuf[offset + 4] = _tempUv.y; // v + _vbuf.set(_spineColorToFloat32Array4(_finalColor), offset + 5); + _vbuf.set(_spineColorToFloat32Array4(_darkColor), offset + 9); + } + } else { + // x y r g b a u v (rr gg bb aa) + for (let v = 0, n = clippedVertices.length, offset = _vertexFloatOffset; v < n; v += _perClipVertexSize, offset += _perVertexSize) { + _vbuf[offset] = clippedVertices[v]; // x + _vbuf[offset + 1] = clippedVertices[v + 1]; // y + _vbuf[offset + 3] = clippedVertices[v + 6]; // u + _vbuf[offset + 4] = clippedVertices[v + 7]; // v + + _vbuf[offset + 5] = clippedVertices[v + 2] * _inv255; + _vbuf[offset + 6] = clippedVertices[v + 3] * _inv255; + _vbuf[offset + 7] = clippedVertices[v + 4] * _inv255; + _vbuf[offset + 8] = clippedVertices[v + 5] * _inv255; + + _vbuf[offset + 9] = clippedVertices[v + 8] * _inv255; + _vbuf[offset + 10] = clippedVertices[v + 9] * _inv255; + _vbuf[offset + 11] = clippedVertices[v + 10] * _inv255; + _vbuf[offset + 12] = clippedVertices[v + 11] * _inv255; + } + } + } + } else if (!clipper.isClipping()) { if (_vertexEffect) { for (let v = _vertexFloatOffset, n = _vertexFloatOffset + _vertexFloatCount; v < n; v += _perVertexSize) { _tempPos.x = _vbuf[v]; @@ -381,9 +466,6 @@ function fillVertices (skeletonColor: spine.Color, _vbuf[v + 4] = _tempUv.y; // v _vbuf.set(_spineColorToFloat32Array4(_finalColor), v + 5); - if (_useTint) { - _vbuf.set(_spineColorToFloat32Array4(_darkColor), v + 9); // dark color - } } } else { _finalColor32.set(_spineColorToFloat32Array4(_finalColor)); @@ -391,9 +473,6 @@ function fillVertices (skeletonColor: spine.Color, for (let v = _vertexFloatOffset, n = _vertexFloatOffset + _vertexFloatCount; v < n; v += _perVertexSize) { _vbuf.set(_finalColor32, v + 5); // light color - if (_useTint) { - _vbuf.set(_darkColor32, v + 9); // dark color - } } } } else { @@ -423,11 +502,7 @@ function fillVertices (skeletonColor: spine.Color, _finalColor.set(clippedVertices[v + 2], clippedVertices[v + 3], clippedVertices[v + 4], clippedVertices[v + 5]); _tempUv.x = clippedVertices[v + 6]; _tempUv.y = clippedVertices[v + 7]; - if (_useTint) { - _darkColor.set(clippedVertices[v + 8], clippedVertices[v + 9], clippedVertices[v + 10], clippedVertices[v + 11]); - } else { - _darkColor.set(0, 0, 0, 0); - } + _darkColor.set(0, 0, 0, 0); _vertexEffect.transform(_tempPos, _tempUv, _finalColor, _darkColor); _vbuf[offset] = _tempPos.x; // x @@ -435,9 +510,6 @@ function fillVertices (skeletonColor: spine.Color, _vbuf[offset + 3] = _tempUv.x; // u _vbuf[offset + 4] = _tempUv.y; // v _vbuf.set(_spineColorToFloat32Array4(_finalColor), offset + 5); - if (_useTint) { - _vbuf.set(_spineColorToFloat32Array4(_darkColor), offset + 9); - } } } else { // x y r g b a u v (rr gg bb aa) @@ -447,17 +519,10 @@ function fillVertices (skeletonColor: spine.Color, _vbuf[offset + 3] = clippedVertices[v + 6]; // u _vbuf[offset + 4] = clippedVertices[v + 7]; // v - _vbuf[offset + 5] = clippedVertices[v + 2] / 255.0; - _vbuf[offset + 6] = clippedVertices[v + 3] / 255.0; - _vbuf[offset + 7] = clippedVertices[v + 4] / 255.0; - _vbuf[offset + 8] = clippedVertices[v + 5] / 255.0; - - if (_useTint) { - _vbuf[offset + 9] = clippedVertices[v + 8] / 255.0; - _vbuf[offset + 10] = clippedVertices[v + 9] / 255.0; - _vbuf[offset + 11] = clippedVertices[v + 10] / 255.0; - _vbuf[offset + 12] = clippedVertices[v + 11] / 255.0; - } + _vbuf[offset + 5] = clippedVertices[v + 2] * _inv255; + _vbuf[offset + 6] = clippedVertices[v + 3] * _inv255; + _vbuf[offset + 7] = clippedVertices[v + 4] * _inv255; + _vbuf[offset + 8] = clippedVertices[v + 5] * _inv255; } } }