@matboks/utilities 0.0.1 → 0.0.18

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.
@@ -1 +1 @@
1
- export declare const randomDefinition = "\nimport { TWO_PI, PI } from \"@/constants\";\n\nexport { \n random, random2, random3,\n randomGauss,\n randomUnit\n };\n\nuint hash(uint value) {\n value ^= value >> 16;\n value *= 569420461u;\n value ^= value >> 15;\n value *= 3545902487u;\n value ^= value >> 15;\n return value;\n}\n\nuint hash(uvec2 v) {\n return hash(v.x ^ hash(v.y));\n}\n\nuint hash(uvec3 v) {\n return hash(v.x ^ hash(v.y ^ hash(v.z)));\n}\n\nuint hash(uvec4 v) {\n return hash(v.x ^ hash(v.y ^ hash(v.z ^ hash(v.w))));\n}\n\nuint hash(uvec4 v, uint u) {\n return hash(v.x ^ hash(v.y ^ hash(v.z ^ hash(v.w ^ hash(u)))));\n}\n\nfloat hashToFloat(uint u) {\n uint mant = 8388607u;\n uint one = 1065353216u;\n\n u &= mant;\n u |= one;\n\n float f = uintBitsToFloat(u);\n return f - 1.0;\n}\n\nfloat random(float p) {\n return hashToFloat(hash(floatBitsToUint(p)));\n}\n\nfloat random(vec2 p) {\n return hashToFloat(hash(floatBitsToUint(p)));\n}\n\nfloat random(vec3 p) {\n return hashToFloat(hash(floatBitsToUint(p)));\n}\n\nfloat random(vec4 p) {\n return hashToFloat(hash(floatBitsToUint(p)));\n}\n\nfloat random(float p, float seed) {\n return random(vec2(p, seed));\n}\n\nfloat random(vec2 p, float seed) {\n return random(vec3(p, seed));\n}\n\nfloat random(vec3 p, float seed) {\n return random(vec4(p, seed));\n}\n\nfloat random(vec4 p, float seed) {\n return hashToFloat(hash(floatBitsToUint(p), floatBitsToUint(seed)));\n}\n\nvec2 random2(float p, float seed) {\n return vec2(random(vec3(p, seed, 0), random(vec3(p, seed, 1))));\n}\n\nvec2 random2(float p) {\n return random2(p, 0.);\n}\n\nvec2 random2(vec2 p, float seed) {\n return vec2(random(vec4(p, seed, 0)), random(vec4(p, seed, 1)));\n}\n\nvec2 random2(vec2 p) {\n return random2(p, 0.0);\n}\n\nvec2 random2(vec3 p, float seed) {\n return vec2(random(vec4(p, seed), 0.), random(vec4(p, seed), 1.));\n}\n\nvec2 random2(vec3 p) {\n return random2(p, 0.0);\n}\n\nvec3 random3(float p, float seed) {\n return vec3(\n random(vec3(p, seed, 0)), \n random(vec3(p, seed, 1)), \n random(vec3(p, seed, 2))\n );\n}\n\nvec3 random3(float p) {\n return random3(p, 0.0);\n}\n\nvec3 random3(vec2 p, float seed) {\n return vec3(random(vec4(p, seed, 0)), random(vec4(p, seed, 1)), random(vec4(p, seed, 2)));\n}\n\nvec3 random3(vec2 p) {\n return random3(p, 0.0);\n}\n\nvec3 random3(vec3 p, float seed) {\n return vec3(random(vec4(p, seed), 0.0), random(vec4(p, seed), 1.0), random(vec4(p, seed), 2.0));\n}\n\nfloat randomGaussian(vec3 p, float seed) {\n float u1 = random(vec4(p, 1), seed);\n float u2 = random(vec4(p, 2), seed);\n return sqrt(-2.0 * log(u1)) * cos(TWO_PI * u2);\n}\n\nvec2 randomUnit(vec2 p, float seed) {\n float direction = TWO_PI*random(p, seed);\n return vec2(cos(direction), sin(direction));\n}\n\nvec3 randomUnit(vec3 p, float seed) {\n return normalize(vec3(\n randomGaussian(p, seed),\n randomGaussian(p, random(seed, 1.)),\n randomGaussian(p, random(seed, 2.))\n ));\n}\n\n\n\n";
1
+ export declare const randomDefinition = "\nimport { TWO_PI, PI } from \"@/constants\";\n\nexport { \n random, random2, random3, random4,\n randomUnit\n };\n\nconst float SEED_OFFSET = 2123.1415926535897932384626433832795;\n\nuint hash(uint x) {\n x += 2127912214u;\n x ^= x >> 15;\n x *= 3545902487u;\n return x;\n}\n\nuint hash(uvec2 v) {\n return hash(v.x ^ hash(v.y));\n}\n\nuint hash(uvec3 v) {\n return hash(v.x ^ hash(v.y ^ hash(v.z)));\n}\n\nuint hash(uvec4 v) {\n return hash(v.x ^ hash(v.y ^ hash(v.z ^ hash(v.w))));\n}\n\nuint hash(uvec4 v, uint u) {\n return hash(v.x ^ hash(v.y ^ hash(v.z ^ hash(v.w ^ hash(u)))));\n}\n\nfloat hashToFloat(uint u) {\n uint mant = 8388607u;\n uint one = 1065353216u;\n\n u &= mant;\n u |= one;\n\n float f = uintBitsToFloat(u);\n return f - 1.0;\n}\n\nfloat random(float p) {\n return hashToFloat(hash(floatBitsToUint(p)));\n}\n\nfloat random(vec2 p) {\n return hashToFloat(hash(floatBitsToUint(p)));\n}\n\nfloat random(vec3 p) {\n return hashToFloat(hash(floatBitsToUint(p)));\n}\n\nfloat random(vec4 p) {\n return hashToFloat(hash(floatBitsToUint(p)));\n}\n\nfloat random(float p, float seed) {\n return random(vec2(p, seed));\n}\n\nfloat random(vec2 p, float seed) {\n return random(vec3(p, seed));\n}\n\nfloat random(vec3 p, float seed) {\n return random(vec4(p, seed));\n}\n\nfloat random(vec4 p, float seed) {\n return hashToFloat(hash(floatBitsToUint(p), floatBitsToUint(seed)));\n}\n\nvec2 random2(float p, float seed) {\n return vec2(\n random(p, seed), \n random(p, seed += SEED_OFFSET)\n );\n}\n\nvec2 random2(float p) {\n return random2(p, 0.);\n}\n\nvec2 random2(vec2 p, float seed) {\n return vec2(\n random(p, seed), \n random(p, seed += SEED_OFFSET)\n );\n}\n\nvec2 random2(vec2 p) {\n return random2(p, 0.0);\n}\n\nvec2 random2(vec3 p, float seed) {\n return vec2(\n random(p, seed), \n random(p, seed += SEED_OFFSET)\n );\n}\n\nvec2 random2(vec3 p) {\n return random2(p, 0.0);\n}\n\nvec3 random3(float p, float seed) {\n return vec3(\n random(p, seed), \n random(p, seed += SEED_OFFSET), \n random(p, seed += SEED_OFFSET)\n );\n}\n\nvec3 random3(float p) {\n return random3(p, 0.0);\n}\n\nvec3 random3(vec2 p, float seed) {\n return vec3(\n random(p, seed), \n random(p, seed += SEED_OFFSET), \n random(p, seed += SEED_OFFSET)\n );\n}\n\nvec3 random3(vec2 p) {\n return random3(p, 0.0);\n}\n\nvec3 random3(vec3 p, float seed) {\n return vec3(\n random(p, seed), \n random(p, seed += SEED_OFFSET), \n random(p, seed += SEED_OFFSET)\n );\n}\n\nvec4 random4(vec4 p, float seed) {\n return vec4(\n random(p, seed),\n random(p, seed += SEED_OFFSET),\n random(p, seed += SEED_OFFSET),\n random(p, seed += SEED_OFFSET)\n );\n}\n\nvec4 random4(vec4 p) {\n return random4(p, 0.0);\n}\n\nvec2 randomUnit(vec2 p, float seed) {\n float direction = TWO_PI*random(p, seed);\n return vec2(cos(direction), sin(direction));\n}\n\nvec3 randomUnit(vec3 p, float seed) {\n float z = -1.0 + 2.0*random(p, seed);\n float theta = TWO_PI*random(p, seed += SEED_OFFSET);\n float r = sqrt(1.0 - z*z);\n return vec3(\n r*cos(theta),\n r*sin(theta),\n z\n );\n}\n\nvec4 randomUnit(vec4 p, float seed) {\n float a1 = TWO_PI*random(p, seed);\n float a2 = TWO_PI*random(p, seed += SEED_OFFSET);\n float u = random(p, seed += SEED_OFFSET);\n float r1 = sqrt(u); \n float r2 = sqrt(1.0 - u);\n\n return vec4(\n cos(a1) * r1,\n sin(a1) * r1, \n cos(a2) * r2, \n sin(a2) * r2\n );\n}\n\n";
@@ -1,147 +1,186 @@
1
- export const randomDefinition = /*glsl*/ `
2
- import { TWO_PI, PI } from "@/constants";
3
-
4
- export {
5
- random, random2, random3,
6
- randomGauss,
7
- randomUnit
8
- };
9
-
10
- uint hash(uint value) {
11
- value ^= value >> 16;
12
- value *= 569420461u;
13
- value ^= value >> 15;
14
- value *= 3545902487u;
15
- value ^= value >> 15;
16
- return value;
17
- }
18
-
19
- uint hash(uvec2 v) {
20
- return hash(v.x ^ hash(v.y));
21
- }
22
-
23
- uint hash(uvec3 v) {
24
- return hash(v.x ^ hash(v.y ^ hash(v.z)));
25
- }
26
-
27
- uint hash(uvec4 v) {
28
- return hash(v.x ^ hash(v.y ^ hash(v.z ^ hash(v.w))));
29
- }
30
-
31
- uint hash(uvec4 v, uint u) {
32
- return hash(v.x ^ hash(v.y ^ hash(v.z ^ hash(v.w ^ hash(u)))));
33
- }
34
-
35
- float hashToFloat(uint u) {
36
- uint mant = 8388607u;
37
- uint one = 1065353216u;
38
-
39
- u &= mant;
40
- u |= one;
41
-
42
- float f = uintBitsToFloat(u);
43
- return f - 1.0;
44
- }
45
-
46
- float random(float p) {
47
- return hashToFloat(hash(floatBitsToUint(p)));
48
- }
49
-
50
- float random(vec2 p) {
51
- return hashToFloat(hash(floatBitsToUint(p)));
52
- }
53
-
54
- float random(vec3 p) {
55
- return hashToFloat(hash(floatBitsToUint(p)));
56
- }
57
-
58
- float random(vec4 p) {
59
- return hashToFloat(hash(floatBitsToUint(p)));
60
- }
61
-
62
- float random(float p, float seed) {
63
- return random(vec2(p, seed));
64
- }
65
-
66
- float random(vec2 p, float seed) {
67
- return random(vec3(p, seed));
68
- }
69
-
70
- float random(vec3 p, float seed) {
71
- return random(vec4(p, seed));
72
- }
73
-
74
- float random(vec4 p, float seed) {
75
- return hashToFloat(hash(floatBitsToUint(p), floatBitsToUint(seed)));
76
- }
77
-
78
- vec2 random2(float p, float seed) {
79
- return vec2(random(vec3(p, seed, 0), random(vec3(p, seed, 1))));
80
- }
81
-
82
- vec2 random2(float p) {
83
- return random2(p, 0.);
84
- }
85
-
86
- vec2 random2(vec2 p, float seed) {
87
- return vec2(random(vec4(p, seed, 0)), random(vec4(p, seed, 1)));
88
- }
89
-
90
- vec2 random2(vec2 p) {
91
- return random2(p, 0.0);
92
- }
93
-
94
- vec2 random2(vec3 p, float seed) {
95
- return vec2(random(vec4(p, seed), 0.), random(vec4(p, seed), 1.));
96
- }
97
-
98
- vec2 random2(vec3 p) {
99
- return random2(p, 0.0);
100
- }
101
-
102
- vec3 random3(float p, float seed) {
103
- return vec3(
104
- random(vec3(p, seed, 0)),
105
- random(vec3(p, seed, 1)),
106
- random(vec3(p, seed, 2))
107
- );
108
- }
109
-
110
- vec3 random3(float p) {
111
- return random3(p, 0.0);
112
- }
113
-
114
- vec3 random3(vec2 p, float seed) {
115
- return vec3(random(vec4(p, seed, 0)), random(vec4(p, seed, 1)), random(vec4(p, seed, 2)));
116
- }
117
-
118
- vec3 random3(vec2 p) {
119
- return random3(p, 0.0);
120
- }
121
-
122
- vec3 random3(vec3 p, float seed) {
123
- return vec3(random(vec4(p, seed), 0.0), random(vec4(p, seed), 1.0), random(vec4(p, seed), 2.0));
124
- }
125
-
126
- float randomGaussian(vec3 p, float seed) {
127
- float u1 = random(vec4(p, 1), seed);
128
- float u2 = random(vec4(p, 2), seed);
129
- return sqrt(-2.0 * log(u1)) * cos(TWO_PI * u2);
130
- }
131
-
132
- vec2 randomUnit(vec2 p, float seed) {
133
- float direction = TWO_PI*random(p, seed);
134
- return vec2(cos(direction), sin(direction));
135
- }
136
-
137
- vec3 randomUnit(vec3 p, float seed) {
138
- return normalize(vec3(
139
- randomGaussian(p, seed),
140
- randomGaussian(p, random(seed, 1.)),
141
- randomGaussian(p, random(seed, 2.))
142
- ));
143
- }
144
-
145
-
146
-
1
+ export const randomDefinition = /*glsl*/ `
2
+ import { TWO_PI, PI } from "@/constants";
3
+
4
+ export {
5
+ random, random2, random3, random4,
6
+ randomUnit
7
+ };
8
+
9
+ const float SEED_OFFSET = 2123.1415926535897932384626433832795;
10
+
11
+ uint hash(uint x) {
12
+ x += 2127912214u;
13
+ x ^= x >> 15;
14
+ x *= 3545902487u;
15
+ return x;
16
+ }
17
+
18
+ uint hash(uvec2 v) {
19
+ return hash(v.x ^ hash(v.y));
20
+ }
21
+
22
+ uint hash(uvec3 v) {
23
+ return hash(v.x ^ hash(v.y ^ hash(v.z)));
24
+ }
25
+
26
+ uint hash(uvec4 v) {
27
+ return hash(v.x ^ hash(v.y ^ hash(v.z ^ hash(v.w))));
28
+ }
29
+
30
+ uint hash(uvec4 v, uint u) {
31
+ return hash(v.x ^ hash(v.y ^ hash(v.z ^ hash(v.w ^ hash(u)))));
32
+ }
33
+
34
+ float hashToFloat(uint u) {
35
+ uint mant = 8388607u;
36
+ uint one = 1065353216u;
37
+
38
+ u &= mant;
39
+ u |= one;
40
+
41
+ float f = uintBitsToFloat(u);
42
+ return f - 1.0;
43
+ }
44
+
45
+ float random(float p) {
46
+ return hashToFloat(hash(floatBitsToUint(p)));
47
+ }
48
+
49
+ float random(vec2 p) {
50
+ return hashToFloat(hash(floatBitsToUint(p)));
51
+ }
52
+
53
+ float random(vec3 p) {
54
+ return hashToFloat(hash(floatBitsToUint(p)));
55
+ }
56
+
57
+ float random(vec4 p) {
58
+ return hashToFloat(hash(floatBitsToUint(p)));
59
+ }
60
+
61
+ float random(float p, float seed) {
62
+ return random(vec2(p, seed));
63
+ }
64
+
65
+ float random(vec2 p, float seed) {
66
+ return random(vec3(p, seed));
67
+ }
68
+
69
+ float random(vec3 p, float seed) {
70
+ return random(vec4(p, seed));
71
+ }
72
+
73
+ float random(vec4 p, float seed) {
74
+ return hashToFloat(hash(floatBitsToUint(p), floatBitsToUint(seed)));
75
+ }
76
+
77
+ vec2 random2(float p, float seed) {
78
+ return vec2(
79
+ random(p, seed),
80
+ random(p, seed += SEED_OFFSET)
81
+ );
82
+ }
83
+
84
+ vec2 random2(float p) {
85
+ return random2(p, 0.);
86
+ }
87
+
88
+ vec2 random2(vec2 p, float seed) {
89
+ return vec2(
90
+ random(p, seed),
91
+ random(p, seed += SEED_OFFSET)
92
+ );
93
+ }
94
+
95
+ vec2 random2(vec2 p) {
96
+ return random2(p, 0.0);
97
+ }
98
+
99
+ vec2 random2(vec3 p, float seed) {
100
+ return vec2(
101
+ random(p, seed),
102
+ random(p, seed += SEED_OFFSET)
103
+ );
104
+ }
105
+
106
+ vec2 random2(vec3 p) {
107
+ return random2(p, 0.0);
108
+ }
109
+
110
+ vec3 random3(float p, float seed) {
111
+ return vec3(
112
+ random(p, seed),
113
+ random(p, seed += SEED_OFFSET),
114
+ random(p, seed += SEED_OFFSET)
115
+ );
116
+ }
117
+
118
+ vec3 random3(float p) {
119
+ return random3(p, 0.0);
120
+ }
121
+
122
+ vec3 random3(vec2 p, float seed) {
123
+ return vec3(
124
+ random(p, seed),
125
+ random(p, seed += SEED_OFFSET),
126
+ random(p, seed += SEED_OFFSET)
127
+ );
128
+ }
129
+
130
+ vec3 random3(vec2 p) {
131
+ return random3(p, 0.0);
132
+ }
133
+
134
+ vec3 random3(vec3 p, float seed) {
135
+ return vec3(
136
+ random(p, seed),
137
+ random(p, seed += SEED_OFFSET),
138
+ random(p, seed += SEED_OFFSET)
139
+ );
140
+ }
141
+
142
+ vec4 random4(vec4 p, float seed) {
143
+ return vec4(
144
+ random(p, seed),
145
+ random(p, seed += SEED_OFFSET),
146
+ random(p, seed += SEED_OFFSET),
147
+ random(p, seed += SEED_OFFSET)
148
+ );
149
+ }
150
+
151
+ vec4 random4(vec4 p) {
152
+ return random4(p, 0.0);
153
+ }
154
+
155
+ vec2 randomUnit(vec2 p, float seed) {
156
+ float direction = TWO_PI*random(p, seed);
157
+ return vec2(cos(direction), sin(direction));
158
+ }
159
+
160
+ vec3 randomUnit(vec3 p, float seed) {
161
+ float z = -1.0 + 2.0*random(p, seed);
162
+ float theta = TWO_PI*random(p, seed += SEED_OFFSET);
163
+ float r = sqrt(1.0 - z*z);
164
+ return vec3(
165
+ r*cos(theta),
166
+ r*sin(theta),
167
+ z
168
+ );
169
+ }
170
+
171
+ vec4 randomUnit(vec4 p, float seed) {
172
+ float a1 = TWO_PI*random(p, seed);
173
+ float a2 = TWO_PI*random(p, seed += SEED_OFFSET);
174
+ float u = random(p, seed += SEED_OFFSET);
175
+ float r1 = sqrt(u);
176
+ float r2 = sqrt(1.0 - u);
177
+
178
+ return vec4(
179
+ cos(a1) * r1,
180
+ sin(a1) * r1,
181
+ cos(a2) * r2,
182
+ sin(a2) * r2
183
+ );
184
+ }
185
+
147
186
  `;
@@ -1,54 +1,54 @@
1
- export const rayMarchingDefinition = /*glsl*/ `
2
-
3
- export {
4
- rayMarch,
5
- Ray, RayMarchResult
6
- }
7
-
8
- struct Ray {
9
- vec3 origin;
10
- vec3 direction;
11
- };
12
-
13
- struct RayMarchResult {
14
- vec3 position;
15
- vec3 partialPosition;
16
- float minimumDistance;
17
- bool hit;
18
- };
19
-
20
- RayMarchResult rayMarch(Ray ray, float minThreshold, float maxThreshold, int partialIterations, float maxIterations, float sdf(vec3)) {
21
- vec3 position;
22
- float t = 0.0;
23
-
24
- vec3 origin = ray.origin;
25
- vec3 direction = ray.direction;
26
-
27
- vec3 partialPosition;
28
- float minimumDistance = 1e20;
29
- bool hit = false;
30
-
31
- int n = int(maxIterations) + 1;
32
- for (int i = 0; i <= n; i++) {
33
- position = origin + direction*t;
34
- float d = sdf(position);
35
-
36
- if (i == partialIterations) partialPosition = position;
37
-
38
- t += d*min(1.0, maxIterations - float(i));
39
- minimumDistance = min(minimumDistance, d);
40
-
41
- if (abs(d) < minThreshold) {
42
- hit = true;
43
- break;
44
- }
45
-
46
- if (d > maxThreshold) {
47
- break;
48
- }
49
- }
50
-
51
- return RayMarchResult(position, partialPosition, minimumDistance, hit);
52
- }
53
-
1
+ export const rayMarchingDefinition = /*glsl*/ `
2
+
3
+ export {
4
+ rayMarch,
5
+ Ray, RayMarchResult
6
+ }
7
+
8
+ struct Ray {
9
+ vec3 origin;
10
+ vec3 direction;
11
+ };
12
+
13
+ struct RayMarchResult {
14
+ vec3 position;
15
+ vec3 partialPosition;
16
+ float minimumDistance;
17
+ bool hit;
18
+ };
19
+
20
+ RayMarchResult rayMarch(Ray ray, float minThreshold, float maxThreshold, int partialIterations, float maxIterations, float sdf(vec3)) {
21
+ vec3 position;
22
+ float t = 0.0;
23
+
24
+ vec3 origin = ray.origin;
25
+ vec3 direction = ray.direction;
26
+
27
+ vec3 partialPosition;
28
+ float minimumDistance = 1e20;
29
+ bool hit = false;
30
+
31
+ int n = int(maxIterations) + 1;
32
+ for (int i = 0; i <= n; i++) {
33
+ position = origin + direction*t;
34
+ float d = sdf(position);
35
+
36
+ if (i == partialIterations) partialPosition = position;
37
+
38
+ t += d*min(1.0, maxIterations - float(i));
39
+ minimumDistance = min(minimumDistance, d);
40
+
41
+ if (abs(d) < minThreshold) {
42
+ hit = true;
43
+ break;
44
+ }
45
+
46
+ if (d > maxThreshold) {
47
+ break;
48
+ }
49
+ }
50
+
51
+ return RayMarchResult(position, partialPosition, minimumDistance, hit);
52
+ }
53
+
54
54
  `;