# Alpha is not working in diffuse light shader

I am following this tutorials series on rastertek.com and I got a bit stuck on the Diffuse Lighting Tutorial. Particulary, the part that does not work for me is alpha channel of the light color. Here are the [I think they are] relevant code parts:

cbuffer MatrixBuffer
{
matrix worldMatrix;
matrix viewMatrix;
matrix projectionMatrix;
};

SamplerState SampleType;

cbuffer LightBuffer
{
float4 ambientColor;
float4 diffuseColor;
float3 lightDirection;
};

struct PixelInputType
{
float4 position : SV_POSITION;
float2 tex : TEXCOORD0;
float3 normal : NORMAL;
};

{
float4 textureColor;
float3 lightDir;
float lightIntensity;
float4 color;

lightDir = -lightDirection;

lightIntensity = saturate(dot(input.normal, lightDir));

color = saturate(diffuseColor * lightIntensity);

color = color * textureColor;

return color;
}

{
PixelInputType output;

input.position.w = 1.0f;

output.position = mul(input.position, worldMatrix);
output.position = mul(output.position, viewMatrix);
output.position = mul(output.position, projectionMatrix);

output.tex = input.tex;

output.normal = mul(input.normal, (float3x3)worldMatrix);

output.normal = normalize(output.normal);

return output;
}


The problem here is the shader completely ignores the alpha channel of the light. If I set the diffuse color to red, then the texture will be reddish no matter what alpha is supplied.

The only way I could make the alpha work is changing the line

color = color * textureColor;


to

color = color * color.w * textureColor;


in the pixel shader. This however gives overall wrong result.

Might it be the DirectX setup issue, here are some parts of the device initialization:

// Set regular 32-bit surface for the back buffer.
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;

[...]

// Set up the description of the depth buffer.
depthBufferDesc.Width = screenWidth;
depthBufferDesc.Height = screenHeight;
depthBufferDesc.MipLevels = 1;
depthBufferDesc.ArraySize = 1;
depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthBufferDesc.SampleDesc.Count = 1;
depthBufferDesc.SampleDesc.Quality = swapChainDesc.SampleDesc.Quality;
depthBufferDesc.Usage = D3D11_USAGE_DEFAULT;
depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
depthBufferDesc.CPUAccessFlags = 0;
depthBufferDesc.MiscFlags = 0;

[...]

// Setup the raster description which will determine how and what polygons will be drawn.
rasterDesc.AntialiasedLineEnable = false;
rasterDesc.CullMode = D3D11_CULL_BACK;
rasterDesc.DepthBias = 0;
rasterDesc.DepthBiasClamp = 0.0f;
rasterDesc.DepthClipEnable = true;
rasterDesc.FillMode = D3D11_FILL_SOLID;
rasterDesc.FrontCounterClockwise = false;
rasterDesc.MultisampleEnable = false;
rasterDesc.ScissorEnable = false;
rasterDesc.SlopeScaledDepthBias = 0.0f;


• @DMGregory to affect the light intensity, i.e. if I set my diffuse light to (1.0, 1.0, 1.0, 0.2), the textured model will only have 20% lightness as if the light source was weak. But my understand of diffuse light might be wrong... – lolbas Feb 12 at 11:09
• @DMGregory but isn't lightIntensity is for intensity across the polygon, not for the intensity of the light source? Or do you mean I can multiply it bydiffuseColor.w to get the result I describe? I actually haven't tried this way and couldn't test it soon but it sounds.. reasonable? – lolbas Feb 12 at 11:28