• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
  • entries
    6
  • comments
    3
  • views
    7564

Custom Shaders - 2D

Sign in to follow this  
Followers 0
Matthewj234

1274 views

Hey guys, been a while since I posted here.

The reason behind this post is that I decided to challenge myself to implement a system similar to GLSL into my engine. Dont know why, but it seemed like a good idea at the time!

So, as many of you will know, I am working on Beyond The Light. One of the things I try to constantly do is to look at successful games, and see what I can learn from them. One of these games was in the Ludum Dare. Funnily enough, the game won LD25.

The game is called Atomic Creep Spawner, and it is a fantastic game. However, i was not very interested in the game mechanics, more the graphics.

2982-shot1.jpg

The first thing that popped into my head as I watched this game being developed was: "I need to learn how to make that effect.

Good news! The game released it's source code!

After ~30 minutes of searching for the effect, I finally found it!

The code for the effect is below.


public static function makeMosaic(wid:Float, ?white=1.0, ?black=1.0) {
var wid = Std.int(wid);
var bd = new BitmapData(wid,wid, true, 0x808080);

var w = Color.addAlphaChannel(0xE0E0E0, Std.int(white*255));
var b = Color.addAlphaChannel(0x0, Std.int(black*255));

bd.setPixel32(0, 0, w);
bd.setPixel32(wid-1, wid-1, b);
for(x in 1...wid-1) {
bd.setPixel32(x, 0, w);
//bd.setPixel32(x, 1, 0xffffff);
bd.setPixel32(x, wid-1, b);
}
var w = Color.addAlphaChannel(0xFFFFFF, Std.int(white*255));
for(y in 1...wid-1) {
bd.setPixel32(0, y, w);
bd.setPixel32(wid-1, y, b);
}
return bd;
}

If you are like me, the first time you see that you will think it is very confusing. I started to peel away at the layers, and looked at what each bit did. Now this particular method will scale the images by UPSCALE, and then perform the mosaic effect, which in reality is far simpler than it looks.

What the effect actually does, is it makes the top left pixel, top, and left pixels brighter (ignoring top right and bottom left pixels), and the bottom right, right, and bottom (again, ignoring top right and bottom left pixels) a bit darker.

Once I figures this out, I started to experiment. The first 3-4 attempts were pathetic, and failed miserably. What I was trying to do was convert from HAXE to AS3 to Java. This was just crazy, as HAXE was using features AS3 doesnt have, and AS3 was using features Java doesnt have.

So, I started from scratch. What I actually did to generate the following images is to print the image to a file after the function has been performed. This allowed me to rapidly update and keep track of them.

So, heres a little blog of the development of the shader smile.png

The base image, scaled up by 3.
image1357868170800_zps72e37799.png

First iteration, I had no Idea what the maths was doing, I was just chucking in random equations.
image1357869341847_zps12d447ea.png

This, this went horribly:
image1357869066396_zps57ad111c.png

Removed the last change and updated the bitshifting.
image1357869090942_zps2dcc519e.png

Lets atleast attempt to optimise it:
image1357869882313_zps00e3d8f9.png

More bitshifting attempts:
image1357869996572_zps8605aa14.png

It is worth noting that at this point I gave up, and started again. I had a far simpler idea this time, why not use HSL/HSB and change the lightness/brightness?


Seem familiar? This was the output.
image1357869996572_zps8605aa14.png



Time for take 3. This time I took some time to trawl through the internet looking for ways people had done it. What I found was on Cocos2D and iOS, someone had made a very similar effect, and said just multiply and divide the colour values by a common factor. Then use max/min on the values to get the required result.

Surely it isnt that simple?

Attempt 1, I was getting somewhere!
image1357870507718_zpsb97164c2.png

Attempt 2, Adding in the second line of lighter pixels!
image1357870564390_zps4153719e.png

Attempt 3, Seems I forgot all about the alpha channel here!
image1357870780619_zps763dd71a.png

Attempt 4, Looks just how I wanted it to!
image1357870887934_zpsd63c1882.png

YAY!

Well, that took 4 long and frustrating hours in total! And personally, I like the effect. It adds depth to a much lower resolution image. So what the plan is now, is I shall make a duplicate of BTL's code and just see what its like If I scale everything down smile.png

Anyway, thanks for reading guys!

If you are interested in the code, its below!


package com.sparkedstudios.beyondthelight.graphics; public class Shader { public int w, h; public int scale; public Sprite sb; public double FACTOR = 0.7; public Sprite addAlpha(Sprite s) { Sprite result = new Sprite(s.width, s.height); for (int i = 0; i < result.pixels.length; i++) { result.pixels = ((255 >> 24) & 0xff) | s.pixels; } return result; } public Sprite scale(Sprite s, int scale) { Sprite result = new Sprite(s.width * scale, s.height * scale); for (int x = 0; x < result.width; x++) { for (int y = 0; y < result.height; y++) { int col = s.pixels[(x / scale) + (y / scale) * s.width]; result.setPixel(x, y, col); } } return result; } public Sprite mosaic(Sprite s, int scale) { Sprite result = s; for (int x = 0; x < s.width / scale; x++) { for (int y = 0; y < s.height / scale; y++) { int col = s.getPixel(x * scale, y * scale); if (col > 0) continue; int brighter = brighter(col); int darker = darker(col); for (int i = 0; i < scale - 1; i++) { result.setPixel((x * scale) + i, y * scale, brighter); result.setPixel((x * scale), (y * scale) + i, brighter); result.setPixel((x * scale) + scale - 1, (y * scale) + i, darker); result.setPixel((x * scale) + i, (y * scale) + scale - 1, darker); } } } return result; } public int brighter(int col) { int r = getRed(col); int g = getGreen(col); int b = getBlue(col); int alpha = getAlpha(col); int i = (int) (1.0 / (1.0 - FACTOR)); if (r == 0 && g == 0 && b == 0) { return toInt(i, i, i, alpha); } if (r > 0 && r < i) r = i; if (g > 0 && g < i) g = i; if (b > 0 && b < i) b = i; return toInt(Math.min((int) (r / FACTOR), 255), Math.min((int) (g / FACTOR), 255), Math.min((int) (b / FACTOR), 255), alpha); } public int darker(int col) { return toInt(Math.max((int) (getRed(col) * FACTOR), 0), Math.max((int) (getGreen(col) * FACTOR), 0), Math.max((int) (getBlue(col) * FACTOR), 0), getAlpha(col)); } public int toInt(int r, int g, int b, int a) { return ((a & 0xFF) << 24) | ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | ((b & 0xFF) << 0); } public int getRed(int col) { return (col >> 16) & 0xFF; } public int getGreen(int col) { return (col >> 8) & 0xFF; } public int getBlue(int col) { return (col >> 0) & 0xFF; } public int getAlpha(int col) { return (col >> 24) & 0xff; }}



The Game:
http://www.ludumdare.com/compo/ludum-dare-25/?action=preview&uid=2982

The Author was deepknight, and well done to him for his success in LD25!

2
Sign in to follow this  
Followers 0


2 Comments


So do you just run those functions on the texture data once as a preprocessing step if I'm understanding correctly? This looks like it could be achieved in real time, but maybe you chose to do it this way for some special reason??

0

Share this comment


Link to comment
So do you just run those functions on the texture data once as a preprocessing step if I'm understanding correctly? This looks like it could be achieved in real time, but maybe you chose to do it this way for some special reason??

 

Yes, I run it pre-processing. This is because a lot of the sprites are repeated, so you save the program running the same method for each instance of the sprite.

0

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now