283 lines
8.8 KiB
JavaScript
283 lines
8.8 KiB
JavaScript
// =========================================================
|
|
// live2d.js — Live2D Cubism 4 (pixi-live2d-display@0.4.0)
|
|
// Requiere: live2dcubismcore.min.js + cubism4.min.js
|
|
// =========================================================
|
|
|
|
const MODELS = {
|
|
mao: {
|
|
name: "Mao",
|
|
url: "https://cdn.jsdelivr.net/gh/Live2D/CubismWebSamples@develop/Samples/Resources/Mao/Mao.model3.json",
|
|
},
|
|
natori: {
|
|
name: "Natori",
|
|
url: "https://cdn.jsdelivr.net/gh/Live2D/CubismWebSamples@develop/Samples/Resources/Natori/Natori.model3.json",
|
|
},
|
|
haru: {
|
|
name: "Haru",
|
|
url: "https://cdn.jsdelivr.net/gh/Live2D/CubismWebSamples@develop/Samples/Resources/Haru/Haru.model3.json",
|
|
},
|
|
rice: {
|
|
name: "Rice",
|
|
url: "https://cdn.jsdelivr.net/gh/Live2D-Garage/CubismWebARSample@master/assets/models/Rice/Rice.model3.json",
|
|
},
|
|
};
|
|
|
|
// IDs de parámetro estándar Cubism 4
|
|
const PARAM = {
|
|
EYE_BALL_X: "ParamEyeBallX",
|
|
EYE_BALL_Y: "ParamEyeBallY",
|
|
ANGLE_X: "ParamAngleX",
|
|
ANGLE_Y: "ParamAngleY",
|
|
ANGLE_Z: "ParamAngleZ",
|
|
MOUTH_OPEN: "ParamMouthOpenY",
|
|
MOUTH_FORM: "ParamMouthForm",
|
|
CHEEK: "ParamCheek",
|
|
EYE_L_SMILE: "ParamEyeLSmile",
|
|
EYE_R_SMILE: "ParamEyeRSmile",
|
|
BODY_X: "ParamBodyAngleX",
|
|
BREATH: "ParamBreath",
|
|
};
|
|
|
|
// Mapeo de audio tags ElevenLabs → configuración de expresión
|
|
const EXPR_MAP = {
|
|
laughs: { happy: 1.0, cheek: 0.8, eyeSmile: 0.9, duration: 3500 },
|
|
giggles: { happy: 0.8, cheek: 0.6, eyeSmile: 0.7, duration: 2500 },
|
|
excited: { happy: 0.95, cheek: 0.5, eyeSmile: 0.8, duration: 3000 },
|
|
whispers: { happy: 0.15, cheek: 0.0, eyeSmile: 0.0, duration: 2000 },
|
|
sighs: { happy: 0.05, cheek: 0.0, eyeSmile: 0.0, duration: 2000 },
|
|
gasps: { happy: 0.4, cheek: 0.2, eyeSmile: 0.1, duration: 1800 },
|
|
crying: { happy: 0.0, cheek: 0.3, eyeSmile: 0.0, duration: 3000 },
|
|
sarcastic:{ happy: 0.3, cheek: 0.1, eyeSmile: 0.2, duration: 2000 },
|
|
};
|
|
|
|
class Live2DAvatar {
|
|
constructor(canvas) {
|
|
this.canvas = canvas;
|
|
this.W = canvas.width;
|
|
this.H = canvas.height;
|
|
|
|
this.app = null;
|
|
this.model = null;
|
|
this.loaded = false;
|
|
|
|
// Gaze
|
|
this.lookX = 0;
|
|
this.lookY = 0;
|
|
this.targetX = 0;
|
|
this.targetY = 0;
|
|
this.tracking = false;
|
|
|
|
// Idle
|
|
this.idleTimer = 0;
|
|
this.idleX = 0;
|
|
this.idleY = 0;
|
|
|
|
// Mouth
|
|
this.speaking = false;
|
|
this.ttsAnalyser = null;
|
|
this.mouthOpen = 0;
|
|
this.mouthPhase = 0;
|
|
|
|
// Expression
|
|
this.happy = 0;
|
|
this.targetHappy = 0;
|
|
this.exprCheek = 0;
|
|
this.exprEyeSmile = 0;
|
|
this._exprTimer = null;
|
|
|
|
this._lastMs = 0;
|
|
this._tickBound = this._tick.bind(this);
|
|
}
|
|
|
|
// ── Public API ──────────────────────────────────────────
|
|
setLook(x, y) {
|
|
this.targetX = Math.max(-1, Math.min(1, x));
|
|
this.targetY = Math.max(-1, Math.min(1, y));
|
|
this.tracking = true;
|
|
this.idleTimer = 2000;
|
|
}
|
|
clearLook() { this.tracking = false; }
|
|
startSpeaking(analyser = null) {
|
|
this.speaking = true;
|
|
this.ttsAnalyser = analyser;
|
|
if (this.targetHappy < 0.5) this.targetHappy = 0.55;
|
|
}
|
|
stopSpeaking() {
|
|
this.speaking = false;
|
|
this.ttsAnalyser = null;
|
|
this.targetHappy = 0;
|
|
}
|
|
setHappy(v) { this.targetHappy = Math.max(0, Math.min(1, v)); }
|
|
start() {}
|
|
stop() { this.app?.destroy(true); }
|
|
|
|
// ── Expresión por audio tag de ElevenLabs ───────────────
|
|
setExpression(tag) {
|
|
const cfg = EXPR_MAP[tag.toLowerCase()];
|
|
if (!cfg) return;
|
|
|
|
clearTimeout(this._exprTimer);
|
|
|
|
this.targetHappy = cfg.happy;
|
|
this.exprCheek = cfg.cheek;
|
|
this.exprEyeSmile = cfg.eyeSmile;
|
|
|
|
// Intentar reproducir motion del modelo si está disponible
|
|
if (this.model) {
|
|
try { this.model.motion("TapBody"); } catch (_) {}
|
|
}
|
|
|
|
this._exprTimer = setTimeout(() => {
|
|
this.exprCheek = 0;
|
|
this.exprEyeSmile = 0;
|
|
this.targetHappy = this.speaking ? 0.55 : 0;
|
|
}, cfg.duration);
|
|
}
|
|
|
|
// ── Load ────────────────────────────────────────────────
|
|
async load(modelKey = "mao") {
|
|
if (!this.app) {
|
|
this.app = new PIXI.Application({
|
|
view: this.canvas,
|
|
autoStart: true,
|
|
backgroundAlpha: 0,
|
|
width: this.W,
|
|
height: this.H,
|
|
antialias: true,
|
|
});
|
|
PIXI.live2d.Live2DModel.registerTicker(PIXI.Ticker);
|
|
}
|
|
|
|
const entry = MODELS[modelKey] || MODELS.mao;
|
|
console.log(`Cargando modelo: ${entry.name} — ${entry.url}`);
|
|
|
|
const model = await PIXI.live2d.Live2DModel.from(entry.url, {
|
|
autoInteract: false,
|
|
});
|
|
|
|
this.model = model;
|
|
this.app.stage.addChild(model);
|
|
|
|
// Escalar grande — ancla en la esquina superior, cara visible
|
|
model.anchor.set(0.5, 0); // ancla en el borde superior del modelo
|
|
const scaleX = (this.W * 1.9) / model.internalModel.originalWidth;
|
|
const scaleY = (this.H * 1.8) / model.internalModel.originalHeight;
|
|
model.scale.set(Math.min(scaleX, scaleY));
|
|
model.x = this.W / 2;
|
|
// La cara está en el ~15% superior del modelo: empujar hacia abajo
|
|
// para que se vea cara + torso y el cuerpo inferior quede recortado
|
|
const modelDispH = model.scale.y * model.internalModel.originalHeight;
|
|
model.y = -(modelDispH * 0.08); // 8% del modelo sobre el borde superior
|
|
|
|
this.loaded = true;
|
|
this._lastMs = performance.now();
|
|
|
|
this.app.ticker.add(this._tickBound, null, PIXI.UPDATE_PRIORITY.LOW);
|
|
|
|
return model;
|
|
}
|
|
|
|
async switchModel(modelKey) {
|
|
this.app.ticker.remove(this._tickBound);
|
|
if (this.model) {
|
|
this.app.stage.removeChild(this.model);
|
|
this.model.destroy();
|
|
this.model = null;
|
|
this.loaded = false;
|
|
}
|
|
await this.load(modelKey);
|
|
}
|
|
|
|
// ── Tick ────────────────────────────────────────────────
|
|
_tick() {
|
|
if (!this.loaded || !this.model) return;
|
|
|
|
const now = performance.now();
|
|
const dt = Math.min((now - this._lastMs) / 1000, 0.05);
|
|
this._lastMs = now;
|
|
|
|
// Gaze wander
|
|
if (!this.tracking) {
|
|
this.idleTimer -= dt * 1000;
|
|
if (this.idleTimer <= 0) {
|
|
this.idleTimer = 2000 + Math.random() * 3000;
|
|
this.idleX = (Math.random() - 0.5) * 0.45;
|
|
this.idleY = (Math.random() - 0.5) * 0.28;
|
|
}
|
|
this.targetX = this.idleX;
|
|
this.targetY = this.idleY;
|
|
}
|
|
const sp = this.tracking ? 0.09 : 0.04;
|
|
this.lookX += (this.targetX - this.lookX) * sp;
|
|
this.lookY += (this.targetY - this.lookY) * sp;
|
|
|
|
// Mouth — lip-sync con analyser TTS o fallback sinusoidal
|
|
if (this.speaking) {
|
|
let level = 0;
|
|
if (this.ttsAnalyser) {
|
|
const buf = new Uint8Array(this.ttsAnalyser.fftSize);
|
|
this.ttsAnalyser.getByteTimeDomainData(buf);
|
|
let sum = 0;
|
|
for (let i = 0; i < buf.length; i++) {
|
|
const v = (buf[i] - 128) / 128;
|
|
sum += v * v;
|
|
}
|
|
level = Math.min(1, Math.sqrt(sum / buf.length) * 5);
|
|
}
|
|
if (level > 0.04) {
|
|
this.mouthOpen = 0.12 + level * 0.78;
|
|
} else {
|
|
this.mouthPhase += dt * 12;
|
|
this.mouthOpen = 0.25 + Math.abs(Math.sin(this.mouthPhase)) * 0.55;
|
|
}
|
|
} else {
|
|
this.mouthOpen *= 0.82;
|
|
}
|
|
|
|
this.happy += (this.targetHappy - this.happy) * 0.05;
|
|
|
|
// Aplicar al core
|
|
try {
|
|
const fx = this.W / 2 + this.lookX * this.W * 0.3;
|
|
const fy = this.H * 0.28 + this.lookY * this.H * 0.18; // apunta a la cara
|
|
this.model.focus(fx, fy);
|
|
|
|
const core = this.model.internalModel.coreModel;
|
|
core.setParameterValueById(PARAM.MOUTH_OPEN, this.mouthOpen);
|
|
|
|
// Happy base
|
|
const h = this.happy;
|
|
if (h > 0.02) {
|
|
this._tryParam(core, PARAM.MOUTH_FORM, h * 0.6);
|
|
}
|
|
|
|
// Expresión override si está activa
|
|
const eyeS = Math.max(h * 0.5, this.exprEyeSmile);
|
|
const cheek = Math.max(h * 0.35, this.exprCheek);
|
|
|
|
if (eyeS > 0.01) {
|
|
this._tryParam(core, PARAM.EYE_L_SMILE, eyeS);
|
|
this._tryParam(core, PARAM.EYE_R_SMILE, eyeS);
|
|
}
|
|
if (cheek > 0.01) {
|
|
this._tryParam(core, PARAM.CHEEK, cheek);
|
|
}
|
|
} catch (_) {}
|
|
}
|
|
|
|
_tryParam(core, id, val) {
|
|
try { core.setParameterValueById(id, val); } catch (_) {}
|
|
}
|
|
}
|
|
|
|
// Poblar el selector
|
|
document.addEventListener("DOMContentLoaded", () => {
|
|
const sel = document.getElementById("modelSelect");
|
|
if (!sel) return;
|
|
sel.innerHTML = Object.entries(MODELS)
|
|
.map(([k, v]) => `<option value="${k}">${v.name}</option>`)
|
|
.join("");
|
|
});
|
|
|
|
window.LIVE2D_MODELS = MODELS;
|
|
window.Live2DAvatar = Live2DAvatar;
|