mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 15:52:40 +08:00
add _calculateSlicedUV to SpriteFrame for fireball/issues/7354 (#2737)
* add _calculateSlicedUV to SpriteFrame for fireball/issues/7354 * refine code
This commit is contained in:
@@ -25,8 +25,10 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
var EventTarget = require("../event/event-target");
|
||||
var textureUtil = require('../utils/texture-util');
|
||||
const EventTarget = require("../event/event-target");
|
||||
const textureUtil = require('../utils/texture-util');
|
||||
|
||||
let temp_uvs = [{u: 0, v: 0}, {u: 0, v: 0}, {u: 0, v: 0}, {u: 0, v: 0}];
|
||||
|
||||
/**
|
||||
* !#en
|
||||
@@ -53,7 +55,7 @@ var textureUtil = require('../utils/texture-util');
|
||||
* node.parent = self.node
|
||||
* });
|
||||
*/
|
||||
var SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
|
||||
let SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
|
||||
name: 'cc.SpriteFrame',
|
||||
extends: require('../assets/CCAsset'),
|
||||
mixins: [EventTarget],
|
||||
@@ -81,7 +83,7 @@ var SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
|
||||
// return (this._texture && this._texture.url) || "";
|
||||
// },
|
||||
// set (url) {
|
||||
// var texture = cc.textureCache.addImage(url);
|
||||
// let texture = cc.textureCache.addImage(url);
|
||||
// this._refreshTexture(texture);
|
||||
// }
|
||||
// }
|
||||
@@ -100,11 +102,11 @@ var SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
|
||||
* @param {Size} [originalSize] - The size of the frame in the texture
|
||||
*/
|
||||
ctor: function () {
|
||||
var filename = arguments[0];
|
||||
var rect = arguments[1];
|
||||
var rotated = arguments[2];
|
||||
var offset = arguments[3];
|
||||
var originalSize = arguments[4];
|
||||
let filename = arguments[0];
|
||||
let rect = arguments[1];
|
||||
let rotated = arguments[2];
|
||||
let offset = arguments[3];
|
||||
let originalSize = arguments[4];
|
||||
|
||||
// the location of the sprite on rendering texture
|
||||
this._rect = null;
|
||||
@@ -156,6 +158,7 @@ var SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
|
||||
this.vertices = null;
|
||||
|
||||
this.uv = [];
|
||||
this.uvSliced = [];
|
||||
|
||||
this._texture = null;
|
||||
this._textureFilename = '';
|
||||
@@ -261,13 +264,13 @@ var SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
|
||||
},
|
||||
|
||||
_textureLoadedCallback () {
|
||||
var self = this;
|
||||
var texture = this._texture;
|
||||
let self = this;
|
||||
let texture = this._texture;
|
||||
if (!texture) {
|
||||
// clearTexture called while loading texture...
|
||||
return;
|
||||
}
|
||||
var w = texture.width, h = texture.height;
|
||||
let w = texture.width, h = texture.height;
|
||||
|
||||
if (self._rotated && cc.game.renderType === cc.game.RENDER_TYPE_CANVAS) {
|
||||
// TODO: rotate texture for canvas
|
||||
@@ -381,7 +384,7 @@ var SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
|
||||
this._rotated = rotated || false;
|
||||
|
||||
// loading texture
|
||||
var texture = textureOrTextureFile;
|
||||
let texture = textureOrTextureFile;
|
||||
if (typeof texture === 'string' && texture) {
|
||||
this._textureFilename = texture;
|
||||
this._loadTexture();
|
||||
@@ -395,7 +398,7 @@ var SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
|
||||
|
||||
_loadTexture: function () {
|
||||
if (this._textureFilename) {
|
||||
var texture = textureUtil.loadImage(this._textureFilename);
|
||||
let texture = textureUtil.loadImage(this._textureFilename);
|
||||
this._refreshTexture(texture);
|
||||
}
|
||||
},
|
||||
@@ -450,8 +453,8 @@ var SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
|
||||
},
|
||||
|
||||
_checkRect: function (texture) {
|
||||
var rect = this._rect;
|
||||
var maxX = rect.x, maxY = rect.y;
|
||||
let rect = this._rect;
|
||||
let maxX = rect.x, maxY = rect.y;
|
||||
if (this._rotated) {
|
||||
maxX += rect.height;
|
||||
maxY += rect.width;
|
||||
@@ -468,6 +471,63 @@ var SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
|
||||
}
|
||||
},
|
||||
|
||||
_calculateSlicedUV () {
|
||||
let rect = this._rect;
|
||||
let atlasWidth = this._texture.width;
|
||||
let atlasHeight = this._texture.height;
|
||||
let leftWidth = this.insetLeft;
|
||||
let rightWidth = this.insetRight;
|
||||
let centerWidth = rect.width - leftWidth - rightWidth;
|
||||
let topHeight = this.insetTop;
|
||||
let bottomHeight = this.insetBottom;
|
||||
let centerHeight = rect.height - topHeight - bottomHeight;
|
||||
|
||||
let uvSliced = this.uvSliced;
|
||||
uvSliced.length = 0;
|
||||
if (this._rotated) {
|
||||
temp_uvs[0].u = (rect.x) / atlasWidth;
|
||||
temp_uvs[1].u = (rect.x + bottomHeight) / atlasWidth;
|
||||
temp_uvs[2].u = (rect.x + bottomHeight + centerHeight) / atlasWidth;
|
||||
temp_uvs[3].u = (rect.x + rect.height) / atlasWidth;
|
||||
temp_uvs[3].v = (rect.y) / atlasHeight;
|
||||
temp_uvs[2].v = (rect.y + leftWidth) / atlasHeight;
|
||||
temp_uvs[1].v = (rect.y + leftWidth + centerWidth) / atlasHeight;
|
||||
temp_uvs[0].v = (rect.y + rect.width) / atlasHeight;
|
||||
|
||||
for (let row = 0; row < 4; ++row) {
|
||||
let rowD = temp_uvs[row];
|
||||
for (let col = 0; col < 4; ++col) {
|
||||
let colD = temp_uvs[3 - col];
|
||||
uvSliced.push({
|
||||
u: rowD.u,
|
||||
v: colD.v
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
temp_uvs[0].u = (rect.x) / atlasWidth;
|
||||
temp_uvs[1].u = (rect.x + leftWidth) / atlasWidth;
|
||||
temp_uvs[2].u = (rect.x + leftWidth + centerWidth) / atlasWidth;
|
||||
temp_uvs[3].u = (rect.x + rect.width) / atlasWidth;
|
||||
temp_uvs[3].v = (rect.y) / atlasHeight;
|
||||
temp_uvs[2].v = (rect.y + topHeight) / atlasHeight;
|
||||
temp_uvs[1].v = (rect.y + topHeight + centerHeight + rect.y) / atlasHeight;
|
||||
temp_uvs[0].v = (rect.y + rect.height) / atlasHeight;
|
||||
|
||||
for (let row = 0; row < 4; ++row) {
|
||||
let rowD = temp_uvs[row];
|
||||
for (let col = 0; col < 4; ++col) {
|
||||
let colD = temp_uvs[col];
|
||||
uvSliced.push({
|
||||
u: colD.u,
|
||||
v: rowD.v
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
_calculateUV () {
|
||||
let rect = this._rect,
|
||||
texture = this._texture,
|
||||
@@ -513,21 +573,23 @@ var SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
|
||||
vertices.nv[i] = vertices.v[i]/texh;
|
||||
}
|
||||
}
|
||||
|
||||
this._calculateSlicedUV();
|
||||
},
|
||||
|
||||
// SERIALIZATION
|
||||
|
||||
_serialize: CC_EDITOR && function (exporting) {
|
||||
var rect = this._rect;
|
||||
var offset = this._offset;
|
||||
var size = this._originalSize;
|
||||
var uuid;
|
||||
var texture = this._texture;
|
||||
let rect = this._rect;
|
||||
let offset = this._offset;
|
||||
let size = this._originalSize;
|
||||
let uuid;
|
||||
let texture = this._texture;
|
||||
if (texture) {
|
||||
uuid = texture._uuid;
|
||||
}
|
||||
if (!uuid) {
|
||||
var url = this._textureFilename;
|
||||
let url = this._textureFilename;
|
||||
if (url) {
|
||||
uuid = Editor.Utils.UuidCache.urlToUuid(url);
|
||||
}
|
||||
@@ -535,7 +597,7 @@ var SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
|
||||
if (uuid && exporting) {
|
||||
uuid = Editor.Utils.UuidUtils.compressUuid(uuid, true);
|
||||
}
|
||||
var capInsets;
|
||||
let capInsets;
|
||||
if (this.insetLeft !== 0 ||
|
||||
this.insetTop !== 0 ||
|
||||
this.insetRight !== 0 ||
|
||||
@@ -568,7 +630,7 @@ var SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
|
||||
},
|
||||
|
||||
_deserialize: function (data, handle) {
|
||||
var rect = data.rect;
|
||||
let rect = data.rect;
|
||||
if (rect) {
|
||||
this.setRect(new cc.Rect(rect[0], rect[1], rect[2], rect[3]));
|
||||
}
|
||||
@@ -580,7 +642,7 @@ var SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
|
||||
}
|
||||
this._rotated = data.rotated === 1;
|
||||
this._name = data.name;
|
||||
var capInsets = data.capInsets;
|
||||
let capInsets = data.capInsets;
|
||||
if (capInsets) {
|
||||
this.insetLeft = capInsets[0];
|
||||
this.insetTop = capInsets[1];
|
||||
@@ -599,14 +661,14 @@ var SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
|
||||
}
|
||||
|
||||
// load texture via _textureSetter
|
||||
var textureUuid = data.texture;
|
||||
let textureUuid = data.texture;
|
||||
if (textureUuid) {
|
||||
handle.result.push(this, '_textureSetter', textureUuid);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
var proto = SpriteFrame.prototype;
|
||||
let proto = SpriteFrame.prototype;
|
||||
|
||||
proto.copyWithZone = proto.clone;
|
||||
proto.copy = proto.clone;
|
||||
|
||||
@@ -333,7 +333,7 @@ module.exports = {
|
||||
data = renderData._data,
|
||||
node = sprite.node;
|
||||
|
||||
let matrix = node._worldMatrix;
|
||||
let matrix = node._worldMatrix,
|
||||
a = matrix.m00, b = matrix.m01, c = matrix.m04, d = matrix.m05,
|
||||
tx = matrix.m12, ty = matrix.m13;
|
||||
|
||||
|
||||
@@ -57,10 +57,6 @@ module.exports = {
|
||||
|
||||
let renderData = sprite._renderData;
|
||||
if (renderData && frame) {
|
||||
if (renderData.uvDirty) {
|
||||
this.updateUVs(sprite);
|
||||
}
|
||||
|
||||
let vertDirty = renderData.vertDirty;
|
||||
if (vertDirty) {
|
||||
this.updateVerts(sprite);
|
||||
@@ -69,69 +65,6 @@ module.exports = {
|
||||
}
|
||||
},
|
||||
|
||||
updateUVs (sprite) {
|
||||
let material = sprite.getMaterial();
|
||||
let renderData = sprite._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 + rect.width) / atlasHeight;
|
||||
data[1].u = (rect.x + bottomHeight) / atlasWidth;
|
||||
data[1].v = (rect.y + leftWidth + centerWidth) / atlasHeight;
|
||||
data[2].u = (rect.x + bottomHeight + centerHeight) / atlasWidth;
|
||||
data[2].v = (rect.y + leftWidth) / atlasHeight;
|
||||
data[3].u = (rect.x + rect.height) / atlasWidth;
|
||||
data[3].v = (rect.y) / atlasHeight;
|
||||
|
||||
for (let row = 0; row < 4; ++row) {
|
||||
let rowD = data[row];
|
||||
for (let col = 0; col < 4; ++col) {
|
||||
let colD = data[3 - col];
|
||||
let world = data[4 + row*4 + col];
|
||||
world.u = rowD.u;
|
||||
world.v = colD.v;
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
|
||||
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.u = colD.u;
|
||||
world.v = rowD.v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
renderData.uvDirty = false;
|
||||
},
|
||||
|
||||
updateVerts (sprite) {
|
||||
let renderData = sprite._renderData,
|
||||
data = renderData._data,
|
||||
@@ -184,16 +117,19 @@ module.exports = {
|
||||
let ibuf = buffer._iData,
|
||||
indiceOffset = buffer.indiceOffset,
|
||||
vertexId = buffer.vertexOffset;
|
||||
|
||||
let uvSliced = sprite.spriteFrame.uvSliced;
|
||||
|
||||
buffer.request(vertexCount, renderData.indiceCount);
|
||||
|
||||
for (let i = 4; i < 20; ++i) {
|
||||
let vert = data[i];
|
||||
let uvs = uvSliced[i - 4];
|
||||
|
||||
vbuf[vertexOffset++] = vert.x;
|
||||
vbuf[vertexOffset++] = vert.y;
|
||||
vbuf[vertexOffset++] = vert.u;
|
||||
vbuf[vertexOffset++] = vert.v;
|
||||
vbuf[vertexOffset++] = uvs.u;
|
||||
vbuf[vertexOffset++] = uvs.v;
|
||||
}
|
||||
|
||||
for (let r = 0; r < 3; ++r) {
|
||||
|
||||
Reference in New Issue
Block a user