calculateLight method
void
calculateLight(
- List<Light> lights,
- Vector3 position,
- Vector3 normal,
- 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.red += lightColor.red * amount;
color.green += lightColor.green * amount;
color.blue += lightColor.blue * amount;
}
else if (light is PointLight){
Vector3 lightPosition = _vector3.setFromMatrixPosition( light.matrixWorld );
num amount = normal.dot( _vector3.sub2( 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.red += lightColor.red * amount;
color.green += lightColor.green * amount;
color.blue += lightColor.blue * amount;
}
}
}