Shader Showcase ~ WGD Lightning Talk

Every now and then, and definitely not when we could find any other speakers to come in, WGD holds Lightning Talks. Any member is free to come along and opine for a few minutes about whatever game-related topic they please. In the past, we’ve had talks on using OpenGL in fancy ways, using fancy DAWs (Digital Audio Workstations) to create funky tunes and “games for people”, for those fancy-pants people who love games involving no equipment except the bodies of you and your friends.

This time round, there were three talks – one was on palette-swapping and different techniques used in the past, and another was about making your own art (it was sorta like the Shia LaBeouf Just Do It talk, but with less shouting and more creepy drawings). Finally (or rather, in the middle of those two), was a talk from someone not at all built for public speech – me. I did an analysis of some of the cool shaders used in Super Mario Odyssey‘s Snapshot Mode, and I’m gonna do it again, right here. It was somewhat refreshing having a whole session of talks about arty subjects, even if they often swayed into technical artistry, given that the society is somewhat programmer-heavy. Never mind that the Lightning Talks took place last term – let’s pretend I’m prompt at blogging.

Anyway, it’s time to jump up in the air, jump up don’t be scared, jump up and make shaders every day!

00_demo_none.png

I’ve set up this demo scene with a few coloured cuboids, one directional light and a boring standard Unity skybox. It’ll do the job for demonstration purposes. In the executable version, you can freely move the camera around.

01_ex_greyscale

I took the most saturated environment I could find, just to desaturate it.

First up is a simple colour transformation. To convert an image to greyscale, all we do is take the luminance of each pixel in the scene and use that value for the red, green and blue values of that pixel’s colour.

float lum = tex.r * 0.3 + tex.g * 0.59 + tex.b * 0.11;

The luminance value takes into account the sensitivity of the human eye to each component of colour; your eyes are more sensitive to green, so it is heavily weighted in this calculation. That’s basically all there is to the greyscale shader.

02_demo_greyscale.png

This is pretty much what you’d expect; the effect is very similar to Odyssey’s.

03_ex_sepia.png

The city environment felt the most appropriate for a sepia-tone shot.

The sepia-tone shader is very similar to the greyscale one, although the transformation is a little more involved. Before, we used the same value for each of the resulting red, blue and green channels to get greyscale values, but this time, each of the resulting red, blue and green are separate functions of the original RGB values.

half3x3 magicNumbers = half3x3 
( 
    0.393 * 1.25, 0.349 * 1.25, 0.272, // Red. 
    0.769 * 1.25, 0.686 * 1.25, 0.534, // Green. 
    0.189 * 1.25, 0.168 * 1.25, 0.131 // Blue. 
);

half3 sepia = mul(tex.rgb, magicNumbers);

We have to resort to some matrix multiplication to achieve this. The 3×3 matrix represents a bunch of numbers magically pulled out of Microsoft’s rear end – the mapping from the original colours to the new ones. The last line here essentially represents a system of three equations, where, for example:

sepia.r = tex.r * 0.393 * 1.25 + tex.g * 0.349 * 1.25 + tex.b * 0.272

If you’ve never done linear algebra or matrix multiplication before, this might look a bit exotic, but trust me – it works!

04_demo_sepia.png

We end up with another unsurprising result. You might find this a bit too yellow, in which case just remove all the multiplications by 1.25 I added.

05_ex_depth.png

This one is where things get a bit more interesting. Upon seeing this effect, I immediately thought to myself, “Aha! They’re just using the depth buffer values! I’m a goddamn genius”! So, what is the depth buffer I hear you ask?

In computer graphics, you need to render things in the correct order. If you have a crate fully obscured by another crate, you wouldn’t want to render the obscured crate over the other one – it’d look really strange! Whenever a pixel is drawn, its depth – the distance from the camera plane to the object being drawn into this pixel in the z-direction – is recorded in the depth buffer (z-buffer); this is just a big ‘ol 2D array the same size as the program’s on-screen resolution. When you try and draw something, you first check the new object’s z-buffer value against whatever is already recorded at that position, and if it’s a higher value (the distance from the camera is further), that means it’s occluded and we discard that pixel.

06_demo_depth

This is the same scene from above, but from a different angle. Each pixel is just showing the greyscale z-buffer value of the final rendered image, which means it’s representing how far away the closest thing at that pixel position is. But why does this not look as good as the Odyssey example? Well, first of all, I’m not Nintendo EPD and they have a few more resources than I do. Second, I’ve not coloured the image myself, although as we saw earlier, this would be a fairly trivial change. The main reason is actually because the scene isn’t very large and we need to understand how the z-buffer does things to understand why.

There needs to be a bound on how small or large the z-buffer values are, else the values would be pretty meaningless. A camera has a near clip distance and a far clip distance, which are the minimum and maximum distances from the camera plane an object can be to be rendered. If you’ve ever clipped the camera through part of a wall in a game, that’s because the near clip plane is further away than the wall is, so that section of wall is culled (not rendered). Values in the z-buffer are typically floating point values between 0 and 1, so a small scene (i.e. one where the clip planes are fairly close together) would result in two objects having more different z-buffer values than the same objects in a large scene.

In the scene above, the near plane is very close to the camera and the far plane is just far enough to contain the whole scene. By default in Unity, the far clip plane is 1000 units away, which would result in the entire scene here being black – all z-buffer values would be close to 0. But having a small scene means you don’t really get much variation in colours, because things are really close and there’s nothing in the background – in the Odyssey screenshots above, there are things clearly in the foreground (Mario and the rocks he’s stood on) and the background (the section of island behind the waterfall). One detail that’s kind of hard to make out is that the furthest island actually shows up in very faint green in the snapshot, so it’s real geometry rather than part of a skybox. Also, the water particles don’t show up in this mode, so they’re likely reading from the depth buffer but not writing to it, and are rendered after all the opaque level geometry.

07_ex_blur

Seaside Kingdom Doggo raised this game’s review average by 19 points.

Next up is a blur effect. I won’t be talking about the radial blur starting from the edge that Super Mario Odyssey uses though; I’ll talk about a simpler blur that I had time to write – a Gaussian blur that operates on the whole image.

This type of blue works by throwing a ‘kernel’ over each pixel in the original image and looking at the neighbouring pixels. The larger the kernel, the more pixels are considered. Then, a weighted average of the pixel values, with the central pixel having most weight, is calculated for the new image. It’s a pretty simple concept, and probably how you’d expect, but is one step more complicated than a box blur which doesn’t bother with weighting the pixels and just does an unweighted average. Commonly, you would do two or three passes with different sized kernels, so the algorithm is run more than once and the resulting blur has more fidelity.

08_demo_blur.png

This image is doing three blurring passes, each with a larger and larger kernel. Each blurring pass actually requires two shader passes, one in the x-direction and another in the y-direction. You could reduce the amount of blurring by making the kernels smaller or reducing the number of passes.

09_ex_edge

TFW you turn up to a formal occasion in casual clothing.

The final algorithm I implemented is an image-space Sobel filter to attempt to detect edges in the image. This also acts in separate x-and y-direction passes; in each direction, a gradient is calculated, where a higher gradient means a more ‘different’ colour. By definition, you can’t do a gradient in both directions simultaneously, hence the need for two passes. This effect wouldn’t be used in a game if you wanted objects to stand out against objects of similar colour, but it’s pretty simple to understand conceptually.

10_demo_edge.png

Running the algorithm on our sample scene results in pillars that are pretty cleanly defined, but this is mostly due to their block-colouring. You can also see a couple of spots where pillars seem to ‘blend’ into one another, and all shadows are also edge-detected because they are dark areas right next to light flooring.

For a bit of fun, I ‘converted’ this effect into a cel-shading effect. Again, it’s not exactly Borderlands quality, but it was a fun exercise for me. This effect is also not present in Odyssey, so there is no reference image comparison.

11_demo_cel.png

The Sobel edge detection filter from above is inverted and multiplied by the source image, such that ‘edge’ areas become black and all other areas retain their original colouring. The areas where colours are similar clearly lack cel-shading here, such as the red pillars right-of-centre. You can also see on the yellow pillars where the skybox turns lighter that the black lines ‘break’ a little. Of course, because it’s based on the Sobel edge detection filter, you also get cel shading on the shadows, but the effect is less pronounced here because the shadows are dark.

