70 lines
5.6 KiB
Python
70 lines
5.6 KiB
Python
|
|
from pathlib import Path
|
||
|
|
import struct,json,copy,numpy as np
|
||
|
|
src=Path('Demon_v0.19/art/succubus/succubus_v01.glb');raw=src.read_bytes();length=struct.unpack_from('<I',raw,12)[0];doc=json.loads(raw[20:20+length]);blob=bytearray(raw[28+length:]);components=json.loads(Path('tools/succubus_components.json').read_text())
|
||
|
|
types={5126:'<f4',5125:'<u4',5123:'<u2',5121:'u1'}; sizes={'SCALAR':1,'VEC2':2,'VEC3':3,'VEC4':4,'MAT4':16}
|
||
|
|
def read(i):
|
||
|
|
a=doc['accessors'][i];v=doc['bufferViews'][a['bufferView']];return np.frombuffer(blob,types[a['componentType']],a['count']*sizes[a['type']],v.get('byteOffset',0)+a.get('byteOffset',0)).reshape(-1,sizes[a['type']]).copy()
|
||
|
|
def accessor(data,typ,component=5126):
|
||
|
|
data=np.asarray(data,dtype=types[component]);blob.extend(b'\0'*((-len(blob))%4));offset=len(blob);blob.extend(data.tobytes());view=len(doc['bufferViews']);doc['bufferViews'].append({'buffer':0,'byteOffset':offset,'byteLength':data.nbytes});a={'bufferView':view,'componentType':component,'count':len(data),'type':typ}
|
||
|
|
if typ=='VEC3':a.update(min=data.min(axis=0).tolist(),max=data.max(axis=0).tolist())
|
||
|
|
doc['accessors'].append(a);return len(doc['accessors'])-1
|
||
|
|
skin_mat=len(doc['materials']);doc['materials'].append({'name':'Bare thighs - warm skin','pbrMetallicRoughness':{'baseColorFactor':[.60,.36,.28,1],'metallicFactor':0,'roughnessFactor':.64}})
|
||
|
|
boot_mat=len(doc['materials']);doc['materials'].append({'name':'Thigh high oxblood black leather','pbrMetallicRoughness':{'baseColorFactor':[.032,.015,.022,1],'metallicFactor':.14,'roughnessFactor':.31}})
|
||
|
|
# Shoulder narrowing deforms the real mesh, leaving facial features and wings intact.
|
||
|
|
def narrow(v,mat):
|
||
|
|
v=v.copy()
|
||
|
|
if mat in [2,11,12,13,14,15]:return v
|
||
|
|
x=v[:,0];y=v[:,1];weight=np.clip((np.abs(x)-.105)/.14,0,1)*np.clip((1.52-y)/.13,0,1)*np.clip((y-.92)/.26,0,1)
|
||
|
|
v[:,0]-=np.sign(x)*.037*weight
|
||
|
|
return v
|
||
|
|
new=[];stats={}
|
||
|
|
for original in doc['meshes'][1]['primitives']:
|
||
|
|
p=copy.deepcopy(original);mat=p['material'];v=read(p['attributes']['POSITION']);faces=read(p['indices']).reshape(-1,3).astype(np.int64);keep=np.ones(len(faces),bool)
|
||
|
|
labels=np.array(components[str(mat)]['labels']);drop=set()
|
||
|
|
for root in np.unique(labels):
|
||
|
|
part=v[labels==root];lo=part.min(axis=0);hi=part.max(axis=0)
|
||
|
|
if mat==5:drop.add(root) # broad metal shoulder pads
|
||
|
|
if mat in [3,10] and hi[1]<1.05 and lo[1]>.90 and max(abs(lo[0]),abs(hi[0]))<.3:drop.add(root)
|
||
|
|
if mat==0 and lo[1]>.11 and hi[1]<.66 and max(abs(lo[0]),abs(hi[0]))<.3:drop.add(root) # replace knee boots with fitted thigh shafts
|
||
|
|
if drop:keep&=~np.isin(labels[faces[:,0]],list(drop))
|
||
|
|
v=narrow(v,mat)
|
||
|
|
if mat==3:
|
||
|
|
center=np.abs(v[:,0])<.25
|
||
|
|
# Keep the fitted garment outside the original torso shell.
|
||
|
|
upper=center&(v[:,1]>1.33);v[upper,1]-=.025*np.clip((v[upper,1]-1.33)/.05,0,1)
|
||
|
|
if mat in [0,10]:
|
||
|
|
foot=(v[:,1]<.17)&(np.abs(v[:,0])<.3)
|
||
|
|
v[foot,1]+=.087*np.clip((.14-v[foot,2])/.14,0,1)*np.clip((.17-v[foot,1])/.13,0,1)
|
||
|
|
p['attributes']['POSITION']=accessor(v,'VEC3')
|
||
|
|
faces=faces[keep]
|
||
|
|
# Stable normals for the deformed bind mesh.
|
||
|
|
normals=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(normals,faces[:,k],cross)
|
||
|
|
normals/=np.maximum(np.linalg.norm(normals,axis=1,keepdims=True),1e-12);p['attributes']['NORMAL']=accessor(normals,'VEC3');p['attributes'].pop('TANGENT',None)
|
||
|
|
if len(faces)==0:continue
|
||
|
|
if mat==1:
|
||
|
|
y=v[faces].mean(axis=1)[:,1];x=v[faces].mean(axis=1)[:,0]
|
||
|
|
skin=(y>.72)&(y<.86)&(np.abs(x)<.3)
|
||
|
|
boots=(y>.115)&(y<=.72)&(np.abs(x)<.3)
|
||
|
|
for mask,material in [(skin,skin_mat),(boots,boot_mat),(~(skin|boots),mat)]:
|
||
|
|
part=copy.deepcopy(p);part['material']=material;part['indices']=accessor(faces[mask].astype('<u4').ravel().reshape(-1,1),'SCALAR',5125);new.append(part)
|
||
|
|
body=p;body_v=v
|
||
|
|
else:p['indices']=accessor(faces.astype('<u4').ravel().reshape(-1,1),'SCALAR',5125);new.append(p)
|
||
|
|
stats[mat]=int(len(faces))
|
||
|
|
# Real tapered heels, skinned to the same foot weights as the original model.
|
||
|
|
body_j=read(body['attributes']['JOINTS_0']);body_w=read(body['attributes']['WEIGHTS_0'])
|
||
|
|
for sign in [-1,1]:
|
||
|
|
center=np.array([sign*.175,.085,-.009]);nearest=np.argmin(np.sum((body_v-center)**2,axis=1));verts=[];faces=[]
|
||
|
|
for y,r in [(.012,.011),(.025,.012),(.095,.027),(.116,.030)]:
|
||
|
|
for i in range(24):a=i*np.pi*2/24;verts.append([center[0]+np.cos(a)*r,y,center[2]+np.sin(a)*r])
|
||
|
|
for ring in range(3):
|
||
|
|
for i in range(24):a=ring*24+i;b=ring*24+(i+1)%24;c=b+24;d=a+24;faces.extend([[a,b,c],[a,c,d]])
|
||
|
|
verts=np.array(verts);faces=np.array(faces);normals=np.zeros_like(verts);cross=np.cross(verts[faces[:,1]]-verts[faces[:,0]],verts[faces[:,2]]-verts[faces[:,0]])
|
||
|
|
for k in range(3):np.add.at(normals,faces[:,k],cross)
|
||
|
|
normals/=np.maximum(np.linalg.norm(normals,axis=1,keepdims=True),1e-12)
|
||
|
|
attrs={'POSITION':accessor(verts,'VEC3'),'NORMAL':accessor(normals,'VEC3'),'JOINTS_0':accessor(np.tile(body_j[nearest],(len(verts),1)),'VEC4',5123),'WEIGHTS_0':accessor(np.tile(body_w[nearest],(len(verts),1)),'VEC4')}
|
||
|
|
new.append({'attributes':attrs,'material':boot_mat,'indices':accessor(faces.astype('<u4').ravel().reshape(-1,1),'SCALAR',5125)})
|
||
|
|
doc['meshes'][1]['primitives']=new;doc['meshes'][1]['name']='Succubus - narrow shoulders, corset, thigh high heels';doc['buffers'][0]['byteLength']=len(blob)
|
||
|
|
j=json.dumps(doc,separators=(',',':'),ensure_ascii=True).encode();j+=b' '*((-len(j))%4);blob.extend(b'\0'*((-len(blob))%4));out=struct.pack('<III',0x46546c67,2,12+8+len(j)+8+len(blob))+struct.pack('<II',len(j),0x4e4f534a)+j+struct.pack('<II',len(blob),0x004e4942)+blob
|
||
|
|
Path('Demon_v0.19/art/succubus/succubus_v02.glb').write_bytes(out);print('Saved v02',len(out),'bytes; unchanged skin, skeleton and animation channels; materials',len(doc['materials']))
|