@next2d/webgpu 3.14.0 → 3.15.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@next2d/webgpu",
3
- "version": "3.14.0",
3
+ "version": "3.15.1",
4
4
  "description": "Next2D WebGPU Package",
5
5
  "author": "Toshiyuki Ienaga<ienaga@next2d.app> (https://github.com/ienaga/)",
6
6
  "license": "MIT",
@@ -24,7 +24,7 @@
24
24
  "url": "git+https://github.com/Next2D/Player.git"
25
25
  },
26
26
  "dependencies": {
27
- "@next2d/texture-packer": "3.14.0",
28
- "@next2d/render-queue": "3.14.0"
27
+ "@next2d/texture-packer": "3.15.1",
28
+ "@next2d/render-queue": "3.15.1"
29
29
  }
30
30
  }
@@ -1,4 +1,23 @@
1
+ import type { IPoint } from "../../interface/IPoint";
1
2
  import type { IPath } from "../../interface/IPath";
3
+ /**
4
+ * @description カーブの矩形を計算(WebGL版のMeshCalculateCurveRectangleUseCaseと同じ)
5
+ * Calculate curve rectangle (same as WebGL MeshCalculateCurveRectangleUseCase)
6
+ *
7
+ * ストロークの輪郭は図形を初めて描くときに曲線ごとに計算されるため、点やセグメントごとに
8
+ * オブジェクトや配列を生成すると、その確保と回収が描画の停止として現れる。
9
+ * 作業領域を使い回し、結果の配列だけを生成する。
10
+ * Outlines are computed per curve the first time a shape is drawn, so allocating an object or
11
+ * an array per point or segment shows up as a stall. Work buffers are reused and only the
12
+ * result array is allocated.
13
+ *
14
+ * @param {IPoint} start_point - 始点 / Start point
15
+ * @param {IPoint} control_point - 制御点 / Control point
16
+ * @param {IPoint} end_point - 終点 / End point
17
+ * @param {number} thickness - 線の太さ(半分の値) / Half line thickness
18
+ * @return {IPath} カーブ矩形パス / Curve rectangle path
19
+ */
20
+ export declare const calculateCurveRectangle: (start_point: IPoint, control_point: IPoint, end_point: IPoint, thickness: number) => IPath;
2
21
  /**
3
22
  * @description 線の外周を算出して塗りのフォーマットで返却(WebGL版と同じ)
4
23
  * Calculate the outer circumference of the line and return it in the format of the fill
@@ -49,171 +49,178 @@ const $calculateNormalVector = (x, y, thickness) => {
49
49
  };
50
50
  };
51
51
  /**
52
- * @description 線形補間(lerp)
53
- * Linear interpolation between two points
54
- *
55
- * @param {IPoint} p0 - 始点 / Start point
56
- * @param {IPoint} p1 - 終点 / End point
57
- * @param {number} t - 補間パラメータ(0〜1) / Interpolation parameter (0-1)
58
- * @return {IPoint} 補間された点 / Interpolated point
52
+ * @description 2次ベジェ曲線を半分に分割する回数(WebGL版と同じ。分割後は 32 セグメント)
53
+ * Number of times a quadratic Bezier curve is halved (same as WebGL; 32 segments)
59
54
  */
60
- const $lerp = (p0, p1, t) => ({
61
- "x": p0.x + (p1.x - p0.x) * t,
62
- "y": p0.y + (p1.y - p0.y) * t
63
- });
55
+ const SPLIT_COUNT = 5;
64
56
  /**
65
- * @description ベクトルの正規化
66
- * Normalize a vector to unit length
67
- *
68
- * @param {IPoint} point - 正規化する点 / Point to normalize
69
- * @return {IPoint} 正規化された点 / Normalized point
57
+ * @description 分割後のセグメント数
58
+ * Number of segments after splitting
70
59
  */
