2D lights and shadows sample

Edit: This appears to be my video card, as the example here also renders that way. I opened that link on another PC with an nVidia card, and it’s fine. I have an AMD RX 6800. I’m open to solutions, but I suspect this would be hard to troubleshoot without the same card/drivers. I’m on the latest drivers.

This doesn’t seem to be rendering correctly for me on the latest Defold 1.4.8. It’s as if the shadows are too close to the light source. I’ve included a screenshot of the sample from the repo and my own project where the same effect is happening. Is there a quick fix for this? I’m using this in a game jam that ends this weekend, so will need to find an alternative if I can’t get this to work. I’m not familiar with shader programming, and I’ll get lost deep in the woods for hours if I try to figure it out now.

Many thanks to everyone who’s contributed to this and some other extensions/libraries I’m using (I will surely list them in the credits). It’s been really easy to import and adapt them, and I’m surprised how fast this is coming along. This is my first game jam, and maybe I’ll finally publish my first game.

image

1 Like

I made a change to the shadow_map.fp that fixes this for AMD and doubles the shadow distance for nVidia. Since it also works correctly on my iPhone without any change, I consider it an AMD driver bug.

I’ve never tried to work with shaders before, so I’m not sure that I understand fully what is going on here. I think it stores the distance to the occluder in a 1d texture. It gets this from the depth buffer bit. I doubled this distance before it is written to the shadow map, and that fixes it for AMD and breaks nVidia.

I’m hoping that gives someone that is interested some idea of how to work around this with AMD and still work with nVidia. I feel like it’s something with how AMD calculates the depth bit vs nVidia, but this is uneducated speculation on my part. Maybe there is some assumption in the shader program that works for nVidia but not the AMD drivers. Something with precision or clamping.

I would not even have noticed this if I wasn’t using my AMD card at the time… I feel it would be useful to fix this if it does affect all AMD cards and current drivers. Here is the change I made in a fork of this repo:

3 Likes

Hey i am curious how were you able to sample the background inside of the cone, while having the rest of the screen around it Darker? I have been having a hard time trying to get RT of the light cone also include the texture thats behind it.

It’s been a while since I’ve dabbled in game dev and Defold. You can download the project at the link below and open it in the latest version of Defold. It was a game jam game I wasn’t able to finish, so it’s pretty rough. Things are not organized intuitively, and the code is a bit messy.

I think the answer to your question can be found in the infinite_map.collection. In that collection, check the game object named “objects”, then “player” under that. You’ll see the player.script there. In player.script, there are two go.property at the top for light arc and distance. I was going to change these values during gameplay, but I don’t think I got that part finished.

You’ll also see under “player” a game object for “light” with the “lights/lightsource.script” attached. That is from the 2D Lights and Shadows library (which I had manually added to the project since I had made the change described above to fix the AMD bug). Note that the “light” game object is positioned at the end of the flashlight of the player sprite. That is where the light cone will originate from, and it is parented to the player so it inherits its position and rotation.

Basically, the arc and distance properties are given in that lightsource.script. I just set those to match the similarly named properties under my player.script using the go.set function. You can see that in the reset_player function at the top of player.script. It’s those properties under lightsource.script that define the cone (as well as color, falloff, etc.).

You can also see in player.script, on_message where I check the ghost angle to the light source to determine “collision” with the ghosts.

The game still builds and runs in the latest version of Defold, so give it a try. Let me know if you have any other questions.

5 Likes

Congratulations on your first released game @paulbaucom! :partying_face:

It’s a nice one game, great for a start, so wishing you more and more successes! :wink:

3 Likes