feat: SodiumPet 小钠桌宠 v0.1.0(Live2D 桌宠 / 经济任务 / 音乐 / 成就 / 编辑器 / MOD)

This commit is contained in:
Tian_XiaRi
2026-12-01 16:41:07 +08:00
commit d25e726ef7
261 changed files with 33676 additions and 0 deletions
+172
View File
@@ -0,0 +1,172 @@
const fs = require('fs')
const p = 'C:/Users/ASUS/Documents/deepseek-harness/default-workspace/desktop-pet/src/renderer/src/pet/adapters/live2d.ts'
let c = fs.readFileSync(p, 'utf8')
function rep(from, to) {
if (!c.includes(from)) {
console.log('MISS ::', from.slice(0, 70).replace(/\n/g, '\\n'))
return
}
c = c.replace(from, to)
}
// 1) layout 支持忽略(不可信的)校准值
rep(
` private layout(): void {
if (!this.app || !this.model) return
const { width, height } = this.app.renderer.screen
const factor = this.scaleOverride ?? this.ctx.skin.avatar.scale ?? 1
const cal = this.ctx.skin.avatar.calibration
let scale: number
if (cal && cal.refHeight > 0) {`,
` private ignoreCal = false
private layout(): void {
if (!this.app || !this.model) return
const { width, height } = this.app.renderer.screen
const factor = this.scaleOverride ?? this.ctx.skin.avatar.scale ?? 1
const cal = this.ctx.skin.avatar.calibration as
| (NonNullable<typeof this.ctx.skin.avatar.calibration> & { v?: number })
| undefined
const imported = (this.ctx.skin as { flavor?: string }).flavor === 'normal'
const calUsable =
!!cal && cal.refHeight > 0 && !this.ignoreCal && (!imported || (cal.v ?? 0) >= 2)
let scale: number
if (calUsable && cal) {`
)
// 2) mount 里:导入角色且校准不可信时,先忽略
rep(
` this.layout()
console.log(
\`[live2d] loaded canvas=\${this.baseSize.w}x\${this.baseSize.h}\`
)`,
` {
const cal0 = this.ctx.skin.avatar.calibration as { v?: number } | undefined
const imported0 = (this.ctx.skin as { flavor?: string }).flavor === 'normal'
if (imported0 && (!cal0 || (cal0.v ?? 0) < 2)) this.ignoreCal = true
}
this.layout()
console.log(
\`[live2d] loaded canvas=\${this.baseSize.w}x\${this.baseSize.h}\`
)`
)
// 3) 自动校准:带边沿检测的重试循环
const oldFn = c.slice(c.indexOf(' private autoCalibrated = false'), c.indexOf(' lookAt(x: number, y: number): void {'))
const newFn = ` private autoCalibrated = false
/** 读一帧像素,返回内容包围盒(CSS 坐标),空则 null */
private readBBox(res: number, w: number, h: number): { x0: number; y0: number; x1: number; y1: number } | null {
const gl = this.app.renderer.gl
const buf = new Uint8Array(w * h * 4)
gl.bindFramebuffer(gl.FRAMEBUFFER, null)
gl.readPixels(0, 0, w, h, gl.RGBA, gl.UNSIGNED_BYTE, buf)
let minX = 1e9
let minY = 1e9
let maxX = -1
let maxY = -1
for (let y = 0; y < h; y++) {
const row = (h - 1 - y) * w * 4
for (let x = 0; x < w; x++) {
if (buf[row + x * 4 + 3] > 16) {
if (x < minX) minX = x
if (x > maxX) maxX = x
if (y < minY) minY = y
if (y > maxY) maxY = y
}
}
}
if (!Number.isFinite(minX) || maxX <= minX || maxY <= minY) return null
return { x0: minX / res, y0: minY / res, x1: maxX / res, y1: maxY / res }
}
/**
* 导入模型自动校准:先把模型缩放到画布放得下(贴边就再缩),
* 量出内容包围盒后换算成校准值并写回 skin.json。
*/
tryAutoCalibrate(): void {
if (this.autoCalibrated) return
try {
const renderer = this.app.renderer
const res = renderer.resolution || 1
const wCss = renderer.screen.width
const hCss = renderer.screen.height
const w = Math.floor(wCss * res)
const h = Math.floor(hCss * res)
try {
this.model.internalModel?.motionManager?.stopAllMotions?.()
} catch {
/* 忽略 */
}
let s = hCss / (this.baseSize.h || 1)
let found: { x0: number; y0: number; x1: number; y1: number } | null = null
for (let attempt = 0; attempt < 7; attempt++) {
this.model.scale.set(s)
this.model.x = wCss / 2 - (this.baseSize.w / 2) * s
this.model.y = hCss - this.baseSize.h * s
renderer.render(this.app.stage)
const bb = this.readBBox(res, w, h)
if (!bb) {
s *= 0.6
continue
}
const clipped = bb.x0 <= 1 || bb.y0 <= 1 || bb.x1 >= wCss - 2 || bb.y1 >= hCss - 1
if (clipped) {
s *= 0.65
continue
}
found = bb
break
}
if (!found) {
this.ignoreCal = true
this.layout()
console.warn('[live2d] 自动校准失败:内容始终贴边,退化为画布适配')
return
}
const s0 = this.model.scale.x || 1
const ly0 = (found.y0 - this.model.y) / s0
const ly1 = (found.y1 - this.model.y) / s0
const cx = ((found.x0 + found.x1) / 2 - this.model.x) / s0
const H = Math.max(1, ly1 - ly0)
const refHeight = 470
const cal = {
scale: (0.92 * refHeight) / H,
centerX: cx,
bottom: ly1,
refHeight,
v: 2
}
this.ctx.skin.avatar.calibration = cal as never
this.ignoreCal = false
this.autoCalibrated = true
this.layout()
console.log('[live2d] 自动校准完成', JSON.stringify(cal))
try {
;(window as unknown as { pet?: { saveCalibration?: (id: string, c: unknown) => void } }).pet?.saveCalibration?.(
this.ctx.skin.id,
cal
)
} catch {
/* 忽略 */
}
} catch (err) {
console.warn('[live2d] 自动校准异常', err)
}
}
`
c = c.replace(oldFn, newFn)
fs.writeFileSync(p, c, 'utf8')
console.log('calibration v2 patched')