71
- const $normalize = (point) => {
72
- const length = Math.sqrt(point.x * point.x + point.y * point.y);
73
- return length === 0
74
- ? { "x": 0, "y": 0 }
75
- : { "x": point.x / length, "y": point.y / length };
76
- };
60
+ const SEGMENT_COUNT = 1 << SPLIT_COUNT;
77
61
  /**
78
- * @description 二次ベジェ曲線上の座標を計算
79
- * Calculate a point on a quadratic Bezier curve
80
- *
81
- * @param {number} t - 曲線パラメータ(0〜1) / Curve parameter (0-1)
82
- * @param {IPoint} s0 - 始点 / Start point
83
- * @param {IPoint} s1 - 制御点 / Control point
84
- * @param {IPoint} s2 - 終点 / End point
85
- * @return {IPoint} 曲線上の座標 / Point on the curve
62
+ * @description 分割中のセグメント(1セグメント = 始点・制御点・終点の x, y で 6 要素)。2 本を交互に使う(GC回避)
63
+ * Segments being split (six values each: x, y of start, control and end points).
64
+ * Two buffers used alternately to avoid GC overhead
86
65
  */
87
- const $getQuadraticBezierPoint = (t, s0, s1, s2) => ({
88
- "x": (1 - t) ** 2 * s0.x + 2 * (1 - t) * t * s1.x + t ** 2 * s2.x,
89
- "y": (1 - t) ** 2 * s0.y + 2 * (1 - t) * t * s1.y + t ** 2 * s2.y
90
- });
66
+ let $segments = new Float64Array(SEGMENT_COUNT * 6);
67
+ let $nextSegments = new Float64Array(SEGMENT_COUNT * 6);
91
68
  /**
92
- * @description 二次ベジェ曲線上の接線ベクトルを計算
93
- * Calculate the tangent vector on a quadratic Bezier curve
94
- *
95
- * @param {number} t - 曲線パラメータ(0〜1) / Curve parameter (0-1)
96
- * @param {IPoint} s0 - 始点 / Start point
97
- * @param {IPoint} s1 - 制御点 / Control point
98
- * @param {IPoint} s2 - 終点 / End point
99
- * @return {IPoint} 接線ベクトル / Tangent vector
69
+ * @description 右サイドの点(1セグメント = t=0, 0.5, 1 の x, y で 6 要素)。逆順に出力するため一旦ためる(GC回避)
70
+ * Points of the right side (six values each: x, y at t = 0, 0.5 and 1),
71
+ * kept until the end because they are emitted in reverse order
100
72
  */
101
- const $getQuadraticBezierTangent = (t, s0, s1, s2) => ({
102
- "x": 2 * (1 - t) * (s1.x - s0.x) + 2 * t * (s2.x - s1.x),
103
- "y": 2 * (1 - t) * (s1.y - s0.y) + 2 * t * (s2.y - s1.y)
104
- });
73
+ const $rightPoints = new Float64Array(SEGMENT_COUNT * 6);
105
74
  /**
106
- * @description 二次ベジェ曲線を分割
107
- * Split a quadratic Bezier curve at a given parameter
108
- *
109
- * @param {IPoint} s0 - 始点 / Start point
110
- * @param {IPoint} s1 - 制御点 / Control point
111
- * @param {IPoint} s2 - 終点 / End point
112
- * @param {number} t - 分割パラメータ / Split parameter
113
- * @return {Array<IPoint[]>} 分割された2つの曲線 / Two split curves
75
+ * @description $offsetPoint の結果
76
+ * Result of $offsetPoint
114
77
  */
