GMS 2 An arts based lighting solution for anyone using GPU Alpha Blending.

Discussion in 'Advanced Programming Discussion' started by Robert Russell, Nov 29, 2018.

  1. Robert Russell

    Robert Russell Member

    Aug 6, 2017
    Fake lighting using GPU Alpha Blending.

    Preamble and whinging .. skip as needed ... you probably should.
    This is something I have been meaning to post for a while .. however life gets in the way .. not least of which is that we are packing up to emigrate. So, this is just how far I've got. Unfortunately in with the many things on the go at the moment I have changed IDE so I do not really work in GM at the moment .. sorry. However I think if you do work with GM and come from an illustration background this is a nice technique for adding some nice lighting effects to your game .. it is also relatively fast to iterate through different ideas, because you are only really altering the visibility of pre-baked lighting options with a stack of masks. In short the whole lighting model (if you could call it that) fits into about 60 lines of code (incl spaces :) ), then your main challenge is to decide what to dynamically shadow and paint up some lighting options and hide/reveal masks .. once you get it, it is really fast to do.

    This is the bit to really skip over ..
    On the whinging side I am really really disappointed that an easy to use lighting model that understands normal maps is not even on the horizon. Lighting effects can be a core mechanic that alter the mood and challenge of a game hugely.
    Well, in general, I am just really disappointed of late. I have been sitting here for about a hour trying nor to write a long list of MY demands for GMS2, yes MY MY ME ME ME what I WANT. However I am old, and I have been using techy stuff for a lonnnnng time and you do get quite an accurate sense of when the wheels are about to come off. Anyway, instead of a long list of unpalatable demands here is a true story. Our biggest park has a cafe. The council ran it for years and never made a profit. Last year they closed it for 'refurbishment' .. council speak translated as closed forever ... confirmed by the To Let sign that appeared later. Some friends of ours run a village cafe which is lovely and we frequent it way too often .. when your 10yr old can walk in and say 'I will have my usual please' you know you have spent a lot of money on coffee and cakes (and for the record beans on white bread not toasted with cheese And the beans in little bowls on the side so the exact layering can be self constructed). The park cafe came up in conversation with the usual 'no one makes a profit'. My response was 'you have to be kidding right' (yeah .. at work they try to hold meetings when I am not there). The fact is, if you stand there next to the cafe in the park a potential customer (usually an elderly person or mum/dad with pre-school age child) walks past every 2 minutes ALL DAY ... and here it is WITH THEIR DOG. The council cafe said NO DOGS=NO CUSTOMERS, no customers all day. About 2 months later our friends with an admirable straight face revealed they had successfully engaged the let with their plans for a shabby chic dog friendly cafe with lots of space and toddler friendly toy box, traditional cakes and an extra line in dog treats. I now often have to wait for a table or at least have to do an undignified scramble for the last one. So .. my wife and I help out at youth groups, schools and in hospitals in various capacities either working or volunteering and here is what customers sorry meant to say parents and children are exited about ... my phone .. the camera and apps on my phone .. the accelerometer on MY PHONE .. the Arduino and microbit stuff from school (I WISHED THIS TALKED TO MY COMPUTER BETTER) .. look at this game WOW IT LOOKS PRETTY WITH THAT LIGHTING ... bluetooth iot bluetooth BLUETOOTH IOT ... oh 2D my kids are all into 3D .. I wish we could just do everything in one place .. list goes on ... thousands of parents every year desperate to get there kids going with STEM thousands of kids every year wanting to write games both needing lots of hand holding with reasonably priced books, learning tools etc ... thousands of foot steps every year past the door, past the door, ... PAST the door.
    However it is nice to know when I get back to GMS2 I can now output my unlit retro pixel art game to the Switch ... yeh.
    I advised you to skip.

    So this is a first shot at using GPU based blending to set up a nice fake lighting system with dynamic shadows. I have used GM quite a bit but I am a self taught coder so some of this probably needs A LOT of optimization and improvement .. I would call this a proof of concept only. The process basically takes a dark state and a lit state and reveals the lit part of the image where needed. If most of your art is flat backgrounds this allows use to select the main features you want shadowed then effectively cut them out from the background and make a reasonably effective shadow. Anyway it is easy to do things like torch direction, light through bars etc. Also it is easy with layering to control shadows correctly e.g shadows of tall objects over short ones but not the other way round

    The images below should show my light state an dark state for a little battle arena. I then drew some simple masks over areas of the background to shadow and made objects .. objects that 'collide' with the light object then add themselves to the array of objects that need their shadows dealing with. The core of the process is then to add everything cooker cutter fashion to to a GPU alpha mask that controls the reveal of the overlying lit image. On my website .. Rivingten .. there is a much longer/better explanation and a shot of doing this with a hero object. I know people hate pics of code but this is the best way I could explain it.

    lightingProcess_01.jpg lightShadowsCode1.jpg
    Got to pack ..

    Regards, Rob.

    PS there are some nice explanations of masking about .. there are some links from the website.

    PPS the website is just a lot clearer .. it is not trying to sell you anything.
    Carl Nylander likes this.
  2. RujiK

    RujiK Member

    Jun 21, 2016
    This is all very good, but are you asking a question anywhere? Are you asking for lighting method ideas? Also you mention a nice website a few times but I don't see a link anywhere. If you are just posting this to show off, why not include a demo or at least the code in text format? I doubt many people are going to copy the text from your images just to see if it works.

    Finally, your weird story/rant thing at the beginning would probably be a better fit for off topic. You will probably get more feedback if your future posts are more concise.
    Ido-f and Nocturne like this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice