Custom obj loader multiple objects materials OBJ 3D File Reader Basic online web conversion program Obj format overview triangle only obj file obj file mtllib tree mtl o Icosphere v 0 089624 1 419387 0 052847 vt 0 818181 0 000000 vn 0 5499 0 7413 0 3847 usemtl Material 001 f 1 1 1 14 2 1 13 3 1 mtl file newmtl Material 001 Ns 225 000000 Ka 1 000000 1 000000 1 000000 Kd 0 217401 0 800000 0 176586 Ks 0 500000 0 500000 0 500000 Ke 0 000000 0 000000 0 000000 Ni 1 450000 d 1 000000 illum 2 function readObj txt var objects objects push name test v vt vn f usemtl 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 o create one big object objects push name parts 1 v vt vn f usemtl break 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 usemtl usemtl Material 001 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 objects reverse 0 f push parts 1 split 0 1 objects reverse 0 f push parts 1 split 1 1 objects reverse 0 f push parts 1 split 2 1 objects reverse 0 f push parts 2 split 0 1 objects reverse 0 f push parts 2 split 1 1 objects reverse 0 f push parts 2 split 2 1 objects reverse 0 f push parts 3 split 0 1 objects reverse 0 f push parts 3 split 1 1 objects reverse 0 f push parts 3 split 2 1 break default end switch for return objects readObj let p document createElement p p innerHTML select obj 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 meshes readObj objtxt console log Num vertices meshes 0 v length console log Num indices meshes 0 f length console log Num normals meshes 0 vn length reader readAsText selectedFile console log ready
break case usemtl usemtl Material 001 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 objects reverse 0 f push parts 1 split 0 1 objects reverse 0 f push parts 1 split 1 1 objects reverse 0 f push parts 1 split 2 1 objects reverse 0 f push parts 2 split 0 1 objects reverse 0 f push parts 2 split 1 1 objects reverse 0 f push parts 2 split 2 1 objects reverse 0 f push parts 3 split 0 1 objects reverse 0 f push parts 3 split 1 1 objects reverse 0 f push parts 3 split 2 1 break default end switch for return objects readObj let p document createElement p p innerHTML select obj 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 meshes readObj objtxt console log Num vertices meshes 0 v length console log Num indices meshes 0 f length console log Num normals meshes 0 vn length reader readAsText selectedFile console log ready