|
13 | 13 | * limitations under the License. |
14 | 14 | */ |
15 | 15 |
|
| 16 | +import { drawMeshWithGPU, isWebGPUMeshReady } from "./webgpu_mesh.js"; |
16 | 17 | import { |
17 | 18 | FormatError, |
18 | 19 | info, |
@@ -282,48 +283,48 @@ function drawTriangle(data, context, p1, p2, p3, c1, c2, c3) { |
282 | 283 | const bytes = data.data, |
283 | 284 | rowSize = data.width * 4; |
284 | 285 | let tmp; |
285 | | - if (coords[p1 + 1] > coords[p2 + 1]) { |
| 286 | + if (coords[p1 * 2 + 1] > coords[p2 * 2 + 1]) { |
286 | 287 | tmp = p1; |
287 | 288 | p1 = p2; |
288 | 289 | p2 = tmp; |
289 | 290 | tmp = c1; |
290 | 291 | c1 = c2; |
291 | 292 | c2 = tmp; |
292 | 293 | } |
293 | | - if (coords[p2 + 1] > coords[p3 + 1]) { |
| 294 | + if (coords[p2 * 2 + 1] > coords[p3 * 2 + 1]) { |
294 | 295 | tmp = p2; |
295 | 296 | p2 = p3; |
296 | 297 | p3 = tmp; |
297 | 298 | tmp = c2; |
298 | 299 | c2 = c3; |
299 | 300 | c3 = tmp; |
300 | 301 | } |
301 | | - if (coords[p1 + 1] > coords[p2 + 1]) { |
| 302 | + if (coords[p1 * 2 + 1] > coords[p2 * 2 + 1]) { |
302 | 303 | tmp = p1; |
303 | 304 | p1 = p2; |
304 | 305 | p2 = tmp; |
305 | 306 | tmp = c1; |
306 | 307 | c1 = c2; |
307 | 308 | c2 = tmp; |
308 | 309 | } |
309 | | - const x1 = (coords[p1] + context.offsetX) * context.scaleX; |
310 | | - const y1 = (coords[p1 + 1] + context.offsetY) * context.scaleY; |
311 | | - const x2 = (coords[p2] + context.offsetX) * context.scaleX; |
312 | | - const y2 = (coords[p2 + 1] + context.offsetY) * context.scaleY; |
313 | | - const x3 = (coords[p3] + context.offsetX) * context.scaleX; |
314 | | - const y3 = (coords[p3 + 1] + context.offsetY) * context.scaleY; |
| 310 | + const x1 = (coords[p1 * 2] + context.offsetX) * context.scaleX; |
| 311 | + const y1 = (coords[p1 * 2 + 1] + context.offsetY) * context.scaleY; |
| 312 | + const x2 = (coords[p2 * 2] + context.offsetX) * context.scaleX; |
| 313 | + const y2 = (coords[p2 * 2 + 1] + context.offsetY) * context.scaleY; |
| 314 | + const x3 = (coords[p3 * 2] + context.offsetX) * context.scaleX; |
| 315 | + const y3 = (coords[p3 * 2 + 1] + context.offsetY) * context.scaleY; |
315 | 316 | if (y1 >= y3) { |
316 | 317 | return; |
317 | 318 | } |
318 | | - const c1r = colors[c1], |
319 | | - c1g = colors[c1 + 1], |
320 | | - c1b = colors[c1 + 2]; |
321 | | - const c2r = colors[c2], |
322 | | - c2g = colors[c2 + 1], |
323 | | - c2b = colors[c2 + 2]; |
324 | | - const c3r = colors[c3], |
325 | | - c3g = colors[c3 + 1], |
326 | | - c3b = colors[c3 + 2]; |
| 319 | + const c1r = colors[c1 * 4], |
| 320 | + c1g = colors[c1 * 4 + 1], |
| 321 | + c1b = colors[c1 * 4 + 2]; |
| 322 | + const c2r = colors[c2 * 4], |
| 323 | + c2g = colors[c2 * 4 + 1], |
| 324 | + c2b = colors[c2 * 4 + 2]; |
| 325 | + const c3r = colors[c3 * 4], |
| 326 | + c3g = colors[c3 * 4 + 1], |
| 327 | + c3b = colors[c3 * 4 + 2]; |
327 | 328 |
|
328 | 329 | const minY = Math.round(y1), |
329 | 330 | maxY = Math.round(y3); |
@@ -494,26 +495,39 @@ class MeshShadingPattern extends BaseShadingPattern { |
494 | 495 | paddedWidth, |
495 | 496 | paddedHeight |
496 | 497 | ); |
497 | | - const tmpCtx = tmpCanvas.context; |
498 | 498 |
|
499 | | - const data = tmpCtx.createImageData(width, height); |
500 | | - if (backgroundColor) { |
501 | | - const bytes = data.data; |
502 | | - for (let i = 0, ii = bytes.length; i < ii; i += 4) { |
503 | | - bytes[i] = backgroundColor[0]; |
504 | | - bytes[i + 1] = backgroundColor[1]; |
505 | | - bytes[i + 2] = backgroundColor[2]; |
506 | | - bytes[i + 3] = 255; |
| 499 | + if (isWebGPUMeshReady()) { |
| 500 | + tmpCanvas.context.drawImage( |
| 501 | + drawMeshWithGPU( |
| 502 | + this._figures, |
| 503 | + context, |
| 504 | + backgroundColor, |
| 505 | + paddedWidth, |
| 506 | + paddedHeight, |
| 507 | + BORDER_SIZE |
| 508 | + ), |
| 509 | + 0, |
| 510 | + 0 |
| 511 | + ); |
| 512 | + } else { |
| 513 | + const data = tmpCanvas.context.createImageData(width, height); |
| 514 | + if (backgroundColor) { |
| 515 | + const bytes = data.data; |
| 516 | + for (let i = 0, ii = bytes.length; i < ii; i += 4) { |
| 517 | + bytes[i] = backgroundColor[0]; |
| 518 | + bytes[i + 1] = backgroundColor[1]; |
| 519 | + bytes[i + 2] = backgroundColor[2]; |
| 520 | + bytes[i + 3] = 255; |
| 521 | + } |
507 | 522 | } |
| 523 | + for (const figure of this._figures) { |
| 524 | + drawFigure(data, figure, context); |
| 525 | + } |
| 526 | + tmpCanvas.context.putImageData(data, BORDER_SIZE, BORDER_SIZE); |
508 | 527 | } |
509 | | - for (const figure of this._figures) { |
510 | | - drawFigure(data, figure, context); |
511 | | - } |
512 | | - tmpCtx.putImageData(data, BORDER_SIZE, BORDER_SIZE); |
513 | | - const canvas = tmpCanvas.canvas; |
514 | 528 |
|
515 | 529 | return { |
516 | | - canvas, |
| 530 | + canvas: tmpCanvas.canvas, |
517 | 531 | offsetX: offsetX - BORDER_SIZE * scaleX, |
518 | 532 | offsetY: offsetY - BORDER_SIZE * scaleY, |
519 | 533 | scaleX, |
|
0 commit comments