Friday, January 11, 2013

Google you still don't get this

Nothing to do with procedural generation, but maybe a bit about artificial intelligence.

Almost two years ago I was complaining about Google's lack of intelligence in matching ads to content. A bit later I was doing posts about tree and forest generation and Google kept serving ads about gardening and genealogical trees.

You would expect two years would have made a difference, then a week ago I started posting about T.I.L.E.S. These are the ads I get now:


Just makes no sense to me. With all the tracking and understanding they do, just a few words are enough to get them off the scent.

I'm glad they got Ray Kurzweil and all, but I begin to wonder whether the Singularity will be just sub-par intelligence at a massive scale. We are bracing for this:



When what we may be getting is this:



Thursday, January 10, 2013

Tile Genetics

This is a continuation of an earlier post which was some kind of introduction to Wang tiles. These tiles allow you to cover an infinite plane using a small set, and you can do it in a way that makes repeating patterns a lot less noticeable.

I was recommending everyone to learn about them because there are many problems in procedural generation that lend very well to aperiodic tiling. But the approach in vanilla Wang tiles was only the beginning  This same principle can be applied in many different ways. For instance, here is a brilliant approach to doing rooms using what the author has called Herringbone Wang Tiles. Here you can see how they were used to generate a system or corridors and rooms.


You can find a description of the method here.

There is also a quite popular improvement over classic Wang tiles which uses colored corners instead of colored edges. It results in smaller sets, more diversity and of course their main claim, which is to solve the "corner problem" in Wang tiles. This problem arises from the fact Wang tiles constrains which tiles can be placed along the edges, but says nothing about the corners, that is, the diagonal directions: NW, NE, SE and SW. This often creates noticeable artifacts in the tiling. Corner tiles still suffers from this. The discontinuities are now pushed to the middle of the edges but the problem is somewhat smaller since only two tiles meet there, compared to four tiles in the corner for vanilla Wang.

And there is this very popular adaptation of the Wang algorithm to the GPU in GPU Gems 2. I found this nice tutorial about how to create textures for it. This is from the game Path Of Exile, which appears to use Wang tiles extensively with great results.

As I was saying in my earlier post, the real challenge with all these methods is to come up with the content for the tiles. One approach is to synthesize the content of the tiles entirely. This way you are always in control of what happens at the connection points (corners, edges, etc. depending on which tile flavor you have picked).

This is how I built the cave system for the realtime engine. The crypt profiles, along with narrow tunnels, are drawn by a program. This program makes sure everything connects properly. Here is an example of the output;



The dots inside the large space are massive columns. These are in the tiles as well.

This is not difficult and provides good results as long as you are targeting simple shapes, something you can synthesize. Problem is, in most cases like with textures, you want to use samples you have taken from the real world and use them for the tiles.

If you read the Path Of Exile tutorial I linked before, you will see that this is not a simple task. A lot of work and skill was needed to produce good looking tiles. And this method is only for two colors, that is, the simplest set possible. Imagine if you wanted to have 10 different colors. It would be madness.

There are ways to automate this. If you have an original sample that is large enough, you can take different portions of it and assemble the texture set. Here is a fairly good description of the method (PDF). There is a lot of Frankenstein-esque ingenuity involved, as you must stitch different parts together, hoping the texturing discontinuities will remain small. This produces fairly good results if the original texture is homogeneous, but in most cases "hoping" is not good enough.

I tried this method for terrain synthesis. As a source I used a heightmap with some dramatic features in it. Here you can see a sample:


As the original sample was far from homogeneous, which is after all what you want in interesting terrain, I quickly saw that just one iteration of the method was not enough. This was an optimization problem, somehow I had to minimize the discontinuities produced by the tiles. It is the kind of problem you could solve using genetic algorithms.

You may feel you are entering the mad scientist realm, growing human ears out of mice, playing God with bits, and in fact you are.

The fist step was to design a DNA sequence. Each possible solution (an individual) can be defined as the border and tile coordinates within the sample bitmap. I was doing a set of 200 tiles, with 10 borders in each direction. That resulted in a few hundred bits of DNA.

Next I would do the first population of individuals, which I kept at a thousand. Each individual was randomly generated, just by flipping bits in the DNA.

Next a "fitness" coefficient is computed for each individual. This is a number that indicates how good the individual is as a solution. In my case it was simple, as I could compute the amount of discontinuities the solution had. I also threw in a histogram comparison with the original source, just to make sure all the tones in the original were kept. Adding these two errors gave me a direct measure of the quality of the individual.

Using this measure I could rank the entire population and throw some kind of sexy party. The hot individuals got to reproduce the most, but even the not-so-good ones got their chance to immortalize their seed. This makes sense because a very undesirable trait could become the base of a very nice feature in a future generation. Even the Great Mutato could be the beginning of something beautiful.

I used a very specific way of mixing the DNA, which is called "one-point crossover". It gets a random portion of the father's DNA and the remainder space is copied from the mother's DNA.


This way a new generation is formed. But only 80% of the new members are descendants of existing individuals. The remaining 20% is generated randomly generated, just to make sure there will be enough genetic variety. And of course there is mutation. Each children would have some of its DNA bits randomly flipped.

Watching these things evolve and get better over time is mesmerizing. But it was kind of slow. I realized a needed a massive approach to it. I split the logic into two sections. The part keeping track of the population, doing crossovers and mutations ran in a single process. Then the part that evaluated the fitness of an individual, which was the time-consuming part, ran in a little army of processes over a LAN. (I used good old TCP/IP for distributing the work, ZeroMQ evangelists please go away)

Now this thing ran in eight machines, averaging 3 cores. That is roughly 24 instances of the fitness evaluator. After a week running, I started seeing solutions that I could use. For instance, this image is the result of tiling one of these individuals:


This may seem like a success story, but to me it does not feel like that. The amount of effort and energy that goes into producing tilesets may not justify the advantages you get out of them. Even after weeks of running, most of the individuals still produced noticeable discontinuities.

I know in my case there is a lot of room for improvement. The cutting strategy for mixing tiles with borders can be improved a lot. One thing I want to try it to use patterns in the texture to govern the cutting path. This should mask the transitions even more natural.

The bottom line for me is, Wang tiles are amazing things until you try to use them seriously. They work great for stuff you can synthesize from the ground up. If you are trying to mix samples from real life, get ready for some trouble.



Thursday, January 3, 2013

Introduction to Wang Tiles

This is guaranteed to be a very colorful post.

Since the early days of computer graphics, texturing has been the favorite method to bring detail into 3D surfaces.

The problem with textures is they take a lot of memory. While there are some attempts to have 3D worlds uniquely textured, like Id's Rage, the traditional approach remains to tile a smaller rectangle of texture across a much larger 3D surface.

This certainly does the job, but it also brings out these repeating patterns we dislike so much. There are a number of ways to mask the repetition, sometimes by blending more than one texture, or by adding splats of unique detail in strategic places.

Another approach is to use Wang tiles. If they are new to you, once you are done reading this you should Google them up and get soaked on what they are and how they work. You will love learning about this. It will expand the way you look at problems in the future. Wang tiles are much more than just a method to avoid repeating textures. You could use them to generate terrain, geometry, architecture. If you get carried away, you'd see they are actually full fledged computing devices on their own.

You could say we have used them already without knowing, as the traditional way we tile textures is one specific case of Wang tiles.

But forget Wang tiles for a moment. The way you would normally texture a brick wall is to use a 2D image and let the GPU do the tiling. If you just take a picture of a brick wall in real life and use it, you would see there are very noticeable discontinuities where the edges meet. The colors on the North edge of the image do not connect properly to the colors in the South edge, and the same happens with East and West.

This is very noticeable in the following polka dots texture:


This is easily solved by making the texture "seamless". What it involves is to make sure the West and North edges match the East and South edges respectively. In most cases this is done manually, using a tool like Photoshop.

Here you see the fixed texture:


Problem solved. But this texture still has very noticeable repeating patterns. If you tile it long enough they are hard to miss.

Here comes the twist. We made this texture work by making sure edges connected properly. We have one edge that is shared horizontally, and another one that is shared vertically:

So what is inside the tile does not matter as long as the edges connect properly. Instead of just one texture we could use several, all sharing the same edges. Here is an example:


We could pick which tile to use randomly. This will eliminate the repeating patterns coming from the center area of the tiles. But we would still have some repetition coming from the edges.

Here enters Mr Hao Wang. He predicted what we are about to do was not possible, and for being wrong this method bears his name. In reality he did all the heavy work so it is more than deserved. Here is a picture of Hao Wang winning the 20 Km men's race in Chihuahua:


Actually that is not Wang, but it is the spirit what counts.

Wang tiles allow us to have more than just one vertical and one horizontal border. You can choose to have as many as you want. As soon as you know how many different borders there are, the method tells you how many tiles you need so you can tile them all you want without leaving any holes.

Let's say you choose to have two horizontal borders, blue and yellow, and two vertical borders, red and green. Here is a resulting set of 16 tiles:


This is what the tiling authorities call a "complete set", meaning it has all the possible tiles. It contains every combination of every combination of colors. This is how you would compute the number of tiles in a complete set:

Where N is the number of horizontal borders and M is the number of vertical ones. Hence 2 horizontal and 2 vertical make a set of 16 tiles. Now these numbers explode quickly. For 3 colors in each direction you now have 81 tiles. For 4 colors it would be 256 tiles. Four colors do not provide too much variation along the edges, still the number of tiles is huge. If you had to store this info in video memory it would be a problem.

Thankfully the math guys proved you do not need a complete set to achieve full tiling. A much more smaller set exists which can be created in the following way: For each combination of North and West colors, you need at least two tiles. Now the tile count becomes:

This is a lot more manageable. Two borders on each direction requires eight tiles. Four borders, 32.

Once you have your tile set, you can use it in different ways. When time comes to place them, there are a few simple rules to follow. You start from the top-left corner, North-West. At this point you can place any tile from the set, picking one at random will do.


The next tile you place must match the existing one. You can still pick a random tile, but its West side must match the East side of the previous tile. You can continue to do so for the first row:


When starting the second row, you will have to match the South border of the tile on top, in this case you can pick any tile in the set that has a green North.


And then for the rest of the row, you will find tiles that match both the North and West color requirements:


At this point you are ready to go on tiling forever:


While all this seems great and guaranteed to make you interesting at parties, the difficulties with Wang tiling are not in picking edge colors and using the sets. The real challenge is to come up with content for the tiles. In my case I had to resort to genetic algorithms running many instances over a LAN.

I will cover this in my next post.



Thursday, December 13, 2012

Golem Interview

I was interviewed recently by the Golem magazine. They were researching for an article on procedural generation. The article is in German.

 http://www.golem.de/news/prozedurale-schoepfung-kuenstliche-welten-auf-knopfdruck-1212-96323.html 

Only 7% of ProcWorld readers come from Germany. For the English readers I though it would be nice to post the original round of questions I got from Golem, along with the answers:

What are you working on at the moment? What are you most proud of in that regard? 

 
Right now I am working on real-time architecture. Buildings are generated as you approach them. It is challenging for many reasons. First you need ways to teach the system how to generate interesting buildings. Then you need to make it happen really fast. It is the worst combination possible: a hard problem that needs to be solved in very little time.

I am proud of the potential. It is not obvious right now, but as the hardware continues to grow this platform could produce incredibly rich environments.

What does procedural generation mean for artists? How will it transform their job - and how will they have to adapt to benefit?

Robots are coming for our jobs everywhere, even journalism is being automated (you could be a robot for all that I know). The key question is how creative your job really is. If you perform unimaginative tasks, even as a "creator", odds are you will be replaced by some technology. Artists are no exception. How to adapt? Be more human, less robotic :)

What makes procedural generation important for the future? What will it enable?

It helps looking at the past first. I see three ages in procedural generation. The first coming was when storage and delivery was very limited, so there was an advantage in generating stuff on-the-fly. This is when games like Elite first appeared. We were very forgiving as an audience back then. You could throw a few points on screen and call it a galaxy.

Then hardware grew to a point where more realistic representations could be stored and rendered. At this point it became possible to pack quality artwork into a product. This gave us the second age, where most content was hand made. We are still in that era. Consumer grade hardware, like current console generation, is not able to produce content that is rich enough to challenge human-created content. There is some proceduralism there and there, but it is relegated to the background.

If Moore's law continues to hold, we will enter a third stage. Hardware is growing at a faster pace than artist output. The industry needs to keep moving the new hardware, but it is becoming prohibitively expensive to do it in the traditional model. Automation is the only choice.

What will be the future? You can think of a variation of the Turing's test: A human plays a game without ever realizing most of it was created by a computer.

In what way could it make gaming better? Or worse? ;-)

I think it will produce a lot of horrible games. Look at what the auto-tune has done for music. But then it will produce some unique experiences.

I also think we should not worry about that. Imagine a house is burning and the people inside are wondering whether it is sunny outside. It does not matter because they have to come out anyway. Automation is inevitable in the games industry, otherwise the whole industry will stagnate and burn like the house. Whether it will be worse or better, makes no difference.

What are the worst problems - from a programming and an artistic standpoint?

Let's say you were building a game the traditional way. You would have one guy doing the story, some guys do concept art, some other guys do the actual environments, level design, others will create and place props and so on. In theory all of these tasks can be automated.

The reality is some of these tasks are a lot simpler. For instance the guy modeling rocks can be easily replaced, but what about the one doing the concept art or the storyline?

There are some tough problems there. They are on the fringe of AI and we have no solutions for them today. But then once you look at what we can do with automated translation, self-driving cars, and so on, we may not be far from that. There is also the advent of "the cloud" as a computing device. I think that changes everything.

What do you think has been the most important progress to procedural generation during the last years? An what can we expect from the future? And how important are hardware and 3D engines for the further advance of procedural content?

The hardware has evolved to a point where some cool things are now possible. We have known about them for many years. It was just too slow to try them.

There has been a lot of research in synthetic reality, for a long time now. The real bottleneck has been and continues to be the hardware. Look at the movie Avatar for example. It is a procedural world what you have there, still it costed millions to generate. This has to come to the point where an equally rich world could be generated by your home console or computer, or by cloud services you can afford.

Monday, December 3, 2012

Videos of Caves and Buildings

Here is a series of videos I captured over the weekend.

The first one shows a bit of the architecture, also how a couple of useful underground passages can save you time while travelling:



The second is a trip I did to get close to a tower I saw. It seemed simple at the beginning, but the terrain had some severe accidents so I had to figure out my way. I found some caves that helped me get there, also saw I needed to do a quick bridge over a chasm. It runs at double the speed so you would not get bored by all the wandering. Also note that at the end of the video I carve some holes into the tower walls.



The next video also runs at double the speed. It shares the same beginning as the first video posted here, but goes a lot farther.



And the last video shows how the caves can sometimes go really deep underground. You often see cave openings into the surface, but also they could be right under your feet and you would not know. I cheated in this video by showing the wireframe, otherwise it would have taken an awful lot of time to find these caves.