All of the source code and a built executable for Windows can be found over on my Github. It’s really fun to pick apart effects you’ve seen in video games and I wholeheartedly recommend you try the same! If I have time over the holidays, I may try to pick apart some of the other effects, such as the NES/SNES filters which are probably just pixelation filters with a palette swap applied on top.

Advertisements

2017 Games in Review

After I wrote last year about every single game I played in 2016, I feel it’s time again to delve deep into my mind and recall every game I played in 2017. Between university work and social commitments, it’s sometimes difficult to find time for gaming. But with the launch of the Nintendo Switch, I’ve been able to sandwich in about half an hour extra a day on the bus.

Fair warning: my mini-reviews might contain spoilers for each game, so if you’ve not played any of these then close your eyes while reading.

Nintendo Switch

Nintendo is back, as so many news sites and bloggers have proclaimed! You know, because they fell off the Earth’s surface the moment the Wii U was announced. The Switch, and its first-year lineup, choosing not to compete with its rivals in terms of raw power, and instead adopting a Blue Ocean strategy with a home console you can chuck in your bag and take with you anywhere. As someone who commutes onto campus every day on a bus, I’ve been milking the portable aspect of my Switch when I’d otherwise be staring out the window. It’s like the system was tailor-made for people like me! The concept of splitting a pair of Joy-cons works exceptionally well for a university Nintendo Society, and hell, even cheesy launch game 1-2-Switch is a decent ice-breaker for newcomers.

Nintendo also made the intelligent decision to stagger their big hitters throughout the year rather than saturate the launch and holiday periods, as is common for platform holders. They knew that Breath of the Wild would grab headlines, and that Super Mario Odyssey would fuel the Christmas period – why cannibalise yourself and release more than that? It remains to be seen if the momentum will continue, but with a handful of releases primed for next year and main-series Pokemon and Metroid confirmed for Switch, it’s unsurprising that it’s on track to outsell the Wii U’s entire lifespan in just one year.

That’s 2017’s major hardware release – now let’s talk about the games. I’ll be saving my favourite game of the year for its own blog post (subtle hint: it’s Zelda). These are in approximate chronological order.

Fast RMX (Nintendo Switch)

With Breath of the Wild being the largest title at launch and the choice being otherwise limited (I ain’t buying Super Bomberman R for £50), I picked this up to see how the diddy new Joy-con controllers fare in a multiplayer environment, alongside Snipperclips. It offers a rip-roaring, fluid racing experience in F-Zero’s vein, with a buttery smooth framerate. Some of the tracks feel kind of similar to others, but there’s also enough variety here to keep you interested for a while. It doesn’t have the depth and “family-friendly” factor of Mario Kart 8 Deluxe, but it’s still a solid racer.

Verdict: Fast.

fast_rmx_4player

Snipperclips (Nintendo Switch)

I was surprised to learn that this game wasn’t developed in-house at Nintendo; it was built by London-based indie studio SFB Games and merely published by the Big N. There’s the sort of polish and charm you might expect from a first-party Ninty game – and it’s clear that Nintendo gave its support and guidance to this title.

snipperclipsImage courtesy of Nintendo.

The core concept is simple: each character can ‘snip’ shapes out of the other characters to form buckets, hooks and even egg nests to solve a range of puzzles. Sure, it won’t take you long to see everything it has to offer, but this bundle is well worth the asking price as an entry-level game that near enough anyone can enjoy and it’s a nice example of how split Joy-cons can be used effectively. Even one of my non-gamer friends bugged me to try it out!

Verdict: Charming.

1-2-Switch (Nintendo Switch)

I didn’t purchase this game due to the absurd price tag, and I feel it may have worked better as a pack-in title (I know I’m not alone in this regard), but I appreciate its value as an ice-breaker game and something that acts as a tech demo for the features of the Joy-con controllers.

1_2_switchImage courtesy of Nintendo. I wouldn’t sully my screenshots folder with this!

It’s the sort of game you’d pick up for a few quid in a bargain bin and bring it to a party, play one or twice with a large group of friends, then probably leave on a shelf as a dust magnet. It’s shallow as shallow gets, but it’s pretty good at doing what it set out to do. Just make sure you don’t spend more than about five pounds on it.

Verdict: Milking your wallet.

