drawGrid method

dynamic drawGrid()

Draw a grid and add it to the scene

Implementation

drawGrid() {
  if (scene != null) {
    var color = 0x888888;
    var multiplier = 3;
    var size, divisions, maxLength, tick;

    if (camera is OrthographicCamera) {
      var width = camera.right - camera.left;
      var height = camera.bottom - camera.top;

      maxLength = Math.max(width, height);
      tick = maxLength / 20;

      size = maxLength / camera.zoom * multiplier;
      divisions = size / tick * camera.zoom;
    } else if (camera is PerspectiveCamera) {
      var distance = camera.position.distanceTo(_gizmos.position);
      var halfFovV = MathUtils.deg2rad * camera.fov * 0.5;
      var halfFovH = Math.atan((camera.aspect) * Math.tan(halfFovV));

      maxLength = Math.tan(Math.max(halfFovV, halfFovH)) * distance * 2;
      tick = maxLength / 20;

      size = maxLength * multiplier;
      divisions = size / tick;
    }

    if (_grid == null) {
      _grid = GridHelper(size, divisions, color, color);
      _grid.position.copy(_gizmos.position);
      _gridPosition.copy(_grid.position);
      _grid.quaternion.copy(camera.quaternion);
      _grid.rotateX(Math.pi * 0.5);

      scene!.add(_grid);
    }
  }
}