a03f13d0a5
原问题
```
历史角色动作是一个兼容例外:
后端允许它没有 editor_project_resource 资源行,只要布局自身保存了完整图片序列帧。
```
- 把生成动作的原视频asstetkind改为 video, 只把图片序列作为action
- db asset新增字段image_sequence_frames_json image_seq_duration_ms等, (原来动作的这些数据存在generation-input中,并不合适)
修改了externaljob,把这部分数据正确填写到数据库中。
- 为避免到处fallback,做了数据库迁移, 脚本: `scripts/spacetime-normalize-editor-character-actions.mjs` 手动进行过画布+素材库中动作序列帧+原始视频 迁移的测试
- 移除preview_video_path字段, reason:
>preview video
→ separate resource/asset with assetKind="video"
final transparent action
→ assetKind="character-animation"
→ source_resource_id points to the preview-video resource
→ image_sequence_frames_json contains the actual playable result
>
> So preview_video_path on the final action duplicates the source video resource’s image_src.
- 移除每个frame的index字段 原因: 这个只用于后端内部处理时有一个并发请求, 每个赋一个index方便收集, 后续没有再用到,且与数组本身重复
- 清除副产品preview video的generation_input_json, 因为会影响改造功能, 迁移后预览视频不提供改造(参数), 只有序列帧动作有改造
仍存在的共性问题: #134
- 导出:
下载改为完整序列帧, 封面不再作为fallback, 部分帧读取失败时行为:仍生成 ZIP,并记录失败帧, 不变
画布放置:
before:

after:

精选模块: 主页展示, 审核部分UI:


