23 lines
2.3 KiB
Python
23 lines
2.3 KiB
Python
|
|
from pathlib import Path
|
||
|
|
import struct,json,numpy as np,hashlib
|
||
|
|
outdir=Path('Demon_v0.19/art/souls_v04');outdir.mkdir(exist_ok=True);report=[]
|
||
|
|
for kind in range(5,10):
|
||
|
|
raw=Path(f'Demon_v0.19/art/souls_v03/soul_{kind:02}.glb').read_bytes();length=struct.unpack_from('<I',raw,12)[0];doc=json.loads(raw[20:20+length]);blob=bytearray(raw[28+length:]);changed=0
|
||
|
|
def read(i):
|
||
|
|
a=doc['accessors'][i];v=doc['bufferViews'][a['bufferView']];return np.frombuffer(blob,{5126:'<f4',5125:'<u4',5123:'<u2',5121:'u1'}[a['componentType']],a['count']*{'SCALAR':1,'VEC2':2,'VEC3':3,'VEC4':4,'MAT4':16}[a['type']],v.get('byteOffset',0)+a.get('byteOffset',0)).reshape(a['count'],-1).copy()
|
||
|
|
def add(v):
|
||
|
|
v=np.asarray(v,dtype='<f4');blob.extend(b'\0'*((-len(blob))%4));offset=len(blob);blob.extend(v.tobytes());doc['bufferViews'].append({'buffer':0,'byteOffset':offset,'byteLength':v.nbytes});doc['accessors'].append({'bufferView':len(doc['bufferViews'])-1,'componentType':5126,'count':len(v),'type':'VEC3','min':v.min(axis=0).tolist(),'max':v.max(axis=0).tolist()});return len(doc['accessors'])-1
|
||
|
|
for mesh in doc['meshes']:
|
||
|
|
for p in mesh['primitives']:
|
||
|
|
original=read(p['attributes']['POSITION']);v=original.copy();y=v[:,1]
|
||
|
|
upper=np.clip((y-.94)/.22,0,1)*(1-np.clip((y-1.42)/.12,0,1));v[:,0]*=1-.14*upper
|
||
|
|
v[:,2]=.025+(v[:,2]-.025)*(1-.12*upper)
|
||
|
|
if np.max(np.abs(v-original))<1e-6:continue
|
||
|
|
p['attributes']['POSITION']=add(v);p['attributes'].pop('TANGENT',None)
|
||
|
|
faces=read(p['indices']).reshape(-1,3).astype(int) if 'indices' in p else np.arange(len(v)).reshape(-1,3)
|
||
|
|
n=np.zeros_like(v);cross=np.cross(v[faces[:,1]]-v[faces[:,0]],v[faces[:,2]]-v[faces[:,0]])
|
||
|
|
for k in range(3):np.add.at(n,faces[:,k],cross)
|
||
|
|
n/=np.maximum(np.linalg.norm(n,axis=1,keepdims=True),1e-12);p['attributes']['NORMAL']=add(n);changed+=len(v)
|
||
|
|
doc['buffers'][0]['byteLength']=len(blob);j=json.dumps(doc,separators=(',',':')).encode();j+=b' '*((-len(j))%4);blob.extend(b'\0'*((-len(blob))%4));out=struct.pack('<III',0x46546c67,2,28+len(j)+len(blob))+struct.pack('<II',len(j),0x4e4f534a)+j+struct.pack('<II',len(blob),0x004e4942)+blob;p=outdir/f'soul_{kind:02}.glb';p.write_bytes(out);report.append({'kind':kind,'modified_vertices':changed,'chest_width_factor':.86,'chest_depth_factor':.88,'sha256':hashlib.sha256(out).hexdigest()})
|
||
|
|
(outdir/'manifest.json').write_text(json.dumps(report,indent=2));print(report)
|