├── .gitignore ├── .editorconfig ├── package.json ├── license ├── readme.md └── index.glsl /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | *.log 4 | .vscode 5 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | indent_style = tab 6 | indent_size = 2 7 | end_of_line = lf 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | 12 | [{package.json}] 13 | indent_style = space 14 | indent_size = 2 15 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "glsl-pcg-prng", 3 | "version": "1.0.2", 4 | "description": "Glsl implementation of the PCG algorithm for the generation of random numbers.", 5 | "main": "index.js", 6 | "scripts": { 7 | "release": "np" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git://github.com/riccardoscalco/glsl-pcg-prng.git" 12 | }, 13 | "bugs": { 14 | "url": "https://github.com/riccardoscalco/glsl-pcg-prng/issues" 15 | }, 16 | "keywords": [ 17 | "glsl", 18 | "pcg", 19 | "prng", 20 | "random", 21 | "hash", 22 | "random number generator", 23 | "hash function", 24 | "webgl2", 25 | "glslify", 26 | "math" 27 | ], 28 | "license": "MIT", 29 | "devDependencies": { 30 | "np": "^7.0.0" 31 | }, 32 | "publishConfig": { 33 | "access": "public" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | Copyright 2020 Riccardo Scalco 2 | 3 | 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: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | 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. 8 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # glsl-pcg-prng 2 | 3 | PCG random number generators ported to an NPM package, so that you can require it from glslify. 4 | The code is based (mostly copied) from https://www.shadertoy.com/view/XlGcRh by Mark Jarzynski. 5 | 6 | References: 7 | 8 | * Mark Jarzynski and Marc Olano, Hash Functions for GPU Rendering, Journal of 9 | Computer Graphics Techniques (JCGT), vol. 9, no. 3, 21-38, 2020 10 | Available online http://jcgt.org/published/0009/03/02/ 11 | * https://www.pcg-random.org/ 12 | 13 | ## Install 14 | 15 | ```sh 16 | npm install glsl-pcg-prng 17 | ``` 18 | 19 | ## Usage 20 | 21 | Note that **glsl-pcg-prng** needs OpenGL ES 3.0 (WebGL 2.0). 22 | 23 | ```glsl 24 | #pragma glslify: prng = require(glsl-pcg-prng) 25 | 26 | // Create a seed 27 | vec4 seed = vec4(1000., 2000., 3000., 4000.); 28 | 29 | // Return one random number, it takes a float or a vec2 as input 30 | float r1 = prng(seed.x); 31 | float r2 = prng(seed.xy); 32 | 33 | // Return three random numbers 34 | vec3 r3 = prng(seed.xyz); 35 | 36 | // Return four random numbers 37 | vec4 r4 = prng(seed.xyzw); 38 | ``` 39 | 40 | ## Demo 41 | 42 | * https://github.com/riccardoscalco/glsl-pcg-example 43 | * https://observablehq.com/@riccardoscalco/pcg-random-number-generators-in-glsl. 44 | -------------------------------------------------------------------------------- /index.glsl: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | 4 | PCG random number generators ported to an NPM package, so that you can require it from glslify. 5 | 6 | The code is based (mostly copied) from https://www.shadertoy.com/view/XlGcRh by Mark Jarzynski. 7 | 8 | References: 9 | 10 | * Mark Jarzynski and Marc Olano, Hash Functions for GPU Rendering, Journal of 11 | Computer Graphics Techniques (JCGT), vol. 9, no. 3, 21-38, 2020 12 | Available online http://jcgt.org/published/0009/03/02/ 13 | 14 | * https://www.pcg-random.org/ 15 | 16 | */ 17 | 18 | uint pcg(uint v) { 19 | uint state = v * uint(747796405) + uint(2891336453); 20 | uint word = ((state >> ((state >> uint(28)) + uint(4))) ^ state) * uint(277803737); 21 | return (word >> uint(22)) ^ word; 22 | } 23 | 24 | float prng (float p) { 25 | return float(pcg(uint(p))) / float(uint(0xffffffff)); 26 | } 27 | 28 | float prng (vec2 p) { 29 | return float(pcg(pcg(uint(p.x)) + uint(p.y))) / float(uint(0xffffffff)); 30 | } 31 | 32 | uvec3 pcg(uvec3 v) { 33 | v = v * uint(1664525) + uint(1013904223); 34 | 35 | v.x += v.y * v.z; 36 | v.y += v.z * v.x; 37 | v.z += v.x * v.y; 38 | 39 | v ^= v >> uint(16); 40 | 41 | v.x += v.y * v.z; 42 | v.y += v.z * v.x; 43 | v.z += v.x * v.y; 44 | 45 | return v; 46 | } 47 | 48 | vec3 prng (vec3 p) { 49 | return vec3(pcg(uvec3(p))) / float(uint(0xffffffff)); 50 | } 51 | 52 | uvec4 pcg(uvec4 v) { 53 | v = v * uint(1664525) + uint(1013904223); 54 | 55 | v.x += v.y * v.w; 56 | v.y += v.z * v.x; 57 | v.z += v.x * v.y; 58 | v.w += v.y * v.z; 59 | 60 | v.x += v.y * v.w; 61 | v.y += v.z * v.x; 62 | v.z += v.x * v.y; 63 | v.w += v.y * v.z; 64 | 65 | v = v ^ (v >> uint(16)); 66 | 67 | return v; 68 | } 69 | 70 | vec4 prng (vec4 p) { 71 | return vec4(pcg(uvec4(p))) / float(uint(0xffffffff)); 72 | } 73 | 74 | #pragma glslify: export(prng) 75 | --------------------------------------------------------------------------------