Page 1 of 2

Zephyris' Procedural Buildings Tool

Posted: 09 Nov 2010 01:24
by Zephyris
So here's something a bit different:

I have decided I was bored of drawing buildings and wanted a tool that could draw them for me, so I am working on a procedural building generator! This, at the moment, is made up of two halves; one half generates the building structure (including construction stages and the four orientations) based on a simple numerical description of the building's storeys and roofs. The second half procedurally colours the building with the TTD palette. It is all still under heavy development but it's (IMO) quite impressive so I wanted to show off how powerful it could be!

Here is an example building definition:

Code: Select all

storeyheight=6;
windowwidth=3;
windowheight=3;
windowbaseheight=2;
windowspacing=2;
buildingdefinition=newArray(
	0, 1, 8, 12, 22, 8,
	1, 2, 16, 8, 2, 20,
	0, 1, 16, 24, 2, 4,
	1, 3, 8, 12, 22, 8,
	1, 1, 16, 16, 2, 4,
	2, 2, 2, 16, 2, 4,
	2, 1, 14, 16, 4, 4,
	3, 3, 14, 16, 4, 4
);
auxdefinition=newArray(
	1, 0, 1, 1,
	0, 0, 0, 0,
	0, 1, 1, 1,
	0.7, 1, 0, 0,
	1, 0, 1, 0,
	0, 0, 0, 0,
	1, 1, 1, 0,
	0.7, 0, 0, 0
);
The two big arrays contain most of the info about the building structure. The building definition array has 6 entries per line which define the following:

Code: Select all

s, t, w, d, x, y
s=storey (i.e. 0 (ground), 1 (first), 2 (second), etc.)
t=type (1=walls, 2=flat roof, 3=sloped roof)
w=width (in 3D)
d=depth (in 3D)
x=x location (in 3D)
y=y location (in 3D)
The aux definition array has 4 entries per line which define which walls have windows in for walls, the height and orientation of a sloped roof, and will be where any other future variables will sit.

This set of instructions gives the following building:
Building1.png
Building1.png (3.37 KiB) Viewed 5420 times
This can be fully "proceduralised" with some random numbers...

Code: Select all

gwa=randomRangeEven(12, 20);
gwb=tilew-gwa-2;
bya=randomRangeEven(0, 8);
byb=randomRangeEven(0, 8);
gda=randomRangeEven(16, 32-bya);
gdb=randomRangeEven(16, 32-byb);
uda=randomRangeEven(8, 14);
udb=randomRangeEven(8, 14);
bxa=0;
bxb=gwa+2;
buildingdefinition=newArray(
	0, 1, gwa, gda, bxa, bya,
	1, 2, gwa, gda-uda, bxa, bya+uda,
	1, 1, gwa, uda, bxa, bya,
	2, 1, gwa, uda, bxa, bya,
	3, 3, gwa, uda, bxa, bya,
	0, 1, gwb, gdb, bxb, byb,
	1, 2, gwb, gdb-udb, bxb, byb+udb,
	1, 1, gwb, udb, bxb, byb,
	2, 3, gwb, udb, bxb, byb
);
auxdefinition=newArray(
	1, 1, randomBinary(), 1,
	0, 0, 0, 0,
	1, randomBinary(), 1, randomBinary(),
	1, randomBinary(), randomBinary(), 1,
	0.7, randomBinary(), 0, 0,
	1, 1, randomBinary(), 1,
	0, 0, 0, 0,
	1, randomBinary(), randomBinary(), 1,
	0.7, randomBinary(), 0, 0
);
These procedural instructions give, for example, the following buildings:
ProcBuilding2.png
ProcBuilding2.png (3.94 KiB) Viewed 5420 times
ProcBuilding3.png
ProcBuilding3.png (3.89 KiB) Viewed 5420 times
A few sets of procedural instructions should be able to generate a very wide range of buildings, from terraced houses to skyscrapers, extremely quickly and consistently... It would also make generating regional variations of architecture style (e.g. typical roof slopes, building size, window shape/size, etc.) for a building set really simple!

Re: Zephyris' Procedural Buildings Tool

Posted: 09 Nov 2010 01:28
by Zephyris
The second half of the program colours the building, and does so in a very similar drawing style to my existing OpenGFX and UK town set buildings... Using the example building from above here are some typical results that pop out:
Building1a.png
Building1a.png (14.36 KiB) Viewed 5418 times
Building1b.png
Building1b.png (12.23 KiB) Viewed 5418 times
Building1c.png
Building1c.png (15.55 KiB) Viewed 5418 times
The colouring is all based on random or customised base palette entries for the walls, roof, floor and windows, then is shaded and textured in 8bpp by some custom tools I have written. Other neat tricks it can do are vertically and horizontally (taking into account perspective) striped wall textures.

Re: Zephyris' Procedural Buildings Tool

Posted: 09 Nov 2010 01:31
by supermop
This is great! I've been wanting to do a metabolist building set once i get my shed grf tidied up, and this is perfect (from a metabolist architecture perspective - i kind of like drawing the buildings).

I'll be watching this space!

Re: Zephyris' Procedural Buildings Tool

Posted: 09 Nov 2010 08:14
by kamnet
Niiiiiiiice! :bow:

Re: Zephyris' Procedural Buildings Tool

Posted: 09 Nov 2010 14:03
by Purno
This is cheating. How can a coder possibly keep up with the speed you're creating sprites? :mrgreen:
Just kidding

Interesting tool. Very interesting.

Re: Zephyris' Procedural Buildings Tool

Posted: 09 Nov 2010 14:50
by Ammler
Purno wrote:How can a coder possibly keep up with the speed you're creating sprites?
I would assume, it does create the code too :-)

(looks almost like nml code already anyway)

Re: Zephyris' Procedural Buildings Tool

Posted: 09 Nov 2010 15:04
by planetmaker
This looks very impresive and awesome, Zeyphris! I'm looking forward seeing it in action :-)
Purno wrote:This is cheating. How can a coder possibly keep up with the speed you're creating sprites? :mrgreen:
Not a problem. The sprites are all aligned in the same pattern, thus coding can be just as fast :-)

Re: Zephyris' Procedural Buildings Tool

Posted: 09 Nov 2010 19:08
by cmoiromain
Sir, you are a pure genius. This is mindblowing!

Re: Zephyris' Procedural Buildings Tool

Posted: 10 Nov 2010 00:31
by Zephyris
Thanks for all the positive comments! Heres a little update on the colouring aspect - multiple colour schemes per tile (defined on a per storey/roof basis, max 8 colour schemes). The colour schemes are encoded in the building structure by grey shade, e.g. the outer rear wall is 128 for colour scheme 1, 129 for colour scheme 2, 130 for colour scheme 3, etc, then coloured appropriately by the colouring program.
Building.png
Building.png (4 KiB) Viewed 5141 times
BuildingCol.png
BuildingCol.png (19.66 KiB) Viewed 5141 times
I think the next fight is going to be accurate z sorting of the various building objects. It is working at the moment, but is quite hacky and I'm sure it will fail with some cases; it's really hard to get your head round! I'm also not exactly sure what to do for generation of the ground tiles for house-like buildings. And chimneys.
Purno wrote:How can a coder possibly keep up with the speed you're creating sprites?
I do plan to automatically generate some useful code to speed up coding of sprites. I was planning to just dump the sprite location and x/y offsets and image file name in the form of the real sprites for the building... Would any other code be useful? (bounding box? whatever those are :) ). TBH I have never coded buildings and I'm not quite sure what is needed.

Re: Zephyris' Procedural Buildings Tool

Posted: 10 Nov 2010 01:36
by Zephyris
One more treat for tonight... Procedural step-back skyscrapers building structures.

You definately want to view the attachments :)
Building1.png
(32.79 KiB) Downloaded 2 times
Building2.png
(20.47 KiB) Downloaded 2 times

Re: Zephyris' Procedural Buildings Tool

Posted: 10 Nov 2010 02:04
by Yexo
Very impressive work Zephyris. With regards to coding: a filename, x/y offset for every sprite (unless it's always the same), width/height of every sprite (again: unless it's always the same). The bounding box could be the same for every house except for the height, which could be computed from the height of the sprite. If you can provide this information in a machine-readable format I'll write the code / write a generator for the code. This work is too good not to be used because it's not coded.

