Have solid pseudo code, need concrete shader help.

The GOAL shader takes a normal_map (texture), a float for surface height (optional), and a vector3 light_position, and draws pure blackness on each pixel, with an alpha value determined by the angle to the light, and the pixels normal…

THATS ALL… The rest is handled by game using render-targets and blend states…

I need a shader that takes a normal-map, and a position of the light relative to the texture,
and returns an alpha value…

Does any one have this on hand, or can you help me make it? Seems like it would be simple, if you knew the right key-words.

SO I pasted this post into chat GPT, it gave me this:

> Texture2D<float4> normalMapTexture : register(t0); // Input normal map texture
> float3 lightPosition : register(c0); // Light position in texture space
> 
> SamplerState sampler : register(s0); // Sampler for the texture
> 
> struct PixelInput
> {
>     float2 TexCoord : TEXCOORD0;
> };
> 
> float4 main(PixelInput input) : SV_Target
> {
>     // Sample the normal map
>     float4 normalSample = normalMapTexture.Sample(sampler, input.TexCoord);
>     
>     // Convert the RGB values from [0, 1] to [-1, 1] for normal calculations
>     float3 normal = 2.0f * normalSample.rgb - 1.0f;
>     
>     // Calculate the vector from the pixel to the light source
>     float3 lightDir = lightPosition - float3(input.TexCoord, 0);
>     
>     // Normalize the light direction and the normal
>     lightDir = normalize(lightDir);
>     normal = normalize(normal);
>     
>     // Calculate the dot product of the light direction and the normal
>     float lightDot = dot(lightDir, normal);
>     
>     // Clamp the result to ensure it's in the range [0, 1]
>     lightDot = saturate(lightDot);
>     
>     // Use the dot product as the alpha value
>     return float4(0, 0, 0, lightDot);
> }

It looks real at first glance, can anyone share an opinion?

Wouldn’t compile / run… So I went over it in ChatGPT a few iterations… Finally got THIS to compile and load in the game.

#if OPENGL
#define SV_POSITION POSITION
#define VS_SHADERMODEL vs_3_0
#define PS_SHADERMODEL ps_3_0
#else
#define VS_SHADERMODEL vs_4_0_level_9_1
#define PS_SHADERMODEL ps_4_0_level_9_1
#endif
// Shader Parameters
texture normalMapTexture : register(t0); // Input normal map texture
sampler2D _sampler : register(s0); // Sampler for the texture

float3 lightPosition : register(c0); // Light position in texture space

// Pixel shader input structure
struct PixelInput
{
float2 TexCoord : TEXCOORD0;
};

// Pixel shader output structure
struct PixelOutput
{
float4 Color : SV_TARGET;
};

// Pixel shader
PixelOutput pixel_main(PixelInput input)
{
PixelOutput output;

// Sample the normal map
float4 normalSample = tex2D(_sampler, input.TexCoord);

// Convert the RGB values from [0, 1] to [-1, 1] for normal calculations
float3 normal = 2.0f * normalSample.rgb - 1.0f;

// Calculate the vector from the pixel to the light source
float3 lightDir = lightPosition - float3(input.TexCoord, 0);

// Normalize the light direction and the normal
lightDir = normalize(lightDir);
normal = normalize(normal);

// Calculate the dot product of the light direction and the normal
float lightDot = dot(lightDir, normal);

// Clamp the result to ensure it's in the range [0, 1]
lightDot = saturate(lightDot);

// Use the dot product as the alpha value
output.Color = float4(0, 0, 0, lightDot);

return output;

}

// Technique and Pass
technique LightingTechnique
{
pass LightingPass
{
PixelShader = compile ps_3_0 pixel_main();
}
}

CHAT GPT did it for me…
Here is a shader you can use to calculate shading level for a pixel, relative to a lights position.

-I had to wiggle it a little to make it run, but now it does… Feed it the position of the light in texture-space, and that’s it.

#if OPENGL
#define SV_POSITION POSITION
#define VS_SHADERMODEL vs_3_0
#define PS_SHADERMODEL ps_3_0
#else
#define VS_SHADERMODEL vs_4_0_level_9_1
#define PS_SHADERMODEL ps_4_0_level_9_1
#endif
// Shader Parameters
// Shader Parameters
// Shader Parameters
// Shader Parameters
texture normalMapTexture : register(t0); // Input normal map texture
sampler2D _sampler : register(s0); // Sampler for the texture

float3 lightPosition : register(c0); // Light position in texture space

// Pixel shader input structure
struct PixelInput
{
float2 TexCoord : TEXCOORD0;
};

// Pixel shader output structure
struct PixelOutput
{
float4 Color : SV_TARGET;
};

// Pixel shader
PixelOutput pixel_main(PixelInput input)
{
PixelOutput output;

// Sample the normal map
float4 normalSample = tex2D(_sampler, input.TexCoord);

// Convert the RGB values from [0, 1] to [-1, 1] for normal calculations
float3 normal = 2.0f * normalSample.rgb - 1.0f;

// Calculate the vector from the pixel to the light source
float3 lightDir = lightPosition - float3(input.TexCoord, 0);

// Normalize the light direction and the normal
lightDir = normalize(lightDir);
normal = normalize(normal);

// Calculate the dot product of the light direction and the normal
float lightDot = dot(lightDir, normal);

// Clamp the result to ensure it's in the range [0, 1]
lightDot = saturate(lightDot);

// Use the dot product as the alpha value
output.Color = float4(0, 0, 0, lightDot);

return output;

}

// Technique and Pass
technique LightingTechnique
{
pass LightingPass
{
PixelShader = compile ps_3_0 pixel_main();
}
}