Giter VIP home page Giter VIP logo

penumbra's Introduction

What is this sorcery?

Penumbra allows users to easily add 2D lighting with shadowing effects to their games.

Note that this project is no longer in development. I do try to fix any bugs though!

Platformer2D sample

Getting Started

Building source and samples

The following is required to successfully compile the Penumbra MonoGame solution:

Using Penumbra

Currently available only for MonoGame WindowsDX platform targeting .NET 4.5+!

Install the assembly through NuGet:

Install-Package MonoGame.Penumbra.WindowsDX

In the game constructor, create the Penumbra component and add to components:

PenumbraComponent penumbra;

public Game1()
{
  // ...
  penumbra = new PenumbraComponent(this);
  Components.Add(penumbra);
}

In the game's Draw method, make sure to call BeginDraw before any other drawing takes place:

protected override void Draw(GameTime gameTime)
{
  penumbra.BeginDraw();
  GraphicsDevice.Clear(Color.CornflowerBlue);
  // Rest of the drawing calls to be affected by Penumbra ...
}
...

This will swap the render target to a custom texture so that the generated lightmap can be blended atop of it once PenumbraComponent is drawn.

By default, Penumbra operates in the same coordinate space as SpriteBatch. Custom coordinate space can be configured by setting:

penumbra.SpriteBatchTransformEnabled = false;

Custom transform matrix is set through the Transform property.

Working with lights

Penumbra supports three types of lights: PointLight, Spotlight, TexturedLight

PointLight Spotlight TexturedLight

While PointLight and Spotlight are generated on the shader, TexturedLight allows for more customization by requiring a custom texture used for lighting.

Lights provide three types of shadowing schemes: ShadowType.Solid, ShadowType.Occluded, ShadowType.Illuminated

Solid Occluded Illuminated

To add a light:

penumbra.Lights.Add(light);

Working with shadow hulls

Hulls are polygons from which shadows are cast. They are usually created using the same geometry as the scene and can be ordered both clockwise or counter-clockwise. Hull points can be manipulated through the hull.Points property.

For a hull to be valid and included in the shadow mask generation, it must conform to the following rules:

  • Contain at least 3 points
  • Points must form a simple polygon (polygon where no two edges intersect)

Hull validity can be checked through the hull.Valid property.

To add a hull:

penumbra.Hulls.Add(hull);

Assemblies

  • MonoGame.Penumbra: The core project for the lighting system.

Samples

  • HelloPenumbra: Simple sample which sets up bare basics of Penumbra with a single light source and shadow hull.
  • Platformer2D: Penumbra lighting applied to MonoGame Platformer2D samples game.
  • Sandbox: Generic sandbox for testing out various different scenarios.
  • Common: Supporting library providing common functionality for samples.
  • FarseerPhysics: Create physical bodies out of sprites and add them as hulls to Penumbra!

Development

Release a New Version

  • Make sure version numbers are updated in .csproj files.
  • Create packages:
dotnet pack -c Release MonoGame.Penumbra.DesktopGL.sln
dotnet pack -c Release MonoGame.Penumbra.WindowsDX.sln
  • Publish packages (substitute <version> with version to be released):
VERSION=<version>
dotnet nuget push Source/bin/Release/MonoGame.Penumbra.DesktopGL.$VERSION.nupkg --api-key $NUGET_API_KEY --source https://api.nuget.org/v3/index.json
dotnet nuget push Source/bin/Release/MonoGame.Penumbra.WindowsDX.$VERSION.nupkg --api-key $NUGET_API_KEY --source https://api.nuget.org/v3/index.json

penumbra's People

Contributors

blizzcrafter avatar captainkidd5 avatar discosultan avatar sqrmin1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

penumbra's Issues

Feature: provide a way to attach lights/hulls together

Could be done in some helpers class as it is should not be a Penumbra feature itself.

The concern here is to build compound object, typically a car:
one or more hull(s)
many lights (headlights, brake lights, turn lights and others depending of the application like fog lights, extra lights for rally racing, ...)

For now, it could be a serious pain to:
-first position lights in the right location
-define the right origin
-make everything run altogether, for moving and rotating

An extra container which contains everything and have a single origin, can enable/disable any of its component (for dealing with damage and/or improved parts) would be very great.
But I honestly don't know if it could be part of a Penumbra companion library or it is completely unrelated at all.

Any thoughts ?

PointClamp Penumbra and RenderTarget2D again..