115
- const $splitQuadraticBezier = (s0, s1, s2, t = 0.5) => {
116
- const M0 = $lerp(s0, s1, t);
117
- const M1 = $lerp(s1, s2, t);
118
- const M01 = $lerp(M0, M1, t);
119
- return [
120
- [s0, M0, M01],
121
- [M01, M1, s2]
122
- ];
123
- };
78
+ let $offsetX = 0;
79
+ let $offsetY = 0;
124
80
  /**
125
- * @description ベジェ曲線を複数回分割
126
- * Split a Bezier curve multiple times
81
+ * @description 2次ベジェ曲線上の t の点を、法線方向へ offset だけずらした点を $offsetX / $offsetY に求める
82
+ * (曲線上の座標・接線・正規化の計算を、オブジェクトを生成せずに行う)
83
+ * Computes into $offsetX / $offsetY the point at t on a quadratic Bezier curve, moved by
84
+ * offset along the normal (position, tangent and normalization without allocating objects)
127
85
  *
128
- * @param {IPoint} s0 - 始点 / Start point
129
- * @param {IPoint} s1 - 制御点 / Control point
130
- * @param {IPoint} s2 - 終点 / End point
131
- * @param {number} n - 分割回数 / Number of splits
132
- * @return {Array<IPoint[]>} 分割されたセグメント配列 / Array of split segments
133
- */
134
- const $splitBezierMultipleTimes = (s0, s1, s2, n = 4) => {
135
- let segments = [[s0, s1, s2]];
136
- for (let i = 0; i < n; i++) {
137
- const newSegments = [];
138
- for (const seg of segments) {
139
- const splitted = $splitQuadraticBezier(seg[0], seg[1], seg[2], 0.5);
140
- newSegments.push(splitted[0], splitted[1]);
141
- }
142
- segments = newSegments;
143
- }
144
- return segments;
145
- };
146
- /**
147
- * @description 2次ベジェのオフセットを計算
148
- * Calculate offset of a quadratic Bezier curve
149
- *
150
- * @param {IPoint} s0 - 始点 / Start point
151
- * @param {IPoint} s1 - 制御点 / Control point
152
- * @param {IPoint} s2 - 終点 / End point
86
+ * @param {number} t - 曲線パラメータ(0〜1) / Curve parameter (0-1)
87
+ * @param {number} x0 - 始点のx / Start point x
88
+ * @param {number} y0 - 始点のy / Start point y
89
+ * @param {number} x1 - 制御点のx / Control point x
90
+ * @param {number} y1 - 制御点のy / Control point y
91
+ * @param {number} x2 - 終点のx / End point x
92
+ * @param {number} y2 - 終点のy / End point y
153
93
  * @param {number} offset - オフセット距離 / Offset distance
154
- * @return {IPoint[]} オフセットされた点配列 / Array of offset points
94
+ * @return {void}
155
95
  */