这两处序列帧们的加载设计为惰式的, 只有hover和单独preview才会全部加载
Reviewed-on: http://192.168.35.82/git/GenarrativeAI/Genarrative/pulls/117
Reviewed-by: 段舒康 <kdletters@qq.com>
Co-authored-by: 王德宇 <kvtodev@outlook.com>
Co-committed-by: 王德宇 <kvtodev@outlook.com>
246 lines
6.9 KiB
TypeScript
246 lines
6.9 KiB
TypeScript
import {
|
|
DEFAULT_CANVAS_BACKGROUND_COLOR,
|
|
normalizeCanvasBackgroundHex,
|
|
} from './ImageCanvasEditorModel';
|
|
import type { CanvasLayer, CanvasViewport } from './ImageCanvasEditorTypes';
|
|
|
|
export const PROJECT_COVER_SNAPSHOT_ASSET_KIND = 'project-cover-snapshot';
|
|
export const PROJECT_COVER_SNAPSHOT_OBJECT_ASSET_KIND =
|
|
'editor_project_cover_snapshot';
|
|
export const PROJECT_COVER_SNAPSHOT_SIZE = { width: 320, height: 240 } as const;
|
|
export const PROJECT_COVER_SNAPSHOT_MIN_VIEWPORT_SIZE = {
|
|
width: 1280,
|
|
height: 960,
|
|
} as const;
|
|
|
|
export type ProjectCoverSnapshotViewportSize = {
|
|
width: number;
|
|
height: number;
|
|
};
|
|
|
|
export type ProjectCoverDrawableLayer = {
|
|
id: string;
|
|
src: string;
|
|
objectKey: string | null;
|
|
cacheVersion: string;
|
|
x: number;
|
|
y: number;
|
|
width: number;
|
|
height: number;
|
|
zIndex: number;
|
|
};
|
|
|
|
function numberOrFallback(value: number, fallback: number) {
|
|
return Number.isFinite(value) ? value : fallback;
|
|
}
|
|
|
|
function positiveNumberOrFallback(value: number, fallback: number) {
|
|
return Number.isFinite(value) && value > 0 ? value : fallback;
|
|
}
|
|
|
|
function normalizeProjectCoverViewportSize(
|
|
viewportSize: ProjectCoverSnapshotViewportSize,
|
|
) {
|
|
return {
|
|
width: positiveNumberOrFallback(
|
|
viewportSize.width,
|
|
PROJECT_COVER_SNAPSHOT_SIZE.width,
|
|
),
|
|
height: positiveNumberOrFallback(
|
|
viewportSize.height,
|
|
PROJECT_COVER_SNAPSHOT_SIZE.height,
|
|
),
|
|
};
|
|
}
|
|
|
|
export function normalizeProjectCoverSnapshotViewport({
|
|
viewport,
|
|
viewportSize,
|
|
}: {
|
|
viewport: CanvasViewport;
|
|
viewportSize: ProjectCoverSnapshotViewportSize;
|
|
}) {
|
|
const sourceViewportSize = {
|
|
width: positiveNumberOrFallback(
|
|
viewportSize.width,
|
|
PROJECT_COVER_SNAPSHOT_MIN_VIEWPORT_SIZE.width,
|
|
),
|
|
height: positiveNumberOrFallback(
|
|
viewportSize.height,
|
|
PROJECT_COVER_SNAPSHOT_MIN_VIEWPORT_SIZE.height,
|
|
),
|
|
};
|
|
const normalizedViewportSize = {
|
|
width: Math.max(
|
|
PROJECT_COVER_SNAPSHOT_MIN_VIEWPORT_SIZE.width,
|
|
sourceViewportSize.width,
|
|
),
|
|
height: Math.max(
|
|
PROJECT_COVER_SNAPSHOT_MIN_VIEWPORT_SIZE.height,
|
|
sourceViewportSize.height,
|
|
),
|
|
};
|
|
return {
|
|
viewport: {
|
|
x:
|
|
numberOrFallback(viewport.x, 0) +
|
|
(normalizedViewportSize.width - sourceViewportSize.width) / 2,
|
|
y:
|
|
numberOrFallback(viewport.y, 0) +
|
|
(normalizedViewportSize.height - sourceViewportSize.height) / 2,
|
|
scale: numberOrFallback(viewport.scale, 1),
|
|
},
|
|
viewportSize: normalizedViewportSize,
|
|
};
|
|
}
|
|
|
|
export function normalizeProjectCoverBackgroundColor(backgroundColor: string) {
|
|
return (
|
|
normalizeCanvasBackgroundHex(backgroundColor) ??
|
|
DEFAULT_CANVAS_BACKGROUND_COLOR
|
|
);
|
|
}
|
|
|
|
function normalizeObjectKey(value: string | null | undefined) {
|
|
const normalized = value?.trim().replace(/^\/+/u, '') ?? '';
|
|
return normalized || null;
|
|
}
|
|
|
|
function resolveLayerSnapshotSource(layer: CanvasLayer) {
|
|
if (layer.hidden || layer.mediaType === 'audio') {
|
|
return null;
|
|
}
|
|
if (layer.mediaType === 'image-sequence') {
|
|
const frame = layer.imageSequenceFrames?.[0];
|
|
const frameSrc = frame?.imageSrc?.trim();
|
|
if (frameSrc) {
|
|
return {
|
|
src: frameSrc,
|
|
objectKey: normalizeObjectKey(frame?.objectKey),
|
|
cacheVersion: `${layer.taskId ?? layer.resourceId}:0`,
|
|
};
|
|
}
|
|
return null;
|
|
}
|
|
const snapshotSrc = layer.thumbnailSrc?.trim() || layer.src.trim();
|
|
if (!snapshotSrc) {
|
|
return null;
|
|
}
|
|
return {
|
|
src: snapshotSrc,
|
|
objectKey: layer.thumbnailSrc?.trim()
|
|
? null
|
|
: normalizeObjectKey(layer.objectKey),
|
|
cacheVersion: layer.thumbnailSrc?.trim()
|
|
? `${layer.taskId ?? layer.resourceId}:poster`
|
|
: (layer.taskId ?? layer.resourceId),
|
|
};
|
|
}
|
|
|
|
export function resolveProjectCoverDrawableLayers(
|
|
layers: readonly CanvasLayer[],
|
|
): ProjectCoverDrawableLayer[] {
|
|
return layers
|
|
.map((layer) => {
|
|
const source = resolveLayerSnapshotSource(layer);
|
|
if (!source) {
|
|
return null;
|
|
}
|
|
return {
|
|
id: layer.id,
|
|
src: source.src,
|
|
objectKey: source.objectKey,
|
|
cacheVersion: source.cacheVersion,
|
|
x: numberOrFallback(layer.x, 0),
|
|
y: numberOrFallback(layer.y, 0),
|
|
width: Math.max(1, numberOrFallback(layer.width, layer.originalWidth)),
|
|
height: Math.max(
|
|
1,
|
|
numberOrFallback(layer.height, layer.originalHeight),
|
|
),
|
|
zIndex: numberOrFallback(layer.zIndex, 0),
|
|
} satisfies ProjectCoverDrawableLayer;
|
|
})
|
|
.filter((layer): layer is ProjectCoverDrawableLayer => Boolean(layer))
|
|
.sort((left, right) => left.zIndex - right.zIndex);
|
|
}
|
|
|
|
export function buildProjectCoverSnapshotSignature({
|
|
layers,
|
|
viewport,
|
|
viewportSize,
|
|
backgroundColor,
|
|
}: {
|
|
layers: readonly CanvasLayer[];
|
|
// we may call it camera
|
|
viewport: CanvasViewport;
|
|
viewportSize: ProjectCoverSnapshotViewportSize;
|
|
backgroundColor: string;
|
|
}) {
|
|
const coverLayers = resolveProjectCoverDrawableLayers(layers);
|
|
if (coverLayers.length === 0) {
|
|
return null;
|
|
}
|
|
const normalizedViewportSize =
|
|
normalizeProjectCoverViewportSize(viewportSize);
|
|
return JSON.stringify({
|
|
viewport: {
|
|
x: numberOrFallback(viewport.x, 0),
|
|
y: numberOrFallback(viewport.y, 0),
|
|
scale: numberOrFallback(viewport.scale, 1),
|
|
},
|
|
viewportSize: normalizedViewportSize,
|
|
backgroundColor: normalizeProjectCoverBackgroundColor(backgroundColor),
|
|
layers: coverLayers,
|
|
});
|
|
}
|
|
|
|
export function resolveProjectCoverSnapshotFrame({
|
|
viewport,
|
|
viewportSize,
|
|
}: {
|
|
viewport: CanvasViewport;
|
|
viewportSize: ProjectCoverSnapshotViewportSize;
|
|
}) {
|
|
const normalizedViewportSize =
|
|
normalizeProjectCoverViewportSize(viewportSize);
|
|
const renderScale =
|
|
viewport.scale > 0 && Number.isFinite(viewport.scale) ? viewport.scale : 1;
|
|
const frameScale = Math.max(
|
|
PROJECT_COVER_SNAPSHOT_SIZE.width / normalizedViewportSize.width,
|
|
PROJECT_COVER_SNAPSHOT_SIZE.height / normalizedViewportSize.height,
|
|
);
|
|
return {
|
|
viewportX: numberOrFallback(viewport.x, 0),
|
|
viewportY: numberOrFallback(viewport.y, 0),
|
|
renderScale,
|
|
frameScale,
|
|
offsetX:
|
|
(PROJECT_COVER_SNAPSHOT_SIZE.width -
|
|
normalizedViewportSize.width * frameScale) /
|
|
2,
|
|
offsetY:
|
|
(PROJECT_COVER_SNAPSHOT_SIZE.height -
|
|
normalizedViewportSize.height * frameScale) /
|
|
2,
|
|
};
|
|
}
|
|
|
|
export function resolveProjectCoverPlacement(
|
|
viewport: CanvasViewport,
|
|
viewportSize: ProjectCoverSnapshotViewportSize,
|
|
layer: ProjectCoverDrawableLayer,
|
|
) {
|
|
const frame = resolveProjectCoverSnapshotFrame({ viewport, viewportSize });
|
|
return {
|
|
x:
|
|
frame.offsetX +
|
|
(frame.viewportX + layer.x * frame.renderScale) * frame.frameScale,
|
|
y:
|
|
frame.offsetY +
|
|
(frame.viewportY + layer.y * frame.renderScale) * frame.frameScale,
|
|
width: layer.width * frame.renderScale * frame.frameScale,
|
|
height: layer.height * frame.renderScale * frame.frameScale,
|
|
};
|
|
}
|