123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893 |
- -------------------------------------------------------------------------------------
- -- ThreeJSExporter.ms
- -- Exports geometry from 3ds max to Three.js models in ASCII JSON format
- -- By alteredq / http://alteredqualia.com
- -------------------------------------------------------------------------------------
- rollout ThreeJSExporter "ThreeJSExporter"
- (
- -- Variables
- local ostream,
- headerFormat = "// Converted from: %
- // vertices: %
- // normals: %
- // uvs: %
- // triangles: %
- // materials: %
- //
- // Generated with 3ds max ThreeJSExporter
- // http://github.com/alteredq/three.js/blob/master/utils/exporters/max/ThreeJSExporter.ms
- ",
- vertexFormat = "%,%,%",
- vertexNormalFormat = "%,%,%",
- UVFormat = "%,%",
- triFormat = "%,%,%,%",
- triUVFormat = "%,%,%,%,%,%,%",
- triNFormat = "%,%,%,%,%,%,%",
- triUVNFormat = "%,%,%,%,%,%,%,%,%,%",
- footerFormat = "}\n\npostMessage( model );"
- -------------------------------------------------------------------------------------
- -- User interface
- group "ThreeJSExporter v0.2"
- (
- label msg "Exports selected meshes in Three.js ascii JSON format" align:#left
- hyperLink lab1 "Original source at GitHub" address:"https://github.com/mrdoob/three.js" align:#left
- checkbox exportUv "Export uvs" checked:true enabled:true
- checkbox exportNormal "Export normals" checked:true enabled:true
- checkbox smoothNormal "Use vertex normals" checked:false enabled:true
- checkbox exportColor "Export vertex colors" checked:false enabled:false
- checkbox flipYZ "Flip YZ" checked:true enabled:true
- checkbox flipUV "Flip UV" checked:true enabled:true
- checkbox flipFace "Flip all faces" checked:false enabled:true
- checkbox autoflipFace "Try fixing flipped faces" checked:false enabled:true
- )
- button btn_export "Export selected objects"
- -------------------------------------------------------------------------------------
- -- Dump vertices
- function DumpVertices src =
- (
- Format "'vertices': [" to:ostream
- num = src.count
- if num > 0 then
- (
- for i = 1 to num do
- (
- vert = src[i]
- if flipYZ.checked then
- (
- x = vert.x
- y = vert.z
- z = vert.y
- z *= -1
- )
- else
- (
- x = vert.x
- y = vert.y
- z = vert.z
- )
-
- Format vertexFormat x y z to:ostream
-
- if i < num then Format "," to:ostream
- )
- )
- Format "],\n\n" to:ostream
- )
- -------------------------------------------------------------------------------------
- -- Dump normals
- function DumpNormals src =
- (
- Format "'normals': [" to:ostream
- num = src.count
- if num > 0 and exportNormal.checked then
- (
- for i = 1 to num do
- (
- normal = src[i]
- normal = normalize normal as point3
- if flipYZ.checked then
- (
- x = normal.x
- y = normal.z
- z = normal.y
-
- z *= -1
-
- )
- else
- (
- x = normal.x
- y = normal.y
- z = normal.z
- )
- Format vertexNormalFormat x y z to:ostream
- if i < num then Format "," to:ostream
- )
- )
- Format "],\n\n" to:ostream
- )
- -------------------------------------------------------------------------------------
- -- Dump uvs
- function DumpUvs src =
- (
- Format "'uvs': [" to:ostream
- num = src.count
- if num > 0 and exportUv.checked then
- (
- for i = 1 to num do
- (
- uvw = src[i]
- u = uvw.x
- if flipUV.checked then
- (
- v = 1 - uvw.y
- )
- else
- (
- v = uvw.y
- )
- Format UVFormat u v to:ostream
- if i < num then Format "," to:ostream
- )
- )
- Format "],\n\n" to:ostream
- )
- -------------------------------------------------------------------------------------
- -- Dump face type
- function DumpFaceType label content =
- (
- Format "'%': [" label to:ostream
- num = content.count
- if num > 0 then
- (
- for i = 1 to num do
- (
- zface = content[i]
- fv = zface[1]
- fuv = zface[2]
- m = zface[3] - 1
- needsFlip = zface[4]
- hasUVs = (classof fuv == Point3)
- va = (fv.x - 1) as Integer
- vb = (fv.y - 1) as Integer
- vc = (fv.z - 1) as Integer
- if smoothNormal.checked then
- (
- -- normals have the same indices as vertices
- na = va
- nb = vb
- nc = vc
- )
- else
- (
- -- normals have the same indices as face
- na = i - 1
- nb = na
- nc = na
- )
- if hasUVs then
- (
-
- ua = (fuv.x - 1) as Integer
- ub = (fuv.y - 1) as Integer
- uc = (fuv.z - 1) as Integer
- )
- if flipFace.checked or needsFlip then
- (
- tmp = vb
- vb = vc
- vc = tmp
- tmp = nb
- nb = nc
- nc = tmp
- if hasUVs then
- (
- tmp = ub
- ub = uc
- uc = tmp
- )
- )
- if label == "triangles" then
- (
- Format triFormat va vb vc m to:ostream
- )
- else if label == "trianglesUvs" then
- (
- Format triUVFormat va vb vc m ua ub uc to:ostream
- )
- else if label == "trianglesNormals" then
- (
- Format triNFormat va vb vc m na nb nc to:ostream
- )
- else if label == "trianglesNormalsUvs" then
- (
- Format triUVNFormat va vb vc m na nb nc ua ub uc to:ostream
- )
- if i < num then Format "," to:ostream
- )
- )
- Format "],\n\n" to:ostream
- )
- -------------------------------------------------------------------------------------
- -- Dump faces
- function DumpFaces src =
- (
- hasUVs = true
- triangles = #()
- trianglesUvs = #()
- trianglesNormals = #()
- trianglesNormalsUvs = #()
- quads = #()
- quadsUvs = #()
- quadsNormals = #()
- quadsNormalsUvs = #()
- num = src.count
- if num > 0 then
- (
- for i = 1 to num do
- (
- zface = src[i]
- fuv = zface[2]
- hasUVs = (classof fuv == Point3)
- if hasUVs and exportUv.checked and exportNormal.checked then
- (
- append trianglesNormalsUvs zface
- )
- else if exportNormal.checked then
- (
- append trianglesNormals zface
- )
- else if hasUVs and exportUv.checked then
- (
- append trianglesUvs zface
- )
- else
- (
- append triangles zface
- )
- )
- )
- DumpFaceType "triangles" triangles
- DumpFaceType "trianglesUvs" trianglesUvs
- DumpFaceType "trianglesNormals" trianglesNormals
- DumpFaceType "trianglesNormalsUvs" trianglesNormalsUvs
- DumpFaceType "quads" quads
- DumpFaceType "quadsUvs" quadsUvs
- DumpFaceType "quadsNormals" quadsNormals
- DumpFaceType "quadsNormalsUvs" quadsNormalsUvs
- )
- -------------------------------------------------------------------------------------
- -- Dump color
- function DumpColor pcolor label =
- (
- r = pcolor.r / 255
- g = pcolor.g / 255
- b = pcolor.b / 255
- fr = formattedPrint r format:".4f"
- fg = formattedPrint g format:".4f"
- fb = formattedPrint b format:".4f"
- Format "'%' : [%, %, %],\n" label fr fg fb to:ostream
- )
- -------------------------------------------------------------------------------------
- -- Dump map
- function DumpMap pmap label =
- (
- if classof pmap == BitmapTexture then
- (
- bm = pmap.bitmap
- if bm != undefined then
- (
- fname = filenameFromPath bm.filename
- Format "'%' : '%',\n" label fname to:ostream
- )
- )
- )
- -------------------------------------------------------------------------------------
- -- Export materials
- function ExportMaterials zmaterials hasDummyMaterial =
- (
- Format "'materials': [\n" to:ostream
- totalMaterials = zmaterials.count
- for i = 1 to totalMaterials do
- (
- mat = zmaterials[i]
- Format "{\n" to:ostream
- -- debug
- Format "'DbgIndex' : %,\n" (i-1) to:ostream
- Format "'DbgName' : '%',\n" mat.name to:ostream
- -- colors
- DumpColor mat.diffuse "colorDiffuse"
- DumpColor mat.ambient "colorAmbient"
- DumpColor mat.specular "colorSpecular"
- t = mat.opacity / 100
- s = mat.glossiness
-
- Format "'transparency' : %,\n" t to:ostream
- Format "'specularCoef' : %,\n" s to:ostream
- -- maps
- DumpMap mat.diffuseMap "mapDiffuse"
- DumpMap mat.ambientMap "mapAmbient"
- DumpMap mat.specularMap "mapSpecular"
- DumpMap mat.bumpMap "mapBump"
- DumpMap mat.opacityMap "mapAlpha"
- Format "}" to:ostream
- if ( i < totalMaterials or hasDummyMaterial ) then Format "," to:ostream
- Format "\n\n" to:ostream
- )
-
- if hasDummyMaterial then
- (
- Format "{\n" to:ostream
- Format "'DbgIndex' : %,\n" totalMaterials to:ostream
- Format "'DbgName' : '%',\n" "dummy" to:ostream
-
- Format "}" to:ostream
- )
- Format "],\n\n" to:ostream
- )
- -------------------------------------------------------------------------------------
- -- Extract vertices from mesh
- function ExtractVertices obj whereto =
- (
- n = obj.numVerts
- for i = 1 to n do
- (
- v = GetVert obj i
- append whereto v
- )
- )
- -------------------------------------------------------------------------------------
- -- Extract normals from mesh
- function ExtractNormals obj whereto needsFlip =
- (
- if smoothNormal.checked then
- (
- num = obj.numVerts
-
- for i = 1 to num do
- (
- n = GetNormal obj i
- if flipFace.checked or needsFlip then
- (
- n.x *= -1
- n.y *= -1
- n.z *= -1
- )
- append whereto n
- )
- )
- else
- (
- num = obj.numFaces
- for i = 1 to num do
- (
- n = GetFaceNormal obj i
- if flipFace.checked or needsFlip then
- (
- n.x *= -1
- n.y *= -1
- n.z *= -1
- )
- append whereto n
- )
- )
- )
- -------------------------------------------------------------------------------------
- -- Extract uvs from mesh
- function ExtractUvs obj whereto =
- (
- n = obj.numTVerts
-
- for i = 1 to n do
- (
- v = GetTVert obj i
- append whereto v
- )
- )
- -------------------------------------------------------------------------------------
- -- Extract faces from mesh
- function ExtractFaces objMesh objMaterial whereto allMaterials needsFlip offsetVert offsetUv =
- (
- n = objMesh.numFaces
- hasUVs = objMesh.numTVerts > 0
- useMultiMaterial = false
- materialIDList = #()
-
- materialClass = classof objMaterial
-
- if materialClass == StandardMaterial then
- (
- fm = findItem allMaterials objMaterial
- )
- else if materialClass == MultiMaterial then
- (
- useMultiMaterial = true
- for i = 1 to n do
- (
- mID = GetFaceMatID objMesh i
- materialIndex = findItem objMaterial.materialIDList mID
- subMaterial = objMaterial.materialList[materialIndex]
- mMergedIndex = findItem allMaterials subMaterial
- if mMergedIndex > 0 then
- (
- materialIDList[mID] = mMergedIndex
- )
- )
- )
- else
- (
-
- -- undefined material
-
- fm = 0
-
- )
-
- for i = 1 to n do
- (
- zface = #()
-
- fv = GetFace objMesh i
- fv.x += offsetVert
- fv.y += offsetVert
- fv.z += offsetVert
- if useMultiMaterial then
- (
- mID = GetFaceMatID objMesh i
- fm = materialIDList[mID]
- )
- if hasUVs then
- (
- fuv = GetTVFace objMesh i
-
- fuv.x += offsetUv
- fuv.y += offsetUv
- fuv.z += offsetUv
- )
- else
- (
- fuv = false
- )
- append zface fv
- append zface fuv
- append zface fm
- append zface needsFlip
- append whereto zface
- )
- )
- -------------------------------------------------------------------------------------
- -- Extract materials from eventual multimaterial
-
- function ExtractMaterials objMesh objMaterial whereto =
- (
-
- materialClass = classof objMaterial
- if materialClass == StandardMaterial then
- (
- if ( findItem whereto objMaterial ) == 0 then
- (
- append whereto objMaterial
- )
- )
- else if materialClass == MultiMaterial then
- (
- n = objMesh.numFaces
-
- for i = 1 to n do
- (
-
- mID = getFaceMatId objMesh i
- materialIndex = findItem objMaterial.materialIDList mID
- subMaterial = objMaterial.materialList[materialIndex]
- if ( findItem whereto subMaterial ) == 0 then
- (
- append whereto subMaterial
- )
- )
- )
- )
- -------------------------------------------------------------------------------------
- -- Hack to figure out if normals are messed up
-
- function NeedsFaceFlip node =
- (
- needsFlip = false
- local tmp = Snapshot node
- face_normal = normalize ( getfacenormal tmp 1 )
- face = getface tmp 1
- va = getvert tmp face[1]
- vb = getvert tmp face[2]
- vc = getvert tmp face[3]
- computed_normal = normalize ( cross (vc - vb) (va - vb) )
- if distance computed_normal face_normal > 0.1 then needsFlip = true
- delete tmp
- return needsFlip
- )
- -------------------------------------------------------------------------------------
- -- Extract only things that either already are or can be converted to meshes
- function ExtractMesh node =
- (
- if SuperClassOf node == GeometryClass then
- (
- needsFlip = false
-
- if autoflipFace.checked then
- (
- needsFlip = NeedsFaceFlip node
- )
-
- return #( SnapshotAsMesh node, node.name, node.material, needsFlip )
- )
- -- Not geometry ... could be a camera, light, etc.
-
- return #( false, node.name, 0 )
- )
- -------------------------------------------------------------------------------------
- -- Export scene
- function ExportScene =
- (
- -- Extract meshes
- meshObjects = #()
- mergedVertices = #()
- mergedNormals = #()
- mergedUvs = #()
- mergedFaces = #()
- mergedMaterials = #()
- hasDummyMaterial = true
-
- for obj in selection do
- (
- result = ExtractMesh obj
- meshObj = result[1]
- meshName = result[2]
- meshMaterial = result[3]
- needsFlip = result[4]
- if ClassOf meshObj == TriMesh then
- (
- append meshObjects result
- vertexOffset = mergedVertices.count
- uvOffset = mergedUvs.count
- ExtractMaterials meshObj meshMaterial mergedMaterials
- ExtractVertices meshObj mergedVertices
- ExtractNormals meshObj mergedNormals needsFlip
- ExtractUvs meshObj mergedUvs
- ExtractFaces meshObj meshMaterial mergedFaces mergedMaterials needsFlip vertexOffset uvOffset
- )
- )
- totalVertices = mergedVertices.count
- totalFaces = mergedFaces.count
- totalMaterials = mergedMaterials.count
- totalNormals = 0
- if exportNormal.checked then
- (
- totalNormals = mergedNormals.count
- )
- totalUvs = 0
- if exportUv.checked then
- (
- totalUvs = mergedUvs.count
- )
- -- Dump header
- Format headerFormat maxFileName totalVertices totalNormals totalUvs totalFaces totalMaterials to:ostream
- -- Dump objects (debug)
- Format "// Source objects:\n\n" to:ostream
- i = 0
- for obj in meshObjects do
- (
- meshName = obj[2]
- Format "// %: %\n" i meshName to:ostream
- i += 1
- )
- -- Dump model
- Format "\n\nvar model = {\n\n" to:ostream
- -- Dump all materials in the scene
- ExportMaterials mergedMaterials hasDummyMaterial
- -- Dump merged data from all selected geometries
- DumpVertices mergedVertices
- DumpNormals mergedNormals
- DumpUvs mergedUvs
- DumpFaces mergedFaces
- -- Dump footer
- Format footerFormat to:ostream
- )
- -------------------------------------------------------------------------------------
- -- Open and prepare a file handle for writing
- function GetSaveFileStream =
- (
- zname = getFilenameFile maxFileName
- zname += ".js"
- fname = GetSaveFileName filename:zname types:"JavaScript file (*.js)|*.js|All Files(*.*)|*.*|"
- if fname == undefined then
- return undefined
- ostream = CreateFile fname
- if ostream == undefined then
- (
- MessageBox "Couldn't open file for writing !"
- return undefined
- )
- return ostream
- )
- -------------------------------------------------------------------------------------
- -- Export button click handler
- on btn_export pressed do
- (
- ostream = GetSaveFileStream()
- if ostream != undefined then
- (
- ExportScene()
- close ostream
- )
- )
- )
- createDialog ThreeJSExporter width:300
|