support tiled sprite mode

This commit is contained in:
yinkaile
2017-12-05 10:12:33 +08:00
parent 48662f125c
commit eed10189f4
@@ -29,9 +29,9 @@ const SpriteType = Sprite.Type;
const RenderData = renderEngine.RenderData;
const math = renderEngine.math;
var _matrix = math.mat4.create();
let _matrix = math.mat4.create();
var simpleRenderData = {
let simpleRenderData = {
createData (sprite) {
let renderData = RenderData.alloc();
renderData.xysLength = 4;
@@ -44,52 +44,66 @@ var simpleRenderData = {
updateUVs (sprite) {
let effect = sprite.getEffect();
let data = sprite._renderData;
if (effect && data) {
let texture = effect.getValue('texture');
let texw = texture._width,
texh = texture._height;
let frame = sprite.spriteFrame;
let rect = frame._rect;
let l, b, r, t;
if (sprite.trim) {
l = rect.x;
t = rect.y;
r = rect.x + rect.width;
b = rect.y + rect.height;
} else {
let originalSize = frame._originalSize;
let offset = frame._offset;
let ow = originalSize.width,
oh = originalSize.height,
rw = rect.width,
rh = rect.height;
let ox = rect.x + (rw - ow) / 2 - offset.x;
let oy = rect.y + (rh - oh) / 2 - offset.y;
l = ox;
t = oy;
r = ox + ow;
b = oy + oh;
}
l = texw === 0 ? 0 : l / texw;
r = texw === 0 ? 0 : r / texw;
b = texh === 0 ? 0 : b / texh;
t = texh === 0 ? 0 : t / texh;
let uvs = data._uvs;
uvs.u[0] = l;
uvs.u[1] = r;
uvs.u[2] = l;
uvs.u[3] = r;
uvs.v[0] = b;
uvs.v[1] = b;
uvs.v[2] = t;
uvs.v[3] = t;
data.uvDirty = false;
if (!effect || !data) return;
let texture = effect.getValue('texture');
let texw = texture._width,
texh = texture._height;
let frame = sprite.spriteFrame;
let rect = frame._rect;
let l, b, r, t;
if (sprite.trim) {
l = rect.x;
t = rect.y;
r = rect.x + rect.width;
b = rect.y + rect.height;
} else {
let originalSize = frame._originalSize;
let offset = frame._offset;
let ow = originalSize.width,
oh = originalSize.height,
rw = rect.width,
rh = rect.height;
let ox = rect.x + (rw - ow) / 2 - offset.x;
let oy = rect.y + (rh - oh) / 2 - offset.y;
l = ox;
t = oy;
r = ox + ow;
b = oy + oh;
}
l = texw === 0 ? 0 : l / texw;
r = texw === 0 ? 0 : r / texw;
b = texh === 0 ? 0 : b / texh;
t = texh === 0 ? 0 : t / texh;
let u = data._uvs.u;
let v = data._uvs.v;
if (frame._rotated) {
u[0] = l;
u[1] = l;
u[2] = r;
u[3] = r;
v[0] = t;
v[1] = b;
v[2] = t;
v[3] = b;
}
else {
u[0] = l;
u[1] = r;
u[2] = l;
u[3] = r;
v[0] = b;
v[1] = b;
v[2] = t;
v[3] = t;
}
data.uvDirty = false;
},
updateVerts (sprite) {
@@ -108,22 +122,21 @@ var simpleRenderData = {
let appx = data._pivotX * width,
appy = data._pivotY * height;
var verts = data._verts;
verts.x[0] = -appx;
verts.x[1] = width - appx;
verts.x[2] = -appx;
verts.x[3] = width - appx;
verts.y[0] = -appy;
verts.y[1] = -appy;
verts.y[2] = height - appy;
verts.y[3] = height - appy;
let x = data._verts.x;
let y = data._verts.y;
x[0] = -appx;
x[1] = width - appx;
x[2] = -appx;
x[3] = width - appx;
y[0] = -appy;
y[1] = -appy;
y[2] = height - appy;
y[3] = height - appy;
data.vertDirty = false;
},
fillVertexBuffer (sprite, index, vbuf, uintbuf) {
let texture = sprite.getEffect().getValue('texture');
let off = index * sprite._vertexFormat._bytes / 4;
let node = sprite.node;
let data = sprite._renderData;
@@ -163,7 +176,7 @@ var simpleRenderData = {
}
};
var slicedRenderData = {
let slicedRenderData = {
createData (sprite) {
let renderData = RenderData.alloc();
renderData.xysLength = 4;
@@ -176,48 +189,48 @@ var slicedRenderData = {
updateUVs (sprite) {
let effect = sprite.getEffect();
let data = sprite._renderData;
if (effect) {
let texture = effect.getValue('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 u = data._uvs.u;
let v = data._uvs.v;
if (frame._rotated) {
u[0] = (rect.x) / atlasWidth;
u[1] = (bottomHeight + rect.x) / atlasWidth;
u[2] = (bottomHeight + centerHeight + rect.x) / atlasWidth;
u[3] = (rect.x + rect.height) / atlasWidth;
v[0] = (rect.y) / atlasHeight;
v[1] = (leftWidth + rect.y) / atlasHeight;
v[2] = (leftWidth + centerWidth + rect.y) / atlasHeight;
v[3] = (rect.y + rect.width) / atlasHeight;
}
else {
u[0] = (rect.x) / atlasWidth;
u[1] = (leftWidth + rect.x) / atlasWidth;
u[2] = (leftWidth + centerWidth + rect.x) / atlasWidth;
u[3] = (rect.x + rect.width) / atlasWidth;
v[3] = (rect.y) / atlasHeight;
v[2] = (topHeight + rect.y) / atlasHeight;
v[1] = (topHeight + centerHeight + rect.y) / atlasHeight;
v[0] = (rect.y + rect.height) / atlasHeight;
}
data.uvDirty = false;
if (!effect || !data) return;
let texture = effect.getValue('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 u = data._uvs.u;
let v = data._uvs.v;
if (frame._rotated) {
u[0] = (rect.x) / atlasWidth;
u[1] = (bottomHeight + rect.x) / atlasWidth;
u[2] = (bottomHeight + centerHeight + rect.x) / atlasWidth;
u[3] = (rect.x + rect.height) / atlasWidth;
v[0] = (rect.y) / atlasHeight;
v[1] = (leftWidth + rect.y) / atlasHeight;
v[2] = (leftWidth + centerWidth + rect.y) / atlasHeight;
v[3] = (rect.y + rect.width) / atlasHeight;
}
else {
u[0] = (rect.x) / atlasWidth;
u[1] = (leftWidth + rect.x) / atlasWidth;
u[2] = (leftWidth + centerWidth + rect.x) / atlasWidth;
u[3] = (rect.x + rect.width) / atlasWidth;
v[3] = (rect.y) / atlasHeight;
v[2] = (topHeight + rect.y) / atlasHeight;
v[1] = (topHeight + centerHeight + rect.y) / atlasHeight;
v[0] = (rect.y + rect.height) / atlasHeight;
}
data.uvDirty = false;
},
updateVerts (sprite) {
@@ -294,7 +307,7 @@ var slicedRenderData = {
fillIndexBuffer (sprite, offset, vertexId, ibuf) {
for (let r = 0; r < 3; ++r) {
for (let c = 0; c < 3; ++c) {
var start = vertexId + r*4 + c;
let start = vertexId + r*4 + c;
ibuf[offset++] = start;
ibuf[offset++] = start + 1;
ibuf[offset++] = start + 4;
@@ -304,13 +317,200 @@ var slicedRenderData = {
}
}
}
}
};
var dataUpdater = {};
let tiledRenderData = {
createData (sprite) {
return RenderData.alloc();
},
updateUVs (sprite) {
let effect = sprite.getEffect();
let data = sprite._renderData;
if (!effect || !data) return;
let texture = effect.getValue('texture');
let texw = texture._width,
texh = texture._height;
let frame = sprite.spriteFrame;
let rect = frame._rect;
let contentSize = sprite.node._contentSize;
let rectWidth = rect.width;
let rectHeight = rect.height;
let hRepeat = contentSize.width / rectWidth;
let vRepeat = contentSize.height / rectHeight;
let row = Math.ceil(vRepeat),
col = Math.ceil(hRepeat);
let u = data._uvs.u;
let v = data._uvs.v;
u.length = v.length = 8;
let l, b, r, t;
if (!frame._rotated) {
l = (rect.x) / texw;
r = (rect.x + rectWidth) / texw;
b = (rect.y + rectHeight) / texh;
t = (rect.y) / texh;
u[0] = l;
v[0] = b;
u[1] = r;
v[1] = b;
u[2] = l;
v[2] = t;
u[3] = r;
v[3] = t;
u[4] = l;
v[4] = b;
u[5] = l + (r-l) * Math.min(1, hRepeat - col + 1);
v[5] = b;
u[6] = l;
v[6] = b + (t-b) * Math.min(1, vRepeat - row + 1);
u[7] = u[5];
v[7] = v[6];
} else {
l = (rect.x) / texw;
r = (rect.x + rectHeight) / texw;
b = (rect.y + rectWidth) / texh;
t = (rect.y) / texh;
u[0] = l;
v[0] = t;
u[1] = l;
v[1] = b;
u[2] = r;
v[2] = t;
u[3] = r;
v[3] = b;
u[4] = l;
v[4] = t;
u[5] = l;
v[5] = t + (b - t) * Math.min(1, vRepeat - cow + 1);
u[6] = l + (r - l) * Math.min(1, hRepeat - row + 1);
v[6] = t;
u[7] = u[5];
v[7] = v[6];
}
data._verts.x.length = col + 1;
data._verts.y.length = row + 1;
data.vertexCount = row * col * 4;
data.indiceCount = row * col * 6;
data.uvDirty = false;
},
updateVerts (sprite) {
let data = sprite._renderData,
frame = sprite.spriteFrame,
rect = frame._rect,
rectWidth = rect.width,
rectHeight = rect.height,
width = data._width,
height = data._height,
appx = data._pivotX * width,
appy = data._pivotY * height;
let l, b, r, t;
let x = data._verts.x;
let y = data._verts.y;
for (let i = 0; i < x.length; ++i) {
x[i] = Math.min(rectWidth * i, width) - appx;
}
for (let i = 0; i < y.length; ++i) {
y[i] = Math.min(rectHeight * i, height) - appy;
}
data.vertDirty = false;
},
fillVertexBuffer (sprite, index, vbuf, uintbuf) {
let off = index * sprite._vertexFormat._bytes / 4;
let node = sprite.node;
let data = sprite._renderData;
let x = data._verts.x,
y = data._verts.y,
u = data._uvs.u,
v = data._uvs.v,
z = node.z;
let color = node._color._val;
node.getWorldMatrix(_matrix);
let a = _matrix.m00,
b = _matrix.m01,
c = _matrix.m04,
d = _matrix.m05,
tx = _matrix.m12,
ty = _matrix.m13;
for (let yindex = 0, ylength = y.length - 1; yindex < ylength; ++yindex) {
for (let xindex = 0, xlength = x.length - 1; xindex < xlength; ++xindex) {
let lasty = yindex + 1 === ylength;
let lastx = xindex + 1 === xlength;
// lb
vbuf[off++] = x[xindex] * a + y[yindex] * c + tx;
vbuf[off++] = x[xindex] * b + y[yindex] * d + ty;
vbuf[off++] = z;
uintbuf[off++] = color;
vbuf[off++] = lastx ? u[4] : u[0];
vbuf[off++] = lasty ? v[4] : v[0];
// rb
vbuf[off++] = x[xindex+1] * a + y[yindex] * c + tx;
vbuf[off++] = x[xindex+1] * b + y[yindex] * d + ty;
vbuf[off++] = z;
uintbuf[off++] = color;
vbuf[off++] = lastx ? u[5] : u[1];
vbuf[off++] = lasty ? v[5] : v[1];
// lt
vbuf[off++] = x[xindex] * a + y[yindex+1] * c + tx;
vbuf[off++] = x[xindex] * b + y[yindex+1] * d + ty;
vbuf[off++] = z;
uintbuf[off++] = color;
vbuf[off++] = lastx ? u[6] : u[2];
vbuf[off++] = lasty ? v[6] : v[2];
// rt
vbuf[off++] = x[xindex+1] * a + y[yindex+1] * c + tx;
vbuf[off++] = x[xindex+1] * b + y[yindex+1] * d + ty;
vbuf[off++] = z;
uintbuf[off++] = color;
vbuf[off++] = lastx ? u[7] : u[3];
vbuf[off++] = lasty ? v[7] : v[3];
}
}
},
fillIndexBuffer (sprite, offset, vertexId, ibuf) {
let data = sprite._renderData;
let length = data.indiceCount;
for (let i = 0; i < length; i+=6) {
ibuf[offset++] = vertexId;
ibuf[offset++] = vertexId+1;
ibuf[offset++] = vertexId+2;
ibuf[offset++] = vertexId+1;
ibuf[offset++] = vertexId+3;
ibuf[offset++] = vertexId+2;
vertexId += 4;
}
}
};
let dataUpdater = {};
dataUpdater[SpriteType.SIMPLE] = simpleRenderData;
dataUpdater[SpriteType.SLICED] = slicedRenderData;
dataUpdater[SpriteType.TILED] = tiledRenderData;
var spriteAssembler = {
let spriteAssembler = {
updateRenderData (sprite) {
let updater = dataUpdater[sprite.type];
// Create render data if needed