├── .gitignore ├── LICENSE ├── README.md ├── example ├── example.frag ├── example.vert ├── index.html └── main.js ├── index.html ├── index.js ├── package-lock.json └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | 3 | -------------------------------------------------------------------------------- /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 25 | 26 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /example/main.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | /* global requestAnimationFrame*/ 4 | 5 | var mat4 = require('gl-mat4') 6 | var Geometry = require('gl-geometry') 7 | var glShader = require('gl-shader') 8 | var glslify = require('glslify') 9 | var dragon = require('stanford-dragon/3') 10 | var Trackball = require('trackball-controller') 11 | var normals = require('normals') 12 | 13 | var center = require('../index') 14 | 15 | window.onload = function () { 16 | var canvas = document.getElementById('render-canvas') 17 | 18 | var gl = canvas.getContext('webgl') 19 | gl.enable(gl.DEPTH_TEST) 20 | gl.enable(gl.CULL_FACE) 21 | gl.cullFace(gl.BACK) 22 | gl.clearColor(0, 0, 0, 0) 23 | 24 | dragon.positions = center(dragon.positions) 25 | 26 | var geometry = Geometry(gl) 27 | .attr('aPosition', dragon.positions) 28 | .attr('aNormal', normals.vertexNormals( 29 | dragon.cells, 30 | dragon.positions 31 | )) 32 | .faces(dragon.cells) 33 | 34 | var view = mat4.create() 35 | var projection = mat4.create() 36 | 37 | var program = glShader(gl, glslify('./example.vert'), glslify('./example.frag')) 38 | 39 | function render () { 40 | canvas.width = canvas.clientWidth 41 | canvas.height = canvas.clientHeight 42 | gl.viewport(0, 0, canvas.width, canvas.height) 43 | mat4.lookAt(view, [0, 0, 72], [0, 0, 0], [0, 1, 0]) 44 | mat4.perspective(projection, Math.PI / 2, canvas.width / canvas.height, 0.1, 1000.0) 45 | 46 | gl.clear(gl.DEPTH_BUFFER_BIT | gl.COLOR_BUFFER_BIT) 47 | 48 | program.bind() 49 | geometry.bind(program) 50 | program.uniforms.uModel = trackball.rotation 51 | program.uniforms.uView = view 52 | program.uniforms.uProjection = projection 53 | geometry.draw(gl.TRIANGLES) 54 | requestAnimationFrame(render) 55 | } 56 | 57 | var trackball = new Trackball(canvas) 58 | 59 | render() 60 | } 61 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "index.js", 3 | "scripts": { 4 | "build": "browserify -t glslify example/main.js > example/bundle.js", 5 | "start": "budo --live example/main.js:example/static/js/bundle.js -- -t glslify", 6 | "standard": "standard", 7 | "deps": "dependency-check . --no-dev", 8 | "deps-unused": "dependency-check . --unused --no-dev", 9 | "deps-dev": "dependency-check . --entry example/main.js", 10 | "deps-dev-unused": "dependency-check . --entry example/main.js --unused -i browserify -i budo -i dependency-check -i standard", 11 | "test": "npm run standard && npm run deps && npm run deps-unused && npm run deps-dev && npm run deps-dev-unused" 12 | }, 13 | "keywords": [ 14 | "stackgl" 15 | ], 16 | "author": "Rye Terrell (http://wwwtyro.github.io)", 17 | "license": "Unlicense", 18 | "devDependencies": { 19 | "browserify": "^13.0.0", 20 | "budo": "^11.6.4", 21 | "dependency-check": "^2.5.1", 22 | "gl-geometry": "^1.3.0", 23 | "gl-shader": "^4.1.1", 24 | "glslify": "^7.1.0", 25 | "normals": "^1.0.1", 26 | "standard": "^14.3.4", 27 | "stanford-dragon": "^1.1.1", 28 | "trackball-controller": "^1.1.1" 29 | }, 30 | "directories": { 31 | "example": "example" 32 | }, 33 | "dependencies": { 34 | "geo-3d-transform-mat4": "^1.0.0", 35 | "gl-mat4": "^1.1.4", 36 | "vertices-bounding-box": "^1.0.0" 37 | }, 38 | "name": "geo-center", 39 | "description": "Centers vertices around a point.", 40 | "version": "1.0.3", 41 | "repository": { 42 | "type": "git", 43 | "url": "git+https://github.com/wwwtyro/geo-center.git" 44 | }, 45 | "bugs": { 46 | "url": "https://github.com/wwwtyro/geo-center/issues" 47 | }, 48 | "homepage": "https://github.com/wwwtyro/geo-center#readme" 49 | } 50 | --------------------------------------------------------------------------------