setupView method

void setupView(
  1. List<Light> lights,
  2. Camera camera
)

Implementation

void setupView(List<Light> lights, Camera camera) {
  int directionalLength = 0;
  int pointLength = 0;
  int spotLength = 0;
  int rectAreaLength = 0;
  int hemiLength = 0;

  final viewMatrix = camera.matrixWorldInverse;

  for (int i = 0, l = lights.length; i < l; i++) {
    final light = lights[i];

    if (light is DirectionalLight) {
      final uniforms = state.directional[directionalLength];

      uniforms["direction"].setFromMatrixPosition(light.matrixWorld);
      vector3.setFromMatrixPosition(light.target!.matrixWorld);
      uniforms["direction"].sub(vector3);
      uniforms["direction"].transformDirection(viewMatrix);

      directionalLength++;
    }
    else if (light is SpotLight) {
      final uniforms = state.spot[spotLength];

      uniforms["position"].setFromMatrixPosition(light.matrixWorld);
      uniforms["position"].applyMatrix4(viewMatrix);

      uniforms["direction"].setFromMatrixPosition(light.matrixWorld);
      vector3.setFromMatrixPosition(light.target!.matrixWorld);
      uniforms["direction"].sub(vector3);
      uniforms["direction"].transformDirection(viewMatrix);

      spotLength++;
    }
    else if (light is RectAreaLight) {
      final uniforms = state.rectArea[rectAreaLength];

      uniforms["position"].setFromMatrixPosition(light.matrixWorld);
      uniforms["position"].applyMatrix4(viewMatrix);

      // extract local rotation of light to derive width/height half vectors
      matrix42.setFrom(Matrix4.identity());
      matrix4.setFrom(light.matrixWorld);
      matrix4.multiply(viewMatrix);
      matrix42.extractRotation(matrix4);

      uniforms["halfWidth"].setValues(light.width! * 0.5, 0.0, 0.0);
      uniforms["halfHeight"].setValues(0.0, light.height! * 0.5, 0.0);

      uniforms["halfWidth"].applyMatrix4(matrix42);
      uniforms["halfHeight"].applyMatrix4(matrix42);

      rectAreaLength++;
    }
    else if (light is PointLight) {
      final uniforms = state.point[pointLength];

      (uniforms["position"] as Vector3).setFromMatrixPosition(light.matrixWorld);
      uniforms["position"].applyMatrix4(viewMatrix);

      pointLength++;
    }
    else if (light is HemisphereLight) {
      final uniforms = state.hemi[hemiLength];

      uniforms["direction"].setFromMatrixPosition(light.matrixWorld);
      uniforms["direction"].transformDirection(viewMatrix);

      hemiLength++;
    }
  }
}