random_wgsl 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,19 @@
1
+
2
+ export interface WgslBundle {
3
+ /** name of the package, e.g. wgsl-rand */
4
+ name: string;
5
+
6
+ /** npm version of the package e.g. 0.4.1 */
7
+ version: string;
8
+
9
+ /** wesl edition of the code e.g. wesl_unstable_2024_1 */
10
+ edition: string;
11
+
12
+ /** map of wesl/wgsl modules:
13
+ * keys are file paths, relative to package root (e.g. "./lib.wgsl")
14
+ * values are wgsl/wesl code strings
15
+ */
16
+ modules: Record<string, string>;
17
+ }
18
+ export declare const wgslBundle: WgslBundle;
19
+ export default wgslBundle;
@@ -0,0 +1,12 @@
1
+
2
+ export const wgslBundle = {
3
+ "name": "random_wgsl",
4
+ "version": "0.1.0",
5
+ "edition": "wesl_unstable_2024_1",
6
+ "modules": {
7
+ "lib.wgsl": "// PCG pseudo random generator from vec2u to vec4f\n// the random output is in the range from zero to 1\nexport fn pcg_2u_3f(pos: vec2u) -> vec3f {\n let seed = mix2to3(pos);\n let random = pcg_3u_3u(seed);\n let normalized = ldexp(vec3f(random), vec3(-32));\n return vec3f(normalized);\n}\n\n// PCG random generator from vec3u to vec3u\n// adapted from http://www.jcgt.org/published/0009/03/02/\nexport fn pcg_3u_3u(seed: vec3u) -> vec3u {\n var v = seed * 1664525u + 1013904223u;\n\n v = mixing(v);\n v ^= v >> vec3(16u);\n v = mixing(v);\n\n return v;\n}\n\n// permuted lcg \nfn mixing(v: vec3u) -> vec3u {\n var m: vec3u = v;\n m.x += v.y * v.z;\n m.y += v.z * v.x;\n m.z += v.x * v.y;\n\n return m;\n}\n\n// mix position into a seed as per: https://www.shadertoy.com/view/XlGcRh\nfn mix2to3(p: vec2u) -> vec3u {\n let seed = vec3u(\n p.x,\n p.x ^ p.y,\n p.x + p.y,\n );\n return seed;\n}\n\n// from https://stackoverflow.com/questions/12964279/whats-the-origin-of-this-glsl-rand-one-liner\nexport fn sinRand(co: vec2f) -> f32 {\n return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);\n}"
8
+ }
9
+ }
10
+
11
+ export default wgslBundle;
12
+
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "random_wgsl",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "files": [
6
+ "src",
7
+ "dist"
8
+ ],
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/wgslBundle.d.ts",
12
+ "import": "./dist/wgslBundle.js"
13
+ }
14
+ }
15
+ }
package/src/lib.wgsl ADDED
@@ -0,0 +1,45 @@
1
+ // PCG pseudo random generator from vec2u to vec4f
2
+ // the random output is in the range from zero to 1
3
+ export fn pcg_2u_3f(pos: vec2u) -> vec3f {
4
+ let seed = mix2to3(pos);
5
+ let random = pcg_3u_3u(seed);
6
+ let normalized = ldexp(vec3f(random), vec3(-32));
7
+ return vec3f(normalized);
8
+ }
9
+
10
+ // PCG random generator from vec3u to vec3u
11
+ // adapted from http://www.jcgt.org/published/0009/03/02/
12
+ export fn pcg_3u_3u(seed: vec3u) -> vec3u {
13
+ var v = seed * 1664525u + 1013904223u;
14
+
15
+ v = mixing(v);
16
+ v ^= v >> vec3(16u);
17
+ v = mixing(v);
18
+
19
+ return v;
20
+ }
21
+
22
+ // permuted lcg
23
+ fn mixing(v: vec3u) -> vec3u {
24
+ var m: vec3u = v;
25
+ m.x += v.y * v.z;
26
+ m.y += v.z * v.x;
27
+ m.z += v.x * v.y;
28
+
29
+ return m;
30
+ }
31
+
32
+ // mix position into a seed as per: https://www.shadertoy.com/view/XlGcRh
33
+ fn mix2to3(p: vec2u) -> vec3u {
34
+ let seed = vec3u(
35
+ p.x,
36
+ p.x ^ p.y,
37
+ p.x + p.y,
38
+ );
39
+ return seed;
40
+ }
41
+
42
+ // from https://stackoverflow.com/questions/12964279/whats-the-origin-of-this-glsl-rand-one-liner
43
+ export fn sinRand(co: vec2f) -> f32 {
44
+ return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
45
+ }