• 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.
Sign in to follow this  
Followers 0
mokaschitta

Fast way to calculate 2D signed distance field

8 posts in this topic

Hi,

I am currently searching for a fast way to compute a signed distance field in 2D. I can only think of the brute force method i.e. (pseudocode):

[CODE]
for each pixel in bitmap do
closest = max number
for each other pixel do
dist = distance(pixel, other pixel)
if dist < closest then
closest = dist
end
end
end
[/CODE]

Are there any obious optimizations? Did anybody maybe try to do it on the GPU? I know that the game pixel junk shooter computes a distance field each frame for dynamic collision objects so there has to be a way to get this pretty fast. Every idea, papers, links welcome!

Thank you!
0

Share this post


Link to post
Share on other sites
The fastest CPU algorithm I know is the one described in this paper: http://perso.telecom-paristech.fr/~bloch/ANIM/Danielsson.pdf
It is linear in the number of pixels (unlike the algorithm you describe, which is quadratic).
1

Share this post


Link to post
Share on other sites
Also I have an implementation of 3D signed distance transform in GPU Gems 2, chapter 8. You can find it here: ftp://download.nvidia.com/developer/GPU_Gems_2/CD/Copy%20of%20Index.html
The code is in dispmap/distance.cpp.
1

Share this post


Link to post
Share on other sites
The brute force way is slow, but accurate. As far as I can tell, all of the faster algorithms sacrifice varying degrees of accuracy to achieve their speed. While researching systems for rendering text for my current project I came across the following papers:

[url="http://infoscience.epfl.ch/record/86623"]http://infoscience.e...ch/record/86623[/url] (This link proposes a few corrections to Danielsson's technique)

[url="http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.102.7988&rep=rep1&type=pdf"]http://citeseerx.ist...p=rep1&type=pdf[/url] (The "dead reckoning" signed distance transform)

I ended up implementing the transform outlined in the second paper, which is pretty quick, but has a few flaws.
2

Share this post


Link to post
Share on other sites
Just an FYI, jump to page 43 of this: http://fumufumu.q-games.com/gdc2010/shooterGDC.pdf to get a better look at PixelJunk's approach(es). They settle on some pretty SPU-intensive algorithms, so it may be of limited use to you. They do reference a few methods though.

If you don't anything that accurate (i.e. just edge detection), then you can always do a simple blur for the field approximation near edges, or the iterative style (Only comparing immediately neighbouring cells) for just a few repeats can also give a similar edge detection. Basically, it's worth asking yourself - Do I *[i]need[/i]* the entire field, or just the edges? [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
1

Share this post


Link to post
Share on other sites
yeah I actually also thought about an iterative approach to circularily iterate surrounding pixels. that should be pretty fast in a pixel shader :). thanks for the paper, I will browse through it!
0

Share this post


Link to post
Share on other sites
Should it not be possible to create a good approximation in practically zero time by rendering a series of textured quads with additive blending? I'm considered unsigned now because it's conceptually easier to grok, but I guess signed works just the same by dividing by 2 and offsetting by 1/2.

The idea is this:
A distance map encodes the distance to the nearest set pixel (in a value from 0 to 255, assuming an 8 bit texture). Pixels that are "in" or very close to are white or nearly so, pixels that are far away are black. The closer they get to "in", the lighter a shade of grey they take.
Also, you could say that the closer a pixel is to "in", the higher the likelihood that at any uniformly distributed random sample from a disk with a certain radius will "hit" the "in" pixel.

Thus, enable additive blending, set the scale to 1/256, and render 256 fullscreen quads which are biased by an evenly distributed random offset within some small radius (ideally 128, I figure?). The 256 quads could all go into one vertex buffer and render with one draw call.

Pixels will end up brighter the closer they are to an "in" texel. If this is not accurate enough, use a 16 bit texture (or enable/disable writes to the 3 color channels of a typical RGBA8 texture one after another) and do the same thing a few thousand times.

Graphics cards are ridiculously fast at drawing textured quads. On a "typical" texture size of, say 512x512, I would expect drawing a few hundred textured quads at several hundred FPS, even on a not-so-high-end-card.
1

Share this post


Link to post
Share on other sites
hah, crazy idea! Not sure if the method is accurate enough for collision detection though, but its certainly a creative thought!
0

Share this post


Link to post
Share on other sites
Hey, mokaschitta, have you solved your problem yet and what did you end up using? I'm currently looking for a way to create a 2D signed distance field, too, but all those papers are awfully long and hard to understand. I hope you can shed some light on all of this.

Greetings
0

Share this post


Link to post
Share on other sites

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
Sign in to follow this  
Followers 0