setupView method

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

Implementation

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

  var viewMatrix = camera.matrixWorldInverse;

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

    if (light.type == "DirectionalLight") {
      var 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.type == "SpotLight") {
      var 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.type == "RectAreaLight") {
      var 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.identity();
      matrix4.copy(light.matrixWorld);
      matrix4.premultiply(viewMatrix);
      matrix42.extractRotation(matrix4);

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

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

      rectAreaLength++;
    } else if (light.type == "PointLight") {
      var uniforms = state.point[pointLength];

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

      pointLength++;
    } else if (light.type == "HemisphereLight") {
      var uniforms = state.hemi[hemiLength];

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

      hemiLength++;
    }
  }
}