maxscript
Код
-- RE4 UHD BIN MODEL IMPORT BIN CODEMAN 2014 (TESTED ON 3DSMAX 2009 ONLY)
if (heapSize < 200000) then
heapSize = 2000000 -- allow ~ 40 MB instead of just 7.5 MB. Prevents "Runtime Error: Out of scripter memory"
clearlistener()
obj=$*;delete obj
fname = getOpenFileName \
caption:" [PC]RE4 UHD Model File" \
types:"All Files (*.*)|*.*|RE4 UHD Model File(*.bin)|*.bin" \
historyCategory:"RE4UHDObjectPresets"
fn ReadBElong fstream = (
bit.swapBytes (bit.swapBytes (readlong fstream #unsigned) 1 4) 2 3
)
fn ReadBEfloat fstream = (
bit.intAsFloat (bit.swapBytes (bit.swapBytes (readlong fstream #unsigned) 1 4) 2 3)
)
fn CALCBEUCOORD fstream = (
fl= (Readlong fstream ) as float
if fl>=(0xFFFF/2) do fl-=(0xFFFF);fl)
--(fl/0x10)
fn CALCBEuTCOORD fstream = (
fl=(ReadBElong fstream )/32768.0
if fl>=1.0 do fl-=2.0;fl
)
fn CALCBEUhTCOORD fstream = (
(readshort fstream #unsigned)
fl=((bit.swapBytes (readshort fstream #unsigned) 1 2)as float)/32767.5
if fl>=1 do fl-=2;fl
)
fn CALCBETCOORD fstream = (
fl=((bit.swapBytes (readshort fstream #unsigned) 1 2)as float)/32767.5
if fl>=1 do fl-=2;fl
)
fn CALCTCOORD fstream = (
fl=((readshort fstream #unsigned) as float)/32767.5
if fl>=1 do fl-=2;fl
)
fn paddstring len instring = (
local str="";if instring.count <=len then(
for i = 1 to (len-instring.count) do(str+="0")
str = (str+instring))else(
for i = 1 to len do(str+="0";str[i]=instring[i]));str)
fn uppercase instring = (
local upper, lower, outstring
-- upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
-- lower="abcdefghijklmnopqrstuvwxyz"
upper="ABCDEF"
lower="abcdef"
outstring=copy instring
for i=1 to outstring.count do (
j=findString lower outstring
[i]if (j != undefined) do outstring[i]=upper[j])
outstring)
fn ReadFixedString bstream fixedLen = (
local str = ""
for i = 1 to fixedLen do (
str += bit.intAsChar (ReadByte bstream #unsigned))
str
)
struct obj_entry (offset,size)
struct bone_entry (index,parent,position,matrix,bool)
struct weight_palette (count, weights, boneids)
boneArray = (bone_entry index:#() parent:#() position:#() matrix:#() bool:#())
objArray = (obj_entry offset:#() size:#())
boneNames = #()
f = fopen fname "rb"
a = (Readlong f) --headersize
b = (Readlong f) --zeros
c = (Readlong f) --version ? maybe two words instead ?
if (a==96) and (b==0) and (c == 80) do --check header quickly
(
fseek f 16 #seek_set
uvpos=(Readlong f)
weightaroffset=(Readlong f)
weightcount=(Readbyte f)
bonecount=(Readbyte f) --must be +1
grpcount=(readshort f #unsigned)
grppos=(Readlong f)
unknowa=(Readlong f) --wtf is that ? may be for morph target support, seems unused as far as itested models
matcount=(Readlong f)
(readshort f #unsigned) --- 4 ?
(readshort f #unsigned) --same as weightcount
(Readlong f) --zeros ??
fseek f 48 #seek_set
vpos=(Readlong f)
npos=(Readlong f)
arvecount=(readshort f #unsigned)
aruvcount=(readshort f #unsigned)
nn=(Readlong f)
fseek f 64 #seek_set
palentriesoffset=(Readlong f)
matrixpaletteoffset=(Readlong f)
vwidarpos=(Readlong f) --vertex weights id's array (2 words )* numvertex
vwarpos=(Readlong f) --vertex weights array (2 words )* numvertex
Vert_array = #()
Normal_array = #()
UV_array = #()
fseek f vpos #seek_set
for x =1 to arvecount do
(
tu = (Readfloat f)
tw = (Readfloat f)
tv = (Readfloat f)
append Vert_array [tu,tv,tw] --save UVs to UV_array
)
fseek f npos #seek_set
--not sure this is normals, can be weights or color or wtf else ?
for x =1 to arvecount do
(
nx = (CALCBEUCOORD f)
nz = (CALCBEUCOORD f)
ny = (CALCBEUCOORD f)
append Normal_array [nx,ny,nz]
)
fseek f uvpos #seek_set
for x =1 to aruvcount do
(
tu = (readfloat f)
tv = (readfloat f)
append UV_array [tu,tv,0]
)
fseek f grppos #seek_set
fcount=0
ftype=0
ucount=1
for ii=1 to grpcount do
(
Face_array = #()
fseek f 12 #seek_cur --full zero's
matid =(readbyte f)
fseek f 11 #seek_cur
dtsize=(readlong f)
totalgrpfaces=(readlong f)
dtcount=(readlong f)
reste=dtsize-4-(dtcount*4)
for x=1 to dtcount do
(
ftype = (readshort f #unsigned)
fcount = (readshort f #unsigned)
bkface=-1
--TODO : maybe add support for triangle fan if any model use it ?
if ftype == 5 do --trianglelist
(
a= ucount
for y = 1 to (fcount/3) do
(
p1 = a
p2 = a+1
p3 = a+2
a = a + 3
ucount = ucount + 3
if p1<arvecount and p2<arvecount and p3<arvecount do
(
if (p1 != p2) and (p1 != p3) and (p2 != p3) do
(
append Face_array [(p1),(p3),(p2)]
)
)
)
)
if ftype == 6 do --tristrip
(
bkface=-1
a= ucount
n=0
p1 = a
p2 = a+1
p3 = a+2
a = a + 3
bkface=bkface*-1 --reboot face strip for each strips grrrr
ucount = ucount + 3
if p1<arvecount and p2<arvecount and p3<arvecount do
(
append Face_array [(p1),(p3),(p2)]
)
for y = 2 to fcount-2 do
(
bkface = bkface * -1
p1 = p2
p2 = p3
p3 = a
a=a+1
ucount = ucount + 1
if p1<arvecount and p2<arvecount and p3<arvecount do
(
if (p1 != p2) and (p1 != p3) and (p2 != p3) do
(
if bkface==1 do
(
append Face_array [(p1),(p3),(p2)]
)
if bkface==-1 do
(
append Face_array [(p1),(p2),(p3)]
)
)
)
)
)
if ftype == 8 do --quad list
(
a= ucount
for y = 1 to (fcount/4) do
(
p1 = a
p2 = a+1
p3 = a+2
p4 = a+3
a = a + 4
ucount = ucount + 4
if p1<arvecount and p2<arvecount and p3<arvecount do
(
if (p1 != p2) and (p1 != p3) and (p2 != p3) do
(
append Face_array [(p1),(p3),(p2)]
append Face_array [(p1),p4,(p3)]
)
)
)
)
)
if reste>0 do
(
fseek f reste #seek_cur
)
msh = mesh vertices:Vert_array faces:Face_array --build mesh
msh.numTVerts = UV_array.count
buildTVFaces msh
for j = 1 to UV_array.count do setTVert msh j UV_array
[j]for j = 1 to Face_array.count do setTVFace msh j Face_array
[j]for j = 1 to Normal_array.count do setNormal msh j Normal_array
[j])
)--end if a&b&c
fclose f