Best approach to create lights with parallax shift

Jan 23, 2012 at 10:25 AM

Hello community,

I recently started using krypton for my project. It's a great and quite straight forward engine to use. The only troubles I get with parallax effects.

To draw the parallax shifted layers I store a corresponding viewing matrix within a list. This is my current draw function:

public void draw(SpriteBatch sb, GameTime gameTime)
{
    Camera playerCamera = PlayerOwner.PlayerCamera;

     krypton.Matrix = playerCamera.Views[0];
     krypton.LightMapPrepare();

     GameInfo.graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

     foreach (Layer layer in Layers)
     {
           sb.Begin(SpriteSortMode.Deferred, null, null, null, null, null, playerCamera.Views[layer.ViewID]);
           layer.draw(sb, gameTime);
           sb.End();
     }
     //Draw lights
     krypton.Draw(gameTime);
}

Currently I'm only drawing lights within the (1,1) layer which is in my case always the viewing matrix with index 0.

What would be the best approach to get in parallax shifted lights? The problem is I don't know how to directly control which lights are drawn when. Is it best to create several instances of krypton the render the lights for each layer (I got that feeling that's not a good idea) or should I fill the Lights list of krypton with the corresponding lights of the layer and then start with pre-calculations of the light map and the actual drawing. Or is there some parallax functionaility I just haven't seen yet.

If someone could point me in the right direction or show me an example code snipped, that would be great.

 

Coordinator
Jan 24, 2012 at 8:25 PM

This is an excellent question.

Actually, in my mind, the problem is larger than what you've stated here.

For instance, how should the layers of shadows be combined?

Imagine a scenario where your player is exploring a castle (Super Mario 3 style), and comes accross a portion of the castle which has a hole in the wall. The lights inside the castle create shadows against a ledge, and part of that shadow falls on top of the hole in the wall. Obviously, the shadow should not be drawn on that portion of the castle, as there is light on the outside of the castle which illuminates the hole. Now, image having multiple paralax layers of the castle, each with different lights and hulls.

That may be a bit more complicated that what you're going for, but the problem is still present.

Here's a solution:

1) pre-render the shadows for each layer of the scene onto a single lightmap, sectioned off into different viewports (assuming 4 layers, you could use 4 quadrants of the lightmap texture - one for each layer).

2) Draw each layer, first clearing the alpha channel, drawing the layer, and then multiplying that layer's portion of the lightmap across the whole scene, but doing so only to the amount that is indicated in the alpha channel for each pixel (ie, draw the shadows only where that layer had been drawn also).

By no means does Krypton directly support this. The Future's portion of the project (available in the latest source) does give more control that will allow for this kind of thing, but there definitely a lot of working going into it.

Finally:

I may have misunderstood the question. But, I took the liberty of taking notes about how I would implement this kind of functionality, so that in the future I will have them.

Coordinator
Jan 24, 2012 at 8:26 PM

Can you provide a visual example of what you're going for?

Feb 2, 2012 at 3:59 PM

Hello,

first thanks for your hints. Sry that I reply that late, but some other issues called my attention last week.

The example you described hit's exaclty the problem I was speaking about. At the moment I have just simplyfied it to just draw a light at the correct shifted parallax position in a different layer. As soon as my working environment is fully set up I will provide you with a visual example.

Feb 6, 2012 at 5:28 PM

Ok I made up a little picture to illustrate what I'm going for. So the red light is in the fron with parallax (1, 1) and the red light is shifted by a parallax of let's say (0.5, 0.5). So for now I just want the red form to be moved according to the parallax setting and light the assets with the same parallax settings, whereas it shouldn't light the assets in front of them as the light would be considered as behind those assets.

Later on it would be cool to have lights in the front lighting assets in the back according to their range. Also the feature you described earlier as lights in the background shining through holes in the front assets would be brilliant. As for now I would be happy to get the lights in the back moving according to the parallax shift.