├── .gitignore ├── LICENSE ├── README.md ├── angle-normals.js ├── demo.js └── package.json /.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 -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | angle-normals 2 | ============= 3 | Computes vertex normals for a mesh using angle weights. This is slower, but produces results which are more robust to the mesh subdivision. 4 | 5 | # Install 6 | Angle normals 7 | 8 | ``` 9 | npm i angle-normals 10 | ``` 11 | 12 | # Example 13 | 14 | ```javascript 15 | var bunny = require('bunny') 16 | var normals = require('angle-normals')(bunny.cells, bunny.positions) 17 | 18 | console.log(normals) 19 | ``` 20 | 21 | # API 22 | 23 | #### `require('angle-normals')(cells, positions)` 24 | Computes vertex normals from for a mesh. 25 | 26 | * `cells` are the cells of the mesh 27 | * `positions` are the positions of the mesh's vertices 28 | 29 | **Returns** An array of normals 30 | 31 | # License 32 | (c) 2015 Mikola Lysenko. MIT License 33 | -------------------------------------------------------------------------------- /angle-normals.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | module.exports = angleNormals 4 | 5 | function hypot(x, y, z) { 6 | return Math.sqrt(Math.pow(x,2) + Math.pow(y,2) + Math.pow(z,2)) 7 | } 8 | 9 | function weight(s, r, a) { 10 | return Math.atan2(r, (s - a)) 11 | } 12 | 13 | function mulAdd(dest, s, x, y, z) { 14 | dest[0] += s * x 15 | dest[1] += s * y 16 | dest[2] += s * z 17 | } 18 | 19 | function angleNormals(cells, positions) { 20 | var numVerts = positions.length 21 | var numCells = cells.length 22 | 23 | //Allocate normal array 24 | var normals = new Array(numVerts) 25 | for(var i=0; i