Files
cocos4/cocos2d/core/renderer/assemblers/sprite/sliced.js
T

181 lines
7.1 KiB
JavaScript

/****************************************************************************
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://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 Sprite = require('../../../components/CCSprite');
const FillType = Sprite.FillType;
const simpleRenderUtil = require('./simple');
module.exports = {
createData (sprite) {
let renderData = sprite.requestRenderData();
// 0-4 for local verts
// 5-20 for world verts
renderData.dataLength = 20;
renderData.vertexCount = 16;
renderData.indiceCount = 54;
return renderData;
},
updateUVs (sprite) {
let material = sprite.getMaterial();
let renderData = sprite._renderData;
if (material && renderData) {
let texture = material.effect.getProperty('texture');
let frame = sprite.spriteFrame;
let rect = frame._rect;
let atlasWidth = texture._width;
let atlasHeight = texture._height;
// caculate texture coordinate
let leftWidth = frame.insetLeft;
let rightWidth = frame.insetRight;
let centerWidth = rect.width - leftWidth - rightWidth;
let topHeight = frame.insetTop;
let bottomHeight = frame.insetBottom;
let centerHeight = rect.height - topHeight - bottomHeight;
// uv computation should take spritesheet into account.
let data = renderData._data;
if (frame._rotated) {
data[0].u = (rect.x) / atlasWidth;
data[0].v = (rect.y) / atlasHeight;
data[1].u = (bottomHeight + rect.x) / atlasWidth;
data[1].v = (leftWidth + rect.y) / atlasHeight;
data[2].u = (bottomHeight + centerHeight + rect.x) / atlasWidth;
data[2].v = (leftWidth + centerWidth + rect.y) / atlasHeight;
data[3].u = (rect.x + rect.height) / atlasWidth;
data[3].v = (rect.y + rect.width) / atlasHeight;
}
else {
data[0].u = (rect.x) / atlasWidth;
data[1].u = (leftWidth + rect.x) / atlasWidth;
data[2].u = (leftWidth + centerWidth + rect.x) / atlasWidth;
data[3].u = (rect.x + rect.width) / atlasWidth;
data[3].v = (rect.y) / atlasHeight;
data[2].v = (topHeight + rect.y) / atlasHeight;
data[1].v = (topHeight + centerHeight + rect.y) / atlasHeight;
data[0].v = (rect.y + rect.height) / atlasHeight;
}
renderData.uvDirty = false;
}
},
updateVerts (sprite) {
let renderData = sprite._renderData,
data = renderData._data,
node = sprite.node,
width = node.width, height = node.height,
appx = node.anchorX * width, appy = node.anchorY * height;
let frame = sprite.spriteFrame;
let rect = frame._rect;
let leftWidth = frame.insetLeft;
let rightWidth = frame.insetRight;
let topHeight = frame.insetTop;
let bottomHeight = frame.insetBottom;
let sizableWidth = width - leftWidth - rightWidth;
let sizableHeight = height - topHeight - bottomHeight;
let xScale = width / (leftWidth + rightWidth);
let yScale = height / (topHeight + bottomHeight);
xScale = (isNaN(xScale) || xScale > 1) ? 1 : xScale;
yScale = (isNaN(yScale) || yScale > 1) ? 1 : yScale;
sizableWidth = sizableWidth < 0 ? 0 : sizableWidth;
sizableHeight = sizableHeight < 0 ? 0 : sizableHeight;
data[0].x = -appx;
data[0].y = -appy;
data[1].x = leftWidth * xScale - appx;
data[1].y = bottomHeight * yScale - appy;
data[2].x = data[1].x + sizableWidth;
data[2].y = data[1].y + sizableHeight;
data[3].x = width - appx;
data[3].y = height - appy;
renderData.vertDirty = false;
},
fillBuffers (sprite, batchData, vertexId, vbuf, uintbuf, ibuf) {
let vertexOffset = batchData.byteOffset / 4,
indiceOffset = batchData.indiceOffset;
let node = sprite.node;
let z = node._position.z;
let color = node._color._val;
let data = sprite._renderData._data;
for (let i = 4; i < 20; ++i) {
let vert = data[i];
vbuf[vertexOffset] = vert.x;
vbuf[vertexOffset + 1] = vert.y;
vbuf[vertexOffset + 2] = z;
vbuf[vertexOffset + 4] = vert.u;
vbuf[vertexOffset + 5] = vert.v;
uintbuf[vertexOffset + 3] = color;
vertexOffset += 6;
}
for (let r = 0; r < 3; ++r) {
for (let c = 0; c < 3; ++c) {
let start = vertexId + r*4 + c;
ibuf[indiceOffset++] = start;
ibuf[indiceOffset++] = start + 1;
ibuf[indiceOffset++] = start + 4;
ibuf[indiceOffset++] = start + 1;
ibuf[indiceOffset++] = start + 5;
ibuf[indiceOffset++] = start + 4;
}
}
},
updateWorldVerts (sprite) {
let node = sprite.node,
renderData = sprite._renderData,
data = renderData._data,
matrix = node._worldMatrix;
let a = matrix.m00, b = matrix.m01, c = matrix.m04, d = matrix.m05,
tx = matrix.m12, ty = matrix.m13;
for (let row = 0; row < 4; ++row) {
let rowD = data[row];
for (let col = 0; col < 4; ++col) {
let colD = data[col];
let world = data[4 + row*4 + col];
world.x = colD.x*a + rowD.y*c + tx;
world.y = colD.x*b + rowD.y*d + ty;
world.u = colD.u;
world.v = rowD.v;
}
}
renderData.worldMatDirty = false;
},
update: simpleRenderUtil.update
};