@matboks/utilities 0.0.1

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.
Files changed (177) hide show
  1. package/dist/constants.d.ts +1 -0
  2. package/dist/constants.js +1 -0
  3. package/dist/geometry/aabb.d.ts +40 -0
  4. package/dist/geometry/aabb.js +143 -0
  5. package/dist/geometry/cut-polygon.d.ts +7 -0
  6. package/dist/geometry/cut-polygon.js +116 -0
  7. package/dist/geometry/hollowgon.d.ts +24 -0
  8. package/dist/geometry/hollowgon.js +156 -0
  9. package/dist/geometry/index.d.ts +13 -0
  10. package/dist/geometry/index.js +14 -0
  11. package/dist/geometry/line.d.ts +10 -0
  12. package/dist/geometry/line.js +26 -0
  13. package/dist/geometry/polygon-operations.d.ts +31 -0
  14. package/dist/geometry/polygon-operations.js +159 -0
  15. package/dist/geometry/polygon.d.ts +55 -0
  16. package/dist/geometry/polygon.js +353 -0
  17. package/dist/geometry/polyline.d.ts +23 -0
  18. package/dist/geometry/polyline.js +100 -0
  19. package/dist/geometry/ray.d.ts +6 -0
  20. package/dist/geometry/ray.js +6 -0
  21. package/dist/geometry/ray3.d.ts +7 -0
  22. package/dist/geometry/ray3.js +10 -0
  23. package/dist/geometry/segment.d.ts +16 -0
  24. package/dist/geometry/segment.js +50 -0
  25. package/dist/geometry/shared.d.ts +5 -0
  26. package/dist/geometry/shared.js +60 -0
  27. package/dist/geometry/transformations.d.ts +7 -0
  28. package/dist/geometry/transformations.js +28 -0
  29. package/dist/geometry/vec2.d.ts +71 -0
  30. package/dist/geometry/vec2.js +225 -0
  31. package/dist/geometry/vec3.d.ts +102 -0
  32. package/dist/geometry/vec3.js +256 -0
  33. package/dist/geometry/vec4.d.ts +71 -0
  34. package/dist/geometry/vec4.js +238 -0
  35. package/dist/index.d.ts +11 -0
  36. package/dist/index.js +11 -0
  37. package/dist/math/clamp.d.ts +1 -0
  38. package/dist/math/clamp.js +5 -0
  39. package/dist/math/fract.d.ts +1 -0
  40. package/dist/math/fract.js +3 -0
  41. package/dist/math/index.d.ts +8 -0
  42. package/dist/math/index.js +8 -0
  43. package/dist/math/mix.d.ts +1 -0
  44. package/dist/math/mix.js +3 -0
  45. package/dist/math/mod.d.ts +1 -0
  46. package/dist/math/mod.js +5 -0
  47. package/dist/math/signed.d.ts +1 -0
  48. package/dist/math/signed.js +3 -0
  49. package/dist/math/smoothstep.d.ts +3 -0
  50. package/dist/math/smoothstep.js +24 -0
  51. package/dist/math/split-into-int-and-fract.d.ts +1 -0
  52. package/dist/math/split-into-int-and-fract.js +5 -0
  53. package/dist/math/units.d.ts +2 -0
  54. package/dist/math/units.js +6 -0
  55. package/dist/noise/idw.d.ts +11 -0
  56. package/dist/noise/idw.js +10 -0
  57. package/dist/noise/index.d.ts +6 -0
  58. package/dist/noise/index.js +6 -0
  59. package/dist/noise/perlin.d.ts +5 -0
  60. package/dist/noise/perlin.js +29 -0
  61. package/dist/noise/random.d.ts +13 -0
  62. package/dist/noise/random.js +39 -0
  63. package/dist/noise/value.d.ts +4 -0
  64. package/dist/noise/value.js +20 -0
  65. package/dist/noise/voronoise.d.ts +10 -0
  66. package/dist/noise/voronoise.js +26 -0
  67. package/dist/noise/worley.d.ts +4 -0
  68. package/dist/noise/worley.js +39 -0
  69. package/dist/shader-modules/index.d.ts +2 -0
  70. package/dist/shader-modules/index.js +2 -0
  71. package/dist/shader-modules/library.d.ts +2 -0
  72. package/dist/shader-modules/library.js +36 -0
  73. package/dist/shader-modules/modules/camera.d.ts +1 -0
  74. package/dist/shader-modules/modules/camera.js +43 -0
  75. package/dist/shader-modules/modules/color/blend.d.ts +1 -0
  76. package/dist/shader-modules/modules/color/blend.js +108 -0
  77. package/dist/shader-modules/modules/color/index.d.ts +1 -0
  78. package/dist/shader-modules/modules/color/index.js +135 -0
  79. package/dist/shader-modules/modules/constants.d.ts +1 -0
  80. package/dist/shader-modules/modules/constants.js +14 -0
  81. package/dist/shader-modules/modules/geometry.d.ts +1 -0
  82. package/dist/shader-modules/modules/geometry.js +110 -0
  83. package/dist/shader-modules/modules/math.d.ts +1 -0
  84. package/dist/shader-modules/modules/math.js +19 -0
  85. package/dist/shader-modules/modules/noise.d.ts +1 -0
  86. package/dist/shader-modules/modules/noise.js +410 -0
  87. package/dist/shader-modules/modules/random.d.ts +1 -0
  88. package/dist/shader-modules/modules/random.js +147 -0
  89. package/dist/shader-modules/modules/ray-marching.d.ts +1 -0
  90. package/dist/shader-modules/modules/ray-marching.js +54 -0
  91. package/dist/shader-modules/modules/sdf/index.d.ts +1 -0
  92. package/dist/shader-modules/modules/sdf/index.js +183 -0
  93. package/dist/shader-modules/modules/sdf/operations.d.ts +1 -0
  94. package/dist/shader-modules/modules/sdf/operations.js +77 -0
  95. package/dist/shader-modules/modules/utils.d.ts +1 -0
  96. package/dist/shader-modules/modules/utils.js +115 -0
  97. package/dist/shader-modules/registry.d.ts +2 -0
  98. package/dist/shader-modules/registry.js +7 -0
  99. package/dist/shader-modules/shaders.d.ts +1 -0
  100. package/dist/shader-modules/shaders.js +109 -0
  101. package/dist/shader-renderer/helpers.d.ts +10 -0
  102. package/dist/shader-renderer/helpers.js +19 -0
  103. package/dist/shader-renderer/index.d.ts +2 -0
  104. package/dist/shader-renderer/index.js +2 -0
  105. package/dist/shader-renderer/pixel-shader.d.ts +32 -0
  106. package/dist/shader-renderer/pixel-shader.js +172 -0
  107. package/dist/shader-renderer/pixel-vertex-shader.d.ts +1 -0
  108. package/dist/shader-renderer/pixel-vertex-shader.js +14 -0
  109. package/dist/shader-renderer/texture.d.ts +28 -0
  110. package/dist/shader-renderer/texture.js +97 -0
  111. package/dist/shader-renderer/types.d.ts +17 -0
  112. package/dist/shader-renderer/types.js +4 -0
  113. package/dist/shader-renderer/uniform.d.ts +16 -0
  114. package/dist/shader-renderer/uniform.js +134 -0
  115. package/dist/tilings/delaunay.d.ts +11 -0
  116. package/dist/tilings/delaunay.js +28 -0
  117. package/dist/tilings/grid.d.ts +5 -0
  118. package/dist/tilings/grid.js +29 -0
  119. package/dist/tilings/hexagononal.d.ts +4 -0
  120. package/dist/tilings/hexagononal.js +40 -0
  121. package/dist/tilings/index.d.ts +12 -0
  122. package/dist/tilings/index.js +12 -0
  123. package/dist/tilings/line.d.ts +5 -0
  124. package/dist/tilings/line.js +19 -0
  125. package/dist/tilings/mediterranean.d.ts +3 -0
  126. package/dist/tilings/mediterranean.js +49 -0
  127. package/dist/tilings/pythagorean.d.ts +3 -0
  128. package/dist/tilings/pythagorean.js +40 -0
  129. package/dist/tilings/random-cuts.d.ts +6 -0
  130. package/dist/tilings/random-cuts.js +16 -0
  131. package/dist/tilings/recursive-cuts.d.ts +5 -0
  132. package/dist/tilings/recursive-cuts.js +11 -0
  133. package/dist/tilings/rhombille.d.ts +3 -0
  134. package/dist/tilings/rhombille.js +16 -0
  135. package/dist/tilings/rightangled-triangle.d.ts +3 -0
  136. package/dist/tilings/rightangled-triangle.js +29 -0
  137. package/dist/tilings/triangle.d.ts +3 -0
  138. package/dist/tilings/triangle.js +17 -0
  139. package/dist/tilings/voronoi.d.ts +11 -0
  140. package/dist/tilings/voronoi.js +29 -0
  141. package/dist/tilings/weaving.d.ts +1 -0
  142. package/dist/tilings/weaving.js +8 -0
  143. package/dist/transforms/camera/camera.d.ts +32 -0
  144. package/dist/transforms/camera/camera.js +60 -0
  145. package/dist/transforms/camera/index.d.ts +3 -0
  146. package/dist/transforms/camera/index.js +3 -0
  147. package/dist/transforms/camera/orthographic.d.ts +15 -0
  148. package/dist/transforms/camera/orthographic.js +31 -0
  149. package/dist/transforms/camera/perspective.d.ts +16 -0
  150. package/dist/transforms/camera/perspective.js +40 -0
  151. package/dist/transforms/index.d.ts +2 -0
  152. package/dist/transforms/index.js +2 -0
  153. package/dist/transforms/normalize-transform.d.ts +12 -0
  154. package/dist/transforms/normalize-transform.js +20 -0
  155. package/dist/types.d.ts +1 -0
  156. package/dist/types.js +1 -0
  157. package/dist/utilities/create-frame.d.ts +2 -0
  158. package/dist/utilities/create-frame.js +6 -0
  159. package/dist/utilities/ensure-array.d.ts +1 -0
  160. package/dist/utilities/ensure-array.js +3 -0
  161. package/dist/utilities/idw-interpolator.d.ts +9 -0
  162. package/dist/utilities/idw-interpolator.js +18 -0
  163. package/dist/utilities/index.d.ts +8 -0
  164. package/dist/utilities/index.js +8 -0
  165. package/dist/utilities/marching-squares.d.ts +51 -0
  166. package/dist/utilities/marching-squares.js +177 -0
  167. package/dist/utilities/point-sampler.d.ts +12 -0
  168. package/dist/utilities/point-sampler.js +24 -0
  169. package/dist/utilities/poisson/grid.d.ts +21 -0
  170. package/dist/utilities/poisson/grid.js +51 -0
  171. package/dist/utilities/poisson/poissonnier.d.ts +50 -0
  172. package/dist/utilities/poisson/poissonnier.js +118 -0
  173. package/dist/utilities/resolution.d.ts +10 -0
  174. package/dist/utilities/resolution.js +14 -0
  175. package/dist/utilities/rng.d.ts +13 -0
  176. package/dist/utilities/rng.js +80 -0
  177. package/package.json +28 -0
