import-animation.cjs 4.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Locate complete silhouettes before slicing. Use one scale per animation and a shared motion anchor.
  2. const fs=require('fs'),path=require('path');
  3. const sharp=require('C:/Users/EDY/.cache/codex-runtimes/codex-primary-runtime/dependencies/node/node_modules/sharp');
  4. const root=path.resolve(__dirname,'../..'),art=path.join(root,'Assets/BuffaloSiege/Art');
  5. function components(data,w,h){
  6. const seen=new Uint8Array(w*h),queue=new Int32Array(w*h),out=[];
  7. for(let p=0;p<w*h;p++){
  8. if(seen[p]||data[p*4+3]<32)continue;let head=0,tail=0,count=0,l=w,t=h,r=0,b=0,sx=0,sy=0;queue[tail++]=p;seen[p]=1;
  9. while(head<tail){const i=queue[head++],x=i%w,y=Math.floor(i/w);count++;sx+=x;sy+=y;l=Math.min(l,x);r=Math.max(r,x);t=Math.min(t,y);b=Math.max(b,y);
  10. for(let dy=-1;dy<=1;dy++)for(let dx=-1;dx<=1;dx++){const nx=x+dx,ny=y+dy,n=ny*w+nx;if(nx<0||nx>=w||ny<0||ny>=h||seen[n]||data[n*4+3]<32)continue;seen[n]=1;queue[tail++]=n;}
  11. }
  12. if(count>200)out.push({left:Math.max(0,l-3),top:Math.max(0,t-3),width:Math.min(w-1,r+3)-Math.max(0,l-3)+1,height:Math.min(h-1,b+3)-Math.max(0,t-3)+1,count,cx:sx/count,cy:sy/count});
  13. }
  14. return out.sort((a,b)=>Math.floor(a.cy/(h/2))-Math.floor(b.cy/(h/2))||a.cx-b.cx);
  15. }
  16. async function main(){
  17. const report=[];const previews=[];
  18. for(const id of ['rabbit','bison','wolf','vulture','scorpion','king']){
  19. const source=path.join(__dirname,'sources/animation.'+id+'.png');const {data,info}=await sharp(source).ensureAlpha().raw().toBuffer({resolveWithObject:true});
  20. const boxes=components(data,info.width,info.height);if(boxes.length!==6)throw Error(id+' expected six isolated silhouettes; found '+boxes.length);
  21. const anchors=boxes.map((b,i)=>({x:b.cx,y:b.top+b.height+(id==='rabbit'?[0,12,30,14,0,0][i]:0)}));
  22. if(id==='vulture')[[300,365],[790,365],[1280,365],[285,800],[790,828],[1300,830]].forEach(([x,y],i)=>anchors[i]={x,y});
  23. const left=Math.max(...boxes.map((b,i)=>anchors[i].x-b.left)),right=Math.max(...boxes.map((b,i)=>b.left+b.width-anchors[i].x));
  24. const top=Math.max(...boxes.map((b,i)=>anchors[i].y-b.top)),bottom=Math.max(...boxes.map((b,i)=>b.top+b.height-anchors[i].y));
  25. const scale=Math.min(222/(left+right),222/(top+bottom));const targetX=128+(left-right)*scale/2,targetY=128+(top-bottom)*scale/2;
  26. const folder=path.join(art,'EnemyAnimations',id);fs.mkdirSync(folder,{recursive:true});
  27. const frameBuffers=[];
  28. for(let i=0;i<6;i++){
  29. const b=boxes[i],crop={left:b.left,top:b.top,width:b.width,height:b.height};const input=await sharp(source).extract(crop).resize(Math.round(b.width*scale),Math.round(b.height*scale),{fit:'fill'}).png().toBuffer();
  30. const x=Math.round(targetX-(anchors[i].x-b.left)*scale),y=Math.round(targetY-(anchors[i].y-b.top)*scale);
  31. const frame=await sharp({create:{width:256,height:256,channels:4,background:'#00000000'}}).composite([{input,left:x,top:y}]).png().toBuffer();
  32. fs.writeFileSync(path.join(folder,'frame-'+String(i).padStart(2,'0')+'.png'),frame);frameBuffers.push(frame);
  33. previews.push({input:await sharp(frame).resize(150).toBuffer(),left:i*160+5,top:report.length*182+27});
  34. }
  35. fs.writeFileSync(path.join(art,'enemy.'+id+'.png'),frameBuffers[0]);fs.copyFileSync(path.join(art,'enemy.'+id+'.png'),path.join(root,'docs/ui-style-a/assets/sprites/enemy.'+id+'.png'));
  36. report.push({id,frameCount:6,fps:id==='rabbit'?12:id==='vulture'||id==='king'?8:10,canvas:[256,256],pivot:[.5,.5],source:'sources/animation.'+id+'.png',scale,anchors,sourceRects:boxes,files:Array.from({length:6},(_,i)=>'EnemyAnimations/'+id+'/frame-'+String(i).padStart(2,'0')+'.png')});
  37. }
  38. const labels=Buffer.from('<svg width="960" height="1092">'+report.map((r,i)=>'<text x="8" y="'+(i*182+20)+'" fill="#ffe9ba" font-family="Arial" font-size="16">'+r.id+' · '+r.fps+' FPS · 6 frames</text>').join('')+'</svg>');
  39. await sharp({create:{width:960,height:1092,channels:4,background:'#523e51'}}).composite([...previews,{input:labels}]).png().toFile(path.join(__dirname,'enemy-frames.png'));
  40. fs.writeFileSync(path.join(__dirname,'animation-manifest.json'),JSON.stringify(report,null,2)+'\n');
  41. for(const file of [path.join(art,'asset-manifest.json'),path.join(root,'docs/ui-style-a/asset-manifest.json')]){
  42. const doc=JSON.parse(fs.readFileSync(file));for(const a of report){const e=doc.assets.find(x=>x.id==='enemy.'+a.id);for(const key of ['atlas','grid','cellIndex','sourceRect','uv'])delete e[key];Object.assign(e,{width:256,height:256,transparentPadding:[16,16,16,16],source:'docs/art-refresh-2026-10-29/sources/animation.'+a.id+'.png',sourceRoot:'project',revision:'2026-10-29',animation:a.files,animationFps:a.fps});}
  43. doc.enemyAnimationManifest='docs/art-refresh-2026-10-29/animation-manifest.json';fs.writeFileSync(file,JSON.stringify(doc,null,2)+'\n');
  44. }
  45. console.log(report.map(x=>({id:x.id,frames:x.frameCount,scale:x.scale,boxes:x.sourceRects.map(b=>[b.left,b.top,b.width,b.height])})));
  46. }
  47. main().catch(e=>{console.error(e);process.exit(1);});