Merge pull request #2512 from 2youyou2/mesh-sprite

support mesh sprite
This commit is contained in:
Jare Guo
2018-04-10 14:30:02 +08:00
committed by GitHub
9 changed files with 295 additions and 91 deletions
+33 -1
View File
@@ -153,6 +153,8 @@ var SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
*/
this.insetRight = 0;
this.vertices = null;
this._texture = null;
this._textureFilename = '';
@@ -459,6 +461,18 @@ var SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
}
},
_normalizeUV () {
let vertices = this.vertices;
let tw = this._texture.width;
let th = this._texture.height;
vertices.nu = [];
vertices.nv = [];
for (let i = 0; i < vertices.u.length; i++) {
vertices.nu[i] = vertices.u[i]/tw;
vertices.nv[i] = vertices.v[i]/th;
}
},
// SERIALIZATION
_serialize: CC_EDITOR && function (exporting) {
@@ -486,6 +500,18 @@ var SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
this.insetBottom !== 0) {
capInsets = [this.insetLeft, this.insetTop, this.insetRight, this.insetBottom];
}
let vertices;
if (this.vertices) {
vertices = {
triangles: this.vertices.triangles,
x: this.vertices.x,
y: this.vertices.y,
u: this.vertices.u,
v: this.vertices.v
};
}
return {
name: this._name,
texture: uuid || undefined,
@@ -494,7 +520,8 @@ var SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
offset: offset ? [offset.x, offset.y] : undefined,
originalSize: size ? [size.width, size.height] : undefined,
rotated: this._rotated ? 1 : undefined,
capInsets: capInsets
capInsets: capInsets,
vertices: vertices
};
},
@@ -522,6 +549,11 @@ var SpriteFrame = cc.Class(/** @lends cc.SpriteFrame# */{
this._atlasUuid = data.atlas;
}
if (data.vertices) {
this.vertices = data.vertices;
this.on('load', this._normalizeUV, this);
}
// load texture via _textureSetter
var textureUuid = data.texture;
if (textureUuid) {
+14 -6
View File
@@ -370,8 +370,10 @@ var Sprite = cc.Class({
set: function (value) {
if (this._isTrimmedMode !== value) {
this._isTrimmedMode = value;
if (this._type === SpriteType.SIMPLE && this._renderData) {
if ((this._type === SpriteType.SIMPLE || this._type === SpriteType.MESH) &&
this._renderData) {
this._renderData.uvDirty = true;
this._renderData.vertDirty = true;
}
}
},
@@ -519,6 +521,7 @@ var Sprite = cc.Class({
if (!this._renderData) {
this._renderData = this._assembler.createData(this);
this._renderData.worldMatDirty = true;
this._renderData.material = this._material;
}
},
@@ -651,13 +654,18 @@ var Sprite = cc.Class({
}
var spriteFrame = this._spriteFrame;
if (spriteFrame && (!oldFrame || spriteFrame._texture !== oldFrame._texture)) {
if (spriteFrame.textureLoaded()) {
this._onTextureLoaded(null);
if (spriteFrame) {
if (!oldFrame || spriteFrame._texture !== oldFrame._texture) {
if (spriteFrame.textureLoaded()) {
this._onTextureLoaded(null);
}
else {
spriteFrame.once('load', this._onTextureLoaded, this);
spriteFrame.ensureLoadTexture();
}
}
else {
spriteFrame.once('load', this._onTextureLoaded, this);
spriteFrame.ensureLoadTexture();
this._applySpriteSize();
}
}
@@ -63,7 +63,7 @@ module.exports = {
if (vertDirty) {
this.updateVerts(sprite, fillStart, fillEnd);
}
if (renderData.worldMatDirty) {
if (vertDirty || renderData.worldMatDirty) {
this.updateWorldVerts(sprite);
}
},
@@ -35,6 +35,7 @@ const slicedRenderUtil = require('./sliced');
const tiledRenderUtil = require('./tiled');
const radialFilledRenderUtil = require('./radial-filled');
const barFilledRenderUtil = require('./bar-filled');
const meshRenderUtil = require('./mesh');
// Inline all type switch to avoid jit deoptimization during inlined function change
let spriteAssembler = js.addon({
@@ -58,6 +59,9 @@ let spriteAssembler = js.addon({
util = barFilledRenderUtil;
}
break;
case SpriteType.MESH:
util = meshRenderUtil;
break;
}
return util;
@@ -65,7 +69,7 @@ let spriteAssembler = js.addon({
updateRenderData (sprite) {
let datas = sprite.__allocedDatas;
if (!sprite.spriteFrame) {
if (!sprite.spriteFrame || !sprite.getMaterial() || !sprite._renderData) {
return datas;
}
@@ -0,0 +1,170 @@
/****************************************************************************
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.
****************************************************************************/
module.exports = {
createData (sprite) {
return sprite.requestRenderData();
},
update (sprite) {
let renderData = sprite._renderData;
let frame = sprite.spriteFrame;
let vertices = frame.vertices;
if (!vertices) return;
if (renderData.vertexCount !== vertices.x.length) {
renderData.vertexCount = vertices.x.length;
renderData.indiceCount = vertices.triangles.length;
// 1 for world vertices, 2 for local vertices
renderData.dataLength = renderData.vertexCount * 2;
renderData.uvDirty = renderData.vertDirty = true;
}
if (renderData.uvDirty) {
this.updateUVs(sprite);
}
let vertDirty = renderData.vertDirty;
if (vertDirty) {
this.updateVerts(sprite);
}
if (vertDirty || renderData.worldMatDirty) {
this.updateWorldVerts(sprite);
}
},
updateUVs (sprite) {
let material = sprite.getMaterial();
let texture = material.effect.getProperty('texture');
let texw = texture._width,
texh = texture._height;
let vertices = sprite.spriteFrame.vertices,
u = vertices.nu,
v = vertices.nv;
let renderData = sprite._renderData;
let data = renderData._data;
for (let i = 0, l = u.length; i < l; i++) {
let vertice = data[i];
vertice.u = u[i];
vertice.v = v[i];
}
renderData.uvDirty = false;
},
updateVerts (sprite) {
let node = sprite.node,
contentWidth = Math.abs(node.width),
contentHeight = Math.abs(node.height),
appx = node.anchorX * contentWidth,
appy = node.anchorY * contentHeight;
let frame = sprite.spriteFrame,
vertices = frame.vertices,
x = vertices.x,
y = vertices.y,
originalWidth = frame._originalSize.width,
originalHeight = frame._originalSize.height,
rectWidth = frame._rect.width,
rectHeight = frame._rect.height,
offsetX = frame._offset.x,
offsetY = frame._offset.y;
trimX = offsetX + (originalWidth - rectWidth) / 2,
trimY = offsetY + (originalHeight - rectHeight) / 2;
let scaleX = contentWidth / (sprite.trim ? rectWidth : originalWidth),
scaleY = contentHeight / (sprite.trim ? rectHeight : originalHeight);
let renderData = sprite._renderData;
let data = renderData._data;
if (!sprite.trim) {
for (let i = 0, l = x.length; i < l; i++) {
let vertice = data[i+l];
vertice.x = (x[i]) * scaleX - appx;
vertice.y = (originalHeight - y[i]) * scaleY - appy;
}
}
else {
for (let i = 0, l = x.length; i < l; i++) {
let vertice = data[i+l];
vertice.x = (x[i] - trimX) * scaleX - appx;
vertice.y = (originalHeight - y[i] - trimY) * scaleY - appy;
}
}
renderData.vertDirty = false;
},
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 i = 0, l = renderData.vertexCount; i < l; i++) {
let local = data[i+l];
let world = data[i];
world.x = local.x * a + local.y * c + tx;
world.y = local.x * b + local.y * d + ty;
}
renderData.worldMatDirty = false;
},
fillBuffers (sprite, batchData, vertexId, vbuf, uintbuf, ibuf) {
let vertexOffset = batchData.byteOffset / 4,
indiceOffset = batchData.indiceOffset;
// update verts
let node = sprite.node;
let renderData = sprite._renderData;
let data = renderData._data;
let z = node._position.z;
let color = node._color._val;
for (let i = 0, l = renderData.vertexCount; i < l; i++) {
let vertice = data[i];
vbuf[vertexOffset++] = vertice.x;
vbuf[vertexOffset++] = vertice.y;
vbuf[vertexOffset++] = z;
uintbuf[vertexOffset++] = color;
vbuf[vertexOffset++] = vertice.u;
vbuf[vertexOffset++] = vertice.v;
}
let triangles = sprite.spriteFrame.vertices.triangles;
for (let i = 0, l = triangles.length; i < l; i++) {
ibuf[indiceOffset++] = vertexId + triangles[i];
}
}
};
@@ -23,7 +23,6 @@
THE SOFTWARE.
****************************************************************************/
const Sprite = require('../../../components/CCSprite');
const simpleRenderUtil = require('./simple');
const PI_2 = Math.PI * 2;
@@ -23,8 +23,6 @@
THE SOFTWARE.
****************************************************************************/
const Sprite = require('../../../components/CCSprite');
const js = require('../../../platform/js');
const assembler = require('../assembler');
@@ -87,45 +85,43 @@ module.exports = js.addon({
updateUVs (sprite) {
let material = sprite.getMaterial();
let renderData = sprite._renderData;
if (material && renderData) {
let data = renderData._data;
let texture = material.effect.getProperty('texture');
let texw = texture._width,
texh = texture._height;
let frame = sprite.spriteFrame;
let rect = frame._rect;
if (frame._rotated) {
let l = texw === 0 ? 0 : rect.x / texw;
let r = texw === 0 ? 0 : (rect.x + rect.height) / texw;
let b = texh === 0 ? 0 : (rect.y + rect.width) / texh;
let t = texh === 0 ? 0 : rect.y / texh;
data[0].u = l;
data[0].v = t;
data[1].u = l;
data[1].v = b;
data[2].u = r;
data[2].v = t;
data[3].u = r;
data[3].v = b;
}
else {
let l = texw === 0 ? 0 : rect.x / texw;
let r = texw === 0 ? 0 : (rect.x + rect.width) / texw;
let b = texh === 0 ? 0 : (rect.y + rect.height) / texh;
let t = texh === 0 ? 0 : rect.y / texh;
data[0].u = l;
data[0].v = b;
data[1].u = r;
data[1].v = b;
data[2].u = l;
data[2].v = t;
data[3].u = r;
data[3].v = t;
}
renderData.uvDirty = false;
let data = renderData._data;
let texture = material.effect.getProperty('texture');
let texw = texture._width,
texh = texture._height;
let frame = sprite.spriteFrame;
let rect = frame._rect;
if (frame._rotated) {
let l = texw === 0 ? 0 : rect.x / texw;
let r = texw === 0 ? 0 : (rect.x + rect.height) / texw;
let b = texh === 0 ? 0 : (rect.y + rect.width) / texh;
let t = texh === 0 ? 0 : rect.y / texh;
data[0].u = l;
data[0].v = t;
data[1].u = l;
data[1].v = b;
data[2].u = r;
data[2].v = t;
data[3].u = r;
data[3].v = b;
}
else {
let l = texw === 0 ? 0 : rect.x / texw;
let r = texw === 0 ? 0 : (rect.x + rect.width) / texw;
let b = texh === 0 ? 0 : (rect.y + rect.height) / texh;
let t = texh === 0 ? 0 : rect.y / texh;
data[0].u = l;
data[0].v = b;
data[1].u = r;
data[1].v = b;
data[2].u = l;
data[2].v = t;
data[3].u = r;
data[3].v = t;
}
renderData.uvDirty = false;
},
updateWorldVerts (sprite) {
@@ -43,45 +43,43 @@ module.exports = {
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;
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) {
@@ -23,8 +23,6 @@
THE SOFTWARE.
****************************************************************************/
const Sprite = require('../../../components/CCSprite');
module.exports = {
createData (sprite) {
return sprite.requestRenderData();
@@ -35,7 +33,6 @@ module.exports = {
if (!renderData.uvDirty && !renderData.vertDirty) return;
let material = sprite.getMaterial();
if (!material || !renderData) return;
let texture = material.effect.getProperty('texture');
let texw = texture._width,