@next2d/webgpu 3.15.1 → 3.16.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.
@@ -27,6 +27,18 @@ export const getAdaptiveResolution = (stops_length) => {
27
27
  * 注意: R, G, B, A は 0-255 範囲
28
28
  * LUTは0-1の範囲の色を生成し、spread処理はシェーダー側で行う
29
29
  *
30
+ * 新しいグラデーションが初めて出るたびに (キャッシュに無いたびに) 256〜1024 画素ぶん
31
+ * 走るので、1 画素ごとにオブジェクトを作らず、ストップの検索もやり直さない。
32
+ * 位置 t は画素とともに増えるので、「t 以上の位置を持つ最初のストップ」は後ろへしか
33
+ * 動かない。区間が変わったときだけ、その区間の両端の色を用意する。
34
+ * 結果は、画素ごとにストップを先頭から検索する方法と同じ。
35
+ *
36
+ * This runs over 256 to 1024 texels every time a gradient misses the cache, so it
37
+ * allocates nothing per texel and never restarts the stop search: t grows with the
38
+ * texel index, so the first stop at or after t only moves forward, and the end colors
39
+ * of a segment are prepared only when the segment changes. The result equals searching
40
+ * the stops from the start for every texel.
41
+ *
30
42
  * @param {number[]} stops - グラデーションストップ配列 / Gradient stop array
31
43
  * @param {number} _spread - スプレッドメソッド(未使用、シェーダー側で処理)/ Spread method (unused, handled by shader)
32
44
  * @param {number} interpolation - 補間方法 (0: linearRGB, 1: RGB) / Interpolation method (WebGL compatible)
@@ -40,144 +52,111 @@ export const generateGradientLUT = (stops, _spread, interpolation) => {
40
52
  const resolution = getAdaptiveResolution(stopsLength);
41
53
  // RGBA形式のLUTデータを作成
42
54
  const lutData = new Uint8Array(resolution * 4);
43
- // 各ピクセルの色を補間
55
+ if (stopsLength < 1) {
56
+ return lutData;
57
+ }
58
+ // WebGL互換: interpolation === 0 がlinearRGB
59
+ const linear = interpolation === 0;
60
+ const scale = resolution - 1;
61
+ const last = (stopsLength - 1) * 5;
62
+ // t 以上の位置を持つ最初のストップ (無ければ stopsLength)
63
+ let end = 0;
64
+ // 色を用意済みの区間 (end の値) と、その両端
65
+ let segment = -1;
66
+ let offset = 0;
67
+ let span = 0;
68
+ let r0 = 0, g0 = 0, b0 = 0, a0 = 0;
69
+ let r1 = 0, g1 = 0, b1 = 0, a1 = 0;
44
70
  // spread処理はシェーダー側で行うため、LUTは単純に0-1の範囲を生成
45
71
  for (let i = 0; i < resolution; i++) {
46
- const t = i / (resolution - 1);
47
- // 色を補間(色は0-255範囲で返される)
48
- const color = $interpolateColor(stops, t, interpolation);
72
+ const t = i / scale;
73
+ while (end < stopsLength && !(stops[end * 5] >= t)) {
74
+ end++;
75
+ }
76
+ // 色は0-255範囲
77
+ let r, g, b, a;
78
+ if (end >= stopsLength) {
79
+ // 最後のストップを超えている
80
+ r = stops[last + 1];
81
+ g = stops[last + 2];
82
+ b = stops[last + 3];
83
+ a = stops[last + 4];
84
+ }
85
+ else if (end === 0 || stops[end * 5] === t) {
86
+ // 最初のストップより手前、またはストップの位置そのもの
87
+ const index = end * 5;
88
+ r = stops[index + 1];
89
+ g = stops[index + 2];
90
+ b = stops[index + 3];
91
+ a = stops[index + 4];
92
+ }
93
+ else {
94
+ if (segment !== end) {
95
+ segment = end;
96
+ const from = end * 5 - 5;
97
+ const to = end * 5;
98
+ // offset は既に 0-1 範囲
99
+ offset = stops[from];
100
+ span = stops[to] - offset;
101
+ if (linear) {
102
+ // linearRGB補間(ガンマ補正): 0-255 → 0-1に正規化してからリニア変換
103
+ // WebGL版と同じガンマ値 2.23333333 を使用
104
+ r0 = Math.pow(stops[from + 1] / 255, 2.23333333);
105
+ g0 = Math.pow(stops[from + 2] / 255, 2.23333333);
106
+ b0 = Math.pow(stops[from + 3] / 255, 2.23333333);
107
+ r1 = Math.pow(stops[to + 1] / 255, 2.23333333);
108
+ g1 = Math.pow(stops[to + 2] / 255, 2.23333333);
109
+ b1 = Math.pow(stops[to + 3] / 255, 2.23333333);
110
+ }
111
+ else {
112
+ r0 = stops[from + 1];
113
+ g0 = stops[from + 2];
114
+ b0 = stops[from + 3];
115
+ r1 = stops[to + 1];
116
+ g1 = stops[to + 2];
117
+ b1 = stops[to + 3];
118
+ }
119
+ a0 = stops[from + 4];
120
+ a1 = stops[to + 4];
121
+ }
122
+ const localT = (t - offset) / span;
123
+ if (linear) {
124
+ // リニア色空間で補間してから sRGB へ戻す (WebGL版と同じガンマ値 0.45454545)
125
+ r = Math.pow(r0 + (r1 - r0) * localT, 0.45454545) * 255;
126
+ g = Math.pow(g0 + (g1 - g0) * localT, 0.45454545) * 255;
127
+ b = Math.pow(b0 + (b1 - b0) * localT, 0.45454545) * 255;
128
+ }
129
+ else {
130
+ // RGB補間(リニア、デフォルト)- 0-255範囲でそのまま補間
131
+ r = r0 + (r1 - r0) * localT;
132
+ g = g0 + (g1 - g0) * localT;
133
+ b = b0 + (b1 - b0) * localT;
134
+ }
135
+ a = a0 + (a1 - a0) * localT;
136
+ }
49
137
  // WebGL版と同じ: プリマルチプライドアルファは適用しない
50
138
  // LUTにはストレート(非プリマルチプライド)の色を格納
51
139
  // プリマルチプライドはシェーダー側でサンプリング後に行う
52
140
  // これにより、線形補間時に正しい色が得られる
53
- // LUTデータに書き込み(非プリマルチプライド)
54
- const offset = i * 4;
55
- lutData[offset + 0] = Math.round(Math.max(0, Math.min(255, color.r)));
56
- lutData[offset + 1] = Math.round(Math.max(0, Math.min(255, color.g)));
57
- lutData[offset + 2] = Math.round(Math.max(0, Math.min(255, color.b)));
58
- lutData[offset + 3] = Math.round(Math.max(0, Math.min(255, color.a)));
141
+ // 0-255 に収めて四捨五入する (非数は 0)
142
+ const index = i * 4;
143
+ lutData[index] = r > 0 ? r < 255 ? r + 0.5 | 0 : 255 : 0;
144
+ lutData[index + 1] = g > 0 ? g < 255 ? g + 0.5 | 0 : 255 : 0;
145
+ lutData[index + 2] = b > 0 ? b < 255 ? b + 0.5 | 0 : 255 : 0;
146
+ lutData[index + 3] = a > 0 ? a < 255 ? a + 0.5 | 0 : 255 : 0;
59
147
  }
60
148
  return lutData;
61
149
  };
62
- /**
63
- * @description 色を補間する
64
- * Interpolate color between gradient stops.
65
- * 色は0-255範囲で入力され、0-255範囲で出力される
66
- * Colors are input in 0-255 range and output in 0-255 range.
67
- *
68
- * @param {number[]} stops - グラデーションストップ配列 / Gradient stop array
69
- * @param {number} t - 補間位置 (0-1) / Interpolation position (0-1)
70
- * @param {number} interpolation - 補間方法 (0: linearRGB, 1: RGB) / Interpolation method (WebGL compatible)
71
- * @return {{ r: number, g: number, b: number, a: number }} 補間された色 / Interpolated color
72
- * @private
73
- */
74
- const $interpolateColor = (stops, t, interpolation) => {
75
- const stopsLength = stops.length / 5;
76
- // 最初と最後のストップを見つける
77
- // endIdx の番兵は -1(0 は「最初のストップが該当」の正当な値のため、
78
- // 0 を番兵にすると t が最初のストップ未満のときに最後のストップと
79
- // 誤って補間され、範囲外へ外挿されてしまう)
80
- let startIdx = 0;
81
- let endIdx = -1;
82
- for (let i = 0; i < stopsLength; i++) {
83
- // offset は既に 0-1 範囲
84
- const offset = stops[i * 5];
85
- if (offset <= t) {
86
- startIdx = i;
87
- }
88
- if (offset >= t && endIdx === -1) {
89
- endIdx = i;
90
- break;
91
- }
92
- }
93
- // 最後のストップを超えている場合
94
- if (endIdx === -1) {
95
- endIdx = stopsLength - 1;
96
- }
97
- // 同じストップの場合(色は0-255範囲)
98
- if (startIdx === endIdx) {
99
- const idx = startIdx * 5;
100
- return {
101
- "r": stops[idx + 1],
102
- "g": stops[idx + 2],
103
- "b": stops[idx + 3],
104
- "a": stops[idx + 4]
105
- };
106
- }
107
- // 補間係数を計算(offset は既に 0-1 範囲)
108
- const startOffset = stops[startIdx * 5];
109
- const endOffset = stops[endIdx * 5];
110
- const localT = (t - startOffset) / (endOffset - startOffset);
111
- // 色を取得(0-255範囲)
112
- const startR = stops[startIdx * 5 + 1];
113
- const startG = stops[startIdx * 5 + 2];
114
- const startB = stops[startIdx * 5 + 3];
115
- const startA = stops[startIdx * 5 + 4];
116
- const endR = stops[endIdx * 5 + 1];
117
- const endG = stops[endIdx * 5 + 2];
118
- const endB = stops[endIdx * 5 + 3];
119
- const endA = stops[endIdx * 5 + 4];
120
- // 補間(WebGL互換: interpolation === 0 がlinearRGB)
121
- if (interpolation === 0) {
122
- // linearRGB補間(ガンマ補正)
123
- // 0-255 → 0-1に正規化してからリニア変換
124
- return {
125
- "r": $linearToSRGB($lerp($sRGBToLinear(startR / 255), $sRGBToLinear(endR / 255), localT)) * 255,
126
- "g": $linearToSRGB($lerp($sRGBToLinear(startG / 255), $sRGBToLinear(endG / 255), localT)) * 255,
127
- "b": $linearToSRGB($lerp($sRGBToLinear(startB / 255), $sRGBToLinear(endB / 255), localT)) * 255,
128
- "a": $lerp(startA, endA, localT)
129
- };
130
- }
131
- // RGB補間(リニア、デフォルト)- 0-255範囲でそのまま補間
132
- return {
133
- "r": $lerp(startR, endR, localT),
134
- "g": $lerp(startG, endG, localT),
135
- "b": $lerp(startB, endB, localT),
136
- "a": $lerp(startA, endA, localT)
137
- };
138
- };
139
- /**
140
- * @description 線形補間を行う
141
- * Perform linear interpolation between two values
142
- *
143
- * @param {number} a - 開始値 / Start value
144
- * @param {number} b - 終了値 / End value
145
- * @param {number} t - 補間係数 (0-1) / Interpolation factor (0-1)
146
- * @return {number} 補間結果 / Interpolated result
147
- * @private
148
- */
149
- const $lerp = (a, b, t) => {
150
- return a + (b - a) * t;
151
- };
152
- /**
153
- * @description sRGBからリニア色空間へ変換する(入力: 0-1正規化値)
154
- * Convert from sRGB to linear color space (input: 0-1 normalized value).
155
- * WebGL版と同じガンマ値 2.23333333 を使用
156
- *
157
- * @param {number} value - sRGB色空間の正規化値 (0-1) / Normalized value in sRGB color space
158
- * @return {number} リニア色空間の値 / Value in linear color space
159
- * @private
160
- */
161
- const $sRGBToLinear = (value) => {
162
- return Math.pow(value, 2.23333333);
163
- };
164
- /**
165
- * @description リニア色空間からsRGBへ変換する(出力: 0-1正規化値)
166
- * Convert from linear color space to sRGB (output: 0-1 normalized value).
167
- * WebGL版と同じガンマ値 0.45454545 (= 1/2.2) を使用
168
- *
169
- * @param {number} value - リニア色空間の値 / Value in linear color space
170
- * @return {number} sRGB色空間の正規化値 / Normalized value in sRGB color space
171
- * @private
172
- */
173
- const $linearToSRGB = (value) => {
174
- return Math.pow(value, 0.45454545);
175
- };
176
150
  /**
177
151
  * @description フィルター用グラデーションLUTテクスチャデータを生成する
178
152
  * Generate gradient LUT texture data for filters.
179
153
  * ratios, colors, alphas配列から1D LUTを生成
180
154
  *
155
+ * 1 画素ごと、1 ストップごとのオブジェクトは作らない。区間が変わったときだけ、
156
+ * その区間の両端の色を用意する。
157
+ * No object is allocated per texel or per stop; the end colors of a segment are
158
+ * prepared only when the segment changes.
159
+ *
181
160
  * @param {Float32Array} ratios - 比率配列 (0-255) / Ratio array (0-255)
182
161
  * @param {Float32Array} colors - 色配列 (32bit整数) / Color array (32-bit integers)
183
162
  * @param {Float32Array} alphas - アルファ配列 (0-1) / Alpha array (0-1)
@@ -189,48 +168,55 @@ export const generateFilterGradientLUT = (ratios, colors, alphas) => {
189
168
  const resolution = 256;
190
169
  const lutData = new Uint8Array(resolution * 4);
191
170
  const stopsLength = ratios.length;
192
- // ストップデータを構築
193
- const stops = [];
194
- for (let i = 0; i < stopsLength; i++) {
195
- const color = colors[i];
196
- stops.push({
197
- "offset": ratios[i] / 255,
198
- "r": (color >> 16 & 0xFF) / 255,
199
- "g": (color >> 8 & 0xFF) / 255,
200
- "b": (color & 0xFF) / 255,
201
- "a": alphas[i]
202
- });
203
- }
171
+ const scale = resolution - 1;
172
+ // 色を用意済みの区間と、その両端 (色は 0-1 範囲)
173
+ let segmentStart = -1;
174
+ let segmentEnd = -1;
175
+ let offset = 0;
176
+ let span = 0;
177
+ let r0 = 0, g0 = 0, b0 = 0, a0 = 0;
178
+ let r1 = 0, g1 = 0, b1 = 0, a1 = 0;
204
179
  // 各ピクセルの色を補間
205
180
  for (let i = 0; i < resolution; i++) {
206
- const t = i / (resolution - 1);
181
+ const t = i / scale;
207
182
  // ストップを見つける
208
183
  let startIdx = 0;
209
184
  let endIdx = stopsLength - 1;
210
185
  for (let j = 0; j < stopsLength - 1; j++) {
211
- if (stops[j].offset <= t && stops[j + 1].offset >= t) {
186
+ if (ratios[j] / 255 <= t && ratios[j + 1] / 255 >= t) {
212
187
  startIdx = j;
213
188
  endIdx = j + 1;
214
189
  break;
215
190
  }
216
191
  }
217
- // 補間
218
- const start = stops[startIdx];
219
- const end = stops[endIdx];
220
- let localT = 0;
221
- if (end.offset !== start.offset) {
222
- localT = (t - start.offset) / (end.offset - start.offset);
192
+ if (segmentStart !== startIdx || segmentEnd !== endIdx) {
193
+ segmentStart = startIdx;
194
+ segmentEnd = endIdx;
195
+ const startColor = colors[startIdx];
196
+ const endColor = colors[endIdx];
197
+ offset = ratios[startIdx] / 255;
198
+ span = ratios[endIdx] / 255 - offset;
199
+ r0 = (startColor >> 16 & 0xFF) / 255;
200
+ g0 = (startColor >> 8 & 0xFF) / 255;
201
+ b0 = (startColor & 0xFF) / 255;
202
+ a0 = alphas[startIdx];
203
+ r1 = (endColor >> 16 & 0xFF) / 255;
204
+ g1 = (endColor >> 8 & 0xFF) / 255;
205
+ b1 = (endColor & 0xFF) / 255;
206
+ a1 = alphas[endIdx];
223
207
  }
224
- const r = $lerp(start.r, end.r, localT);
225
- const g = $lerp(start.g, end.g, localT);
226
- const b = $lerp(start.b, end.b, localT);
227
- const a = $lerp(start.a, end.a, localT);
208
+ // 補間
209
+ const localT = span !== 0 ? (t - offset) / span : 0;
210
+ const r = r0 + (r1 - r0) * localT;
211
+ const g = g0 + (g1 - g0) * localT;
212
+ const b = b0 + (b1 - b0) * localT;
213
+ const a = a0 + (a1 - a0) * localT;
228
214
  // プリマルチプライドアルファで書き込み
229
- const offset = i * 4;
230
- lutData[offset + 0] = Math.round(r * a * 255);
231
- lutData[offset + 1] = Math.round(g * a * 255);
232
- lutData[offset + 2] = Math.round(b * a * 255);
233
- lutData[offset + 3] = Math.round(a * 255);
215
+ const index = i * 4;
216
+ lutData[index] = Math.round(r * a * 255);
217
+ lutData[index + 1] = Math.round(g * a * 255);
218
+ lutData[index + 2] = Math.round(b * a * 255);
219
+ lutData[index + 3] = Math.round(a * 255);
234
220
  }
235
221
  return lutData;
236
222
  };
@@ -4,54 +4,10 @@
4
4
  */
5
5
  export class InstanceBufferAllocator {
6
6
  constructor(device, capacity = 65536) {
7
- Object.defineProperty(this, "device", {
8
- enumerable: true,
9
- configurable: true,
10
- writable: true,
11
- value: void 0
12
- });
13
- Object.defineProperty(this, "maxSize", {
14
- enumerable: true,
15
- configurable: true,
16
- writable: true,
17
- value: void 0
18
- });
19
- Object.defineProperty(this, "capacity", {
20
- enumerable: true,
21
- configurable: true,
22
- writable: true,
23
- value: void 0
24
- });
25
- Object.defineProperty(this, "buffer", {
26
- enumerable: true,
27
- configurable: true,
28
- writable: true,
29
- value: null
30
- });
31
- Object.defineProperty(this, "staging", {
32
- enumerable: true,
33
- configurable: true,
34
- writable: true,
35
- value: void 0
36
- });
37
- Object.defineProperty(this, "offset", {
38
- enumerable: true,
39
- configurable: true,
40
- writable: true,
41
- value: 0
42
- });
43
- Object.defineProperty(this, "dirtyStart", {
44
- enumerable: true,
45
- configurable: true,
46
- writable: true,
47
- value: 0
48
- });
49
- Object.defineProperty(this, "retiredBuffers", {
50
- enumerable: true,
51
- configurable: true,
52
- writable: true,
53
- value: []
54
- });
7
+ this.buffer = null;
8
+ this.offset = 0;
9
+ this.dirtyStart = 0;
10
+ this.retiredBuffers = [];
55
11
  this.device = device;
56
12
  this.maxSize = device.limits?.maxBufferSize ?? 268435456;
57
13
  this.capacity = Math.min(this.maxSize, capacity);
@@ -15,9 +15,12 @@ import type { IPath } from "../../interface/IPath";
15
15
  * @param {IPoint} control_point - 制御点 / Control point
16
16
  * @param {IPoint} end_point - 終点 / End point
17
17
  * @param {number} thickness - 線の太さ(半分の値) / Half line thickness
18
+ * @param {number} [pixel_scale=0] - 曲線の座標 1 単位が画面上で何ピクセルになるか。0 以下なら最大数で分割する
19
+ * / On-screen pixels per unit of the curve's coordinates. 0 or less splits
20
+ * into the maximum number of segments
18
21
  * @return {IPath} カーブ矩形パス / Curve rectangle path
19
22
  */
20
- export declare const calculateCurveRectangle: (start_point: IPoint, control_point: IPoint, end_point: IPoint, thickness: number) => IPath;
23
+ export declare const calculateCurveRectangle: (start_point: IPoint, control_point: IPoint, end_point: IPoint, thickness: number, pixel_scale?: number) => IPath;
21
24
  /**
22
25
  * @description 線の外周を算出して塗りのフォーマットで返却(WebGL版と同じ)
23
26
  * Calculate the outer circumference of the line and return it in the format of the fill
@@ -111,6 +111,83 @@ const $offsetPoint = (t, x0, y0, x1, y1, x2, y2, offset) => {
111
111
  $offsetY = positionY + normalY / length * offset;
112
112
  }
113
113
  };
114
+ /**
115
+ * @description 画面上の誤差の許容値 (ピクセル)。輪郭の近似がこの距離以内に収まる分割回数を選ぶ。
116
+ * Allowed on-screen error in pixels. The split count is chosen so that the outline
117
+ * approximation stays within this distance.
118
+ *
119
+ * @type {number}
120
+ * @const
121
+ */
122
+ const STROKE_TOLERANCE = 0.05;
123
+ /**
124
+ * @description 2次ベジェ曲線の線の輪郭を作るときに、曲線を半分に分割する回数を求める。
125
+ *
126
+ * 輪郭は、分割した各区間を「t = 0, 0.5, 1 の点を法線方向へずらした 3 点」の 2 次ベジェで
127
+ * 近似する。区間 1 つの弦からのふくらみは (D + thickness * θ² / 8) / 4^n、近似の誤差は
128
+ * その約半分になる (D = |P0 - 2 * P1 + P2| / 4、θ = 始点と終点の接線がなす角、n = 分割回数)。
129
+ * 見積もりに 1.5 倍の余裕を持たせ、画面上の誤差が STROKE_TOLERANCE 以内になる最小の n を返す。
130
+ * 折り返しに近い曲線 (θ が 2 ラジアンを超える) は見積もりが甘くなるので、最大回数にする。
131
+ * pixel_scale が 0 以下のとき (画面上の拡大率が分からない) も最大回数にする。
132
+ *
133
+ * Returns how many times a quadratic Bezier curve is halved when building a stroke outline.
134
+ * Each resulting segment is approximated by the quadratic through the points at t = 0, 0.5
135
+ * and 1 moved along the normal. The sagitta of one segment is
136
+ * (D + thickness * θ² / 8) / 4^n and the approximation error is about half of it
137
+ * (D = |P0 - 2 * P1 + P2| / 4, θ = angle between the end tangents, n = split count).
138
+ * With a 1.5x margin, the smallest n keeping the on-screen error within STROKE_TOLERANCE is
139
+ * returned. Curves that nearly fold back (θ above 2 radians) and an unknown scale
140
+ * (pixel_scale <= 0) use the maximum.
141
+ *
142
+ * @param {number} x0
143
+ * @param {number} y0
144
+ * @param {number} x1
145
+ * @param {number} y1
146
+ * @param {number} x2
147
+ * @param {number} y2
148
+ * @param {number} thickness
149
+ * @param {number} pixel_scale
150
+ * @return {number}
151
+ * @method
152
+ * @private
153
+ */
154
+ const $strokeSplitCount = (x0, y0, x1, y1, x2, y2, thickness, pixel_scale) => {
155
+ if (!(pixel_scale > 0)) {
156
+ return SPLIT_COUNT;
157
+ }
158
+ const ax = x1 - x0;
159
+ const ay = y1 - y0;
160
+ const bx = x2 - x1;
161
+ const by = y2 - y1;
162
+ const lengthA = Math.sqrt(ax * ax + ay * ay);
163
+ const lengthB = Math.sqrt(bx * bx + by * by);
164
+ if (lengthA === 0 || lengthB === 0) {
165
+ return SPLIT_COUNT;
166
+ }
167
+ let cosine = (ax * bx + ay * by) / (lengthA * lengthB);
168
+ if (cosine > 1) {
169
+ cosine = 1;
170
+ }
171
+ else if (cosine < -1) {
172
+ cosine = -1;
173
+ }
174
+ const theta = Math.acos(cosine);
175
+ if (theta > 2) {
176
+ return SPLIT_COUNT;
177
+ }
178
+ const dx = x0 - 2 * x1 + x2;
179
+ const dy = y0 - 2 * y1 + y2;
180
+ const sagitta = Math.sqrt(dx * dx + dy * dy) / 4
181
+ + Math.abs(thickness) * theta * theta / 8;
182
+ // 誤差 (ピクセル) = sagitta / 2 * 1.5 (余裕) * pixel_scale / 4^n <= STROKE_TOLERANCE
183
+ let error = sagitta * 0.75 * pixel_scale;
184
+ let count = 0;
185
+ while (error > STROKE_TOLERANCE && count < SPLIT_COUNT) {
186
+ error *= 0.25;
187
+ ++count;
188
+ }
189
+ return count;
190
+ };
114
191
  /**
115
192
  * @description カーブの矩形を計算(WebGL版のMeshCalculateCurveRectangleUseCaseと同じ)
116
193
  * Calculate curve rectangle (same as WebGL MeshCalculateCurveRectangleUseCase)
@@ -126,10 +203,15 @@ const $offsetPoint = (t, x0, y0, x1, y1, x2, y2, offset) => {
126
203
  * @param {IPoint} control_point - 制御点 / Control point
127
204
  * @param {IPoint} end_point - 終点 / End point
128
205
  * @param {number} thickness - 線の太さ(半分の値) / Half line thickness
206
+ * @param {number} [pixel_scale=0] - 曲線の座標 1 単位が画面上で何ピクセルになるか。0 以下なら最大数で分割する
207
+ * / On-screen pixels per unit of the curve's coordinates. 0 or less splits
208
+ * into the maximum number of segments
129
209
  * @return {IPath} カーブ矩形パス / Curve rectangle path
130
210
  */
131
- export const calculateCurveRectangle = (start_point, control_point, end_point, thickness) => {
132
- // WebGL版と同じ分割数(5回分割 = 32セグメント)。De Casteljau で t = 0.5 の分割を繰り返す
211
+ export const calculateCurveRectangle = (start_point, control_point, end_point, thickness, pixel_scale = 0) => {
212
+ // 画面上の誤差から必要な分割回数を決める (WebGL版と同じ)。拡大率が分からなければ最大 (32セグメント)
213
+ const splitCount = $strokeSplitCount(start_point.x, start_point.y, control_point.x, control_point.y, end_point.x, end_point.y, thickness, pixel_scale);
214
+ // De Casteljau で t = 0.5 の分割を splitCount 回繰り返す
133
215
  let segments = $segments;
134
216
  let nextSegments = $nextSegments;
135
217
  segments[0] = start_point.x;
@@ -139,7 +221,7 @@ export const calculateCurveRectangle = (start_point, control_point, end_point, t
139
221
  segments[4] = end_point.x;
140
222
  segments[5] = end_point.y;
141
223
  let count = 1;
142
- for (let split = 0; split < SPLIT_COUNT; ++split) {
224
+ for (let split = 0; split < splitCount; ++split) {
143
225
  for (let idx = 0; idx < count; ++idx) {
144
226
  const source = idx * 6;
145
227
  const x0 = segments[source];
@@ -594,6 +676,12 @@ export const generateStrokeOutline = (vertices, thickness) => {
594
676
  const prevPoint = $prevPoint;
595
677
  prevPoint.x = 0;
596
678
  prevPoint.y = 0;
679
+ // 曲線の座標 1 単位が画面上で何ピクセルになるか (曲線の分割数を決めるのに使う)。
680
+ // 描画行列の x 軸 / y 軸方向の拡大率の大きい方。
681
+ const matrix = $context.$matrix;
682
+ const scaleX = Math.sqrt(matrix[0] * matrix[0] + matrix[1] * matrix[1]);
683
+ const scaleY = Math.sqrt(matrix[3] * matrix[3] + matrix[4] * matrix[4]);
684
+ const pixelScale = scaleX > scaleY ? scaleX : scaleY;
597
685
  const rectangles = [];
598
686
  for (let idx = 3; idx < vertices.length; idx += 3) {
599
687
  const x = vertices[idx];
@@ -606,7 +694,7 @@ export const generateStrokeOutline = (vertices, thickness) => {
606
694
  endPoint.x = x;
607
695
  endPoint.y = y;
608
696
  if (vertices[idx - 1]) {
609
- rectangles.push(calculateCurveRectangle(startPoint, controlPoint, endPoint, thickness));
697
+ rectangles.push(calculateCurveRectangle(startPoint, controlPoint, endPoint, thickness, pixelScale));
610
698
  }
611
699
  else {
612
700
  rectangles.push($calculateLineRectangle(startPoint, endPoint, thickness));
@@ -89,9 +89,11 @@ export declare class PathCommand {
89
89
  * @param {number} cy2
90
90
  * @param {number} x
91
91
  * @param {number} y
92
+ * @param {number} [pixel_scale=0] 曲線の座標 1 単位が画面上で何ピクセルになるか。渡すと、近似の誤差から
93
+ * 2次ベジェ曲線の本数を決める。0 以下なら従来の分割。
92
94
  * @return {void}
93
95
  */
94
- bezierCurveTo(cx1: number, cy1: number, cx2: number, cy2: number, x: number, y: number): void;
96
+ bezierCurveTo(cx1: number, cy1: number, cx2: number, cy2: number, x: number, y: number, pixel_scale?: number): void;
95
97
  /**
96
98
  * @description 円弧を描画(WebGL版と同じ実装: 4つの三次ベジェ曲線を使用)
97
99
  * @param {number} x