├── .gitignore ├── LICENSE ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | test* 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Thibaut Séguy 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | parse-wavefront-obj 2 | =================== 3 | ### Wavefront OBJ parser 4 | 5 | Parses a [Wavefront OBJ](http://en.wikipedia.org/wiki/Wavefront_.obj_file) string or buffer. If you're looking for a stream parser check [this](https://github.com/mikolalysenko/parse-obj). 6 | 7 | Supports vertex normals and UV coordinates. Doesn't support material libraries (though I'm open to PRs), nor multiple meshes embedded in the same file. 8 | 9 | Install 10 | ------- 11 | 12 | ```bash 13 | $ npm install parse-wavefront-obj 14 | ``` 15 | 16 | Usage 17 | ----- 18 | 19 | ```javascript 20 | var parseOBJ = require('parse-wavefront-obj'); 21 | var fs = require('fs'); 22 | 23 | var buf = fs.readFileSync('mesh.obj'); 24 | var mesh = parseOBJ(buf); 25 | 26 | console.log(mesh); 27 | /* 28 | { 29 | positions: [...], 30 | cells: [...], 31 | 32 | // The following attributes are available when defined 33 | // in the original file 34 | 35 | vertexUVs: [...], // array of UV coordinates 36 | faceUVs: [...], // array of UV indices 37 | vertexNormals: [...], // array of vertex normals 38 | faceNormals: [...], // array of normal indices 39 | name: 'foo' // mesh name 40 | } 41 | */ 42 | ``` -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 2 | 3 | function parse(str) { 4 | if(typeof buf !== 'string') { 5 | str = str.toString(); 6 | } 7 | 8 | var lines = str.trim().split('\n'); 9 | 10 | var positions = []; 11 | var cells = []; 12 | var vertexUVs = []; 13 | var vertexNormals = []; 14 | var faceUVs = []; 15 | var faceNormals = []; 16 | var name = null; 17 | 18 | for(var i=0; i 1) { 64 | if(!isNaN(indices[1])) { 65 | uvIndices.push(convertIndex(indices[1], vertexUVs.length)); 66 | } 67 | if(!isNaN(indices[2])) { 68 | normalIndices.push(convertIndex(indices[2], vertexNormals.length)); 69 | } 70 | } 71 | 72 | }); 73 | 74 | cells.push(positionIndices); 75 | 76 | if(uvIndices.length > 0) { 77 | faceUVs.push(uvIndices); 78 | } 79 | if(normalIndices.length > 0) { 80 | faceNormals.push(normalIndices); 81 | } 82 | 83 | break; 84 | default: 85 | // skip 86 | } 87 | 88 | } 89 | 90 | var mesh = { 91 | positions: positions, 92 | cells: cells 93 | }; 94 | 95 | if(vertexUVs.length > 0) { 96 | mesh.vertexUVs = vertexUVs; 97 | } 98 | 99 | if(faceUVs.length > 0) { 100 | mesh.faceUVs = faceUVs; 101 | } 102 | 103 | if(vertexNormals.length > 0) { 104 | mesh.vertexNormals = vertexNormals; 105 | } 106 | 107 | if(faceNormals.length > 0) { 108 | mesh.faceNormals = faceNormals; 109 | } 110 | 111 | if(name !== null) { 112 | mesh.name = name; 113 | } 114 | 115 | return mesh; 116 | } 117 | 118 | function convertIndex(objIndex, arrayLength) { 119 | return objIndex > 0 ? objIndex - 1 : objIndex + arrayLength; 120 | } 121 | 122 | module.exports = parse; 123 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "parse-wavefront-obj", 3 | "version": "1.0.3", 4 | "description": "Wavefront OBJ parser", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git://github.com/thibauts/parse-wavefront-obj.git" 12 | }, 13 | "keywords": [ 14 | "obj", 15 | "format", 16 | "mesh", 17 | "buffer" 18 | ], 19 | "author": "thibauts", 20 | "license": "MIT" 21 | } 22 | --------------------------------------------------------------------------------