Merge pull request #2487 from pandamicro/next

Support Tiled Sprite
This commit is contained in:
pandamicro
2018-03-30 17:33:04 +08:00
committed by GitHub
5 changed files with 205 additions and 83 deletions
@@ -26,6 +26,7 @@
let SpriteType = require('../../../../components/CCSprite').Type;
let simple = require('./simple');
let sliced = require('./sliced');
let tiled = require('./tiled');
module.exports = {
getAssembler: function (sprite) {
@@ -35,7 +36,7 @@ module.exports = {
case SpriteType.SLICED:
return sliced;
case SpriteType.TILED:
return null;
return tiled;
case SpriteType.FILLED:
if (sprite._fillType === FillType.RADIAL) {
return null;
@@ -50,21 +51,6 @@ module.exports = {
return sprite._assembler.createData(sprite);
},
update (sprite) {
let renderData = sprite._renderData;
if (!renderData) {
return;
}
if (renderData.uvDirty) {
sprite._assembler.updateUVs(sprite);
}
if (renderData.vertDirty) {
sprite._assembler.updateVerts(sprite);
}
},
draw (ctx, sprite) {
let node = sprite.node;
// Check whether need to render
@@ -72,7 +58,7 @@ module.exports = {
return 0;
}
// update
this.update(sprite);
sprite._assembler.update(sprite);
return sprite._assembler.draw(ctx, sprite);
}
@@ -34,6 +34,21 @@ let renderer = {
return renderData;
},
update (sprite) {
let renderData = sprite._renderData;
if (!renderData) {
return;
}
if (renderData.uvDirty) {
this.updateUVs(sprite);
}
if (renderData.vertDirty) {
this.updateVerts(sprite);
}
},
updateUVs (sprite) {
let frame = sprite.spriteFrame;
let renderData = sprite._renderData;
@@ -43,9 +58,9 @@ let renderer = {
if (frame._rotated) {
let l = rect.x;
let r = rect.x + rect.height;
let r = rect.height;
let b = rect.y;
let t = rect.y + rect.width;
let t = rect.width;
data[0].u = l;
data[0].v = t;
data[1].u = r;
@@ -53,9 +68,9 @@ let renderer = {
}
else {
let l = rect.x;
let r = rect.x + rect.width;
let r = rect.width;
let b = rect.y;
let t = rect.y + rect.height;
let t = rect.height;
data[0].u = l;
data[0].v = b;
data[1].u = r;
@@ -75,8 +90,8 @@ let renderer = {
if (sprite.trim) {
l = -appx;
b = -appy;
r = cw - appx;
t = ch - appy;
r = cw;
t = ch;
}
else {
let frame = sprite.spriteFrame,
@@ -90,8 +105,8 @@ let renderer = {
let trimTop = offset.y - (oh - rh) / 2;
l = trimLeft * scaleX - appx;
b = trimBottom * scaleY - appy;
r = cw + trimRight * scaleX - appx;
t = ch + trimTop * scaleY - appy;
r = cw;
t = ch;
}
data[0].x = l;
@@ -122,14 +137,14 @@ let renderer = {
let x = data[0].x;
let y = data[0].y;
let w = data[1].x - x;
let h = data[1].y - y;
let w = data[1].x;
let h = data[1].y;
y = - y - h;
let sx = data[0].u;
let sy = data[0].v;
let sw = data[1].u - sx;
let sh = data[1].v - sy;
let sw = data[1].u;
let sh = data[1].v;
ctx.drawImage(image,
sx, sy, sw, sh,
@@ -24,6 +24,7 @@
****************************************************************************/
const utils = require('../utils');
const simple = require('./simple');
let renderer = {
createData (sprite) {
@@ -32,6 +33,8 @@ let renderer = {
renderData.dataLength = 4;
return renderData;
},
update: simple.update,
updateUVs (sprite) {
let frame = sprite.spriteFrame;
@@ -0,0 +1,73 @@
/****************************************************************************
Copyright (c) 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 utils = require('../utils');
const simple = require('./simple');
let renderer = {
createData (sprite) {
let renderData = sprite.requestRenderData();
return renderData;
},
update (sprite) {},
draw (ctx, sprite) {
let node = sprite.node;
// Transform
let matrix = node._worldMatrix;
let a = matrix.m00, b = matrix.m01, c = matrix.m04, d = matrix.m05,
tx = matrix.m12, ty = matrix.m13;
ctx.transform(a, b, c, d, tx, -ty);
// TODO: handle blend function
// opacity
ctx.glphaAlpha = node.opacity / 255;
let frame = sprite.spriteFrame;
let rect = frame._rect;
let tex = frame._texture;
let sx = rect.x;
let sy = rect.y;
let sw = frame._rotated ? rect.height : rect.width;
let sh = frame._rotated ? rect.width : rect.height;
let image = utils.getFrameCache(tex, node.color, sx, sy, sw, sh);
let w = node.width,
h = node.height,
x = -node.anchorX * w,
y = -node.anchorY * h;
y = - y - h;
ctx.translate(x, y);
ctx.fillStyle = ctx.createPattern(image, 'repeat');
ctx.fillRect(0, 0, w, h);
return 1;
}
}
module.exports = renderer
+99 -54
View File
@@ -24,28 +24,59 @@
****************************************************************************/
const WHITE = (255<<16) + (255<<8) + 255;
const MAX_CANVAS_COUNT = 32;
let textureColorizer = {
colorCanvas: {},
colorUsed: {},
function colorizedFrame (canvas, texture, color, sx, sy, sw, sh) {
let image = texture._image;
let ctx = canvas.getContext("2d");
canvas.width = sw;
canvas.height = sh;
// Draw color
ctx.globalCompositeOperation = 'source-over';
ctx.fillStyle = 'rgba(' + color.r + ',' + color.g + ',' + color.b + ')';
ctx.fillRect(0, 0, sw, sh);
// Multiply color with texture
ctx.globalCompositeOperation = 'multiply';
ctx.drawImage(image, sx, sy, sw, sh, 0, 0, sw, sh);
// Clip out transparent pixels
ctx.globalCompositeOperation = "destination-atop";
ctx.drawImage(image, sx, sy, sw, sh, 0, 0, sw, sh);
return canvas;
}
let canvasMgr = {
canvasMap: {},
canvasUsed: {},
canvasPool: [],
checking: false,
check () {
for (let key in this.colorUsed) {
if (!this.colorUsed[key]) {
let canvas = this.colorCanvas[key];
let exist = false;
for (let key in this.canvasUsed) {
exist = true;
if (!this.canvasUsed[key]) {
let canvas = this.canvasMap[key];
canvas.width = 0;
canvas.height = 0;
this.canvasPool.push(canvas);
delete this.colorCanvas[key];
delete this.colorUsed[key];
if (this.canvasPool.length < 32) {
this.canvasPool.push(canvas);
}
delete this.canvasMap[key];
delete this.canvasUsed[key];
}
else {
this.colorUsed[key] = false;
this.canvasUsed[key] = false;
}
}
if (!exist) {
cc.director.off(cc.Director.EVENT_AFTER_DRAW, this.check, this);
this.checking = false;
}
},
startCheck () {
@@ -53,11 +84,33 @@ let textureColorizer = {
this.checking = true;
},
getImage (texture, color) {
getCanvas (key) {
this.canvasUsed[key] = true;
return this.canvasMap[key];
},
cacheCanvas (canvas, key) {
this.canvasMap[key] = canvas;
this.canvasUsed[key] = true;
if (!this.checking) {
this.startCheck();
}
},
// dropImage (texture, color) {
// let key = texture.url + (color._val & 0x00ffffff);
// if (this.canvasMap[key]) {
// delete this.canvasMap[key];
// }
// }
};
module.exports = {
getColorizedImage (texture, color) {
if (!texture || !texture.url) {
return null;
}
// original image
let cval = color._val & 0x00ffffff;
if (cval === WHITE) {
@@ -66,50 +119,42 @@ let textureColorizer = {
// get from cache
let key = texture.url + cval;
if (this.colorCanvas[key]) {
this.colorUsed[key] = true;
return this.colorCanvas[key];
let cache = canvasMgr.getCanvas(key);
if (!cache) {
cache = canvasMgr.canvasPool.pop() || document.createElement("canvas");
colorizedFrame(cache, texture, color, 0, 0, texture.width, texture.height);
canvasMgr.cacheCanvas(cache, key);
}
let x = 0, y = 0, w = texture.width, h = texture.height;
let image = texture._image;
let canvas = this.canvasPool.pop() || document.createElement("canvas");
let ctx = canvas.getContext("2d");
canvas.width = w;
canvas.height = h;
// Draw color
ctx.globalCompositeOperation = 'source-over';
ctx.fillStyle = 'rgba(' + color.r + ',' + color.g + ',' + color.b + ')';
ctx.fillRect(x, y, w, h);
// Multiply color with texture
ctx.globalCompositeOperation = 'multiply';
ctx.drawImage(image, x, y, w, h);
// Clip out transparent pixels
ctx.globalCompositeOperation = "destination-atop";
ctx.drawImage(image, x, y, w, h);
this.colorCanvas[key] = canvas;
this.colorUsed[key] = true;
if (!this.checking) {
this.startCheck();
}
return canvas;
return cache;
},
dropImage (texture, color) {
let key = texture.url + (color._val & 0x00ffffff);
if (this.colorCanvas[key]) {
delete this.colorCanvas[key];
}
}
};
module.exports = {
getColorizedImage (texture, color) {
return textureColorizer.getImage(texture, color);
getFrameCache (texture, color, sx, sy, sw, sh) {
if (!texture || !texture.url || sx < 0 || sy < 0 || sw <= 0 || sh <= 0) {
return null;
}
let key = texture.url;
let generate = false;
let cval = color._val & 0x00ffffff;
if (cval !== WHITE) {
key += cval;
generate = true;
}
if (sx !== 0 || sy !== 0 && sw !== texture.width && sh !== texture.height) {
key += '_' + sx + '_' + sy + '_' + sw + '_' + sh;
generate = true;
}
if (!generate) {
return texture._image;
}
// get from cache
let cache = canvasMgr.getCanvas(key);
if (!cache) {
cache = canvasMgr.canvasPool.pop() || document.createElement("canvas");
colorizedFrame(cache, texture, color, sx, sy, sw, sh);
canvasMgr.cacheCanvas(cache, key);
}
return cache;
}
};