applyToCanvas method

void applyToCanvas(
  1. Canvas canvas,
  2. Size viewportSize, {
  3. bool isLeftEye = true,
})

Draws the distortion mesh on a canvas, warping the source viewport. Call this AFTER rendering the scene to apply lens correction.

Implementation

void applyToCanvas(
  Canvas canvas,
  Size viewportSize, {
  bool isLeftEye = true,
}) {
  final w = viewportSize.width;
  final h = viewportSize.height;

  // Draw the warped mesh as triangle strips
  for (var y = 0; y < resolution; y++) {
    for (var x = 0; x < resolution; x++) {
      final i00 = y * (resolution + 1) + x;
      final i10 = i00 + 1;
      final i01 = i00 + (resolution + 1);
      final i11 = i01 + 1;

      final dst00 = _toScreen(distortedPoints[i00], w, h);
      final dst10 = _toScreen(distortedPoints[i10], w, h);
      final dst01 = _toScreen(distortedPoints[i01], w, h);
      final dst11 = _toScreen(distortedPoints[i11], w, h);

      // Check if distorted point is within viewport
      if (_isOutOfBounds(dst00, w, h) &&
          _isOutOfBounds(dst10, w, h) &&
          _isOutOfBounds(dst01, w, h) &&
          _isOutOfBounds(dst11, w, h)) {
        continue;
      }

      // Draw the warped quad as wireframe (for debug) or filled mesh
      final path = Path()
        ..moveTo(dst00.dx, dst00.dy)
        ..lineTo(dst10.dx, dst10.dy)
        ..lineTo(dst11.dx, dst11.dy)
        ..lineTo(dst01.dx, dst01.dy)
        ..close();

      // In a real GPU pipeline, this would be a textured mesh.
      // On Canvas, we show the distortion grid for visualization.
      canvas.drawPath(
        path,
        Paint()
          ..color = const Color(0x15FFFFFF)
          ..style = PaintingStyle.stroke
          ..strokeWidth = 0.5,
      );
    }
  }
}