calculateLight method

void calculateLight(
  1. List<Light> lights,
  2. Vector3 position,
  3. Vector3 normal,
  4. Color color,
)

Implementation

void calculateLight(List<Light> lights, Vector3 position, Vector3 normal, Color color) {
  for (int l = 0; l < lights.length; l++) {
    Light light = lights[l];
    Color lightColor = light.color ?? Color();

    if (light is DirectionalLight) {
      Vector3 lightPosition = _vector3.setFromMatrixPosition(light.matrixWorld).normalize();

      num amount = normal.dot(lightPosition);
      if (amount <= 0) continue;
      amount *= light.intensity;
      color.r += lightColor.r * amount;
      color.g += lightColor.g * amount;
      color.b += lightColor.b * amount;
    } else if (light is PointLight) {
      Vector3 lightPosition = _vector3.setFromMatrixPosition(light.matrixWorld);

      num amount = normal.dot(_vector3.subVectors(lightPosition, position).normalize());
      if (amount <= 0) continue;
      amount *= light.distance == 0 ? 1 : 1 - Math.min(position.distanceTo(lightPosition) / light.distance!, 1);
      if (amount == 0) continue;
      amount *= light.intensity;
      color.r += lightColor.r * amount;
      color.g += lightColor.g * amount;
      color.b += lightColor.b * amount;
    }
  }
}