Created
January 26, 2024 07:34
-
-
Save NovaSagittarii/640633e46e4cab3a53990e48d3e3d226 to your computer and use it in GitHub Desktop.
Experimental code snippets to transfer sprite animations between similar sprites, tested on https://flashmercurymcfly.github.io/Arknights-SD-Viewer/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// helper scripts (mostly just for the SD interactions for testing the animation transfer) | |
const C = viewer.app.stage.children; | |
function generateTransposition(target, source){ | |
const transposition = []; // output | |
const collisions = {}; | |
for(let i = target.length-1; i >= 0; i --) collisions[target[i].name] = i; | |
for(let i = source.length-1; i >= 0; i --){ | |
const j = collisions[source[i].name]; | |
if(j === undefined) continue; | |
transposition[i] = j; | |
} | |
return transposition; | |
} | |
function arrayAsObjectKeys(a){ | |
const v = {}; | |
a.forEach(k => v[k] = true); | |
return v; | |
} | |
function transferAnimation(target, source, animationName, defaultAnimationName){ | |
const anim = source.skeleton.data.animations.filter(a => a.name === animationName)[0]; | |
if(anim === undefined) throw `${animationName} not found on source`; | |
const slotTranspose = generateTransposition(target.skeleton.data.slots, source.skeleton.data.slots); // SOURCE => TARGET | |
const boneTranspose = generateTransposition(target.skeleton.data.bones, source.skeleton.data.bones); | |
const ikTranspose = generateTransposition(target.skeleton.data.ikConstraints, source.skeleton.data.ikConstraints); | |
console.log(slotTranspose, boneTranspose, ikTranspose); // DEBUG | |
let defaultTimelines = defaultDrawOrder = [], defaultAttachmentTimelines = []; | |
if(defaultAnimationName){ | |
const anim = target.skeleton.data.animations.filter(a => a.name === defaultAnimationName)[0]; | |
if(anim === undefined) throw `${defaultAnimationName} not found on target to use as template`; | |
const sT = arrayAsObjectKeys(slotTranspose); // just TARGET used indexes | |
const bT = arrayAsObjectKeys(boneTranspose); | |
const ikT = arrayAsObjectKeys(ikTranspose); | |
defaultTimelines = anim.timelines.map(t => { | |
const e = Object.assign(Object.create(Object.getPrototypeOf(t)), t); | |
switch(t.constructor.name){ | |
case "AttachmentTimeline": | |
// we dont talk about these lol | |
defaultAttachmentTimelines[e.slotIndex] = true; | |
break; | |
case "ColorTimeline": | |
case "DeformTimeline": | |
if(sT[e.slotIndex]) return null; | |
break; | |
case "DrawOrderTimeline": | |
console.log(t); | |
defaultDrawOrder = t.drawOrders.slice(0).filter(e => sT[e]); | |
return null; | |
break; | |
case "IkConstraintTimeline": | |
if(ikT[e.ikConstraintIndex]) return null; | |
break; | |
case "RotateTimeline": | |
case "ScaleTimeline": | |
case "ShearTimeline": | |
case "TranslateTimeline": | |
if(bT[e.boneIndex]) return null; | |
break; | |
default: console.warn(e); | |
} | |
return e; | |
}).filter(e => e); | |
console.log([sT, bT, ikT].map(k=>Object.keys(k)), defaultTimelines); // DEBUG | |
} | |
target.skeleton.data.animations.push(new anim.constructor(anim.name, anim.timelines.map(t => { | |
const e = Object.assign(Object.create(Object.getPrototypeOf(t)), t); | |
switch(t.constructor.name){ | |
case "AttachmentTimeline": | |
case "ColorTimeline": | |
case "DeformTimeline": | |
if(t.constructor.name === "AttachmentTimeline" && defaultAttachmentTimelines[slotTranspose[e.slotIndex]]) return null; | |
if((e.slotIndex = slotTranspose[e.slotIndex]) === undefined) return null; | |
break; | |
case "DrawOrderTimeline": | |
{ | |
/*const o = []; | |
const d1 = t.drawOrders.slice(0).map(e => slotTranspose[e]).filter(e => e!==undefined); | |
const d2 = defaultDrawOrder.slice(0); | |
console.log(d1, d2); | |
while(d1.length && d2.length){ | |
const e1 = d1.splice(0, 1)[0]; | |
o.push(e1); console.log(e1); | |
let e2; | |
while(d2.length && e1 != (e2 = d2.splice(0, 1)[0])){ | |
o.push(e2); console.log(e2); | |
} | |
} | |
e.drawOrders = o.concat(d1, d2);*/ // t.drawOrders.slice(0).map(e => slotTranspose[e]).filter(e => e!==undefined).concat(defaultDrawOrder); | |
} | |
break; | |
case "IkConstraintTimeline": | |
if((e.ikConstraintIndex = ikTranspose[e.ikConstraintIndex]) === undefined) return null; | |
break; | |
case "RotateTimeline": | |
case "ScaleTimeline": | |
case "ShearTimeline": | |
case "TranslateTimeline": | |
if((e.boneIndex = boneTranspose[e.boneIndex]) === undefined) return null; | |
break; | |
default: console.warn(e); | |
} | |
return e; | |
}).filter(e => e).concat(defaultTimelines), anim.duration)); | |
} | |
// this part should be rerun when testing | |
// this assumes C[1] (first element) is the target (typically Front) | |
// and C[2] (second element) is the source (typically Dorm) | |
transferAnimation(C[1], C[2], "Move", "Idle"); | |
C.slice(1).forEach(s => s.state.setAnimation(0, "Move", true)); // tihis just syncs them together |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
These were cooked up sometime in mid November 2020.