# Use light source

# Manage light sources in the scene tree

Right-click in the scene tree panel of the developer tools to add a specified type of light source. We regard the directional light with the highest intensity as the main light source, and other light sources are called additional light sources. In the small game framework, the additional light source is implemented by rendering Pass of ForwardAdd by default.

# Customize additional lighting for Effect

If the user wants to use multiple light sources in a customized Effect, then:

  1. Configure the Pass named ForwardAdd in the Effect file, and pay attention to the blending factor in this Pass.
    passes": [
        {
            "lightMode": "ForwardBase",
            ...
        },
        {
            "lightMode": "ForwardAdd",
            "vs": "./LitAdd.vertex.hlsl",
            "ps": "./LitAdd.pixel.hlsl",
            "compileFlags": [...],
            "multiCompile": [...],
            "shaderFeatures": [],
            "skipVariants": [],
            "useMaterialRenderStates": false,
            "renderStates": {
                "blendOn": true,
                "blendSrc": "ONE",
                "blendDst": "ONE",
                "blendFunc": "ADD",
                "cullOn": true,
                "cullFace": "BACK"
            }
        }
    ],

Refer to [Create Effect File] (../resource/effect-material.md) for complete Effect writing rules 2. In the PixelShader of ForwardAdd, get the properties of the light source and perform lighting calculations. We have built-in the data structure of the light source Light in the shader:

struct Light{
    half3 direction;
    half3 color;
    half attenuation;
};
float3 lighting = float3(0, 0, 0);
// MAX_LIGHT_NUM_FA is a built-in macro definition, the maximum number of additional light sources per frame, currently set to 4
for (int i = 0; i <MAX_LIGHT_NUM_FA; i++)
{
    // LIGHT_NUM is a built-in macro definition to get the number of light sources that affect the current object
    if (i <LIGHT_NUM)
    {
        // GetAdditionalLight is a built-in function to get the properties of the light source
        Light light = GetAdditionalLight(i, In.positionWS.xyz);
        // Lighting is a user-defined lighting function
        lighting += Lighting(light,...);
    }
}