well, that's some work, you know?
(Your mix of reading from
mfile and
sfile is a little bit confusing.)
I modified your script (for reading the header
after @0007 I used
mfile only).
It works with the parrot now (first submesh only) and there are some weird faces. Didn't check this in depth, no time, sry.
Parrot.JPG
You can use that modified script for the raven, too.
But you'll have to change this line:
fseek mfile 22#seek_cur -- 21 for the raven!
and to delete this one:
fseek mfile 71439#seek_set -- delete this line for the raven!
It's up to you how to solve this "22/21" issue to make the script useable for both single and multi mesh files.
Also a loop for getting the submeshes has to be built in.
Keep in mind that for the
parrot the VertexStride of the 2nd submesh seems to be 32?
Also to be checked whether
ObjectPrimitiveCount always having the same value or needs an array, too.
Code: Select all
--Short to 16bit Half float conversion function
fn readHalfFloat fstream = (
hf=readshort fstream #unsigned
sign = bit.get hf 16
exponent = (bit.shift (bit.and hf (bit.hexasint "7C00")) -10) as integer - 16
fraction = bit.and hf (bit.hexasint "03FF")
if sign==true then sign = 1 else sign = 0
exponentF = exponent + 127
outputAsFloat = bit.or (bit.or (bit.shift fraction 13) \
(bit.shift exponentF 23)) (bit.shift sign 31)
return bit.intasfloat outputasfloat*2
)
clearlistener()
meshdata_filename = getOpenFileName caption:"Import meshdata File" types:"meshdataFile (*.meshdata)|*.meshdata|All Files (*.*)|*.*"
if meshdata_filename != undefined then mFile = fopen meshdata_filename "rb"
sfile=openfile meshdata_filename mode:"r+"
Vert_Positions_array=#()
Vert_Tangents_array=#()
Vert_BiNormals_array=#()
Vert_Diffuse_Array=#()
Vert_Normal_Array=#()
Vert_Bone_Indices_array=#()
Vert_Blend_Weights_Array=#()
Face_array=#()
BoneIndexSameBool=false
model_Scale = 39
ObjectType = "ObjectType " + readbyte mfile as string
ObjectCountOffset = fseek mfile 6 #seek_set
--ObjectCount = "ObjectCount " + readbyte mfile as string
ObjectCount = readbyte mfile
ObjectNameOffset = seek sfile 7
ObjectVertCount= #()
VertexStride= #()
for i = 1 to ObjectCount do
(
ObjectName= readString mfile --read zero terminated string
ObjectPrimitiveCount = readshort mfile
unk1 = readshort mfile -- high word of ObjectPrimitiveCount?
ObjectVertCount[i] = readlong mfile
fseek mfile 8#seek_cur
VertexStride[i] = readbyte mfile
fseek mfile 37#seek_cur
format "%, %, %, %\n" ObjectName ObjectPrimitiveCount ObjectVertCount[i] VertexStride[i]
)
fseek mfile 22#seek_cur -- 21 for the raven!
FaceIndiceCount =((readlong mfile)/2)
unk2 = readlong mfile
addr= ftell mfile
format "fIndCount: % addr: %\n" FaceIndiceCount addr
--In this section the script reads out data from VertexBlock and appends it to arrays
for vb_read_array = 1 to ObjectVertCount[1] do
(
--Read vertex positions
try(
vpx = readhalffloat mfile
vpy = readhalffloat mfile
vpz = readhalffloat mfile
--format "% % %\n" vpx vpy vpz
for i = 1 to 38 do readbyte mfile
ftell mfile
if
vpx != Undefined and vpy!=undefined and vpz!=undefined do
(
append Vert_Positions_array [(vpx*model_scale),(vpy*model_scale),(vpz*model_scale)]
)
)
catch
if vb_read_array == 1 do
(print"Vert_Positions error"
messagebox "Vert_Positions error")
--Read diffuse UV coordinates
try(
v_diff_U = ReadHalfFloat mfile
v_diff_V = ReadHalfFloat mfile
if
v_diff_U !=Undefined and v_diff_V!=Undefined do
(
append Vert_diffuse_Array [v_diff_U,v_diff_V,0]
)
)
catch
if vb_read_array == 1 do print"Vert_diffuse error"
)
--Read face indices
fseek mfile 71439#seek_set -- delete this line for the raven!
print ("Position before f-indice`s "+((ftell mFile)as string))
for x = 1 to ((FaceIndiceCount/3)) do
(
fa=readshort mFile #unsigned+1
fb=readshort mFile #unsigned+1
fc=readshort mFile #unsigned+1
--format "% % %\n" fa fb fc
append Face_array[fa,fb,fc]
)
--Build Mesh From Arrays
mesh1 = mesh vertices:Vert_Positions_array faces:Face_array
mesh1.numTVerts = Vert_Diffuse_array.count
mesh1.name = objectname
meshop.setNumMaps mesh1 3 keep:false
channelInfo.Dialog ()
channelInfo.NameChannel mesh1 3 1 "Diffuse"
channelInfo.NameChannel mesh1 3 2 "Normal"
buildTVFaces mesh1
meshop.setMapSupport mesh1 1 true
meshop.setMapSupport mesh1 2 true
for j = 1 to Vert_Diffuse_array.count do meshop.setMapVert mesh1 1 j Vert_Diffuse_array[j]
for j = 1 to Face_array.count do meshop.setMapFace mesh1 1 j Face_array[j]
for j = 1 to Vert_Normal_array.count do meshop.setMapVert mesh1 2 j Vert_Normal_array[j]
for j = 1 to Face_array.count do meshop.setMapFace mesh1 2 j Face_array[j]
mesh1.name = objectname
--flip UV`s, apply material/name, add checker map to the diffuse map.
select mesh1
modPanel.addModToSelection (UVW_Xform ()) ui:on
mesh1.modifiers[#UVW_Xform].V_Flip = 1
maxOps.CollapseNode mesh1 off
modPanel.addModToSelection (UVW_Xform ()) ui:on
mesh1.modifiers[#UVW_Xform].Map_Channel = 2
mesh1.modifiers[#UVW_Xform].V_Flip = 1
maxOps.CollapseNode mesh1 off
mesh1.material = standardMaterial()
mesh1.material.name = objectname
mesh1.material.showInViewport = on
rotate mesh1(angleaxis 90 [1,0,0])
Vert_Diffuse_array[1024]
fclose mfile
fclose sfile