Skip to content

Commit 95dd343

Browse files
committed
faster noiseSeed, new randomGenerator
1 parent 9540542 commit 95dd343

4 files changed

Lines changed: 59 additions & 14 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ p5.js version used is **1.1.9**.
298298
| Misc | p5.js | q5.js |
299299
|---|---|---|
300300
| Generating 10,000 `randomGaussian()` sample | 10FPS | 20FPS|
301+
| Calling `noiseSeed()` 1,000 times | 10FPS | 60FPS |
301302
| Generate 10,000 (random) colors with `color(r,g,b)` | 5FPS | 60FPS |
302303
| Rotate a `Vector` 1,000,000 times | 13FPS | 60FPS |
303304

package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "q5xjs",
3+
"version": "0.0.1",
4+
"description": "A small and fast alternative (experimental) implementation of p5.js",
5+
"repository": {
6+
"type": "git",
7+
"url": "git+https://github.com/LingDong-/q5xjs.git"
8+
}
9+
}

q5.js

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ function Q5(scope){
116116

117117
$.VIDEO = {video:true,audio:false};
118118
$.AUDIO = {video:false,audio:true};
119+
120+
$.SHR3 = 1;
121+
$.LCG = 2;
119122

120123
//================================================================
121124
// HINTS
@@ -1687,7 +1690,7 @@ function Q5(scope){
16871690
var PERLIN_YWRAPB = 4; var PERLIN_YWRAP = 1<<PERLIN_YWRAPB;
16881691
var PERLIN_ZWRAPB = 8; var PERLIN_ZWRAP = 1<<PERLIN_ZWRAPB;
16891692
var PERLIN_SIZE = 4095;
1690-
var perlin_octaves = 10;var perlin_amp_falloff = 0.5;
1693+
var perlin_octaves = 4;var perlin_amp_falloff = 0.5;
16911694
var scaled_cosine = function(i) {return 0.5*(1.0-Math.cos(i*Math.PI));};
16921695
var p_perlin;
16931696

@@ -1752,28 +1755,60 @@ function Q5(scope){
17521755
}
17531756
};
17541757
};
1755-
let lcg1 = Lcg();
1756-
let lcg2 = Lcg();
1757-
lcg1.setSeed(Math.random()*4294967296);
1758-
lcg2.setSeed(Math.random()*4294967296);
1758+
const Shr3 = function(){
1759+
let jsr, seed;
1760+
let m = 4294967295;
1761+
return {
1762+
setSeed(val){
1763+
jsr = seed = (val == null ? Math.random() * m : val) >>> 0;
1764+
},
1765+
getSeed() {
1766+
return seed;
1767+
},
1768+
rand() {
1769+
jsr^=(jsr<<17);
1770+
jsr^=(jsr>>13);
1771+
jsr^=(jsr<<5);
1772+
return (jsr>>>0)/m;
1773+
}
1774+
}
1775+
}
1776+
let rng1 = Shr3();
1777+
rng1.setSeed();
1778+
17591779
$.noiseSeed = function(seed) {
1760-
lcg2.setSeed(seed);
1761-
p_perlin = new Array(PERLIN_SIZE + 1);
1762-
for (var i = 0; i < PERLIN_SIZE + 1; i++) {p_perlin[i] = lcg2.rand();}
1780+
let jsr = (seed == undefined) ? (Math.random()*4294967295) : seed;
1781+
if (!p_perlin){
1782+
p_perlin = new Float32Array(PERLIN_SIZE + 1);
1783+
}
1784+
for (var i = 0; i < PERLIN_SIZE + 1; i++) {
1785+
jsr^=(jsr<<17);
1786+
jsr^=(jsr>>13);
1787+
jsr^=(jsr<<5);
1788+
p_perlin[i] = (jsr>>>0)/4294967295;
1789+
}
17631790
};
17641791
$.randomSeed = function(seed){
1765-
lcg1.setSeed(seed);
1792+
rng1.setSeed(seed);
17661793
}
17671794
$.random = function(a,b){
17681795
if (typeof a == 'number'){
17691796
if (b != undefined){
1770-
return lcg1.rand()*(b-a)+a;
1797+
return rng1.rand()*(b-a)+a;
17711798
}else{
1772-
return lcg1.rand()*a;
1799+
return rng1.rand()*a;
17731800
}
17741801
}else{
1775-
return a[~~(a.length*lcg1.rand())];
1802+
return a[~~(a.length*rng1.rand())];
1803+
}
1804+
}
1805+
$.randomGenerator = function(method){
1806+
if (method == $.LCG){
1807+
rng1 = Lcg();
1808+
}else if (method == $.SHR3){
1809+
rng1 = Shr3();
17761810
}
1811+
rng1.setSeed();
17771812
}
17781813

17791814
var ziggurat = new function() {
@@ -1788,7 +1823,7 @@ function Q5(scope){
17881823
var we = new Array(256);
17891824
var fe = new Array(256);
17901825
var SHR3 = function() {
1791-
return lcg1.rand()*4294967296-2147483648;
1826+
return rng1.rand()*4294967296-2147483648;
17921827
};
17931828
var UNI = function() {
17941829
return 0.5 + (SHR3()<<0) * 0.2328306e-9;

q5.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)