I love the project and the video
and for that here I have created a Node.js script obj2js.js to streamline the process of importing 3D models into the canvas.
- This script takes a standard Wavefront .obj file and converts it into a JavaScript file (with the compatible DS)
Usage:
Run the script with Node.js, passing the .obj file as an argument:
node obj2js.js mymodel.obj
This will generate mymodel.js in the same directory.
testing
worked with suitger

as a charm !
the script:
constfs=require('fs');constpath=require('path');constinputFile=process.argv[2];if(!inputFile){console.error("Usage: node obj2js.js <file.obj>");process.exit(1);}constmodelName=path.basename(inputFile,path.extname(inputFile));constoutputFile=modelName+".js";constobjContent=fs.readFileSync(inputFile,'utf-8');constlines=objContent.split('\n');constvs=[];constfs_indices=[];for(constlineoflines){consttrimmed=line.trim();if(trimmed.startsWith('v ')){constparts=trimmed.split(/\s+/);// v x y zvs.push({x: parseFloat(parts[1]),y: parseFloat(parts[2]),z: parseFloat(parts[3])});}elseif(trimmed.startsWith('f ')){constparts=trimmed.split(/\s+/);// f v1/vt1/vn1 v2/vt2/vn2 ...constface=[];for(leti=1;i<parts.length;i++){constvIndex=parseInt(parts[i].split('/')[0])-1;// 1-based to 0-basedface.push(vIndex);}fs_indices.push(face);}}// Center the modelif(vs.length>0){letminX=Infinity,maxX=-Infinity;letminY=Infinity,maxY=-Infinity;letminZ=Infinity,maxZ=-Infinity;for(constvofvs){minX=Math.min(minX,v.x);maxX=Math.max(maxX,v.x);minY=Math.min(minY,v.y);maxY=Math.max(maxY,v.y);minZ=Math.min(minZ,v.z);maxZ=Math.max(maxZ,v.z);}constcx=(minX+maxX)/2;constcy=(minY+maxY)/2;constcz=(minZ+maxZ)/2;for(constvofvs){v.x-=cx;v.y-=cy;v.z-=cz;}}constoutputContent=`models.${modelName} = { vs: ${JSON.stringify(vs,null,4)}, fs: ${JSON.stringify(fs_indices,null,4)}};`;fs.writeFileSync(path.join(__dirname,outputFile),outputContent);console.log(`Converted ${inputFile} to ${outputFile}`);
I love the project and the video
and for that here I have created a Node.js script
obj2js.jsto streamline the process of importing 3D models into the canvas.Usage:
Run the script with Node.js, passing the .obj file as an argument:
This will generate mymodel.js in the same directory.
testing
worked with suitger
as a charm !
the script: