Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I wrote a simple light shader that I tested in a simple 2d spaceship shooter and it worked fine. Once I imported it into my friend's farseer project with rotatable shapes, it doesn't seem to work well with the shapes moving around and rotating. Could someone help me fix it and maybe plugging in good values to get it to work?

code sample:

/*LIGHT DIRECTION VARIABLES*/
float2 lightposition;
float2 textureposition;
float textureheight;
float texturewidth;
float lightwidth;
float lightheight;
float2 screensize;
float2 lightsize;
float rotationrads; /*assumed rotation is in radians*/
float lightgradient; /*a number from 0 to N for how intense the shift in light per pixel from source is */
float texgradient; /*a number from 0 to N for how intense the shift in light per pixel from 0,0 in the texture is */
float lightintensity; /*0-1 for 100% rgb, over 1 for modified whiteness*/


float4 LightDirection(float2 Tex: TEXCOORD0) : COLOR0
{
    float4 Color = tex2D(TextureSampler, Tex);

    float2 abszero;
    abszero.x = 0;
    abszero.y = 0;
    float sizescreen;
    float2 finaldistance;
    float2 percentdistance;
    float length;


    sizescreen = abs(distance(abszero, screensize));

        float2 NewTex;
        NewTex.x = Tex.x * cos(rotationrads) - Tex.y * sin(rotationrads);
        NewTex.y = Tex.x * sin(rotationrads) + Tex.y * cos(rotationrads);
        if(textureposition.x >= lightposition.x && textureposition.x <= lightposition.x + lightwidth)
        {
            finaldistance.x = 0;
        }
        else if(lightposition.x + lightwidth > NewTex.x)
        {
            finaldistance.x = textureposition.x - (texturewidth * ((1.0 - NewTex.x) * texgradient));
        }
        else
        {
            finaldistance.x = textureposition.x + (texturewidth * (NewTex.x * texgradient));
        }


        if(textureposition.y >= lightposition.x && textureposition.y <= lightposition.y + lightheight)
        {
            finaldistance.y = 0;
        }
        else if(lightposition.y + lightheight > textureposition.y)
        {
            finaldistance.y = textureposition.y - (textureheight * ((1.0 -NewTex.y) * texgradient));
        }
        else
        {
            finaldistance.y = textureposition.y + (textureheight * (NewTex.y * texgradient));
        }

    length = abs(distance(lightposition, finaldistance));
    length = (length/sizescreen); 

    Color.r = (Color.r * lightintensity) -(Color.r * (length * lightgradient));
    Color.g = (Color.g * lightintensity)  - (Color.g * (length * lightgradient));
    Color.b = (Color.b * lightintensity)  - (Color.b * (length * lightgradient));


    return Color;
        }
        technique LightOnly
{
    pass Pass1
    {
        PixelShader = compile ps_2_0 LightDirection();
    }
}
share|improve this question

closed as too localized by Byte56, Sean Middleditch, bummzack, ClassicThunder, Trevor Powell Mar 15 '13 at 14:37

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Do you set sampler TextureSampler somewhere? There are some cases where XNA does that small detail for you, but perhaps Farseer does not. Also, you should try to provide a better description of the error that is happening. Surely your new project does not report that it is "falling apart" when it errors out. –  Seth Battin Mar 13 '13 at 1:40
    
the problem is I am plugging in a shader into someone else's game. I'll try to get their xna code and add it to this question. Essentially, in XNA itself when I move the 2d spaceship sprite towards and away from the light, the light intensity works as it should, with the sprite getting lighter as it gets closer, and then the light within the sprite fading into slight darkness the further it is from the light. –  OthelloNYC Mar 14 '13 at 14:21
    
When it gets plugged into the game with farseer, the shading doesn't seem as stable. I can't tell if the issue is my rotation algorithm, or that my distance algorithm can't deal with negative coordinate values, but occasionally as I move a sprite which also rotates due to the physics engine at work, the sprite goes ridiculously dark or light compared to it's previous position, even if it only moved a few pixels over. –  OthelloNYC Mar 14 '13 at 14:45
add comment

1 Answer 1

up vote 0 down vote accepted

A bit of guessing here because I have no clue what a shader "falling apart" means. Most likely Farseer altered the renderstate at some point.

See Shawn Hargreaves Blog for tons of information about render states.

But you probably want to add the before before your drawing code. This should reset everything back to the XNA defaults.

GraphicsDevice.BlendState = BlendState.Opaque;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
share|improve this answer
    
Sorry, what I meant is the still image is shaded fine across the axis from the light source, but the second it rotates, the shading looks as if it's moved far from or right into the light source. I don;'t know if my rotation algorithm is at fault, or the fact that farseer defaults to 0,0 being the center of the screen and measures screen distance in very small units. Generally i was looking for anyone with experience with farseer and shading, as every 2d shader algorithm I can find works in XNA but not farseer. –  OthelloNYC Mar 14 '13 at 2:28
    
"measures screen distance in very small units" Farseer does not measure distances in pixels it measures them in meters. Find out how many pixels are in a meter and multiply the numbers you are using by that value and it may very well fix your issue. –  ClassicThunder Mar 15 '13 at 3:40
    
There's a conversion function I am looking into they have in their demo function. It's just hard to seperate what is just me not using good weight values, and what is a problem with my algorithm, especially the rotation algorithm. –  OthelloNYC Mar 15 '13 at 13:42
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.