Re: Zephyris' Procedural Buildings Tool

Posted: 10 Nov 2010 03:07
by supermop
I know its a stretch, but could some of the workings of this be manifested in a newgrf? perhaps to create random buildings/vehicles in in game?

Re: Zephyris' Procedural Buildings Tool

Posted: 10 Nov 2010 07:49
by Kogut
supermop wrote:I know its a stretch, but could some of the workings of this be manifested in a newgrf? perhaps to create random buildings/vehicles in in game?
Rather no ingame, but it may be possible to integrate it with code generator and NML compiler to gen program that will generate new newgrf every time.

Re: Zephyris' Procedural Buildings Tool

Posted: 10 Nov 2010 09:38
by planetmaker
Yexo wrote:Very impressive work Zephyris. With regards to coding: a filename, x/y offset for every sprite (unless it's always the same), width/height of every sprite (again: unless it's always the same). The bounding box could be the same for every house except for the height, which could be computed from the height of the sprite. If you can provide this information in a machine-readable format I'll write the code / write a generator for the code. This work is too good not to be used because it's not coded.
I'd like to co-sign this pledge, too

Re: Zephyris' Procedural Buildings Tool

Posted: 10 Nov 2010 09:57
by Zephyris
Kogut wrote:
supermop wrote:I know its a stretch, but could some of the workings of this be manifested in a newgrf? perhaps to create random buildings/vehicles in in game?
Rather no ingame, but it may be possible to integrate it with code generator and NML compiler to gen program that will generate new newgrf every time.
Maybe I should have written the scripts in something more portable :) At the moment it is in the ImageJ (my favourite open source scientific imaging program) macro language. I'm sure it could be ported to something more sensible with relative ease, it is a very conventional syntax.
Yexo wrote:Very impressive work Zephyris. With regards to coding: a filename, x/y offset for every sprite (unless it's always the same), width/height of every sprite (again: unless it's always the same). The bounding box could be the same for every house except for the height, which could be computed from the height of the sprite. If you can provide this information in a machine-readable format I'll write the code / write a generator for the code. This work is too good not to be used because it's not coded.
Sounds like a good offer :) I'll keep these variables in mind when coding the generator, I should be able to generate some example numbers in the near future...

Re: Zephyris' Procedural Buildings Tool

Posted: 10 Nov 2010 10:02
by planetmaker
Zephyris wrote:Maybe I should have written the scripts in something more portable :) At the moment it is in the ImageJ (my favourite open source scientific imaging program) macro language. I'm sure it could be ported to something more sensible with relative ease, it is a very conventional syntax.
ImageJ is definitely portable ;-) and it's not like its syntax looks like totally incomprehensible, even though I haven't yet written any scripts for it. A pro would also be that ImageJ is quite well documented.
So unless you find a good reason to move away from it... keep it simple and stick to it. :-)

Re: Zephyris' Procedural Buildings Tool

Posted: 08 Dec 2010 23:59
by Zephyris
Just so you call know I am still fiddling with the generator trying to get the results as good as possible... It is, unfortunately, a classic 80%/20% thing; 20% effort to get 80% finished, 80% effort to get the last 20% sorted! Hopefully I can get it finished at some point though :)

I've also done some stuff on a procedural tree generator... Any thoughts? It makes growth stages dead easy to generate, it would also make creating season variations (if OpenTTD ever supports that, hint hint) of the trees very simple.

Re: Zephyris' Procedural Buildings Tool

Posted: 09 Dec 2010 09:04
by andythenorth
Zephyris wrote:I've also done some stuff on a procedural tree generator.
Looks good.

Re: Zephyris' Procedural Buildings Tool

Posted: 09 Dec 2010 09:42
by Kogut
Can you generate next pack of trees?

Re: Zephyris' Procedural Buildings Tool

Posted: 09 Dec 2010 13:31
by Zephyris
Kogut wrote:Can you generate next pack of trees?
What do you mean? A new tree set? That is the plan!