OBJ to JSON 3D File Version Converter Basic online web conversion program obj to json var script document createElement script script async false script type text javascript script src https cdn jsdelivr net npm webgl obj loader 2 0 8 dist webgl obj loader min js document head appendChild script let p document createElement p p innerHTML select obj file to convert to json file document body appendChild p let input document createElement input document body appendChild input input type file input onchange onFileSelected function onFileSelected event let selectedFile event target files 0 console log file selectedFile name somefile obj let jsonfilename selectedFile name split 0 json let reader new FileReader reader onload function event let objtxt event target result let mesh new OBJ Mesh objtxt console log Num vertices mesh vertices length console log Num indices mesh indices length console log Num normals mesh normals mesh normals length 0 let json vertices mesh vertices faces mesh indices normals mesh vertexNormals mesh vertexNormals console log output json jsonfilename let txt JSON stringify json null 4 let a document createElement a document body appendChild a a href data text plain charset utf 8 encodeURIComponent txt a setAttribute download jsonfilename cheburashka json a click document body removeChild a reader readAsText selectedFile console log ready OBJ to JSON 3D File Version Converter Custom version that allows the mesh to be triangulated or merge multiple meshes into a single mesh let p document createElement p p innerHTML select obj file to convert to json file document body appendChild p let input document createElement input document body appendChild input input type file input onchange onFileSelected function onFileSelected event let selectedFile event target files 0 console log file selectedFile name somefile obj let jsonfilename selectedFile name split 0 json let reader new FileReader reader onload function event let objtxt event target result let mesh new readObj objtxt console log Num vertices mesh 0 v length console log Num indices mesh 0 f length console log Num normals mesh 0 n mesh 0 n length 0 let json vertices mesh 0 v faces mesh 0 f normals mesh 0 n mesh 0 n console log output json jsonfilename let txt JSON stringify json null 4 let a document createElement a document body appendChild a a href data text plain charset utf 8 encodeURIComponent txt a setAttribute download jsonfilename cheburashka json a click document body removeChild a reader readAsText selectedFile console log ready ref https notebook xbdev net index php page obj function readObj txt console log readObj let objects objects push name test v vt vn f usemtl let data v n f txt txt replaceAll let lines txt split n console log num lines lines length for let i 0 i lines length i let line lines i line line trim if line length 2 continue if line 0 continue let parts line split if parts length 1 continue switch parts 0 case v v 0 089624 1 419387 0 052847 objects reverse 0 v push parts 1 objects reverse 0 v push parts 2 objects reverse 0 v push parts 3 break case vt vt 0 818181 0 000000 objects reverse 0 vt push parts 1 objects reverse 0 vt push parts 2 break case vn vn 0 5499 0 7413 0 3847 objects reverse 0 vn push parts 1 objects reverse 0 vn push parts 2 objects reverse 0 vn push parts 3 break case f f 1 1 1 14 2 1 13 3 1 f v1 vt1 vn1 v2 vt2 vn2 v3 vt3 vn3 f v1 vn1 v2 vn2 v3 vn3 double slashes If you have 4 indices e g 0 1 2 3 The division into two triangles would be one with the first 3 indices and one with the first third and fourth In this example 0 1 2 0 2 3 Let s try some ASCII art to illustrate this 3 2 0 1 let i0 parts 1 split 0 1 let i1 parts 2 split 0 1 let i2 parts 3 split 0 1 let i3 parts length 4 undefined parts 4 split 0 1 let n0 parts 1 split 2 1 let n1 parts 2 split 2 1 let n2 parts 3 split 2 1 let n3 parts length 4 undefined parts 4 split 2 1 if parts 1 includes n0 parts 1 split 1 1 n1 parts 2 split 1 1 n2 parts 3 split 1 1 n3 parts length 4 undefined parts 4 split 1 1 if i3 undefined triangles for let g 0 g 3 g let idx i0 i1 i2 g let x0 objects reverse 0 v 3 idx 0 let y0 objects reverse 0 v 3 idx 1 let z0 objects reverse 0 v 3 idx 2 data v push x0 data v push y0 data v push z0 data f push data f length for let g 0 g n0 n1 n2 length g let nx0 objects reverse 0 vn 3 n0 n1 n2 g 0 let ny0 objects reverse 0 vn 3 n0 n1 n2 g 1 let nz0 objects reverse 0 vn 3 n0 n1 n2 g 2 data n push nx0 data n push ny0 data n push nz0 else quads let v for let g 0 g 4 g let idx i0 i1 i2 i3 g let x0 objects reverse 0 v 3 idx 0 let y0 objects reverse 0 v 3 idx 1 let z0 objects reverse 0 v 3 idx 2 v push x x0 y y0 z z0 data v push v 0 x data v push v 0 y data v push v 0 z data v push v 1 x data v push v 1 y data v push v 1 z data v push v 2 x data v push v 2 y data v push v 2 z data f push data f length data v push v 0 x data v push v 0 y data v push v 0 z data v push v 2 x data v push v 2 y data v push v 2 z data v push v 3 x data v push v 3 y data v push v 3 z data f push data f length let n for let g 0 g 4 g let idx n0 n1 n2 n3 g let nx0 objects reverse 0 vn 3 idx 0 let ny0 objects reverse 0 vn 3 idx 1 let nz0 objects reverse 0 vn 3 idx 2 n push x nx0 y ny0 z nz0 data n push n 0 x data n push n 0 y data n push n 0 z data n push n 1 x data n push n 1 y data n push n 1 z data n push n 2 x data n push n 2 y data n push n 2 z data n push n 0 x data n push n 0 y data n push n 0 z data n push n 2 x data n push n 2 y data n push n 2 z data n push n 3 x data n push n 3 y data n push n 3 z break end switch for return data readObj
break case f f 1 1 1 14 2 1 13 3 1 f v1 vt1 vn1 v2 vt2 vn2 v3 vt3 vn3 f v1 vn1 v2 vn2 v3 vn3 double slashes If you have 4 indices e g 0 1 2 3 The division into two triangles would be one with the first 3 indices and one with the first third and fourth In this example 0 1 2 0 2 3 Let s try some ASCII art to illustrate this 3 2 0 1 let i0 parts 1 split 0 1 let i1 parts 2 split 0 1 let i2 parts 3 split 0 1 let i3 parts length 4 undefined parts 4 split 0 1 let n0 parts 1 split 2 1 let n1 parts 2 split 2 1 let n2 parts 3 split 2 1 let n3 parts length 4 undefined parts 4 split 2 1 if parts 1 includes n0 parts 1 split 1 1 n1 parts 2 split 1 1 n2 parts 3 split 1 1 n3 parts length 4 undefined parts 4 split 1 1 if i3 undefined triangles for let g 0 g 3 g let idx i0 i1 i2 g let x0 objects reverse 0 v 3 idx 0 let y0 objects reverse 0 v 3 idx 1 let z0 objects reverse 0 v 3 idx 2 data v push x0 data v push y0 data v push z0 data f push data f length for let g 0 g n0 n1 n2 length g let nx0 objects reverse 0 vn 3 n0 n1 n2 g 0 let ny0 objects reverse 0 vn 3 n0 n1 n2 g 1 let nz0 objects reverse 0 vn 3 n0 n1 n2 g 2 data n push nx0 data n push ny0 data n push nz0 else quads let v for let g 0 g 4 g let idx i0 i1 i2 i3 g let x0 objects reverse 0 v 3 idx 0 let y0 objects reverse 0 v 3 idx 1 let z0 objects reverse 0 v 3 idx 2 v push x x0 y y0 z z0 data v push v 0 x data v push v 0 y data v push v 0 z data v push v 1 x data v push v 1 y data v push v 1 z data v push v 2 x data v push v 2 y data v push v 2 z data f push data f length data v push v 0 x data v push v 0 y data v push v 0 z data v push v 2 x data v push v 2 y data v push v 2 z data v push v 3 x data v push v 3 y data v push v 3 z data f push data f length let n for let g 0 g 4 g let idx n0 n1 n2 n3 g let nx0 objects reverse 0 vn 3 idx 0 let ny0 objects reverse 0 vn 3 idx 1 let nz0 objects reverse 0 vn 3 idx 2 n push x nx0 y ny0 z nz0 data n push n 0 x data n push n 0 y data n push n 0 z data n push n 1 x data n push n 1 y data n push n 1 z data n push n 2 x data n push n 2 y data n push n 2 z data n push n 0 x data n push n 0 y data n push n 0 z data n push n 2 x data n push n 2 y data n push n 2 z data n push n 3 x data n push n 3 y data n push n 3 z break end switch for return data readObj