├── .gitignore ├── .npmignore ├── index.glsl ├── demo ├── vert.glsl ├── frag.glsl ├── index.html └── test.js ├── LICENSE.md ├── package.json └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.log 3 | .DS_Store 4 | bundle.js 5 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.log 3 | .DS_Store 4 | bundle.js 5 | test 6 | test.js 7 | demo 8 | example 9 | .npmignore -------------------------------------------------------------------------------- /index.glsl: -------------------------------------------------------------------------------- 1 | vec3 normals(vec3 pos) { 2 | vec3 fdx = dFdx(pos); 3 | vec3 fdy = dFdy(pos); 4 | return normalize(cross(fdx, fdy)); 5 | } 6 | 7 | #pragma glslify: export(normals) -------------------------------------------------------------------------------- /demo/vert.glsl: -------------------------------------------------------------------------------- 1 | varying vec3 vViewPos; 2 | 3 | void main() { 4 | vec4 pos = vec4(position, 1.0); 5 | vec4 mpos = modelViewMatrix * pos; 6 | gl_Position = projectionMatrix * mpos; 7 | vViewPos = -mpos.xyz; 8 | } -------------------------------------------------------------------------------- /demo/frag.glsl: -------------------------------------------------------------------------------- 1 | #extension GL_OES_standard_derivatives : enable 2 | 3 | varying vec3 vViewPos; 4 | 5 | #pragma glslify: faceNormal = require('../') 6 | 7 | void main() { 8 | vec3 normal = faceNormal(vViewPos); 9 | 10 | gl_FragColor.rgb = normal; 11 | gl_FragColor.a = 1.0; 12 | } -------------------------------------------------------------------------------- /demo/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | glsl-face-normal 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /demo/test.js: -------------------------------------------------------------------------------- 1 | var THREE = require('three') 2 | var createViewer = require('three-orbit-viewer')(THREE) 3 | var glslify = require('glslify') 4 | 5 | var app = createViewer({ 6 | clearColor: 0x000000, 7 | clearAlpha: 1, 8 | fov: 65, 9 | position: new THREE.Vector3(1, 1, -2) 10 | }) 11 | 12 | var geo = new THREE.TorusGeometry(0.85, 0.3, 15, 15) 13 | var mat = new THREE.ShaderMaterial({ 14 | shading: THREE.FlatShading, 15 | vertexShader: glslify('./vert.glsl'), 16 | fragmentShader: glslify('./frag.glsl') 17 | }) 18 | var box = new THREE.Mesh(geo, mat) 19 | app.scene.add(box) 20 | 21 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | ===================== 3 | 4 | Copyright (c) 2014 [stackgl](http://github.com/stackgl/) contributors 5 | 6 | *stackgl contributors listed at * 7 | 8 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 11 | 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 13 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "glsl-face-normal", 3 | "version": "1.0.2", 4 | "description": "approximate face normal in the fragment shader", 5 | "main": "index.js", 6 | "license": "MIT", 7 | "scripts": { 8 | "start": "budo demo/test.js:bundle.js --live -t glslify | garnish", 9 | "build": "browserify demo/test.js -t glslify | uglifyjs -cm > demo/bundle.js" 10 | }, 11 | "author": { 12 | "name": "Matt DesLauriers", 13 | "email": "dave.des@gmail.com", 14 | "url": "http://github.com/mattdesl" 15 | }, 16 | "dependencies": {}, 17 | "devDependencies": { 18 | "browserify": "^10.0.0", 19 | "budo": "^4.0.0", 20 | "garnish": "^2.1.3", 21 | "glslify": "^2.1.2", 22 | "three": "^0.71.0", 23 | "three-orbit-viewer": "^69.3.0", 24 | "uglify-js": "^2.4.20" 25 | }, 26 | "repository": { 27 | "type": "git", 28 | "url": "git://github.com/stackgl/glsl-face-normal.git" 29 | }, 30 | "keywords": [ 31 | "ecosystem:stackgl", 32 | "face", 33 | "faces", 34 | "flat", 35 | "shading", 36 | "stackgl", 37 | "glsl", 38 | "glslify", 39 | "normal", 40 | "normals", 41 | "pixel", 42 | "fragment", 43 | "shader" 44 | ], 45 | "homepage": "https://github.com/stackgl/glsl-face-normal", 46 | "bugs": { 47 | "url": "https://github.com/stackgl/glsl-face-normal/issues" 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # glsl-face-normal 2 | 3 | [![stable](http://badges.github.io/stability-badges/dist/stable.svg)](http://github.com/badges/stability-badges) 4 | 5 | ![demo-image](http://i.imgur.com/PFaEL8y.png) 6 | 7 | [(click for demo)](http://stack.gl/glsl-face-normal/demo) 8 | 9 | 10 | 11 | Approximates face normals in the fragment shader for flat shading from the position in camera space. 12 | 13 | *Note:* You need to enable `GL_OES_standard_derivatives`. 14 | 15 | Fragment: 16 | 17 | ```glsl 18 | #extension GL_OES_standard_derivatives : enable 19 | varying vec3 vViewPos; 20 | 21 | #pragma glslify: faceNormal = require('glsl-face-normal') 22 | 23 | void main() { 24 | vec3 normal = faceNormal(vViewPos); 25 | //... lighting 26 | } 27 | ``` 28 | 29 | Vertex: 30 | 31 | ```glsl 32 | varying vec3 vViewPos; 33 | 34 | void main() { 35 | vec4 pos = vec4(position, 1.0); 36 | vec4 mpos = modelViewMatrix * pos; 37 | gl_Position = projectionMatrix * mpos; 38 | vViewPos = -mpos.xyz; 39 | } 40 | ``` 41 | 42 | ## Usage 43 | 44 | [![NPM](https://nodei.co/npm/glsl-face-normal.png)](https://nodei.co/npm/glsl-face-normal/) 45 | 46 | #### `vec3 normal = faceNormal(vec3 pos)` 47 | 48 | Approximates the face `normal` from the given `pos`, which is typically the position in camera-space. 49 | 50 | For better precision, you can use the [eye relative position](http://www.enkisoftware.com/devlogpost-20150131-1-Normal_generation_in_the_pixel_shader.html) instead. 51 | 52 | ## License 53 | 54 | MIT. See [LICENSE.md](http://github.com/stackgl/glsl-face-normal/blob/master/LICENSE.md) for details. 55 | --------------------------------------------------------------------------------