renderTextureMesh method

  1. @override
void renderTextureMesh(
  1. RenderState renderState,
  2. RenderTexture renderTexture,
  3. Int16List ixList,
  4. Float32List vxList,
)
override

Implementation

@override
void renderTextureMesh(RenderState renderState, RenderTexture renderTexture,
    Int16List ixList, Float32List vxList) {
  final context = _renderingContext;
  final source = renderTexture.source;
  final matrix = renderState.globalMatrix;
  final alpha = renderState.globalAlpha;
  final blendMode = renderState.globalBlendMode;
  final iw = 1.0 / renderTexture.width;
  final ih = 1.0 / renderTexture.height;

  if (_activeAlpha != alpha) {
    _activeAlpha = alpha;
    context.globalAlpha = alpha;
  }

  if (_activeBlendMode != blendMode) {
    _activeBlendMode = blendMode;
    context.globalCompositeOperation = blendMode.compositeOperation;
  }

  context.setTransform(
      matrix.a, matrix.b, matrix.c, matrix.d, matrix.tx, matrix.ty);

  for (var i = 0; i < ixList.length - 2; i += 3) {
    final i1 = ixList[i + 0] << 2;
    final i2 = ixList[i + 1] << 2;
    final i3 = ixList[i + 2] << 2;

    final num x1 = vxList[i1 + 0];
    final num y1 = vxList[i1 + 1];
    final num u1 = vxList[i1 + 2];
    final num v1 = vxList[i1 + 3];

    num x2 = vxList[i2 + 0];
    num y2 = vxList[i2 + 1];
    num u2 = vxList[i2 + 2];
    num v2 = vxList[i2 + 3];

    num x3 = vxList[i3 + 0];
    num y3 = vxList[i3 + 1];
    num u3 = vxList[i3 + 2];
    num v3 = vxList[i3 + 3];

    context.save();
    context.beginPath();
    context.moveTo(x1, y1);
    context.lineTo(x2, y2);
    context.lineTo(x3, y3);
    context.closePath();
    context.clip();

    x2 -= x1;
    y2 -= y1;
    x3 -= x1;
    y3 -= y1;
    u2 -= u1;
    v2 -= v1;
    u3 -= u1;
    v3 -= v1;

    final id = 1.0 / (u2 * v3 - u3 * v2);
    final ma = id * (v3 * x2 - v2 * x3);
    final mb = id * (v3 * y2 - v2 * y3);
    final mc = id * (u2 * x3 - u3 * x2);
    final md = id * (u2 * y3 - u3 * y2);
    final mx = x1 - ma * u1 - mc * v1;
    final my = y1 - mb * u1 - md * v1;

    context.transform(ma * iw, mb * iw, mc * ih, md * ih, mx, my);
    context.drawImage(source!, 0, 0);
    context.restore();
  }
}