fix: stop psd replacements inheriting source masks
This commit is contained in:
parent
1963c84d7c
commit
bcb0006a9e
@ -2,7 +2,7 @@ import type { AgentPsdTextColorConfig } from "@/types/agent";
|
||||
import type { AgentPsdTemplateSecondaryCategoryConfig } from "@/lib/agents/psd-template-secondary-category-config";
|
||||
|
||||
export const AGENT_PSD_TEMPLATES_KEY = "agent_psd_templates";
|
||||
export const AGENT_PSD_TEMPLATE_ASSET_VERSION = 5;
|
||||
export const AGENT_PSD_TEMPLATE_ASSET_VERSION = 6;
|
||||
|
||||
export type AgentPsdTemplateStatus = "active" | "disabled";
|
||||
export type AgentPsdTemplateParseStatus =
|
||||
|
||||
@ -664,8 +664,7 @@ export async function preparePsdTemplateRenderAssets(params: {
|
||||
|
||||
if (
|
||||
!isAgentPsdImageLayerSlot(targetLayer) ||
|
||||
!targetLayer.isEditable ||
|
||||
(!params.forceRefreshLayerAssets && targetLayer.maskUrl)
|
||||
!targetLayer.isEditable
|
||||
) {
|
||||
const layerIndex = nextLayers.findIndex((layer) => layer.id === targetLayer.id);
|
||||
if (layerIndex >= 0 && isAgentPsdImageLayerSlot(nextLayers[layerIndex])) {
|
||||
@ -677,45 +676,19 @@ export async function preparePsdTemplateRenderAssets(params: {
|
||||
continue;
|
||||
}
|
||||
|
||||
const pixelCount = entry.layer.width * entry.layer.height;
|
||||
const maskRgba = new Uint8ClampedArray(pixelCount * 4);
|
||||
let hasVisiblePixel = false;
|
||||
|
||||
for (let index = 0; index < pixelCount; index += 1) {
|
||||
const alpha = rgba[index * 4 + 3] || 0;
|
||||
maskRgba[index * 4] = 255;
|
||||
maskRgba[index * 4 + 1] = 255;
|
||||
maskRgba[index * 4 + 2] = 255;
|
||||
maskRgba[index * 4 + 3] = alpha;
|
||||
if (alpha > 0) {
|
||||
hasVisiblePixel = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasVisiblePixel) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const maskBuffer = await rgbaToPngBuffer(maskRgba, entry.layer.width, entry.layer.height);
|
||||
const uploaded = await uploadFile(
|
||||
`agent-psd-mask-${params.templateId}-${sanitizeFileSegment(targetLayer.layerId, targetLayer.id)}.png`,
|
||||
maskBuffer.buffer.slice(
|
||||
maskBuffer.byteOffset,
|
||||
maskBuffer.byteOffset + maskBuffer.byteLength
|
||||
) as ArrayBuffer
|
||||
);
|
||||
|
||||
const layerIndex = nextLayers.findIndex((layer) => layer.id === targetLayer.id);
|
||||
if (layerIndex >= 0 && isAgentPsdImageLayerSlot(nextLayers[layerIndex])) {
|
||||
nextLayers[layerIndex] = {
|
||||
...nextLayers[layerIndex],
|
||||
contentBounds: visibleContentBounds,
|
||||
maskUrl: uploaded.url,
|
||||
maskPath: uploaded.pathname,
|
||||
// Do not auto-reuse the original PSD image alpha as a hard replacement mask.
|
||||
// Otherwise newly assigned images get trapped inside the source image silhouette.
|
||||
maskUrl: undefined,
|
||||
maskPath: undefined,
|
||||
};
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("[PSD Template] Failed to generate layer mask:", {
|
||||
console.warn("[PSD Template] Failed to prepare editable image layer assets:", {
|
||||
layerId: targetLayer.layerId,
|
||||
error,
|
||||
});
|
||||
|
||||
@ -22,10 +22,7 @@ function hasMissingLayerAssets(template: AgentPsdTemplate) {
|
||||
return template.layers.some(
|
||||
(layer) =>
|
||||
!String(layer.previewUrl || "").trim() ||
|
||||
!String(layer.sourceUrl || "").trim() ||
|
||||
(isAgentPsdImageLayerSlot(layer) &&
|
||||
layer.isEditable &&
|
||||
!String(layer.maskUrl || "").trim())
|
||||
!String(layer.sourceUrl || "").trim()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user