Mario Kart 8 Deluxe (Nintendo Switch)

I owned and loved Mario Kart 8. Right up until launch, I was on the fence about buying this one, but the improved battle mode (read: it now has a battle mode) and character additions were enough to sway me. Plus, it’s Mario Kart 8 – on the go! It still looks exceedingly pretty, and seeing as it’s out so early on in the console’s lifespan, I’m holding out hope that Mario Kart 9 could be a Switch game.

mk8d_electrodome

The battle modes are varied and alone warrant the upgrade from Mario Kart 8 if you have people to play with (I always find these games are best with local multiplayer). This too is a great icebreaker title for groups of people. I still find it rather impressive to compare handheld MK8D to Mario Kart 7 to see how far handheld gaming has come since the Nintendo 3DS.

Verdict: The best Mario Kart game yet.

Magikarp Jump (Android)

This is allegedly a video game. This mobile tribute to Pokémon’s weakest character was initially amusing – watching my Magikarp flop up meters into the air before falling back to Earth and smashing a hole in the ground made me laugh the first time I saw it. But it soon devolves into mindlessly dragging your thumb around the screen to sweep up berries and stressfully hammering your phone to skip the same menus and training animations you’ve seen a hundred times already. It gets so frustrating to advance anywhere and has all the trappings of your typical mobile game pay-to-win system. On the plus side, if you get tired of your weakling fish, it can get eaten by a Pidgeotto or blown up by a Voltorb. It’s cute, but it’s also a classic trashy mobile game.

Verdict: Totally pathetic, unreliable.

This one doesn’t even deserve a screenshot. It’s Magikarp. You know what Magikarp looks like.

Persona 5 (PS4)

I didn’t play this one, but I watched a playthrough and loved it. The game is so good, even the user interface has personality in abundance. It tells a solid story about a bunch of kids who are sick of corrupt adults getting away with, well, anything. You use an app on your phone (because 2017) and hunt down these people’s ‘shadows’ in an alternative sub-world that coexists with ours. Beating the living snot out of a shadow causes the real-world equivalent to have a ‘change of heart’ and repent all their terrible deeds. It’s an uplifting reminder that we all have the power inside us to bring about societal change, even when things look bleak.

persona_5Image courtesy of Atlus.

However, I had a couple of gripes. First of all, the plot reeeeeeally drags on this time around – I feel it had said all it had to say well before the closing credits, and beyond that were a couple of pointless plot twists and revelations that ultimately didn’t really matter. Second, the game hints at, but never truly explores, the idea that mob justice isn’t the answer and that the Phantom Thieves –  the protagonists – could be considered no better than the awful people they’re punishing. After all, while their intentions are noble, they often act out of a lust for more popularity. I feel the cast sweeps aside the notion that they’d be seen as criminals a bit too quickly, and I’d have liked for the idea to be explored further.

Despite all that, it’s a solid RPG overall. Last Surprise is the catchiest battle theme on Earth and as I mentioned, the battle interfaces have style and substance in spades.

Verdict: Stylish.

Minecraft: Nintendo Switch Edition (Nintendo Switch)

I’m fairly convinced by now that more people on the globe have access to a copy of Minecraft than clean water. So, it was inevitable that a Switch version would be coming, and it holds its own against the other console versions, albeit with smaller world sizes and draw distances. There’s not much else to add to the conversation about Minecraft that’s not already been said, but I did spend 250 hours over Summer playing a game I’d already played to death so it must be worth a look! It’s hands-down the best portable version of the game and the Better Together update, which adds cross-play between the Xbox and PC versions, is slated to come to Switch… at some point.

Verdict: The best portable Minecraft.

minecraft_mansionMy modest home.

The Legend of Zelda: A Link Between Worlds (Nintendo 3DS)

I bought this game in 2014 and, for whatever reason, flung it on my shelf straight away and never actually got round to playing it. Before summer rolled round, I made a resolve: I’d get through the unplayed 3DS (and DS) games I still had lying around in my collection. It’s a goal I did actually manage to fulfil! I’ve never played A Link to the Past (I know, I’m going straight to hell for that), but I gotta say – I was surprised by how much I loved this one. It feels very much like a retro throwback (because it is one), but it’s so expertly crafted that it feels like a modern title.

