├── .gitignore ├── index.html ├── example ├── example.frag ├── example.vert ├── index.html └── main.js ├── index.js ├── README.md ├── LICENSE └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | 3 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /example/example.frag: -------------------------------------------------------------------------------- 1 | #define SHADER_NAME test.frag 2 | 3 | precision highp float; 4 | 5 | varying vec3 vNormal; 6 | 7 | void main() { 8 | gl_FragColor = vec4(vNormal, 1); 9 | } 10 | -------------------------------------------------------------------------------- /example/example.vert: -------------------------------------------------------------------------------- 1 | #define SHADER_NAME test.vert 2 | 3 | attribute vec3 aPosition; 4 | attribute vec3 aNormal; 5 | 6 | uniform mat4 uModel; 7 | uniform mat4 uView; 8 | uniform mat4 uProjection; 9 | 10 | varying vec3 vNormal; 11 | 12 | void main() { 13 | gl_Position = uProjection * uView * uModel * vec4(aPosition, 1.0); 14 | vNormal = 0.5 * aNormal + 0.5; 15 | vNormal += pow(clamp(dot(vec3(uModel * vec4(aNormal,1)), normalize(vec3(1,1,2))), 0.0, 1.0), 128.0); 16 | } 17 | -------------------------------------------------------------------------------- /example/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | var boundingBox = require('vertices-bounding-box') 4 | var mat4 = require('gl-mat4') 5 | var tform = require('geo-3d-transform-mat4') 6 | var geoconv = require('geo-convert-position-format') 7 | 8 | module.exports = function center (positions, opts) { 9 | // Set some defaults. 10 | opts = Object.assign({ 11 | center: [0, 0, 0] 12 | }, opts || {}); 13 | 14 | positions = geoconv.convert(positions, geoconv.ARRAY_OF_ARRAYS) 15 | // Calculate the bounding box. 16 | var bb = boundingBox(positions) 17 | 18 | // Translate the geometry center to the origin. 19 | var _translate = [ 20 | -0.5 * (bb[0][0] + bb[1][0]) + opts.center[0], 21 | -0.5 * (bb[0][1] + bb[1][1]) + opts.center[1], 22 | -0.5 * (bb[0][2] + bb[1][2]) + opts.center[2] 23 | ] 24 | var translate = mat4.create() 25 | mat4.translate(translate, translate, _translate) 26 | var centered = tform(positions, translate) 27 | 28 | return centered 29 | } 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # geo-center 2 | 3 | Centers vertices around a point. 4 | 5 | [Demo](http://wwwtyro.github.io/geo-center/) 6 | 7 | ## Install 8 | 9 | ```sh 10 | npm install geo-center 11 | ``` 12 | 13 | ## Example 14 | 15 | ```js 16 | var dragon = require('stanford-dragon/3') 17 | var center = require('geo-center') 18 | 19 | dragon.positions = center(dragon.positions, { 20 | center: [0,0,0] 21 | }) 22 | 23 | ``` 24 | 25 | ## API 26 | 27 | ```js 28 | var center = require('geo-center') 29 | ``` 30 | 31 | #### `var centeredPositions = center(positions, opts)` 32 | 33 | Returns a copy of `positions` centered around `opts.center`. 34 | 35 | `positions` is the vertex array for your mesh. It can be any of: 36 | 37 | * Flat array `[1,2,3,4,5,6]` 38 | * Array of arrays `[[1,2,3], [4,5,6]]` 39 | * TypedArray `new Float32Array([1,2,3,4,5,6])` 40 | * Array of TypedArrays `[new Float32Array([1,2,3]), new Float32Array([4,5,6])]` 41 | * [ndarray](https://www.npmjs.com/package/ndarray) `ndarray(new Float32Array([1,2,3,4,5,6]))` 42 | 43 | `opts` is an object that can have the following properties: 44 | * `center` is the point you want your mesh centered upon. Defaults to `[0,0,0]`. 45 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to