Shader Research

Hey Folks! Not too much to report today, as most of the day was spent researching shaders on OpenFL and HaxeFlixel.

First, why are shaders important? Basically, shaders are what allow games to do things like lighting, bump maps, and other interesting graphics tricks. Traditionally, they're used on 3D geometry like meshes and full-screen effects. However, they can also be applied to plain old bitmaps, which is what most 2D engines like HaxeFlixel use. Getting shader support in HaxeFlixel can provide the ability to make sprites look more interesting/attractive.

And as it stands, HaxeFlixel already supports shaders. But there's one major limitation: any active shaders get applied to everything in the scene (a.k.a. camera). This is fine for things like a screen overlay, but in my ship example where there are bits of walls and lights, this won't really work (without serious hacking behind the scenes). Still, it's easy to do, and I was able to apply shaders to thousands of sprites without any performance issues.

Later, I managed to get a hold of a pull request on GitHub which introduces some shader samples to OpenFL. Included is a lighting (i.e. normal map) shader, which is something I'm interested in trying with the ship prototype.

Getting it to run wasn't too bad. And I was even able to manipulate some things to apply it to moving sprites around the screen. Performance gets a bit bogged-down around 11k sprites, which is great. And if I have a shader instance per-sprite, it bogs at 3k. Still impressive.

However, I had trouble when it came time to make each sprite show positional lighting changes. E.g. sprites behaving like the light is static in the scene as they move around it. I haven't yet figured out how to feed the fragment shader positional info per-sprite, so they all render as if a light was in the same position relative to each sprite (e.g. they all have highlights on the left, all the time).

In 3D engines, I think this'd be handled by the vertex shader passing info to the fragment shader. However, I haven't had a lot of experience writing shader code. Particularly in OpenFL. There is one example out there, the HerokuShaders, which seems to do both vertex and fragment shading in OpenFL. So it may be worth taking a look at that to see if I can piece it together.

The end result, I'm hoping, is some way to render interesting lighting/shadow effects without needing to resort to my tilemap grid hacks. These visuals are not exactly critical to the gameplay, but I'd really like for my next game to be a bit more visually enticing. I envision inhabiting the ship to be an atmospheric experience, whether it be sitting quietly in the glow of the command console, navigating a dark corridor in a derelict, or even being blinded by a methane leak while gas-mining an outer planet.

I can probably do without lighting and just diffuse-color everything (like NEO Scavenger). I'm sure it'd be fine. But while I'm here at the root of the game's architecture, it'd be good to at least know what my options are!