This is kinda the same issue as the one opened earlier but this time it's a lot weirder.
I managed to set the SamplerState to PointClamp and Penumbra now produces quite a sharp picture.
However, that comes at a cost of a couple of glitches.

  1. When drawing, Penumbra cuts an eighth(?) of the side of the game screen and shifts the whole rendertarget no matter what resolution I use. You can see on the screenshots, white and red dots are the center of the screen. The only thing changing between those two screenshots is I am enabling the lighting engine.

Without lighting (red dot is the center of the screen):
2019-01-28 3

With lighting (white dot is the center of the screen):
2019-01-28 1

  1. For some reason, Penumbra elongates the whole rendertarget and distorts it (see screenshots).
    EDIT: pixels are not being elongated but instead the whole image is shrunk in the x-axis. I counted how many pixels are drawn for each in-game pixels: 10x10 pixels without lighting and 8x10 pixels woth lighting.

Square pixels, no lighting:
img_20190128_111725 2 jpg
Elongated pixels, with lighting:
img_20190128_111942 2 jpg

Code for drawing the render targets:

if (UsesLighting) Lighting.BeginDraw();
            
            spriteBatch.Begin(samplerState: SamplerState.PointClamp);
            spriteBatch.Draw(
                renderTarget,
                new Rectangle(0, 0, (int)game.WindowDimensions.X, (int)game.WindowDimensions.Y),
                Color.White);
            spriteBatch.End();

if (UsesLighting) Lighting.Draw(Time.GameTime);

Note that absolutely no rendertarget swapping is happening when I call this draw function (aside from the penumbra engine one). All the rendering is done prior to drawing the lights and rendertargets.

Please help, I'm desperate :(

[Question]: Is there a way to detect which hulls are affected by light?

First of all, great library! It's light years ahead of other attempts on the internet.

I have successfully added a light into my top-down prototype, which is casting shadows beautifully on my hulls. Now I'd like to understand if there is a way to figure out which hulls are casting shadows (in other words, which hulls the light is reaching)?

I didn't see any hooks for this in the Hull class, so I'll keep looking throughout the code base, but thought I'd ask too. If I find something, I'll post it for others too. Thanks.

Normal map support

Hello there,

Thanks a lot for making penumbra! I know this project is not very active at the moment. I just wanted to know whether penumbra supports normal maps in any way.

I can't understand how this works enough to know whether it would be possible or not. I would be willing to implement whatever is missing for supporting normal maps, but I would probably need a nudge towards the right thing to study/research.

Thank you!

Shadows casting in weird directions

Hello, I'm a little stumped on an issue and was wondering if you've seen this before. I've got my hulls and lights setup and they appear to be in the correct locations, but shadows are getting cast in weird directions. They do change dynamically as I move around, but I can't exactly tell what's going on.

capture

Light Origin: invalid value allowed

Following my previous issue, using an Origin with x or y component outside [0,1] interval is allowed by light constructor or setter without raising an exception OR normalizing it before (does it make sens ?) leading to... don't know, unpredictable or invalid result.

Not Updating on Hull removal.

Hello and first of All: thank you for this amazing Project!

I am having a few Problems with the Removal of Hulls and it not Updating as expected. I am building a Game where there are Placeable Objects, that cast Shadows. Everything works as axpected when placing said Objects, but upon Removing the ShadowHull stays there, until there is another Update to the Shadows in the Range of the Light or another Hull is Added to the Collection.
Is this intended Behaviour?
Is there a better way to Remove Hulls?

i am using the OpenGL version.

Transforming Penumbra with Camera?

Apologies if this a newbie-ish question, but I'm wondering how to apply Penumbra within spriteBatch so the _cam.transform makes the lighting attach to the viewport. Currently I have Penumbra drawn statically to the screen.

image

Unfortunately all the samples have static screens without any viewporting so I wasn't able to work from those, is there a way to convert the shader to a XNA.Graphics.Effect to be past as a param to _spriteBatch.Begin?

Cheers heaps!

[Question]: Is there a way I can change the layer depth at which individual shadows are cast?

I'm not sure if Penumbra was intended for 2/3 perspective games so I understand if there isn't any built in way to do this. Most of my layer sorting is done visa Y axis sorting. Objects with larger (lower done) y values are prioritized in front of objects with smaller y values. As you can see in this picture shadows cast from lights below the object create a shadow which covers the object, rather than the area behind it.

Is there a way of specifying the layer depth of individual shadows based on the Y position of objects? I've looked through the source a bit and can't seem to find anything!