@@ -0,0 +1,102 @@
1
+ import { Vec2 } from "./vec2.js";
2
+ export declare function vec3(vector: Vec2, z: number): Vec3;
3
+ export declare function vec3(vector: Vec3): Vec3;
4
+ export declare function vec3(x: number, y?: number, z?: number): Vec3;
5
+ export declare class Vec3 {
6
+ x: number;
7
+ y: number;
8
+ z: number;
9
+ length(): number;
10
+ lengthSquared(): number;
11
+ get xx(): Vec2;
12
+ get yy(): Vec2;
13
+ get zz(): Vec2;
14
+ get xy(): Vec2;
15
+ get xz(): Vec2;
16
+ get yx(): Vec2;
17
+ get yz(): Vec2;
18
+ get zx(): Vec2;
19
+ get zy(): Vec2;
20
+ get xxx(): Vec3;
21
+ get xxy(): Vec3;
22
+ get xxz(): Vec3;
23
+ get xyx(): Vec3;
24
+ get xyy(): Vec3;
25
+ get xyz(): Vec3;
26
+ get xzx(): Vec3;
27
+ get xzy(): Vec3;
28
+ get xzz(): Vec3;
29
+ get yxx(): Vec3;
30
+ get yxy(): Vec3;
31
+ get yxz(): Vec3;
32
+ get yyx(): Vec3;
33
+ get yyy(): Vec3;
34
+ get yyz(): Vec3;
35
+ get yzx(): Vec3;
36
+ get yzy(): Vec3;
37
+ get yzz(): Vec3;
38
+ get zxx(): Vec3;
39
+ get zxy(): Vec3;
40
+ get zxz(): Vec3;
41
+ get zyx(): Vec3;
42
+ get zyy(): Vec3;
43
+ get zyz(): Vec3;
44
+ get zzx(): Vec3;
45
+ get zzy(): Vec3;
46
+ get zzz(): Vec3;
47
+ constructor(vector: Vec2, z: number);
48
+ constructor(vector: Vec3);
49
+ constructor(x: number, y?: number, z?: number);
50
+ static add(...vectors: Array<Vec3>): Vec3;
51
+ static subtract(left: Vec3, right: Vec3): Vec3;
52
+ static multiply(vector: Vec3, value: number | Vec3): Vec3;
53
+ static dot(a: Vec3, b: Vec3): number;
54
+ static cross(a: Vec3, b: Vec3): Vec3;
55
+ static interpolate(start: Vec3, end: Vec3, position: number): Vec3;
56
+ static midpoint(start: Vec3, end: Vec3): Vec3;
57
+ static absolute(vector: Vec3): Vec3;
58
+ static distance(a: Vec3, b: Vec3): number;
59
+ static angle(start: Vec3, end: Vec3): number;
60
+ static axisAngleRotation(vector: Vec3, axis: Vec3, rotation: number): Vec3;
61
+ static smoothstep(vector: Vec3, lower?: number | Vec3, upper?: number | Vec3): Vec3;
62
+ setX(x: number): this;
63
+ setY(y: number): this;
64
+ setZ(z: number): this;
65
+ setCoordinates(vector: Vec2, z: number): Vec3;
66
+ setCoordinates(vector: Vec3): Vec3;
67
+ setCoordinates(x: number, y?: number, z?: number): Vec3;
68
+ clone(): Vec3;
69
+ copy(vector: Vec3): Vec3;
70
+ plus(vector: Vec3): Vec3;
71
+ minus(vector: Vec3): Vec3;
72
+ times(value: number | Vec3): Vec3;
73
+ distance(vector: Vec3): number;
74
+ distanceSquared(vector: Vec3): number;
75
+ dot(vector: Vec3): number;
76
+ cross(vector: Vec3): Vec3;
77
+ axisAngleRotate(axis: Vec3, rotation: number): this;
78
+ scalarProjectOnto(target: Vec3): number;
79
+ projectOnto(target: Vec3): Vec3;
80
+ scalarRejectOnto(target: Vec3): number;
81
+ rejectOnto(target: Vec3): Vec3;
82
+ reflectByVector(vector: Vec3): Vec3;
83
+ reflectByPoint(point: Vec3): Vec3;
84
+ normalize(): Vec3;
85
+ absolute(): Vec3;
86
+ negate(): Vec3;
87
+ floor(): Vec3;
88
+ round(): Vec3;
89
+ fract(): Vec3;
90
+ smoothstep(lower?: number | Vec3, upper?: number | Vec3): Vec3;
91
+ min(): number;
92
+ max(): number;
93
+ mod(modulo: number | Vec3): Vec3;
94
+ clamp(lower?: number | Vec3, upper?: number | Vec3): Vec3;
95
+ signed(): Vec3;
96
+ pow(power: number | Vec3): Vec3;
97
+ reflect(vector: Vec3): Vec3;
98
+ splitIntoIntAndFract(): [Vec3, Vec3];
99
+ toArray(): number[];
100
+ toVec2(): Vec2;
101
+ toHexColor(alpha?: number): string;
102
+ }
@@ -0,0 +1,256 @@
1
+ import { clamp, fract, mod, smoothstep } from "../math/index.js";
2
+ import { splitIntoIntAndFract } from "../math/split-into-int-and-fract.js";
3
+ import { Vec2, vec2 } from "./vec2.js";
4
+ export function vec3(...args) {
5
+ return new Vec3(args[0], args[1], args[2]);
6
+ }
7
+ const { floor, round, hypot, acos, cos, sin, abs, min, max, pow } = Math;
8
+ export class Vec3 {
9
+ length() { return hypot(this.x, this.y, this.z); }
10
+ lengthSquared() { const x = this.x, y = this.y, z = this.z; return x * x + y * y + z * z; }
11
+ get xx() { return vec2(this.x); }
12
+ get yy() { return vec2(this.y); }
13
+ get zz() { return vec2(this.z); }
14
+ get xy() { return vec2(this.x, this.y); }
15
+ get xz() { return vec2(this.x, this.z); }
16
+ get yx() { return vec2(this.y, this.x); }
17
+ get yz() { return vec2(this.y, this.z); }
18
+ get zx() { return vec2(this.z, this.x); }
19
+ get zy() { return vec2(this.z, this.y); }
20
+ get xxx() { return vec3(this.x, this.x, this.x); }
21
+ get xxy() { return vec3(this.x, this.x, this.y); }
22
+ get xxz() { return vec3(this.x, this.x, this.z); }
23
+ get xyx() { return vec3(this.x, this.y, this.x); }
24
+ get xyy() { return vec3(this.x, this.y, this.y); }
25
+ get xyz() { return vec3(this.x, this.y, this.z); }
26
+ get xzx() { return vec3(this.x, this.z, this.x); }
27
+ get xzy() { return vec3(this.x, this.z, this.y); }
28
+ get xzz() { return vec3(this.x, this.z, this.z); }
29
+ get yxx() { return vec3(this.y, this.x, this.x); }
30
+ get yxy() { return vec3(this.y, this.x, this.y); }
31
+ get yxz() { return vec3(this.y, this.x, this.z); }
32
+ get yyx() { return vec3(this.y, this.y, this.x); }
33
+ get yyy() { return vec3(this.y, this.y, this.y); }
34
+ get yyz() { return vec3(this.y, this.y, this.z); }
35
+ get yzx() { return vec3(this.y, this.z, this.x); }
36
+ get yzy() { return vec3(this.y, this.z, this.y); }
37
+ get yzz() { return vec3(this.y, this.z, this.z); }
38
+ get zxx() { return vec3(this.z, this.x, this.x); }
39
+ get zxy() { return vec3(this.z, this.x, this.y); }
40
+ get zxz() { return vec3(this.z, this.x, this.z); }
41
+ get zyx() { return vec3(this.z, this.y, this.x); }
42
+ get zyy() { return vec3(this.z, this.y, this.y); }
43
+ get zyz() { return vec3(this.z, this.y, this.z); }
44
+ get zzx() { return vec3(this.z, this.z, this.x); }
45
+ get zzy() { return vec3(this.z, this.z, this.y); }
46
+ get zzz() { return vec3(this.z, this.z, this.z); }
47
+ constructor(...args) {
48
+ this.setCoordinates(args[0], args[1], args[2]);
49
+ }
50
+ static add(...vectors) {
51
+ let sum = vec3(0);
52
+ for (let vector of vectors)
53
+ sum.plus(vector);
54
+ return sum;
55
+ }
56
+ static subtract(left, right) {
57
+ return left.clone().minus(right);
58
+ }
59
+ static multiply(vector, value) {
60
+ return vector.clone().times(value);
61
+ }
62
+ static dot(a, b) {
63
+ return a.dot(b);
64
+ }
65
+ static cross(a, b) {
66
+ return a.cross(b);
67
+ }
68
+ static interpolate(start, end, position) {
69
+ return start.clone().times(1 - position).plus(Vec3.multiply(end, position));
70
+ }
71
+ static midpoint(start, end) {
72
+ return Vec3.interpolate(start, end, 0.5);
73
+ }
74
+ static absolute(vector) {
75
+ return vector.clone().absolute();
76
+ }
77
+ static distance(a, b) {
78
+ return Vec3.subtract(b, a).length();
79
+ }
80
+ static angle(start, end) {
81
+ return acos(start.dot(end) / (start.length() * end.length()));
82
+ }
83
+ static axisAngleRotation(vector, axis, rotation) {
84
+ return vector.clone().axisAngleRotate(axis, rotation);
85
+ }
86
+ static smoothstep(vector, lower = 0, upper = 1) {
87
+ return vector.clone().smoothstep(lower, upper);
88
+ }
89
+ setX(x) {
90
+ this.x = x;
91
+ return this;
92
+ }
93
+ setY(y) {
94
+ this.y = y;
95
+ return this;
96
+ }
97
+ setZ(z) {
98
+ this.z = z;
99
+ return this;
100
+ }
101
+ setCoordinates(...args) {
102
+ const [a, b, c] = args;
103
+ if (a instanceof Vec3) {
104
+ this.x = a.x;
105
+ this.y = a.y;
106
+ this.z = a.z;
107
+ }
108
+ else if (a instanceof Vec2) {
109
+ this.x = a.x;
110
+ this.y = a.y;
111
+ this.z = b;
112
+ }
113
+ else if (b !== undefined && c === undefined) {
114
+ this.x = a;
115
+ this.y = b;
116
+ this.z = 0;
117
+ }
118
+ else {
119
+ this.x = a;
120
+ this.y = b ?? a;
121
+ this.z = c ?? a;
122
+ }
123
+ return this;
124
+ }
125
+ clone() {
126
+ return vec3(this.x, this.y, this.z);
127
+ }
128
+ copy(vector) {
129
+ return this.setCoordinates(vector);
130
+ }
131
+ plus(vector) {
132
+ return this.setCoordinates(this.x + vector.x, this.y + vector.y, this.z + vector.z);
133
+ }
134
+ minus(vector) {
135
+ return this.setCoordinates(this.x - vector.x, this.y - vector.y, this.z - vector.z);
136
+ }
137
+ times(value) {
138
+ value = ensureVec3(value);
139
+ return this.setCoordinates(value.x * this.x, value.y * this.y, value.z * this.z);
140
+ }
141
+ distance(vector) {
142
+ return hypot(vector.x - this.x, vector.y - this.y, vector.z - this.z);
143
+ }
144
+ distanceSquared(vector) {
145
+ let dx = vector.x - this.x;
146
+ let dy = vector.y - this.y;
147
+ let dz = vector.z - this.z;
148
+ return dx * dx + dy * dy + dz * dz;
149
+ }
150
+ dot(vector) {
151
+ return this.x * vector.x + this.y * vector.y + this.z * vector.z;
152
+ }
153
+ cross(vector) {
154
+ let { x: x0, y: y0, z: z0 } = this;
155
+ let { x: x1, y: y1, z: z1 } = vector;
156
+ return vec3(y0 * z1 - z0 * y1, z0 * x1 - x0 * z1, x0 * y1 - y0 * x1);
157
+ }
158
+ axisAngleRotate(axis, rotation) {
159
+ const c = cos(rotation);
160
+ const s = sin(rotation);
161
+ // Rodrigues rotation formula
162
+ this.setCoordinates(Vec3.add(Vec3.multiply(this, c), Vec3.multiply(axis.cross(this), s), Vec3.multiply(axis, (1 - c) * axis.dot(this))));
163
+ return this;
164
+ }
165
+ scalarProjectOnto(target) {
166
+ return this.dot(target) / target.lengthSquared();
167
+ }
168
+ projectOnto(target) {
169
+ return target.clone().times(this.scalarProjectOnto(target));
170
+ }
171
+ scalarRejectOnto(target) {
172
+ return this.rejectOnto(target).length();
173
+ }
174
+ rejectOnto(target) {
175
+ return Vec3.subtract(this, this.projectOnto(target));
176
+ }
177
+ reflectByVector(vector) {
178
+ const rejection = this.rejectOnto(vector);
179
+ return this.clone().minus(rejection.times(2));
180
+ }
181
+ reflectByPoint(point) {
182
+ const translation = Vec3.subtract(point, this).times(2);
183
+ return this.clone().minus(translation);
184
+ }
185
+ normalize() {
186
+ return this.times(1 / this.length());
187
+ }
188
+ absolute() {
189
+ return this.setCoordinates(abs(this.x), abs(this.y), abs(this.z));
190
+ }
191
+ negate() {
192
+ return this.times(-1);
193
+ }
194
+ floor() {
195
+ return this.setCoordinates(floor(this.x), floor(this.y), floor(this.z));
196
+ }
197
+ round() {
198
+ return this.setCoordinates(round(this.x), round(this.y), round(this.z));
199
+ }
200
+ fract() {
201
+ return this.setCoordinates(fract(this.x), fract(this.y), fract(this.z));
202
+ }
203
+ smoothstep(lower = 0, upper = 1) {
204
+ lower = ensureVec3(lower);
205
+ upper = ensureVec3(upper);
206
+ return this.setCoordinates(smoothstep(this.x, lower.x, upper.x), smoothstep(this.y, lower.y, upper.y), smoothstep(this.z, lower.z, upper.z));
207
+ }
208
+ min() {
209
+ return min(this.x, this.y, this.z);
210
+ }
211
+ max() {
212
+ return max(this.x, this.y, this.z);
213
+ }
214
+ mod(modulo) {
215
+ modulo = ensureVec3(modulo);
216
+ return this.setCoordinates(mod(this.x, modulo.x), mod(this.y, modulo.y), mod(this.z, modulo.z));
217
+ }
218
+ clamp(lower = 0, upper = 1) {
219
+ lower = ensureVec3(lower);
220
+ upper = ensureVec3(upper);
221
+ return this.setCoordinates(clamp(this.x, lower.x, upper.x), clamp(this.y, lower.y, upper.y), clamp(this.z, lower.z, upper.z));
222
+ }
223
+ signed() {
224
+ return this.times(2).plus(vec3(-1));
225
+ }
226
+ pow(power) {
227
+ power = ensureVec3(power);
228
+ return this.setCoordinates(pow(this.x, power.x), pow(this.y, power.y), pow(this.z, power.z));
229
+ }
230
+ reflect(vector) {
231
+ const normalizedVector = vector.clone().normalize();
232
+ const reflectedVector = Vec3.subtract(this, Vec3.multiply(normalizedVector, 2 * this.dot(normalizedVector)));
233
+ return this.setCoordinates(reflectedVector);
234
+ }
235
+ splitIntoIntAndFract() {
236
+ const [xi, xf] = splitIntoIntAndFract(this.x);
237
+ const [yi, yf] = splitIntoIntAndFract(this.y);
238
+ const [zi, zf] = splitIntoIntAndFract(this.z);
239
+ return [vec3(xi, yi, zi), vec3(xf, yf, zf)];
240
+ }
241
+ toArray() {
242
+ return [this.x, this.y, this.z];
243
+ }
244
+ toVec2() {
245
+ return vec2(this.x, this.y);
246
+ }
247
+ toHexColor(alpha) {
248
+ const values = this.toArray();
249
+ if (alpha !== undefined)
250
+ values.push(alpha);
251
+ return "#" + values.map((v) => Math.floor(255 * v).toString(16).padStart(2, "0")).join("");
252
+ }
253
+ }
254
+ function ensureVec3(value) {
255
+ return value = typeof value === "number" ? vec3(value) : value;
256
+ }
@@ -0,0 +1,71 @@
1
+ import { Vec2 } from "./vec2.js";
2
+ import { Vec3 } from "./vec3.js";
3
+ export declare function vec4(vector: Vec4): Vec4;
4
+ export declare function vec4(vector: Vec3, w: number): Vec4;
5
+ export declare function vec4(vector: Vec2, z: number, w: number): Vec4;
6
+ export declare function vec4(x: number, y?: number, z?: number, w?: number): Vec4;
7
+ export declare class Vec4 {
8
+ x: number;
9
+ y: number;
10
+ z: number;
11
+ w: number;
12
+ length(): number;
13
+ lengthSquared(): number;
14
+ constructor(vector: Vec4);
15
+ constructor(vector: Vec3, w: number);
16
+ constructor(vector: Vec2, z: number, w: number);
17
+ constructor(x: number, y?: number, z?: number, w?: number);
18
+ static add(...vectors: Array<Vec4>): Vec4;
19
+ static subtract(left: Vec4, right: Vec4): Vec4;
20
+ static multiply(vector: Vec4, value: number | Vec4): Vec4;
21
+ static dot(a: Vec4, b: Vec4): number;
22
+ static cross(a: Vec4, b: Vec4): Vec4;
23
+ static interpolate(start: Vec4, end: Vec4, position: number): Vec4;
24
+ static midpoint(start: Vec4, end: Vec4): Vec4;
25
+ static absolute(vector: Vec4): Vec4;
26
+ static distance(a: Vec4, b: Vec4): number;
27
+ static angle(start: Vec4, end: Vec4): number;
28
+ static axisAngleRotation(vector: Vec4, axis: Vec4, rotation: number): Vec4;
29
+ static smoothstep(vector: Vec4, lower?: number | Vec4, upper?: number | Vec4): Vec4;
30
+ setX(x: number): this;
31
+ setY(y: number): this;
32
+ setZ(z: number): this;
33
+ setW(w: number): this;
34
+ setCoordinates(vector: Vec4): Vec4;
35
+ setCoordinates(vector: Vec3, w: number): Vec4;
36
+ setCoordinates(vector: Vec2, z: number, w: number): Vec4;
37
+ setCoordinates(x: number, y?: number, z?: number, w?: number): Vec4;
38
+ clone(): Vec4;
39
+ copy(vector: Vec4): Vec4;
40
+ plus(vector: Vec4): Vec4;
41
+ minus(vector: Vec4): Vec4;
42
+ times(value: number | Vec4): Vec4;
43
+ distance(vector: Vec4): number;
44
+ distanceSquared(vector: Vec4): number;
45
+ dot(vector: Vec4): number;
46
+ cross(_vector: Vec4): Vec4;
47
+ axisAngleRotate(axis: Vec4, rotation: number): this;
48
+ scalarProjectOnto(target: Vec4): number;
49
+ projectOnto(target: Vec4): Vec4;
50
+ scalarRejectOnto(target: Vec4): number;
51
+ rejectOnto(target: Vec4): Vec4;
52
+ reflectByVector(vector: Vec4): Vec4;
53
+ reflectByPoint(point: Vec4): Vec4;
54
+ normalize(): Vec4;
55
+ absolute(): Vec4;
56
+ negate(): Vec4;
57
+ floor(): Vec4;
58
+ round(): Vec4;
59
+ fract(): Vec4;
60
+ smoothstep(lower?: number | Vec4, upper?: number | Vec4): Vec4;
61
+ min(): number;
62
+ max(): number;
63
+ mod(modulo: number | Vec4): Vec4;
64
+ clamp(lower?: number | Vec4, upper?: number | Vec4): Vec4;
65
+ signed(): Vec4;
66
+ pow(power: number | Vec4): Vec4;
67
+ reflect(vector: Vec4): Vec4;
68
+ splitIntoIntAndFract(): [Vec4, Vec4];
69
+ toArray(): number[];
70
+ toHexColor(): string;
71
+ }
@@ -0,0 +1,238 @@
1
+ import { clamp, fract, mod, smoothstep } from "../math/index.js";
2
+ import { splitIntoIntAndFract } from "../math/split-into-int-and-fract.js";
3
+ import { Vec2 } from "./vec2.js";
4
+ import { Vec3 } from "./vec3.js";
5
+ export function vec4(...args) {
6
+ return new Vec4(args[0], args[1], args[2], args[3]);
7
+ }
8
+ const { floor, round, hypot, acos, cos, sin, abs, min, max, pow } = Math;
9
+ export class Vec4 {
10
+ length() { return hypot(this.x, this.y, this.z, this.w); }
11
+ lengthSquared() { const { x, y, z, w } = this; return x * x + y * y + z * z + w * w; }
12
+ constructor(...args) {
13
+ this.setCoordinates(args[0], args[1], args[2], args[3]);
14
+ ;
15
+ }
16
+ static add(...vectors) {
17
+ let sum = vec4(0);
18
+ for (let vector of vectors)
19
+ sum.plus(vector);
20
+ return sum;
21
+ }
22
+ static subtract(left, right) {
23
+ return left.clone().minus(right);
24
+ }
25
+ static multiply(vector, value) {
26
+ return vector.clone().times(value);
27
+ }
28
+ static dot(a, b) {
29
+ return a.dot(b);
30
+ }
31
+ static cross(a, b) {
32
+ return a.cross(b);
33
+ }
34
+ static interpolate(start, end, position) {
35
+ return start.clone().times(1 - position).plus(Vec4.multiply(end, position));
36
+ }
37
+ static midpoint(start, end) {
38
+ return Vec4.interpolate(start, end, 0.5);
39
+ }
40
+ static absolute(vector) {
41
+ return vector.clone().absolute();
42
+ }
43
+ static distance(a, b) {
44
+ return Vec4.subtract(b, a).length();
45
+ }
46
+ static angle(start, end) {
47
+ return acos(start.dot(end) / (start.length() * end.length()));
48
+ }
49
+ static axisAngleRotation(vector, axis, rotation) {
50
+ return vector.clone().axisAngleRotate(axis, rotation);
51
+ }
52
+ static smoothstep(vector, lower = 0, upper = 1) {
53
+ return vector.clone().smoothstep(lower, upper);
54
+ }
55
+ setX(x) {
56
+ this.x = x;
57
+ return this;
58
+ }
59
+ setY(y) {
60
+ this.y = y;
61
+ return this;
62
+ }
63
+ setZ(z) {
64
+ this.z = z;
65
+ return this;
66
+ }
67
+ setW(w) {
68
+ this.w = w;
69
+ return this;
70
+ }
71
+ setCoordinates(...args) {
72
+ const [a, b, c, d] = args;
73
+ if (a instanceof Vec4) {
74
+ this.x = a.x;
75
+ this.y = a.y;
76
+ this.z = a.z;
77
+ this.w = a.w;
78
+ }
79
+ else if (a instanceof Vec3) {
80
+ this.x = a.x;
81
+ this.y = a.y;
82
+ this.z = a.z;
83
+ this.w = b;
84
+ }
85
+ else if (a instanceof Vec2) {
86
+ this.x = a.x;
87
+ this.y = a.y;
88
+ this.z = b;
89
+ this.w = c;
90
+ }
91
+ else if (b !== undefined && c !== undefined && d === undefined) {
92
+ this.x = a;
93
+ this.y = b;
94
+ this.z = c;
95
+ this.w = 1;
96
+ }
97
+ else {
98
+ this.x = a;
99
+ this.y = b ?? a;
100
+ this.z = c ?? a;
101
+ this.w = d ?? 1;
102
+ }
103
+ return this;
104
+ }
105
+ clone() {
106
+ return vec4(this.x, this.y, this.z, this.w);
107
+ }
108
+ copy(vector) {
109
+ return this.setCoordinates(vector);
110
+ }
111
+ plus(vector) {
112
+ return this.setCoordinates(this.x + vector.x, this.y + vector.y, this.z + vector.z, this.w + vector.w);
113
+ }
114
+ minus(vector) {
115
+ return this.setCoordinates(this.x - vector.x, this.y - vector.y, this.z - vector.z, this.w - vector.w);
116
+ }
117
+ times(value) {
118
+ value = ensureVec4(value);
119
+ return this.setCoordinates(value.x * this.x, value.y * this.y, value.z * this.z, value.w * this.w);
120
+ }
121
+ distance(vector) {
122
+ return hypot(vector.x - this.x, vector.y - this.y, vector.z - this.z, vector.w - this.w);
123
+ }
124
+ distanceSquared(vector) {
125
+ let dx = vector.x - this.x;
126
+ let dy = vector.y - this.y;
127
+ let dz = vector.z - this.z;
128
+ let dw = vector.w - this.w;
129
+ return dx * dx + dy * dy + dz * dz + dw * dw;
130
+ }
131
+ dot(vector) {
132
+ return this.x * vector.x + this.y * vector.y + this.z * vector.z + this.w * vector.w;
133
+ }
134
+ cross(_vector) {
135
+ throw new Error("Not yet implemented");
136
+ // let { x: x0, y: y0, z: z0 } = this;
137
+ // let { x: x1, y: y1, z: z1 } = vector;
138
+ // return Vec4(
139
+ // y0 * z1 - z0 * y1,
140
+ // z0 * x1 - x0 * z1,
141
+ // x0 * y1 - y0 * x1
142
+ // );
143
+ }
144
+ axisAngleRotate(axis, rotation) {
145
+ const c = cos(rotation);
146
+ const s = sin(rotation);
147
+ // Rodrigues rotation formula
148
+ this.setCoordinates(Vec4.add(Vec4.multiply(this, c), Vec4.multiply(axis.cross(this), s), Vec4.multiply(axis, (1 - c) * axis.dot(this))));
149
+ return this;
150
+ }
151
+ scalarProjectOnto(target) {
152
+ return this.dot(target) / target.lengthSquared();
153
+ }
154
+ projectOnto(target) {
155
+ return target.clone().times(this.scalarProjectOnto(target));
156
+ }
157
+ scalarRejectOnto(target) {
158
+ return this.rejectOnto(target).length();
159
+ }
160
+ rejectOnto(target) {
161
+ return Vec4.subtract(this, this.projectOnto(target));
162
+ }
163
+ reflectByVector(vector) {
164
+ const rejection = this.rejectOnto(vector);
165
+ return this.clone().minus(rejection.times(2));
166
+ }
167
+ reflectByPoint(point) {
168
+ const translation = Vec4.subtract(point, this).times(2);
169
+ return this.clone().minus(translation);
170
+ }
171
+ normalize() {
172
+ return this.times(1 / this.length());
173
+ }
174
+ absolute() {
175
+ return this.setCoordinates(abs(this.x), abs(this.y), abs(this.z), abs(this.w));
176
+ }
177
+ negate() {
178
+ return this.times(-1);
179
+ }
180
+ floor() {
181
+ return this.setCoordinates(floor(this.x), floor(this.y), floor(this.z), floor(this.w));
182
+ }
183
+ round() {
184
+ return this.setCoordinates(round(this.x), round(this.y), round(this.z), round(this.w));
185
+ }
186
+ fract() {
187
+ return this.setCoordinates(fract(this.x), fract(this.y), fract(this.z), fract(this.w));
188
+ }
189
+ smoothstep(lower = 0, upper = 1) {
190
+ lower = ensureVec4(lower);
191
+ upper = ensureVec4(upper);
192
+ return this.setCoordinates(smoothstep(this.x, lower.x, upper.x), smoothstep(this.y, lower.y, upper.y), smoothstep(this.z, lower.z, upper.z), smoothstep(this.w, lower.w, upper.w));
193
+ }
194
+ min() {
195
+ return min(this.x, this.y, this.z, this.w);
196
+ }
197
+ max() {
198
+ return max(this.x, this.y, this.z, this.w);
199
+ }
200
+ mod(modulo) {
201
+ modulo = ensureVec4(modulo);
202
+ return this.setCoordinates(mod(this.x, modulo.x), mod(this.y, modulo.y), mod(this.z, modulo.z), mod(this.w, modulo.w));
203
+ }
204
+ clamp(lower = 0, upper = 1) {
205
+ lower = ensureVec4(lower);
206
+ upper = ensureVec4(upper);
207
+ return this.setCoordinates(clamp(this.x, lower.x, upper.x), clamp(this.y, lower.y, upper.y), clamp(this.z, lower.z, upper.z), clamp(this.w, lower.w, upper.w));
208
+ }
209
+ signed() {
210
+ return this.times(2).plus(vec4(-1));
211
+ }
212
+ pow(power) {
213
+ power = ensureVec4(power);
214
+ return this.setCoordinates(pow(this.x, power.x), pow(this.y, power.y), pow(this.z, power.z), pow(this.w, power.w));
215
+ }
216
+ reflect(vector) {
217
+ const normalizedVector = vector.clone().normalize();
218
+ const reflectedVector = Vec4.subtract(this, Vec4.multiply(normalizedVector, 2 * this.dot(normalizedVector)));
219
+ return this.setCoordinates(reflectedVector);
220
+ }
221
+ splitIntoIntAndFract() {
222
+ const [xi, xf] = splitIntoIntAndFract(this.x);
223
+ const [yi, yf] = splitIntoIntAndFract(this.y);
224
+ const [zi, zf] = splitIntoIntAndFract(this.z);
225
+ const [wi, wf] = splitIntoIntAndFract(this.w);
226
+ return [vec4(xi, yi, zi, wi), vec4(xf, yf, zf, wf)];
227
+ }
228
+ toArray() {
229
+ return [this.x, this.y, this.z, this.w];
230
+ }
231
+ toHexColor() {
232
+ const values = this.toArray();
233
+ return "#" + values.map((v) => Math.floor(255 * v).toString(16).padStart(2, "0")).join("");
234
+ }
235
+ }
236
+ function ensureVec4(value) {
237
+ return value = typeof value === "number" ? vec4(value) : value;
238
+ }
@@ -0,0 +1,11 @@
1
+ export * from "./geometry/index.js";
2
+ export * from "./math/index.js";
3
+ export * from "./utilities/index.js";
4
+ export * from "./noise/index.js";
5
+ export * from "./constants.js";
6
+ export * from "./tilings/index.js";
7
+ export * from "./transforms/index.js";
8
+ export * from "./types.js";
9
+ export * from "./shader-renderer/index.js";
10
+ export * from "./shader-modules/index.js";
11
+ export * from "./constants.js";
package/dist/index.js ADDED
@@ -0,0 +1,11 @@
1
+ export * from "./geometry/index.js";
2
+ export * from "./math/index.js";
3
+ export * from "./utilities/index.js";
4
+ export * from "./noise/index.js";
5
+ export * from "./constants.js";
6
+ export * from "./tilings/index.js";
7
+ export * from "./transforms/index.js";
8
+ export * from "./types.js";
9
+ export * from "./shader-renderer/index.js";
10
+ export * from "./shader-modules/index.js";
11
+ export * from "./constants.js";
@@ -0,0 +1 @@
1
+ export declare function clamp(value: number, lower?: number, upper?: number): number;
@@ -0,0 +1,5 @@
1
+ export function clamp(value, lower = 0, upper = 1) {
2
+ return value < lower ? lower :
3
+ value > upper ? upper :
4
+ value;
5
+ }