lbw_maiamaiSqueak! Squeak! I can hear them all now.

The game is extremely easy (I never saw the game over screen once), although increased difficulty doesn’t really do anything for me so I’m pretty neutral about that. Link’s signature new mechanic of superimposing himself on a wall results in inventive puzzles spanning Hyrule and Lorule, and I felt compelled to collect every last collectable for the first time in a Zelda game (no, I’m never getting BotW‘s 900 Koroks). I can’t pinpoint exactly why I found it so addictive to explore the overworld, but it’s certainly one of my favourite 3DS games. It was an absolute joy to play. Plus, the Maiamai are cute and I want a hundred of them.

Verdict: Squeak.

Splatoon 2 (Nintendo Switch)

When it was first announced, and when the Global Testfire was launched, I was worried that this might be far too similar to the originals. And yes, this sequel isn’t exactly a departure from the first Splatoon, but I think it’s fine to play it safe when the franchise is so young. Taking the same approach as Splatoon did with regards to updates – having a constant schedule of free content – is very welcome. Splatoon 2 continues to add new weapon classes, maps and even a new mode recently, and if it continues at this speed then by the time the 16th Splatfest rolls round, it’ll feel more like a sequel, albeit with a core experience that’s mostly unchanged from the first game. Essentially, the format isn’t broken or tired yet and the Switch is capturing audiences that the Wii U couldn’t. Why change things too much?

Verdict: Woomy.

splatoon_2_game3I had no screenshots of team-wipes to put here, unfortunately.

Fire Emblem Fates: Birthright (Nintendo 3DS)

I’ve only played two Fire Emblem games – this one and The Sacred Stones, courtesy of the 3DS Ambassador Program – and I very much enjoy the core gameplay of the series. However, as an entry that lives and dies with the quality of the story (after all, this game is split into three games pretty exclusively to tell the same story from three angles), I was bitterly disappointed (warning: there are spoilers coming up). It starts off strong, and barring a couple instances of the party going to off-the-beat locations because reasons, I felt it was building itself up to be a memorable and enjoyable story. Until Lilith died. A character that had not been with the main party since an early chapter, with no indication she’d be anything but an NPC inhabiting your home castle, suddenly shows up to do the whole “character jumps in front of would-be fatal blow to the main protagonist and sacrifices self” thing.

I bloody hate it when stories kill off characters for the sake of killing characters, especially if it’s just for the shock/tearjerker factor, but this one was pretty unforgivable as Lilith wasn’t even travelling with the party, she literally flies out of nowhere. How, then, do you think I felt when the game did the same thing later with Elise? Granted, she’d at least been travelling with the party, but there’s no reason for her death either and I just don’t buy it that Xander would accidentally strike her instead of the main character. C’mon guys, you already used up your deus ex machina quota.

Also, Chapter 21 can go die in a fire (it’s ironic, because it’s a lava level). You have to activate statues with your main character to clear a path, but picking the wrong side floods the arena with lava, slowing you down and spawning extra enemies. On top of that, you can’t clear the path for a few turns. There are three of these statues, and I got caught on all three of them, as it wasn’t obvious to me which side I had to pick (looking it up afterwards, I figured out it was the side of the statue holding an orb, but this wasn’t communicated properly). All taken into account, it’s undoubtedly the worst level of a strategy game I’ve ever played and it almost made me stop playing the game entirely.

birthright
You don’t say? I thought it was the field of daisies right over there.

Needless to say, I won’t be picking up the other two editions of this game. I wouldn’t play through that fucking lava chapter again if you paid me. The final fight with the big bad guy and the ending (well, 33% of it) redeemed the game slightly, but ultimately, the bad parts are too plentiful for me to really recommend it.

Verdict: Disappointing.

Pokémon White Version 2 (Nintendo DS)

When I played through Pokémon White back in 2011, it felt fresh, despite retaining practically all of the hallmarks of the series. It was like someone had sprayed the series with Febreze for the first time since the early 2000’s. I think it was the new batch of Pokémon and the reticence in using older generations of Pokémon that solidified it for me; for the first time, I didn’t encounter a gen 1 wild Pokémon every ten steps. Furthermore, it had a plot I genuinely felt engrossed in.

pkmn_white_2

Pokémon White 2, then, is a less fresh Pokémon White with a few more bells and whistles. That’s certainly not a bad thing, and it helped that I didn’t jump into this right after playing the first. Playing this after the slicker 3DS entries was somewhat strange, but not as jarring as I expected. I do wish I’d played this at launch though; between this and Pokémon Sun last year, I’m a bit burnt out on the series, so I probably won’t get round to Pokémon Ultra Sun or Ultra Moon for a while. I’m ecstatic that they’re bringing the series to Nintendo Switch, though.

Verdict: Just more Pokémon White.

Sonic Mania (Nintendo Switch)

I’m not even a fan of 2D Sonic games and I don’t know why I bought this. I played it for a bit and encountered a soft-lock glitch at the end of an inventive boss fight I loved, based on Dr Eggman’s Mean Bean Machine, because of course they stuck Puyo Puyo in this 2D Sonic game. Then, I reached an autoscrolling boss fight, got a bit mad with the inexplicable inertia on Sonic’s jump movement during that section, and put the game down. It’s a well-built game and I don’t wanna rag on it, as I can tell a lot of love and effort went into it, but it’s probably just not for me. At least I didn’t buy Sonic Forces instead.

Verdict: Faster than Fast RMX.

sonic_mania_softlockLook! A soft-lock! In a Sonic game! How surprising.

Stardew Valley (Nintendo Switch)

Life sims are the most dangerous kind of game. The number of real-world days I’ve wasted on games like Animal Crossing is unreal, so buying this was like signing up for a home delivery cocaine subscription service. It’s a good job that Stardew Valley is the most homely gaming cocaine on the planet. I love the characters and locations, and while the days initially felt short, I’ve learned to take things slow and complete tasks at my own pace. Farming is addictive, completing Community Centre bundles feels infinitely rewarding and forming bonds with townsfolk fuels their character progression in the form of special cutscenes.

stardew_valley_sebby

Also, I married the shy, cute guy and made him feel loved in a town where he felt invisible. Aww, this game is adorable.

Verdict: Homely.

Super Mario Odyssey (Nintendo Switch)

A system that launches with a game that gets 97 on Metacritic doesn’t usually need the help of other super-heavyweight titles to sustain momentum, just a few solid titles to tide it over into the next year. Nintendo disagreed and decided to release another game with a Metacritic score of 97 to round off the year. It’s not often that two games of that calibre come out in a single year, let alone two from the same developer. And Super Mario Odyssey deserves all the praise it gets.

smo_new_donk

After collecting all the Power Moons that matter, and getting some pretty stellar scores on some of the minigames (I’m modest, I swear), I’m still taken aback at how well-polished this game is. The Cappy captures are varied and immensely fun and moving Mario around the environments has never been more slick. Only Nintendo could pack so many gameplay features around a single mechanic – throwing Cappy – and only a Mario game could relentlessly throw idea after idea after idea at the player without feeling completely overwhelming.

smo_seaside

As anyone will tell you, New Donk City is the pinnacle of the experience. It’s a three-dimensional labyrinth of movement opportunities. Getting from A to B in this level gives you a billion different options, and, barring your first arrival in the city, the streets are completely free of enemies. It’s just a playground for you to test out Mario’s stunning new abilities by jumping between pretty much any pair of buildings.

I feel as if some Power Moons are so trivial that the diminish the accomplishment of obtaining the harder ones. If I had designed them, I’d have had two classes of moons – something like Mini Moons which litter the world, and the full-fat Power Moons for your harder challenges, especially some of the post-credits challenges.

mario_odyssey_pokio

I’m praying that Nintendo has Super Mario Odyssey 2 in development as we speak; while I’d love some kingdom DLC for Odyssey, I don’t think that cuts it for me. I’m gonna have to be a greedy bastard and demand that Nintendo makes me a no-holds-barred, premium, full-fat videogame steak with Mario and Cappy’s beaming faces on the cartridge. If I have to become a Nintendo shareholder and fly myself out to Kyoto for every shareholder meeting to force them into making it just to shut me up, then so be it.

Also, if you get more than 471 on the New Donk City skipping rope challenge, I’ll duel you on the top of New Donk City Hall at sundown.

Verdict: Super Star.