lights

Penumbra with RenderTarget2D and PointClamp sampler state

I'm rendering my game to a scaled down render target, which I then render at the full window size. It seems that penumbra doesn't want to work when I use a render target.

Here's some pseudo code showing my setup:

// Initialize Penumbra, some lights and hulls.
new PenumbraComponent();

// Create a render target 
RenderTarget2D renderTarget = new RenderTarget2D(GraphicsDevice, WindowWidth / 2, WindowHeight / 2);


// Inside the draw method...
GraphicsDevice.SetRenderTarget(renderTarget);
GraphicsDevice.Clear(Color.White);

spriteBatch.begin(samplerState: SamplerState.PointClamp);

// DO GAME DRAWING HERE

spruteBatch.End();

GraphicsDevice.SetRenderTarget(null);
penumbra.BeginDraw();

spriteBatch.Begin(samplerState: SamplerState.PointClamp);
spriteBatch.Draw(renderTarget, new Rectangle(0, 0, WindowWidth, WindowHeight), Color.White);

spriteBatch.End();
penumbra.Draw(gameTime);

This resulted in the lighting rendered at the original scale of the render target, and the game at the scaled size:
scaled lights

To overcome this, I scaled up the positions of all hulls and lights by 2 (the render target is half the window size). This seemed to fix it, but then I saw the SamplerState.PointClamp wasn't working.

This is what the game looks like without lighting:
no lights

And with lighting:
lighting

It's turned blurry, as if the PointClamp filter was disabled.

Are there any ways to overcome this issue? Would it be possible to allow penumbra to render to the same RenderTarget the game is drawn to, allowing it to be scaled up correctly, with the PointClamp sampler state?

FNA-XNA Support?

There are a few API compatibility issues but I've gotten this working on FNA-XNA, is there any interest in trying to merge this back to the main branch?

No lights drawn on WindowsDX

I seem to have a really weird issue with Penumbra where the whole screen is dimmed as usual but no lights are being rendered on the screen. I ran the HelloPenumbra test project and the result is this:
image

I am using the latest MonoGame version (3.7.1)
Can anyone please test this too and comment the results? please?

[bug] Occluded shadows aren't working in the DesktopGL port

For some time now, I've been trying to implement lighting to my game. I found this library, and I think it's a great resource, however, I have an issue regarding the "Occluded" ShadowType.

I want to develop a cross-platform game using OpenGL, but the Occluded shadows act just as "Illuminated" ones in DesktopGL. I tested every single version of the DesktopGL builds (1.3.0 - 1.4.0) - none of them worked for me.

When I added a hull and changed the ShadowType of the light to Occluded in the example/demo project "HelloPenumbra", it worked (since the example project is using DirectX). Then I made a new OpenGL project, copied the code from the original example, and added the DesktopGL reference - it didn't work.

image

Possibility for light to ignore specific hulls

Trying to achieve flashlight effect. Having 2 players, both have Hulls and both have Spotlights. Problem is that Player1 Hull blocks its own Spotlight, because Position is inside Hull. Would be nice, to set for color (or for hull), which object to ignore in calculations.

Drawing non-affected Elements like in HelloPenumbra doesn't seem to work

Hello!

I have tried to implement Penumbra similarly to how it's done in HelloPenumbra.cs, meaning I call both PenumbraComponent.BeginDraw() and PenumbraComponent.Draw() in my Draw Method, so that I can exclude some things from being affected by Penumbra.

However, in that file, the PenumbraComponent is not added to the Components List (as far as I can tell), which when I try it causes Penumbra to not have any effect at all.
If, however, I do add the PenumbraComponent to the Components List, then the base.Draw() call in my project causes things to break, as it attempts to call all Draw() functions of all Components, which won't work since Penumbra throws an exception if Draw() is called without BeginDraw() having been called beforehand.
Inserting a second BeginDraw() before base.Draw() also doesn't work, since it seems to result in only a black screen.
The only solution I can think of that might work would be to omit the base.Draw() call altogether, but surely that can't be right since that would cause other Components to no longer be drawn.

I can't tell what I'm doing wrong here. Any help would be appreciated!

SharpDXException was Unhandled

Hey,

Whenever I try and use this it seems to throw this error: _"An unhandled exception of type 'SharpDX.SharpDXException' occurred in SharpDX.dll

Additional information: HRESULT: [0x80070057], Module: [General], ApiCode: [E_INVALIDARG/Invalid Arguments], Message: The parameter is incorrect."_

I have no idea what's causing this. I've done exactly what the documentation is telling me to do, and I've been trying to get it to work for a day or two now.

Some details:

  1. Monogame 3.5.1.1679
  2. Latest Penumbra 2016 October.
  3. No lights or shadows have been added yet since I first wanted to make sure it could run.

Tweak color of unlit areas

First, thanks for this library. It's awesome and it's solved so many problems for me.

Is it possible to have the unlit areas be anything other than black? In my game, I want them to be "foggy", and the spotlight from the player should cut through that fog to reveal the play area. (I'd probably eventually want to render that fog with a shader).

I originally thought that using a non-black AmbientColor would work but that also changes the color of the lit areas, which I don't want.

Installing Penumbra

Hi,

I'm having some issues getting Penumbra into my project, I'm hoping to use it for lighting in my 2D game. So I've went into Visual Studio 2019 and used the NuGet Package Manager Console to install Penumbra ("Package-Install MonoGame.Penumbra.WindowsDX")

This installed without any issues in the CLI and when I check the "packages.config" file I can see:
package id="MonoGame.Penumbra.WindowsDX" version="2.1.0" targetFramework="net45"

The issue is when I go into my main .cs file and type "using Penumbra;" I just get a namespace assembly reference error. I'm sure this is an easy fix but I'm not experienced with NuGet and using other peoples packages.

image

Can anyone give me a little more insight on how to properly install Penumbra and get it working in my project?

Thanks

Support for MonoGame 3.8.1

Using current version with 3.8.1 gives the following error:
Unhandled exception. System.Exception: This MGFX effect is for an older release of MonoGame and needs to be rebuilt.

Documentation improvement

I believe Penumbra deserve a better documentation, including subtle tweaking of items parameter (lights and hulls), some tips & tricks (like what is fastest/cutest for example) etc

I know I'm not the best to talk about writing documentation :P and I understand what it could be, but as good as a library is, it is nothing without proper documentation, hope you'll agree. You can also let me know if you need some help, as a beginner users, I can help at least by pointing where I have some trouble understand something or deal with an issue on my own.

[feature?] realistic shadows from topdown viewed shapes

I just wonder if you have already something to create "realistic" shadows from shapes which hide some part of themselves due to the view angle like trees, light pole, fence, gate or grid ?

For now and AFAIK, using Penumbra makes shadow looks resp. from a circle (if we consider trees displayed as a disk of leaves) and a full/opaque square. Not really what we would expect to see.

As I thought of this, I also come with a possible solution to this:
for each assets we want to manage under Penumbra, we would have to provide a kind of front-view (or horizontal view) of it, and Penumbra would use this texture as base to generate better shadow by shearing/stretching the texture like the base shadow does.
It would also allow to add vertical location of the light source (I don't think this is managed now, isn't it ?) as texture could be more and more stretched when light goes low and vice versa.

Example:
fr-shadow1

One point here is to know how much time it takes to shear a texture, the right way,
at run-time, probably not that's much.

How to use it within a tileengine?

Hey ppl,

is there any sample, how to use it in a tileengine? The platformer sample isn't very suited for that.
I tried to combine the tiles to large rectangles but it looks very strange on the corners where the rectangles are connected.

Smaller Render Targets

Is there a preferred way in the API to tell Penumbra target a render target that is a different size than your window? Useful if, say, you have a specific size that your game needs to be drawn and then scale that to the screen.

Complex Hulls

The library is great, but you can´t use as a Hull something that is not a polygon, For example, I would use as a Hull a character, that is using an sprite, and has a transparency. For that I will need to define all vertex that create the contour of the character, and it´s not efficient. It would be great if you could use an sprite as a hull.

[Request] Penumbra OpenGL port

Great job @discosultan! Good library, i liked it!
But i (think not only i) are interested in OpenGL port.
Is it realizable? I think there are many people who support me, for saint cross-platform in fridges, microwaves, calculators and etc. =)
Currently there are only one good OpenGL 2D lighting/shadowing engine. Krypton-XNA, but the author abandoned it from 2011.
I trying to port it to MonoGame 3.5+, but no success. Dynamic shadows dont work properly.
And in dev. release (Krypton) i got only black screen.

So, what about the port of your library?

Regards, TheZNC

.NET Core Support Possible?

I know you stated that you will not update the library, so I am not asking whether you will do it, I am asking how hard would it be. As if it is easy enough I should be able to do it myself and possibly fork this one but if it is hard or near impossible without a complete rewrite, I will not attempt, because I would most likely fail tbh,
Thanks

Occluded ShadowType working incorrectly?

Hello.

Perhaps the issue is on my side, but Occluded ShadowType seem to work the same way as Illuminated.
occluded
Not sure if there is something wrong with my code, ShadowType in Light object seems to be correct.

Edit: just noticed it's in TODO features section in the code? But there is a screen in documentation?

Support for MonoGame 3.7.1

I upgraded my project from MonoGame 3.6 to MonoGame 3.7.1, and now I'm getting these errors:

.../penumbra_hull.fx: ...\\penumbra_hull.fx(4,32-33): error X3530: invalid register specification, expected 'b' binding
...\\penumbra_hull.fx(9,31-32): error X3530: invalid register specification, expected 'b' binding

(similar errors in the other fx files).

Support MonoGame 3.8

As the title says, currently using penumbra with monogame 3.8 throws exception: System.Exception: 'This MGFX effect is for an older release of MonoGame and needs to be rebuilt.'

Can't figure out transform matrix Please help.

Hi i am working on a side project of mine. A 2d game way in the beginning of development. The main problem I have is with the transform matrixes. To be honest I don't know how they work at all but somehow i managed to get them working with a camera and UI where i can render stuff and not be effected by the movement of the camera in the world. Here is what I have:

            // Draw world
            SpriteBatch.Begin(transformMatrix: DisplayManager.GetMatrixScaleWorld());
            StateManager.DrawWorld(spriteBatch: SpriteBatch);
            SpriteBatch.End();

And:

            // Draw GUI<br>
            SpriteBatch.Begin(transformMatrix: DisplayManager.GetMatrixScaleGui());
            StateManager.DrawGui(spriteBatch: SpriteBatch);
            SpriteBatch.End();`

With this type of setup I have it perfect and it scales with different resolutions etc.
Here is how it looks: Image
But when I add " penumbra.BeginDraw();": Image
Here is how the code:

            penumbra.Transform = DisplayManager.GetMatrixScaleWorld();
            penumbra.BeginDraw();

I don't know how to do it. Also how to separate the UI from the world so that the lighting only effects the world but not the uni itself. Sorry if this is not the right place to post this but please help.

Penumbra Showcase [show us your creation(s)!]

Hello ladies and gentlemen!

This "Issue"-page is only for showcasing your projects, which are including the Penumbra lighting system.

We thought it would be awesome to have a list of games using Penumbra to get inspiration or just enjoying to see what people do with it.

It doesn't matter if your project is small or big - just show us what you've got!
It also doesn't matter if you are just starting or if the project is already a longer time in development.

Use the following template to submit your project:

Project Title
Project Website (if you have one - can also be your twitter site for example)
Your IndieDB site (if you have one)

Then add 1 or 2 screenshots of your project, which are showing Penumbra in action.
Alternatively you can add a link to a YouTube video (or other video-sharing websites).

After that you can:

Make a small description what your project is all about.

  • If you want to update your project, then please don't create a new comment. Just edit your old comment.
  • It is also allowed to post more than 1 project.

I will add my own projects to this list as soon as I have my materials ready ;)

Have fun using Penumbra and showing us your creations!

Note: I asked the creator of Penumbra (@discosultan) for permission to open an issue as a showcasing place.

[DOC] light origin need more explanations and example

What "origin" property is supposed to do and to be used ?
It is described as a normalized value ( in [0..1] interval only) but how to deal with this using screen coordinates for light position ?
Which are valid values ? As the setter doesn't do the normalization itself as opposite to the "documentation" seems to suggest.

For example, using a Vector2(10,0) as Origin for a spotlight make it doesn't shows on the screen (no light).
It's unusual to mix values like this and not very easy, a x value of 0.1 make the spotlight away for it's center but how much ? I can't tell unless doing the maths inside light class myself.

[Feature Request] UWP support?

Nuget reports this package is incompatible with UWP, Monogame supports UWP and it's simply a wrapper for the normal Win32 app. Are there anyspecific limitations in UWP that is making supporting it different from a normal Monogame Windows app?

Limit the penumbra effect to a specific area?

Hello,

I'm trying to use penumbra to draw shadows in a sub-section of my game/screen. However, after some experimentation it seems that penumbra always uses the full screen size, as i have to call it before any other draw calls. Is there really no way to limit the affected area to a smaller part of the game window or am I just not thinking about this right?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.