renderinghlsllight

How to calculate UV for sampling spot light mask?


I've got this code:

void BlinnPhong(
        inout float3 outputColor,
        PixelMaterial material,
        float3 lightColor,
        float3 lightDir,
        float3 viewDirection,
        float attenuation)
{
    const float diffuseFactor = dotMax(material.normal, lightDir);
    outputColor += lightColor * diffuseFactor * material.baseColor * attenuation;

    const float3 halfwayDirection = normalize(lightDir + viewDirection);
    float specularFactor = pow(dotMax(material.normal, halfwayDirection), material.roughness);
    if (diffuseFactor == 0.0f)
        specularFactor = 0.0f;
    outputColor += lightColor * specularFactor * material.metalness * attenuation;
}

void EvaluateSpotLights(inout float3 outputColor, PixelMaterial material, float3 pos, float3 viewDirection)
{
    for (uint i = 0; i < spotLights.Length; ++i) {
        SpotLight light = spotLights[i];
        
        const float3 posToLight = light.position - pos;
        const float3 lightDir = normalize(posToLight);

        //light.direction is already normalized
        const float theta = dotMax(lightDir, light.direction);
        const float epsilon = (light.innerOuterCutOff.x - light.innerOuterCutOff.y);
        const float intensity = saturate((theta - light.innerOuterCutOff.x) / epsilon);
        
        const float distance = length(posToLight);
        const float attenuation = (1.0f / (light.constnatLinearQuadratic.x +
                  light.constnatLinearQuadratic.y * distance +
                  light.constnatLinearQuadratic.z * (distance * distance))) * intensity;
        
        BlinnPhong(outputColor, material, light.color, lightDir, viewDirection, attenuation);
    }
}

result: enter image description here

I'd like to apply a mask texture to this spot light.

I've got 2 different masks for testing purposes: enter image description here enter image description here

I've added float mask to BlinnPhong function and multiplied by it right after attenuation:

void BlinnPhong(
        inout float3 outputColor,
        PixelMaterial material,
        float3 lightColor,
        float3 lightDir,
        float3 viewDirection,
        float attenuation,
        float mask)
{
    const float diffuseFactor = dotMax(material.normal, lightDir);
    outputColor += lightColor * diffuseFactor * material.baseColor * attenuation * mask;

    const float3 halfwayDirection = normalize(lightDir + viewDirection);
    float specularFactor = pow(dotMax(material.normal, halfwayDirection), material.roughness);
    if (diffuseFactor == 0.0f)
        specularFactor = 0.0f;
    outputColor += lightColor * specularFactor * material.metalness * attenuation * mask;
}

the question is: what UV should I use to get the proper result?

I've tried using the mesh vertex uv:

//pixel shader
float mask = t_spotlightMask.Sample(g_pointWrap, input.mesh_uv).r;

but the result is the same as on the picture above.

I've also tried converting lightDir into uv:

float theta = acos(lightDir.y);
float phi = atan2(lightDir.z, lightDir.x); 

float u = phi / (2 * PI) + 0.5;
float v = theta / PI;

but still, the mask is not applied correctly.

EDIT:

After trying several different approaches I've managed to achieved something like this:

enter image description here

but as you can see something's still off... (marked red)

float maskSize = 512;
float2 scale = 1.0f / float2(maskSize * 0.5f, maskSize * 0.5f);
float2 offset = float2(0.0f, 0.5f);
float2 uv = clipPosition.xy * scale + offset;

float mask = t_spotlightMask.Sample(g_pointWrap, uv).r;

Solution

  • I've looked into Unity's spot light cookie shader code and found out that I have to calculate worldToLightPerspective matrix:

    const glm::mat4& transform = getLightTransform();
    
    const glm::mat4 lightView = glm::inverse(transform);
    const glm::mat4 lightProj = glm::perspective(glm::radians(outerCutoff), 1.0f, 0.1f, 100.0f);
    const glm::mat4 worldToLightPerspective = lightProj * lightView;
    
    float2 ComputeLightCookieUVSpot(float4x4 worldToLightPerspective, float3 samplePositionWS)
    {
        // Translate, rotate and project 'positionWS' into the light clip space.
        float4 positionCS = mul(worldToLightPerspective, float4(samplePositionWS, 1));
        float2 positionNDC = positionCS.xy / positionCS.w;
    
        // Remap NDC to the texture coordinates, from NDC [-1, 1]^2 to [0, 1]^2.
        return saturate(positionNDC * 0.5 + 0.5);
    }
    
    float2 maskUV = ComputeLightCookieUVSpot(light.worldToLightPerspective, worldPosition.xyz);
    float mask = t_spotlightMask.Sample(g_pointWrap, maskUV).r;
    

    Works great now