2D Light Shader


I’m starting to learn shaders and wanted to make a 2D-Light shader.
This is what I have right now:

    #define VS_SHADERMODEL vs_3_0
    #define PS_SHADERMODEL ps_3_0
    #define VS_SHADERMODEL vs_4_0_level_9_1
    #define PS_SHADERMODEL ps_4_0_level_9_1

sampler s0;
float4 LightColor;
float2 LightPosition;
float LightRadius;

struct PixelInput
    float2 pos : SV_POSITION0;
    float2 uv : TEXCOORD0;

float4 MainPS(PixelInput input) : COLOR0
    float dist = pow(input.pos.x - LightPosition.x, 2) + pow(input.pos.y - LightPosition.y, 2);
    if (dist < pow(LightRadius,2))
        return tex2D(s0, input.uv) * LightColor;
        return float4(0, 0, 0, 1);

technique DrawLight
    pass P0
        PixelShader = compile PS_SHADERMODEL MainPS();

I set LightPosition to MousePosition.

This is working pretty well, except that input.pos.Y seems to be flipped (0 being bottom). Why is that and how do I fix it?

And also all tutorials I’ve seen always use “SV_POSITION” or “POSITION”. That will throw an error for me and is only fixed by using “SV_POSITION0”. What’s the difference?


After you set the mouse to the lightposition on the next line below it.
Add this…

LightPosition.Y = GraphicsDevice.Viewport.Height - LightPosition.Y;

Before you pass in LightPosition to the shader.

Well that works of course. But I’d like a more general solution.
What’s the standard way of handling this? Passing a matrix?

Sorry i don’t understand the question.

Do you mean the general solution to fliping the y coordinate ?
(You shouldn’t have to but im not really sure there isn’t much code to look at there.)
Or something else.

Well, I guess the real question was:
Why is it flipped?
And how can I convert input.Position inside the shader so it uses top-left as origin instead of bottom-left.

The origins of the coordinates (mouse screen VS texture uv) are not top left for both, one is at the bottom.

This is a ‘pixel shader’ only, you don’t manipulate the vertex part so sv_ is required if i remember well.