Need help with this lighting shader

Hi, I’m totally new to shaders and I’m reading several shader tutorials on the Internet. I want to implement a 2D lighting system in my game, and I’ve found this link: (It’s written in English.)

I just copied the code exactly the same from the site but I get error when I try to compile it.
Here is the entire code:

lighteffect.fx

sampler s0;

texture lightMask;
sampler lightSampler = sampler_state { Texture = lightMask; };

float4 PixelShaderLight(float2 coords: TEXCOORD0) : COLOR0
{
    float4 color = tex2D(s0, coords);
    float4 lightColor = tex2D(lightSampler, coords);
    return color * lightColor;
}

technique Technique1
{
    pass Pass1
    {
        PixelShader = compile ps_2_0 PixelShaderLight();
    }
}

Visual Studio doesn’t show me which line invokes the error, it just tells me that I got MSB3073 error. Is there anything wrong with this code?

Anything in your build output in VS?

Hi Arcadenut, I tried to build the effect file in MGCB editor. Still not working, and I got this:

EDIT: This is assuming that the .FX file has the same syntax as C#.

It’s complaining about Line 4.

sampler lightSampler = sampler_state { Texture = lightMask; };

The { Texture = lightMask; } is a class initializer. If sampler is a class then you need to do something like:

sampler lightSampler =  new sampler() { Texture = lightMask; };

If it’s not a class and it’s a type then you need to have something like:

sampler lightSampler = sampler_state;

Either that or you’re missing a ;

sampler lightSampler = sampler_state;
Texture = lightMask;

Depends all on what sampler is and what you intended to do with 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

sampler s0;

texture lightMask;

sampler lightSampler = sampler_state
{
Texture = < lightMask >;
};

float4 PixelShaderLight(float2 coords: TEXCOORD0) : COLOR0
{
float4 color = tex2D(s0, coords);
float4 lightColor = tex2D(lightSampler, coords);
return color * lightColor;
}

technique Technique1
{
pass P0
{
PixelShader = compile PS_SHADERMODEL PixelShaderLight();
}
}

I am new with shaders me too but you can try this … build work with monogame 3.8

Thank you all for the suggestions. I think I need to search for HLSL syntax…
@mathmb1986 Actually that worked, thanks! I found out this works too:

sampler lightSampler = sampler_state { Texture = (lightMask); };
///////////////////////////////////////////////////////////////////////////////////////
// Textures
///////////////////////////////////////////////////////////////////////////////////////
texture colourMap;

///////////////////////////////////////////////////////////////////////////////////////
// Samplers
///////////////////////////////////////////////////////////////////////////////////////
 
sampler colourSampler = sampler_state
 {
	Texture = (colourMap);
	AddressU = CLAMP;
	AddressV = CLAMP;
	MagFilter = LINEAR;
	MinFilter = LINEAR;
	Mipfilter = LINEAR;
};

You should always realise that anything below shadermodel 4 is not supported
A lot of people think it is an optomisation to only use the lowest shadermodel you need, it’s not