移除 @scale @size,调整为自动计算 scale

This commit is contained in:
onvia
2023-08-04 17:24:37 +08:00
parent 6e294f7a19
commit 8d35d76c7a
18 changed files with 8307 additions and 177 deletions

View File

@@ -31,6 +31,7 @@ import { exportImageMgr } from './ExportImageMgr';
import { CCUIOpacity } from './engine/cc/CCUIOpacity';
import { CCUITransform } from './engine/cc/CCUITransform';
import { CCVec3 } from './engine/cc/values/CCVec3';
import { Vec3 } from './values/Vec3';
/***
@@ -124,7 +125,7 @@ export class Main {
// 创建缓存文件
if (args.cache && !fs.existsSync(args.cache)) {
writeCache();
await writeCache();
}
// 在没有缓存文件或者 指定重新缓存的时候,读取项目资源
@@ -186,7 +187,7 @@ export class Main {
console.error(`请设置 --input`);
return false;
}
if (!fs.existsSync(args.input)) {
console.error(`输入路径不存在: ${args.input}`);
return false;
@@ -278,16 +279,15 @@ export class Main {
// 劫持尺寸设置,使用 psd 中配置的尺寸,这里不对原数据进行修改
let size = new CCSize(layer.size.width, layer.size.height);
if (layer.attr?.comps.size) {
let _attrSize = layer.attr.comps.size;
size.width = _attrSize.w ?? size.width;
size.height = _attrSize.h ?? size.height;
// if (layer.attr?.comps.size) {
// let _attrSize = layer.attr.comps.size;
// size.width = _attrSize.w ?? size.width;
// size.height = _attrSize.h ?? size.height;
// }
}
// 对缩放进行处理
size.width = Math.round(Math.abs(size.width / layer.scale.x));
size.height = Math.round(Math.abs(size.height / layer.scale.y));
// // 对缩放进行处理
// size.width = Math.round(Math.abs(size.width / layer.scale.x));
// size.height = Math.round(Math.abs(size.height / layer.scale.y));
// 配置的位置 Y 偏移
let offsetY = 0;
@@ -342,6 +342,18 @@ export class Main {
} else {
// 查找绑定的图像
let _layer = imageMgr.getSerialNumberImage(layer);
// 根据原始图片自动计算缩放
let scaleX = layer.textureSize.width / _layer.textureSize.width;
let scaleY = layer.textureSize.height / _layer.textureSize.height;
if (scaleX != 1 || scaleY != 1) {
layer.scale = new Vec3((layer.isFlipX() ? -1 : 1) * scaleX, (layer.isFlipY() ? -1 : 1) * scaleY, 1);
node._trs.setScale(layer.scale.x, layer.scale.y, layer.scale.z);
node._lscale = new CCVec3(layer.scale.x, layer.scale.y, layer.scale.z);
}
// 使用已缓存的 图片 的 uuid
let imageWarp = imageCacheMgr.get(_layer.md5);
sprite.setSpriteFrame(imageWarp ? imageWarp.textureUuid : _layer.textureUuid);
@@ -417,14 +429,14 @@ export class Main {
}
console.log(`保存图片 [${_layer.imgName}] md5: ${_layer.md5}`);
imageWarp && (imageWarp.isOutput = true);
let fullpath = path.join(out, `${_layer.imgName}.png`);
fs.writeFileSync(fullpath, _layer.imgBuffer);
this.saveImageMeta(_layer, fullpath);
let fullPath = path.join(out, `${_layer.imgName}.png`);
fs.writeFileSync(fullPath, _layer.imgBuffer);
this.saveImageMeta(_layer, fullPath);
});
}
saveImageMeta(layer: PsdImage, fullpath: string) {
saveImageMeta(layer: PsdImage, fullPath: string) {
let _layer = imageMgr.getSerialNumberImage(layer);
let imageWarp = imageCacheMgr.get(_layer.md5);
if (!imageWarp) {
@@ -448,7 +460,7 @@ export class Main {
meta = meta.replace(/\$BORDER_LEFT/g, s9.l as any);
meta = meta.replace(/\$BORDER_RIGHT/g, s9.r as any);
fs.writeFileSync(fullpath + `.meta`, meta);
fs.writeFileSync(fullPath + `.meta`, meta);
}

View File

@@ -40,7 +40,7 @@ export class Parser {
// Group
if (layerType == LayerType.Group) {
group = new PsdGroup(source, parent, rootDoc);
if(group.attr.comps.ignorenode || group.attr.comps.ignore){
if (group.attr.comps.ignorenode || group.attr.comps.ignore) {
return null;
}
} else {
@@ -75,7 +75,7 @@ export class Parser {
imageMgr.add(image);
// 没有设置忽略且不说镜像的情况下才进行缓存
if (!image.isIgnore() && ! image.isBind()) {
if (!image.isIgnore() && !image.isBind()) {
if (!imageCacheMgr.has(image.md5)) {
imageCacheMgr.set(image.md5, {
uuid: image.uuid,

View File

@@ -42,7 +42,7 @@ export class PsdImage extends PsdLayer {
this.md5 = fileUtils.getMD5(this.imgBuffer);
this.textureSize = new Size(canvas.width, canvas.height);
this.scale = new Vec3((this.isFilpX() ? -1 : 1) * this.scale.x, (this.isFilpY() ? -1 : 1) * this.scale.y, 1);
this.scale = new Vec3((this.isFlipX() ? -1 : 1) * this.scale.x, (this.isFlipY() ? -1 : 1) * this.scale.y, 1);
}
onCtor() {
@@ -64,12 +64,12 @@ export class PsdImage extends PsdLayer {
}
/** 是否是 x 方向镜像图片 */
isFilpX() {
isFlipX() {
return typeof this.attr.comps.flipX?.bind !== 'undefined';
}
/** 是否是 y 方向镜像图片 */
isFilpY() {
isFlipY() {
return typeof this.attr.comps.flipY?.bind !== 'undefined';
}
@@ -89,8 +89,8 @@ export class PsdImage extends PsdLayer {
// this.position.x = this.position.x - this.parent.size.width * this.parent.anchorPoint.x + this.size.width * this.anchorPoint.x;
// this.position.y = this.position.y - this.parent.size.height * this.parent.anchorPoint.y + this.size.height * this.anchorPoint.y;
// 如果是镜像图片,则特殊处理
let arX = (this.isFilpX() ? (1 - this.anchorPoint.x) : this.anchorPoint.x);
let arY = (this.isFilpY() ? (1 - this.anchorPoint.y) : this.anchorPoint.y);
let arX = (this.isFlipX() ? (1 - this.anchorPoint.x) : this.anchorPoint.x);
let arY = (this.isFlipY() ? (1 - this.anchorPoint.y) : this.anchorPoint.y);
this.position.x = this.position.x - this.rootDoc.size.width * this.rootDoc.anchorPoint.x + this.size.width * arX;
this.position.y = this.position.y - this.rootDoc.size.height * this.rootDoc.anchorPoint.y + this.size.height * arY;
}

View File

@@ -40,8 +40,8 @@ export interface PsdAttr {
igimg?: {};
full?: {};
size?: { w?: number, h?: number };
scale?: { x?: number, y?: number };
// size?: { w?: number, h?: number };
// scale?: { x?: number, y?: number };
img?: { id?: number, name?: string, bind?: number }
flip?: { bind: number, x?: number, y?: number }
flipX?: { bind: number }
@@ -93,8 +93,9 @@ export abstract class PsdLayer {
this.name = this.chineseToPinyin(this.attr?.name || this.name);
// 使用配置的缩放系数
let _scale = this.attr?.comps.scale;
this.scale = new Vec3(_scale?.x ?? 1, _scale?.y ?? 1, 1);
// let _scale = this.attr?.comps.scale;
// this.scale = new Vec3(_scale?.x ?? 1, _scale?.y ?? 1, 1);
this.scale = new Vec3(1, 1, 1);
}
abstract onCtor();
@@ -195,10 +196,10 @@ export abstract class PsdLayer {
}
// 检查冲突
if (obj.comps.full && obj.comps.size) {
console.warn(`PsdLayer->${obj.name} 同时存在 @full 和 @size`);
}
// // 检查冲突
// if (obj.comps.full && obj.comps.size) {
// console.warn(`PsdLayer->${obj.name} 同时存在 @full 和 @size`);
// }
return obj;
}