├── .gitignore ├── LICENSE ├── README.md ├── example └── example.js ├── index.glsl └── 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) 2014 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 | # glsl-diffuse-lambert 2 | 3 | A simple Lambertian diffuse lighting model (somewhat trivial, but included for completeness). 4 | 5 | # Example 6 | 7 | ```glsl 8 | #pragma glslify: lambert = require(glsl-diffuse-lambert) 9 | 10 | uniform vec3 lightPosition; 11 | 12 | varying vec3 surfacePosition, surfaceNormal; 13 | 14 | void main() { 15 | //Light geometry 16 | vec3 lightDirection = normalize(lightPosition - surfacePosition); 17 | 18 | //Surface properties 19 | vec3 normal = normalize(surfaceNormal); 20 | 21 | //Compute diffuse light intensity 22 | float power = lambert( 23 | lightDirection, 24 | normal); 25 | 26 | gl_FragColor = vec4(power,power,power,1.0); 27 | } 28 | ``` 29 | 30 | # Usage 31 | 32 | Install with npm: 33 | 34 | ``` 35 | npm install glsl-diffuse-lambert 36 | ``` 37 | 38 | Then use with [glslify](https://github.com/stackgl/glslify). 39 | 40 | # API 41 | 42 | ```glsl 43 | #pragma glslify: lambert = require(glsl-diffuse-lambert) 44 | ``` 45 | 46 | ##### `float lambert(vec3 lightDir, vec3 normal)` 47 | Computes the diffuse intensity in the Lambertian model 48 | 49 | * `lightDir` is a *unit length* `vec3` pointing from the surface point toward the light 50 | * `normal` is the *unit length* surface normal at the sample point 51 | 52 | **Returns** A `float` representing the diffuse light intensity 53 | 54 | # License 55 | (c) 2014 Mikola Lysenko. MIT License -------------------------------------------------------------------------------- /example/example.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | var glslify = require('glslify') 4 | var drawTriangle = require('a-big-triangle') 5 | 6 | var createShader = glslify({ 7 | vert: '\ 8 | attribute vec2 position;\ 9 | varying vec2 uv;\ 10 | void main() {\ 11 | uv = position;\ 12 | gl_Position = vec4(position,0,1);\ 13 | }', 14 | frag: '\ 15 | precision mediump float;\n\ 16 | #pragma glslify: diffuse = require(../index.glsl)\n\ 17 | varying vec2 uv;\n\ 18 | uniform vec3 lightPosition;\n\ 19 | void main() {\n\ 20 | float r = sqrt(dot(uv,uv));\n\ 21 | float theta = atan(uv.y,uv.x);\n\ 22 | float phi = asin(r);\n\ 23 | vec3 normal = vec3(\ 24 | cos(theta)*sin(phi),\ 25 | sin(theta)*sin(phi),\ 26 | -cos(phi));\n\ 27 | vec3 position = vec3(normal.xy, normal.z+5.0);\n\ 28 | vec3 lightDir = normalize(lightPosition - position);\n\ 29 | vec3 eyeDir = normalize(-position);\n\ 30 | if(r > 1.0 || dot(normal,eyeDir)<0.0) {\n\ 31 | gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0);\n\ 32 | } else {\n\ 33 | float power = diffuse(lightDir, normal);\n\ 34 | gl_FragColor = vec4(power,power,power, 1.0);\n\ 35 | }\n\ 36 | }', 37 | inline: true 38 | }) 39 | 40 | var canvas = document.createElement('canvas') 41 | canvas.width = window.innerWidth 42 | canvas.height = window.innerHeight 43 | canvas.style.left = "0px" 44 | canvas.style.top = "0px" 45 | canvas.style.position = "absolute" 46 | document.body.appendChild(canvas) 47 | 48 | var gl = canvas.getContext('webgl') 49 | var shader = createShader(gl) 50 | 51 | function render() { 52 | shader.bind() 53 | var theta = Date.now()*0.0005 54 | var x = Math.cos(theta) 55 | var y = Math.sin(theta) 56 | shader.uniforms.lightPosition = [30.0*x, 10.0, 30.0*y] 57 | drawTriangle(gl) 58 | requestAnimationFrame(render) 59 | } 60 | 61 | render() -------------------------------------------------------------------------------- /index.glsl: -------------------------------------------------------------------------------- 1 | float lambertDiffuse( 2 | vec3 lightDirection, 3 | vec3 surfaceNormal) { 4 | return max(0.0, dot(lightDirection, surfaceNormal)); 5 | } 6 | 7 | #pragma glslify: export(lambertDiffuse) -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "glsl-diffuse-lambert", 3 | "version": "1.0.0", 4 | "description": "Lambertian diffuse lighting for GLSL", 5 | "main": "index.js", 6 | "directories": { 7 | "example": "example" 8 | }, 9 | "dependencies": { 10 | }, 11 | "devDependencies": { 12 | "a-big-triangle": "^1.0.0", 13 | "beefy": "^2.1.1", 14 | "glslify": "^1.6.0" 15 | }, 16 | "scripts": { 17 | "test": "beefy example/example.js --open" 18 | }, 19 | "repository": { 20 | "type": "git", 21 | "url": "git://github.com/stackgl/glsl-diffuse-lambert.git" 22 | }, 23 | "keywords": [ 24 | "glsl", 25 | "lambert", 26 | "diffuse", 27 | "lighting", 28 | "glslify" 29 | ], 30 | "author": "Mikola Lysenko", 31 | "license": "MIT", 32 | "bugs": { 33 | "url": "https://github.com/stackgl/glsl-diffuse-lambert/issues" 34 | }, 35 | "browserify": { 36 | "transform": "glslify" 37 | }, 38 | "homepage": "https://github.com/stackgl/glsl-diffuse-lambert" 39 | } 40 | --------------------------------------------------------------------------------