[engine] 修复 Spine 动态合图 uv 未更新的问题

This commit is contained in:
SmallMain 2022-06-27 15:24:52 +08:00
parent dbe9c6d28f
commit 19a018dc5e

View File

@ -83,6 +83,8 @@ let _x, _y, _m00, _m04, _m12, _m01, _m05, _m13;
let _r, _g, _b, _fr, _fg, _fb, _fa, _dr, _dg, _db, _da; let _r, _g, _b, _fr, _fg, _fb, _fa, _dr, _dg, _db, _da;
let _comp, _buffer, _renderer, _node, _needColor, _vertexEffect; let _comp, _buffer, _renderer, _node, _needColor, _vertexEffect;
let _packedRegions = [];
function _getSlotMaterial (tex, blendMode) { function _getSlotMaterial (tex, blendMode) {
let src, dst; let src, dst;
switch (blendMode) { switch (blendMode) {
@ -238,9 +240,39 @@ export default class SpineAssembler extends Assembler {
} }
} }
bindPackedRegion(attachment, region) {
const frame = region._spriteFrame;
sp.SkeletonData.updateRegionUV(region);
if (attachment instanceof sp.spine.MeshAttachment) {
attachment.updateUVs();
} else {
attachment.setRegion(region);
attachment.updateOffset();
}
frame.once("_resetDynamicAtlasFrame", () => {
region.x = region._original._x;
region.y = region._original._y;
region.texture = region._original._texture;
region._original = null;
// update uv
sp.SkeletonData.updateRegionUV(region);
if (attachment instanceof sp.spine.MeshAttachment) {
attachment.updateUVs();
} else {
attachment.setRegion(region);
attachment.updateOffset();
}
});
}
packDynamicAtlasForSpine(comp) { packDynamicAtlasForSpine(comp) {
if (CC_TEST) return false; if (CC_TEST) return false;
_packedRegions.length = 0;
const allowDynamicAtlas = comp.allowDynamicAtlas; const allowDynamicAtlas = comp.allowDynamicAtlas;
if ((cc.sp.allowDynamicAtlas && allowDynamicAtlas === 0) || allowDynamicAtlas === 1) { if ((cc.sp.allowDynamicAtlas && allowDynamicAtlas === 0) || allowDynamicAtlas === 1) {
if (cc.dynamicAtlasManager) { if (cc.dynamicAtlasManager) {
@ -250,58 +282,46 @@ export default class SpineAssembler extends Assembler {
for (const key in attachments) { for (const key in attachments) {
const attachment = attachments[key]; const attachment = attachments[key];
const region = attachment.region; const region = attachment.region;
if (region && !region._original && region.texture && region.texture._texture.packable) {
if (region._spriteFrame) {
region._spriteFrame.destroy();
region._spriteFrame = null;
}
const frame = sp.SkeletonData.createSpriteFrame(region);
const packedFrame = cc.dynamicAtlasManager.insertSpriteFrame(frame);
if (packedFrame) {
frame._setDynamicAtlasFrame(packedFrame);
region._original = { if (region) {
_texture: region.texture, if (region._original) {
_x: region.x, // 可能出现多个 attachment 共用同一个 region
_y: region.y, if (_packedRegions.includes(region)) {
}; this.bindPackedRegion(attachment, region);
region.texture = new sp.SkeletonTexture({
width: packedFrame.texture.width,
height: packedFrame.texture.height,
});
region.texture.setRealTexture(packedFrame.texture);
region.x = packedFrame.x;
region.y = packedFrame.y;
// update uv
sp.SkeletonData.updateRegionUV(region);
if (attachment instanceof sp.spine.MeshAttachment) {
attachment.updateUVs();
} else {
attachment.setRegion(region);
attachment.updateOffset();
} }
} else if (region.texture && region.texture._texture.packable) {
if (region._spriteFrame) {
region._spriteFrame.destroy();
region._spriteFrame = null;
}
const frame = sp.SkeletonData.createSpriteFrame(region);
const packedFrame = cc.dynamicAtlasManager.insertSpriteFrame(frame);
if (packedFrame) {
frame._setDynamicAtlasFrame(packedFrame);
frame.once("_resetDynamicAtlasFrame", () => { region._original = {
region.x = region._original._x; _texture: region.texture,
region.y = region._original._y; _x: region.x,
region.texture = region._original._texture; _y: region.y,
region._original = null; };
region.texture = new sp.SkeletonTexture({
width: packedFrame.texture.width,
height: packedFrame.texture.height,
});
region.texture.setRealTexture(packedFrame.texture);
region.x = packedFrame.x;
region.y = packedFrame.y;
// update uv // update uv
sp.SkeletonData.updateRegionUV(region); region._spriteFrame = frame;
if (attachment instanceof sp.spine.MeshAttachment) { this.bindPackedRegion(attachment, region);
attachment.updateUVs();
} else { _packedRegions.push(region);
attachment.setRegion(region); } else {
attachment.updateOffset(); frame.destroy();
} }
});
region._spriteFrame = frame;
} else {
frame.destroy();
} }
} }
} }
@ -309,6 +329,8 @@ export default class SpineAssembler extends Assembler {
} }
} }
} }
_packedRegions.length = 0;
} }
fillVertices (skeletonColor, attachmentColor, slotColor, clipper, slot) { fillVertices (skeletonColor, attachmentColor, slotColor, clipper, slot) {