render method

void render(
  1. Canvas canvas,
  2. Size size
)

Implementation

void render(Canvas canvas, Size size){
  List<RotateRect> selected = [];
  super.scene.render(canvas, size);
  // create render mesh from objects
  final renderMesh = _makeRenderMesh(scene.levelInfo[scene.selectedLevel].objects,scene.allObjectImage);
  final List<Triangle> renderPolys = _renderObject(renderMesh,scene.levelInfo[scene.selectedLevel].objects);
  final int indexCount = renderPolys.length;
  final Uint16List indices = Uint16List(indexCount * 3);

  // renderPolys.sort((Triangle a, Triangle b){
  //   return _paintersAlgorithm(a,b);
  // });

  for (int i = 0; i < indexCount; i++) {
    //if(renderPolys[i] != null){
      final int index0 = i * 3;
      final int index1 = index0 + 1;
      final int index2 = index0 + 2;
      final Triangle triangle = renderPolys[i];

      indices[index0] = triangle.vertexes[0];
      indices[index1] = triangle.vertexes[1];
      indices[index2] = triangle.vertexes[2];
    //}

    if(i < scene.objectTappedOn.length){
      int sel = scene.objectTappedOn[i].objectLocation;
      final Object obj = scene.levelInfo[scene.selectedLevel].objects[sel];
      final Vector3 newPosition = Vector3.copy(obj.position)+Vector3((obj.scale.x < 0?0.04:-0.04),(obj.scale.y > 0?0.04:-0.04),0);
      newPosition.applyMatrix4(scene.camera.lookAtMatrix);

      selected.add(
        RotateRect(
          angle: obj.rotation.z,
          off: (obj.scale.x < 0?-8/2:8/2)*scene.camera.zoom,
          rect: Rect.fromLTWH(
            newPosition.x+newPosition.x*(scene.camera.zoom-1),
            newPosition.y+newPosition.y*(scene.camera.zoom-1),
            (obj.size.width*obj.scale.x*100+(obj.scale.x < 0?-8:8))*scene.camera.zoom,
            (obj.size.height*obj.scale.y*100+(obj.scale.y > 0?8:-8))*scene.camera.zoom
          )
        )
      );
    }
  }

  final Rect totalRect = Rect.fromLTWH(0, 0, scene.camera.viewportWidth, scene.camera.viewportHeight);
  canvas.saveLayer(totalRect,Paint());

  _drwaVert(
    canvas,
    renderMesh.positions,
    renderMesh.texcoords,
    renderMesh.colors,
    indices,
    renderMesh.texture
  );

  if(selected.isNotEmpty){
    final paint = Paint()
    ..style = PaintingStyle.stroke
    ..color = Colors.blue
    ..strokeWidth = 2
    ..blendMode = BlendMode.srcOver;

    for(int i = 0; i < selected.length;i++){
      canvas.save();
      rotate(
        canvas,
        selected[i].cx,
        selected[i].cy,
        selected[i].angle
      );
      canvas.drawRect(selected[i].rect, paint);
      canvas.restore();
    }

  }
  canvas.restore();

  scene.tapLocation = null;
}