├── .gitignore ├── LICENSE ├── README.md ├── circle.glsl ├── package-lock.json ├── package.json ├── polygon.glsl └── rectangle.glsl /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Florian Morel 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # glsl-2d-primitives 2 | 3 | [![stable](http://badges.github.io/stability-badges/dist/stable.svg)](http://github.com/badges/stability-badges) 4 | 5 | SDF functions to draw 2d shapes in glsl. 6 | Antialiased using [glsl-aastep](https://github.com/glslify/glsl-aastep). 7 | 8 | ![glsl-2d-primitives](https://i.imgur.com/qJmmSxA.jpg) 9 | 10 | ### Installation :package: 11 | 12 | ```bash 13 | npm i glsl-2d-primitives -S 14 | ``` 15 | 16 | ### Usagez :book: 17 | 18 | #### circle(vec2 st, float radius) 19 | #### rectangle(vec2 st, vec2 size) 20 | #### polygon(vec2 st, float radius, float sides) 21 | 22 | ### Example :floppy_disk: 23 | 24 | ```glsl 25 | #pragma glslify: circle = require(glsl-2d-primitives/circle) 26 | #pragma glslify: rectangle = require(glsl-2d-primitives/rectangle) 27 | #pragma glslify: polygon = require(glsl-2d-primitives/polygon) 28 | 29 | attribute vec2 vUv; 30 | uniform sampler2D map; 31 | 32 | void main() { 33 | gl_FragColor = texture2D(map, vUv); 34 | // Mask the image with a centered rectangle 35 | gl_FragColor.a = rectangle(vUv, vec2(0.4, 0.6)); 36 | 37 | // Draw a pentagon, without alpha masking 38 | gl.FragColor = vec4(vec3(polygon(vUv, 0.4, 5.0)), 1.0); 39 | 40 | // Mask the image with a ring, combining 2 circles 41 | float ring = circle(vUv, 0.5) * (1.0 - circle(vUv, 0.52)); 42 | gl_FragColor.a = ring; 43 | } 44 | ``` 45 | 46 | [Demo](http://thebookofshaders.com/edit.php?log=180326195904) 47 | 48 | ### See also 49 | - https://thebookofshaders.com/07/ 50 | - https://github.com/glslify/glsl-aastep 51 | - https://github.com/marklundin/glsl-sdf-primitives 52 | 53 | ### License :pencil: 54 | 55 | MIT. See [LICENSE](http://github.com/ayamflow/glsl-layer/blob/master/LICENSE) for details. -------------------------------------------------------------------------------- /circle.glsl: -------------------------------------------------------------------------------- 1 | #pragma glslify: aastep = require(glsl-aastep) 2 | 3 | float circle(vec2 st, float radius) { 4 | return aastep(radius, length(st - vec2(0.5))); 5 | } 6 | 7 | #pragma glslify: export(circle); 8 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "glsl-2d-primitives", 3 | "version": "1.0.2", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "glsl-aastep": { 8 | "version": "1.0.1", 9 | "resolved": "https://registry.npmjs.org/glsl-aastep/-/glsl-aastep-1.0.1.tgz", 10 | "integrity": "sha1-7q8Wlh5HLJuLPZZdCAq47B1Ma7g=" 11 | }, 12 | "glsl-constants": { 13 | "version": "1.0.0", 14 | "resolved": "https://registry.npmjs.org/glsl-constants/-/glsl-constants-1.0.0.tgz", 15 | "integrity": "sha512-cC/sxTrHutIjeJlPiMLaHCCIkm2MaMgE2VmBjS5uiHzEOS3pCBFFpqWq2TGEhwKRYL73aHKuqHVWHp3ThnJd2A==" 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "glsl-2d-primitives", 3 | "version": "1.0.3", 4 | "description": "SDF functions to draw 2D shapes in glsl.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/ayamflow/glsl-2d-primitives.git" 12 | }, 13 | "keywords": [ 14 | "sdf", 15 | "glsl", 16 | "2d", 17 | "circle", 18 | "box", 19 | "triangle", 20 | "polygon", 21 | "shape", 22 | "signed", 23 | "distance", 24 | "field" 25 | ], 26 | "author": "Florian Morel", 27 | "license": "MIT", 28 | "bugs": { 29 | "url": "https://github.com/ayamflow/glsl-2d-primitives/issues" 30 | }, 31 | "homepage": "https://github.com/ayamflow/glsl-2d-primitives#readme", 32 | "dependencies": { 33 | "glsl-aastep": "^1.0.1", 34 | "glsl-constants": "^1.0.0" 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /polygon.glsl: -------------------------------------------------------------------------------- 1 | #pragma glslify: PI = require(glsl-constants/PI) 2 | #pragma glslify: TWO_PI = require(glsl-constants/TWO_PI) 3 | #pragma glslify: aastep = require(glsl-aastep) 4 | 5 | float polygon(vec2 st, float radius, float sides) { 6 | st = st * 2.0 - 1.0; 7 | float angle = atan(st.x,st.y) + PI; 8 | float slice = TWO_PI / sides; 9 | 10 | return aastep(radius, cos(floor(0.5 + angle / slice ) * slice - angle) * length(st)); 11 | } 12 | 13 | #pragma glslify: export(polygon); 14 | -------------------------------------------------------------------------------- /rectangle.glsl: -------------------------------------------------------------------------------- 1 | #pragma glslify: aastep = require(glsl-aastep) 2 | 3 | float rectangle(vec2 st, vec2 size) { 4 | size = vec2(0.5) - size * 0.5; 5 | vec2 uv = vec2(aastep(size.x, st.x), aastep(size.y, st.y)); 6 | uv *= vec2(aastep(size.x, 1.0 - st.x), aastep(size.y, 1.0 - st.y)); 7 | 8 | return uv.x * uv.y; 9 | } 10 | 11 | #pragma glslify: export(rectangle); 12 | --------------------------------------------------------------------------------