156
- const $approximateOffsetQuadratic = (s0, s1, s2, offset) => {
157
- const tValues = [0, 0.5, 1];
158
- const newPoints = [];
159
- for (const t of tValues) {
160
- const pos = $getQuadraticBezierPoint(t, s0, s1, s2);
161
- const tan = $getQuadraticBezierTangent(t, s0, s1, s2);
162
- const n = $normalize({ "x": -tan.y, "y": tan.x });
163
- newPoints.push({
164
- "x": pos.x + n.x * offset,
165
- "y": pos.y + n.y * offset
166
- });
96
+ const $offsetPoint = (t, x0, y0, x1, y1, x2, y2, offset) => {
97
+ const positionX = (1 - t) ** 2 * x0 + 2 * (1 - t) * t * x1 + t ** 2 * x2;
98
+ const positionY = (1 - t) ** 2 * y0 + 2 * (1 - t) * t * y1 + t ** 2 * y2;
99
+ const tangentX = 2 * (1 - t) * (x1 - x0) + 2 * t * (x2 - x1);
100
+ const tangentY = 2 * (1 - t) * (y1 - y0) + 2 * t * (y2 - y1);
101
+ // 接線を 90 度回した法線を正規化
102
+ const normalX = -tangentY;
103
+ const normalY = tangentX;
104
+ const length = Math.sqrt(normalX * normalX + normalY * normalY);
105
+ if (length === 0) {
106
+ $offsetX = positionX + 0 * offset;
107
+ $offsetY = positionY + 0 * offset;
108
+ }
109
+ else {
110
+ $offsetX = positionX + normalX / length * offset;
111
+ $offsetY = positionY + normalY / length * offset;
167
112
  }
168
- return newPoints;
169
113
  };
170
114
  /**
171
115
  * @description カーブの矩形を計算(WebGL版のMeshCalculateCurveRectangleUseCaseと同じ)
172
116
  * Calculate curve rectangle (same as WebGL MeshCalculateCurveRectangleUseCase)
173
117
  *
118
+ * ストロークの輪郭は図形を初めて描くときに曲線ごとに計算されるため、点やセグメントごとに
119
+ * オブジェクトや配列を生成すると、その確保と回収が描画の停止として現れる。
120
+ * 作業領域を使い回し、結果の配列だけを生成する。
121
+ * Outlines are computed per curve the first time a shape is drawn, so allocating an object or
122
+ * an array per point or segment shows up as a stall. Work buffers are reused and only the
123
+ * result array is allocated.
124
+ *
174
125
  * @param {IPoint} start_point - 始点 / Start point
175
126
  * @param {IPoint} control_point - 制御点 / Control point
176
127
  * @param {IPoint} end_point - 終点 / End point
177
128
  * @param {number} thickness - 線の太さ(半分の値) / Half line thickness
178
129
  * @return {IPath} カーブ矩形パス / Curve rectangle path
179
130
  */
180
- const $calculateCurveRectangle = (start_point, control_point, end_point, thickness) => {
181
- // WebGL版と同じ分割数(5回分割 = 32セグメント)
182
- const segments = $splitBezierMultipleTimes(start_point, control_point, end_point, 5);
183
- const leftCurves = [];
184
- const rightCurves = [];
185
- for (const seg of segments) {
186
- leftCurves.push($approximateOffsetQuadratic(seg[0], seg[1], seg[2], +thickness));
187
- rightCurves.push($approximateOffsetQuadratic(seg[0], seg[1], seg[2], -thickness));
188
- }
189
- // セグメント間の連続性を確保:各セグメントの終点を次のセグメントの始点に強制一致
190
- // これにより内側の曲線のつなぎめの隙間を解消
191
- for (let idx = 0; idx < leftCurves.length - 1; ++idx) {
192
- leftCurves[idx + 1][0] = leftCurves[idx][2];
193
- }
194
- for (let idx = 0; idx < rightCurves.length - 1; ++idx) {
195
- rightCurves[idx + 1][0] = rightCurves[idx][2];
131
+ export const calculateCurveRectangle = (start_point, control_point, end_point, thickness) => {
132
+ // WebGL版と同じ分割数(5回分割 = 32セグメント)。De Casteljau で t = 0.5 の分割を繰り返す
133
+ let segments = $segments;
134
+ let nextSegments = $nextSegments;
135
+ segments[0] = start_point.x;
136
+ segments[1] = start_point.y;
137
+ segments[2] = control_point.x;
138
+ segments[3] = control_point.y;
139
+ segments[4] = end_point.x;
140
+ segments[5] = end_point.y;
141
+ let count = 1;
142
+ for (let split = 0; split < SPLIT_COUNT; ++split) {
143
+ for (let idx = 0; idx < count; ++idx) {
144
+ const source = idx * 6;
145
+ const x0 = segments[source];
146
+ const y0 = segments[source + 1];
147
+ const x1 = segments[source + 2];
148
+ const y1 = segments[source + 3];
149
+ const x2 = segments[source + 4];
150
+ const y2 = segments[source + 5];
151
+ const m0x = x0 + (x1 - x0) * 0.5;
152
+ const m0y = y0 + (y1 - y0) * 0.5;
153
+ const m1x = x1 + (x2 - x1) * 0.5;
154
+ const m1y = y1 + (y2 - y1) * 0.5;
155
+ const m01x = m0x + (m1x - m0x) * 0.5;
156
+ const m01y = m0y + (m1y - m0y) * 0.5;
157
+ const target = idx * 12;
158
+ nextSegments[target] = x0;
159
+ nextSegments[target + 1] = y0;
160
+ nextSegments[target + 2] = m0x;
161
+ nextSegments[target + 3] = m0y;
162
+ nextSegments[target + 4] = m01x;
163
+ nextSegments[target + 5] = m01y;
164
+ nextSegments[target + 6] = m01x;
165
+ nextSegments[target + 7] = m01y;
166
+ nextSegments[target + 8] = m1x;
167
+ nextSegments[target + 9] = m1y;
168
+ nextSegments[target + 10] = x2;
169
+ nextSegments[target + 11] = y2;
170
+ }
171
+ const swap = segments;
172
+ segments = nextSegments;
173
+ nextSegments = swap;
174
+ count *= 2;
196
175
  }
176
+ $segments = segments;
177
+ $nextSegments = nextSegments;
197
178
  // 左サイドの最初のサブカーブ始点
198
- const leftStart = leftCurves[0][0];
199
- const paths = [leftStart.x, leftStart.y, false];
200
- // 左サイド: WebGL版と同じく曲線フラグをtrueに設定(Loop-Blinn法で処理)
201
- for (let idx = 0; idx < leftCurves.length; ++idx) {
202
- const curves = leftCurves[idx];
203
- paths.push(curves[1].x, curves[1].y, true, curves[2].x, curves[2].y, false);
204
- }
205
- const reversedRight = [...rightCurves].reverse();
206
- for (let idx = 0; idx < reversedRight.length; ++idx) {
207
- const [q0, q1, q2] = reversedRight[idx];
208
- reversedRight[idx] = [q2, q1, q0]; // [Q2, Q1, Q0]
209
- }
210
- // 右サイドの最初のサブカーブ始点
211
- const rightEnd = reversedRight[0][0];
212
- paths.push(rightEnd.x, rightEnd.y, false);
179
+ $offsetPoint(0, segments[0], segments[1], segments[2], segments[3], segments[4], segments[5], +thickness);
180
+ const paths = [$offsetX, $offsetY, false];
181
+ const rightPoints = $rightPoints;
182
+ for (let idx = 0; idx < count; ++idx) {
183
+ const source = idx * 6;
184
+ const x0 = segments[source];
185
+ const y0 = segments[source + 1];
186
+ const x1 = segments[source + 2];
187
+ const y1 = segments[source + 3];
188
+ const x2 = segments[source + 4];
189
+ const y2 = segments[source + 5];
190
+ // 左サイド: WebGL版と同じく曲線フラグをtrueに設定(Loop-Blinn法で処理)
191
+ $offsetPoint(0.5, x0, y0, x1, y1, x2, y2, +thickness);
192
+ const controlX = $offsetX;
193
+ const controlY = $offsetY;
194
+ $offsetPoint(1, x0, y0, x1, y1, x2, y2, +thickness);
195
+ paths.push(controlX, controlY, true, $offsetX, $offsetY, false);
196
+ // 右サイドは逆順に出力するので、ここではためておく。
197
+ // セグメント間の連続性を確保するため、各セグメントの始点には前のセグメントの終点を使う
198
+ // (内側の曲線のつなぎめの隙間を解消)。最初のセグメントだけ自身の始点(t=0)を使う。
199
+ if (idx === 0) {
200
+ $offsetPoint(0, x0, y0, x1, y1, x2, y2, -thickness);
201
+ rightPoints[source] = $offsetX;
202
+ rightPoints[source + 1] = $offsetY;
203
+ }
204
+ $offsetPoint(0.5, x0, y0, x1, y1, x2, y2, -thickness);
205
+ rightPoints[source + 2] = $offsetX;
206
+ rightPoints[source + 3] = $offsetY;
207
+ $offsetPoint(1, x0, y0, x1, y1, x2, y2, -thickness);
208
+ rightPoints[source + 4] = $offsetX;
209
+ rightPoints[source + 5] = $offsetY;
210
+ }
211
+ // 右サイドの最初のサブカーブ始点(最後のセグメントの終点)
212
+ const last = (count - 1) * 6;
213
+ paths.push(rightPoints[last + 4], rightPoints[last + 5], false);
213
214
  // 右サイド: WebGL版と同じく曲線フラグをtrueに設定(Loop-Blinn法で処理)
214
- for (let idx = 0; idx < reversedRight.length; ++idx) {
215
- const curves = reversedRight[idx];
216
- paths.push(curves[1].x, curves[1].y, true, curves[2].x, curves[2].y, false);
215
+ for (let idx = count - 1; idx >= 0; --idx) {
216
+ const source = idx * 6;
217
+ if (idx > 0) {
218
+ // このセグメントの始点 = 前のセグメントの終点
219
+ paths.push(rightPoints[source + 2], rightPoints[source + 3], true, rightPoints[source - 2], rightPoints[source - 1], false);
220
+ }
221
+ else {
222
+ paths.push(rightPoints[source + 2], rightPoints[source + 3], true, rightPoints[source], rightPoints[source + 1], false);
223
+ }
217
224
  }
218
225
  return paths;
219
226
  };
@@ -599,7 +606,7 @@ export const generateStrokeOutline = (vertices, thickness) => {
599
606
  endPoint.x = x;
600
607
  endPoint.y = y;
601
608
  if (vertices[idx - 1]) {
602
- rectangles.push($calculateCurveRectangle(startPoint, controlPoint, endPoint, thickness));
609
+ rectangles.push(calculateCurveRectangle(startPoint, controlPoint, endPoint, thickness));
603
610
  }
604
611
  else {
605
612
  rectangles.push($calculateLineRectangle(startPoint, endPoint, thickness));