q5 2.2.3 → 2.2.4
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.
- package/README.md +5 -2
- package/package.json +3 -4
- package/q5.js +65 -25
- package/q5.min.js +1 -1
- package/src/q5-ai.js +4 -5
- package/src/q5-core.js +1 -1
- package/src/q5-vector.js +57 -10
- package/src/q5-webgpu-drawing.js +3 -0
- package/src/q5-webgpu-image.js +12 -7
- package/q5-q2d.js +0 -3085
- package/q5-q2d.min.js +0 -8
package/src/q5-vector.js
CHANGED
|
@@ -3,18 +3,19 @@ Q5.modules.vector = ($) => {
|
|
|
3
3
|
};
|
|
4
4
|
|
|
5
5
|
Q5.Vector = class {
|
|
6
|
-
constructor(
|
|
7
|
-
this.x =
|
|
8
|
-
this.y =
|
|
9
|
-
this.z =
|
|
10
|
-
this._$ =
|
|
6
|
+
constructor(x, y, z, $) {
|
|
7
|
+
this.x = x || 0;
|
|
8
|
+
this.y = y || 0;
|
|
9
|
+
this.z = z || 0;
|
|
10
|
+
this._$ = $ || window;
|
|
11
11
|
this._cn = null;
|
|
12
12
|
this._cnsq = null;
|
|
13
13
|
}
|
|
14
|
-
set(
|
|
15
|
-
this.x =
|
|
16
|
-
this.y =
|
|
17
|
-
this.z =
|
|
14
|
+
set(x, y, z) {
|
|
15
|
+
this.x = x?.x || x || 0;
|
|
16
|
+
this.y = x?.y || y || 0;
|
|
17
|
+
this.z = x?.z || z || 0;
|
|
18
|
+
return this;
|
|
18
19
|
}
|
|
19
20
|
copy() {
|
|
20
21
|
return new Q5.Vector(this.x, this.y, this.z);
|
|
@@ -136,6 +137,12 @@ Q5.Vector = class {
|
|
|
136
137
|
heading() {
|
|
137
138
|
return this._$.atan2(this.y, this.x);
|
|
138
139
|
}
|
|
140
|
+
setHeading(ang) {
|
|
141
|
+
let mag = this.mag(); // Calculate the magnitude of the vector
|
|
142
|
+
this.x = mag * this._$.cos(ang); // Set the new x component
|
|
143
|
+
this.y = mag * this._$.sin(ang); // Set the new y component
|
|
144
|
+
return this;
|
|
145
|
+
}
|
|
139
146
|
rotate(ang) {
|
|
140
147
|
let costh = this._$.cos(ang);
|
|
141
148
|
let sinth = this._$.sin(ang);
|
|
@@ -153,13 +160,52 @@ Q5.Vector = class {
|
|
|
153
160
|
}
|
|
154
161
|
lerp() {
|
|
155
162
|
let args = [...arguments];
|
|
156
|
-
let u = this._arg2v(...args.slice(0, -1));
|
|
157
163
|
let amt = args.at(-1);
|
|
164
|
+
if (amt == 0) return this;
|
|
165
|
+
let u = this._arg2v(...args.slice(0, -1));
|
|
158
166
|
this.x += (u.x - this.x) * amt;
|
|
159
167
|
this.y += (u.y - this.y) * amt;
|
|
160
168
|
this.z += (u.z - this.z) * amt;
|
|
161
169
|
return this;
|
|
162
170
|
}
|
|
171
|
+
slerp() {
|
|
172
|
+
let args = [...arguments];
|
|
173
|
+
let amt = args.at(-1);
|
|
174
|
+
if (amt == 0) return this;
|
|
175
|
+
let u = this._arg2v(...args.slice(0, -1));
|
|
176
|
+
if (amt == 1) return this.set(u);
|
|
177
|
+
|
|
178
|
+
let v0Mag = this.mag();
|
|
179
|
+
let v1Mag = u.mag();
|
|
180
|
+
|
|
181
|
+
if (v0Mag == 0 || v1Mag == 0) {
|
|
182
|
+
return this.mult(1 - amt).add(u.mult(amt));
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
let axis = Q5.Vector.cross(this, u);
|
|
186
|
+
let axisMag = axis.mag();
|
|
187
|
+
let theta = Math.atan2(axisMag, this.dot(u));
|
|
188
|
+
|
|
189
|
+
if (axisMag > 0) {
|
|
190
|
+
axis.div(axisMag);
|
|
191
|
+
} else if (theta < this._$.HALF_PI) {
|
|
192
|
+
return this.mult(1 - amt).add(u.mult(amt));
|
|
193
|
+
} else {
|
|
194
|
+
if (this.z == 0 && u.z == 0) axis.set(0, 0, 1);
|
|
195
|
+
else if (this.x != 0) axis.set(this.y, -this.x, 0).normalize();
|
|
196
|
+
else axis.set(1, 0, 0);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
let ey = axis.cross(this);
|
|
200
|
+
let lerpedMagFactor = 1 - amt + (amt * v1Mag) / v0Mag;
|
|
201
|
+
let cosMultiplier = lerpedMagFactor * Math.cos(amt * theta);
|
|
202
|
+
let sinMultiplier = lerpedMagFactor * Math.sin(amt * theta);
|
|
203
|
+
|
|
204
|
+
this.x = this.x * cosMultiplier + ey.x * sinMultiplier;
|
|
205
|
+
this.y = this.y * cosMultiplier + ey.y * sinMultiplier;
|
|
206
|
+
this.z = this.z * cosMultiplier + ey.z * sinMultiplier;
|
|
207
|
+
return this;
|
|
208
|
+
}
|
|
163
209
|
reflect(n) {
|
|
164
210
|
n.normalize();
|
|
165
211
|
return this.sub(n.mult(2 * this.dot(n)));
|
|
@@ -212,6 +258,7 @@ Q5.Vector.div = (v, u) => v.copy().div(u);
|
|
|
212
258
|
Q5.Vector.dot = (v, u) => v.copy().dot(u);
|
|
213
259
|
Q5.Vector.equals = (v, u, epsilon) => v.equals(u, epsilon);
|
|
214
260
|
Q5.Vector.lerp = (v, u, amt) => v.copy().lerp(u, amt);
|
|
261
|
+
Q5.Vector.slerp = (v, u, amt) => v.copy().slerp(u, amt);
|
|
215
262
|
Q5.Vector.limit = (v, m) => v.copy().limit(m);
|
|
216
263
|
Q5.Vector.heading = (v) => this._$.atan2(v.y, v.x);
|
|
217
264
|
Q5.Vector.magSq = (v) => v.x * v.x + v.y * v.y + v.z * v.z;
|
package/src/q5-webgpu-drawing.js
CHANGED
|
@@ -33,6 +33,7 @@ Q5.renderers.webgpu.drawing = ($, q) => {
|
|
|
33
33
|
};
|
|
34
34
|
|
|
35
35
|
let vertexShader = Q5.device.createShaderModule({
|
|
36
|
+
label: 'drawingVertexShader',
|
|
36
37
|
code: `
|
|
37
38
|
struct VertexOutput {
|
|
38
39
|
@builtin(position) position: vec4<f32>,
|
|
@@ -63,6 +64,7 @@ fn vertexMain(@location(0) pos: vec2<f32>, @location(1) colorIndex: f32, @locati
|
|
|
63
64
|
});
|
|
64
65
|
|
|
65
66
|
let fragmentShader = Q5.device.createShaderModule({
|
|
67
|
+
label: 'drawingFragmentShader',
|
|
66
68
|
code: `
|
|
67
69
|
@group(2) @binding(0) var<storage, read> uColors : array<vec4<f32>>;
|
|
68
70
|
|
|
@@ -80,6 +82,7 @@ fn fragmentMain(@location(1) colorIndex: f32) -> @location(0) vec4<f32> {
|
|
|
80
82
|
|
|
81
83
|
$._createPipeline = (blendConfig) => {
|
|
82
84
|
return Q5.device.createRenderPipeline({
|
|
85
|
+
label: 'drawingPipeline',
|
|
83
86
|
layout: pipelineLayout,
|
|
84
87
|
vertex: {
|
|
85
88
|
module: vertexShader,
|
package/src/q5-webgpu-image.js
CHANGED
|
@@ -2,8 +2,12 @@ Q5.renderers.webgpu.image = ($, q) => {
|
|
|
2
2
|
$.imageStack = [];
|
|
3
3
|
$.textures = [];
|
|
4
4
|
|
|
5
|
+
let verticesStack = [];
|
|
6
|
+
let previousTextureCount = 0;
|
|
7
|
+
|
|
5
8
|
$._hooks.postCanvas.push(() => {
|
|
6
|
-
let
|
|
9
|
+
let vertexShader = Q5.device.createShaderModule({
|
|
10
|
+
label: 'imageVertexShader',
|
|
7
11
|
code: `
|
|
8
12
|
struct VertexOutput {
|
|
9
13
|
@builtin(position) position: vec4<f32>,
|
|
@@ -35,7 +39,8 @@ fn vertexMain(@location(0) pos: vec2<f32>, @location(1) texCoord: vec2<f32>, @lo
|
|
|
35
39
|
`
|
|
36
40
|
});
|
|
37
41
|
|
|
38
|
-
let
|
|
42
|
+
let fragmentShader = Q5.device.createShaderModule({
|
|
43
|
+
label: 'imageFragmentShader',
|
|
39
44
|
code: `
|
|
40
45
|
@group(0) @binding(0) var samp: sampler;
|
|
41
46
|
@group(0) @binding(1) var textures: array<texture_2d<f32>>;
|
|
@@ -65,9 +70,10 @@ fn fragmentMain(@location(0) texCoord: vec2<f32>, @location(1) textureIndex: f32
|
|
|
65
70
|
});
|
|
66
71
|
|
|
67
72
|
$.pipelines[1] = Q5.device.createRenderPipeline({
|
|
73
|
+
label: 'imagePipeline',
|
|
68
74
|
layout: pipelineLayout,
|
|
69
75
|
vertex: {
|
|
70
|
-
module:
|
|
76
|
+
module: vertexShader,
|
|
71
77
|
entryPoint: 'vertexMain',
|
|
72
78
|
buffers: [
|
|
73
79
|
{
|
|
@@ -81,7 +87,7 @@ fn fragmentMain(@location(0) texCoord: vec2<f32>, @location(1) textureIndex: f32
|
|
|
81
87
|
]
|
|
82
88
|
},
|
|
83
89
|
fragment: {
|
|
84
|
-
module:
|
|
90
|
+
module: fragmentShader,
|
|
85
91
|
entryPoint: 'fragmentMain',
|
|
86
92
|
targets: [{ format: 'bgra8unorm' }]
|
|
87
93
|
},
|
|
@@ -112,7 +118,6 @@ fn fragmentMain(@location(0) texCoord: vec2<f32>, @location(1) textureIndex: f32
|
|
|
112
118
|
img.index = $.textures.length;
|
|
113
119
|
$.textures.push(texture);
|
|
114
120
|
};
|
|
115
|
-
img.onerror = reject;
|
|
116
121
|
img.src = src;
|
|
117
122
|
return img;
|
|
118
123
|
};
|
|
@@ -173,7 +178,7 @@ fn fragmentMain(@location(0) texCoord: vec2<f32>, @location(1) textureIndex: f32
|
|
|
173
178
|
let ii = img.index;
|
|
174
179
|
|
|
175
180
|
// prettier-ignore
|
|
176
|
-
|
|
181
|
+
verticesStack.push(
|
|
177
182
|
left, top, 0, 0, ti, ii,
|
|
178
183
|
right, top, 1, 0, ti, ii,
|
|
179
184
|
left, bottom, 0, 1, ti, ii,
|
|
@@ -182,6 +187,6 @@ fn fragmentMain(@location(0) texCoord: vec2<f32>, @location(1) textureIndex: f32
|
|
|
182
187
|
right, bottom, 1, 1, ti, ii
|
|
183
188
|
);
|
|
184
189
|
|
|
185
|
-
$.drawStack.push(6);
|
|
190
|
+
$.drawStack.push(1, 6);
|
|
186
191
|
};
|
|
187
192
|
};
|