├── .gitignore ├── package.json ├── README.md ├── LICENSE └── parse-obj.js /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | 14 | npm-debug.log 15 | node_modules/* 16 | *.DS_Store -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "parse-obj", 3 | "version": "0.0.0", 4 | "description": "Parses .OBJ formatted meshes", 5 | "main": "parse-obj.js", 6 | "dependencies": { 7 | "split": "~0.2.10" 8 | }, 9 | "devDependencies": {}, 10 | "scripts": { 11 | "test": "echo \"Error: no test specified\" && exit 1" 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "git://github.com/mikolalysenko/parse-obj.git" 16 | }, 17 | "keywords": [ 18 | "obj", 19 | "parse", 20 | "mesh", 21 | "3d", 22 | "graphics" 23 | ], 24 | "author": "Mikola Lysenko", 25 | "license": "MIT", 26 | "bugs": { 27 | "url": "https://github.com/mikolalysenko/parse-obj/issues" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | parse-obj 2 | ========= 3 | A simple parser for .OBJ mesh files. 4 | 5 | ## Example 6 | 7 | ```javascript 8 | var fs = require("fs") 9 | var parseOBJ = require("parse-obj") 10 | 11 | parseOBJ(fs.createReadStream("mesh.obj"), function(err, result) { 12 | if(err) { 13 | throw new Error("Error parsing OBJ file: " + err) 14 | } 15 | console.log("Got mesh: ", result) 16 | }) 17 | ``` 18 | 19 | ### `require("parse-obj")(stream, cb(err, result))` 20 | Parses a read stream into a .OBJ format mesh 21 | 22 | * `stream` is a read stream 23 | * `cb` is a callback that gets executed once the stream is parsed. The `result` object is a structure with the following data: 24 | 25 | + `vertexPositions` an array of vertex position data 26 | + `vertexNormals` an array of vertex normal data 27 | + `vertexUVs` an array of vertex UV coordinates 28 | + `facePositions` an array of indices for face positions 29 | + `faceNormals` an array of indices for face normals 30 | + `faceUVs` an array of indices for face texture coordinates 31 | 32 | 33 | ## Credits 34 | (c) 2013 Mikola Lysenko. MIT License 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2013 Mikola Lysenko 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /parse-obj.js: -------------------------------------------------------------------------------- 1 | "use strict" 2 | 3 | var split = require("split") 4 | 5 | module.exports = parseOBJ 6 | 7 | function parseOBJ(stream, cb) { 8 | var v = [] 9 | var vn = [] 10 | var vt = [] 11 | var f = [] 12 | var fn = [] 13 | var ft = [] 14 | stream.pipe(split()) 15 | .on("data", function(line) { 16 | if(line.length === 0 || line.charAt(0) === "#") { 17 | return 18 | } 19 | var toks = line.split(" ") 20 | switch(toks[0]) { 21 | case "v": 22 | if(toks.length < 3) { 23 | throw new Error("parse-obj: Invalid vertex :" + line) 24 | } 25 | v.push([+toks[1], +toks[2], +toks[3]]) 26 | break 27 | 28 | case "vn": 29 | if(toks.length < 3) { 30 | throw new Error("parse-obj: Invalid vertex normal:"+ line) 31 | } 32 | vn.push([+toks[1], +toks[2], +toks[3]]) 33 | break 34 | 35 | case "vt": 36 | if(toks.length < 2) { 37 | throw new Error("parse-obj: Invalid vertex texture coord:" + line) 38 | } 39 | vt.push([+toks[1], +toks[2]]) 40 | break 41 | 42 | case "f": 43 | var position = new Array(toks.length-1) 44 | var normal = new Array(toks.length-1) 45 | var texCoord = new Array(toks.length-1) 46 | for(var i=1; i