├── .gitignore ├── .npmignore ├── LICENSE.md ├── README.md ├── index.glsl ├── package.json └── test.glsl /.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 | test.glsl 8 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | This software is released under the MIT license: 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | this software and associated documentation files (the "Software"), to deal in 5 | the Software without restriction, including without limitation the rights to 6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | the Software, and to permit persons to whom the Software is furnished to do so, 8 | subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 15 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 16 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 17 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # glsl-map 2 | 3 | Map a value between one range to another. For example, if you have a value of 0.3 mapped between 0 and 1, but you want it mapped between 0 and 255, you would use 4 | 5 | `map(0.3, 0.0, 1.0, 0.0, 255.0) = 76.5` 6 | 7 | ## Usage 8 | 9 | `float map(float value, float inMin, float inMax, float outMin, float outMax)` 10 | 11 | `vec2 map(vec2 value, vec2 inMin, vec2 inMax, vec2 outMin, vec2 outMax)` 12 | 13 | `vec3 map(vec3 value, vec3 inMin, vec3 inMax, vec3 outMin, vec3 outMax)` 14 | 15 | `vec4 map(vec4 value, vec4 inMin, vec4 inMax, vec4 outMin, vec4 outMax)` 16 | 17 | This applies no clamping, so if your input value is greater than inMax, you'll receive a value greater than outMax. 18 | 19 | ## License 20 | 21 | MIT. See [LICENSE.md](http://github.com/msfeldstein/glsl-map/blob/master/LICENSE.md) for details. 22 | -------------------------------------------------------------------------------- /index.glsl: -------------------------------------------------------------------------------- 1 | float map(float value, float inMin, float inMax, float outMin, float outMax) { 2 | return outMin + (outMax - outMin) * (value - inMin) / (inMax - inMin); 3 | } 4 | 5 | vec2 map(vec2 value, vec2 inMin, vec2 inMax, vec2 outMin, vec2 outMax) { 6 | return outMin + (outMax - outMin) * (value - inMin) / (inMax - inMin); 7 | } 8 | 9 | vec3 map(vec3 value, vec3 inMin, vec3 inMax, vec3 outMin, vec3 outMax) { 10 | return outMin + (outMax - outMin) * (value - inMin) / (inMax - inMin); 11 | } 12 | 13 | vec4 map(vec4 value, vec4 inMin, vec4 inMax, vec4 outMin, vec4 outMax) { 14 | return outMin + (outMax - outMin) * (value - inMin) / (inMax - inMin); 15 | } 16 | 17 | #pragma glslify: export(map) 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "glsl-map", 3 | "version": "1.0.1", 4 | "description": "Map a value from one range to another. Equivalent to Processing's map() function.", 5 | "main": "index.js", 6 | "license": "MIT", 7 | "scripts": { 8 | "test": "glslify test.glsl | glsl-testify | tap-spec" 9 | }, 10 | "author": { 11 | "name": "Mike and Johan", 12 | "email": "msfeldstein@gmail.com" 13 | }, 14 | "dependencies": {}, 15 | "devDependencies": { 16 | "glsl-testify": "^1.0.0", 17 | "glslify": "^1.6.0", 18 | "tap-spec": "^0.2.0" 19 | }, 20 | "repository": { 21 | "type": "git", 22 | "url": "git://github.com/msfeldstein/glsl-map.git" 23 | }, 24 | "keywords": [ 25 | "min", 26 | "max", 27 | "map", 28 | "mix", 29 | "glsl", 30 | "glslify", 31 | "shader", 32 | "interpolation" 33 | ], 34 | "homepage": "https://github.com/msfeldstein/glsl-map", 35 | "bugs": { 36 | "url": "https://github.com/msfeldstein/glsl-map/issues" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /test.glsl: -------------------------------------------------------------------------------- 1 | precision mediump float; 2 | 3 | #pragma glslify: map = require(./) 4 | 5 | bool test_3_0_10_0_1() { 6 | return map(3.0, 0.0, 10.0, 0.0, 1.0) == 0.3; 7 | } 8 | 9 | bool test_0_100_50() { 10 | return map(0.3, 0.0, 1.0, 0.0, 10.0) == 3.0; 11 | } 12 | 13 | bool test_out_of_range() { 14 | return map(2.0, 0.0, 1.0, 0.0, 10.0) == 20.0; 15 | } 16 | 17 | bool test_vec2() { 18 | return map(vec2(2.0), vec2(0.0), vec2(1.0), vec2(0.0), vec2(10.0)) == vec2(20.0); 19 | } 20 | 21 | bool test_vec2() { 22 | return map(vec2(2.0), vec2(0.0), vec2(1.0), vec2(0.0), vec2(10.0)) == vec2(20.0); 23 | } 24 | 25 | bool test_vec2() { 26 | return map(vec2(2.0), vec2(0.0), vec2(1.0), vec2(0.0), vec2(10.0)) == vec2(20.0); 27 | } 28 | 29 | --------------------------------------------------------------------------------