Simple 2D lighting effects

Started by
10 comments, last by cakefrost 9 years, 7 months ago

Glass_Knife: thank you for the link, I will check it out. The lights will not necessarily stay in the same spot (the player might be able to place torches on the ground, or start a fire, I am not entirely sure but I dont want to limit myself). I also am looking to do much simpler lighting, mostly just rendering certain pixels with less color if it is supposed to be darker.

SyncViews: Awesome, this is something I can look into, thanks. From my original research, I remember reading that there is no multiplication blend (step 7), so this sounds like something I'll use the shader for. However, this is basically back to my original problem of not knowing what I need to search. I tried searching for passing textures/buffers to pixel shaders but I am getting a variety of responses, none of which have proven helpful to me. Is there a particular term that this process is called that I can look into?

What do you mean by passing textures/buffers to the pixel shader? I'm sure your already doing that with your normal rendering, and if I recall correctly even pretty old hardware can support somthing like 8 input textures. For D3D9 I believe you can just declare 2 (or more) samplers in your HLSL pixels hader file, and all the relevant functions take the sampler index like IDirect3DDevice9::SetTexture(DWORD sampler, IDirect3DBaseTexture9*).

Then working on my previous suggestion, I think you can do it pretty much like a simple non-cached lightmap:

  1. Set an RGB texture the same size as your screen as the render target
  2. Clear the texture/render target to 0/black
  3. Set the blending mode to additive (dest = dest + source, rather than say your "dest = dest * (1-alpha) + source* alpha" for alpha blending)
  4. Render your light gradients (they can be coloured, because "a + b == b + a" order does not matter, overlapping areas just get brighter till you saturate the channel at full brightness)
  5. Put your backbuffer back as the render target
  6. Set that RGB lighting texture as the second texture
  7. Render your scene as without lighting, but in the pixel shader, sample that second texture using the screen space coordinate, then multiply it by your unlit colour to get the lit colour
    
    //HLSL pixel shader like pseudo code as not on development system
    sampler texture;//The texture for the sprite your rendering
    sampler lighting;//The lighting texture from 2-4
    
    struct Input
    {
        float2 texcoord : TEXCOORD;
        //anything else you have
        float2 screenPos : VPOS;//provided by D3D9, is the location on the screen/render target. I am assuming its in 0-1 space, correctly orientated etc. You will need to check this.
    };
    
    float4 main(Input input) : COLOR
    {
        //do what you normally would
        float4 col = texture.sample(input.texcoord);
    
        //lighting
        float4 light = texture.sample(screenPos);
        return col * light;
    }
    
    
Advertisement

BarrySkellern: Very cool video! I cant wait to see what you continue to do with it.

SyncViews: Thanks a bunch, you have given me a lot of help. I really appreciate it!

This topic is closed to new replies.

Advertisement