Saturday, April 29, 2017

The curious case of Ecstatica elipsoid game engine by Andrew Spencer - Instancing, Z-buffer, Ambient occlusion and elipsoids in 1994!

So this other day (yesterday) I was mindlessly clicking on Youtube videos when a very interesting video suggestion popped up. It was for a Speedrun of a game called Ecstatica - a game I was familiar with because my brother once brought it home on a few floppy disks.


I watched it for about 4 minutes and I immediately knew I have to play this game once again. Mind you I was only about 10 years old when I played the game on our beloved 486 DX2 and these are the thoughts in sort of "tag" format I remember:

- Very Creepy
- Extremely tough enemies
- No idea what to do
- Interesting "blobby" graphics compared to Alone in the Dark II (played it before this game)
- NO SOUND because we didn't own a soundcard back then
- Interesting mix of real-time graphics and pre-rendered backgrounds (my brother was in the demoscene so by watching him code stuff I already had a good guess for what can be real-time and what not)

I also already had some experience with 3D Studio R3 (it somehow ended up on a few floppy packs on my desk) which is something that even deepened the experience for me as I marveled on about the background graphics and I remember I tried to reproduce some of the stuff I saw but never came close.

So anyway I never came even close to beating the game - got stuck pretty early and lost patience with myself so I turned to other games I was better at (I also failed badly at Alone in the Dark II).

Fast forward to yesterday and the first screens after installing it with DosBox amazed me... just look at this marvel!


What my sort of trained eyes see in the pre-rendered backgrounds (not dealing with the character yet):

- Look at all the blades of grass and leaves and the ground and the rocks. The draw distance is freaking big. This is 1994??? I don't remember the most common 3d programs already doing instancing at that time.
- Yep everything I see is made of small spheres and I think I see what seems to be instanced pre-made blocks (slightly similar to minceraft blocks).
- The little spheres everything is made of are definitely not made out of polygons because this would require an enormous RAM capacity unheard of for those times (we had 8 mb of ram). Most likely just math functions - analytic geometry that raycast/raytrace renderers can render.


- Yup ... blades of grass are elongated analytical spheres
- Rock-blocks seem to use analytical boxes instead of sphere for a spiky/jagged look
- I do seem to see the grass on the edge throwing a shadow on the mud road (in an area right under the horse). Everything else seem to not have a shadow.. strange. I remember using no-shadow trick in Povray speeding things up a bit. Who knows...


- Nice! even the walls of houses are made out of blobby blocks which give it a rather bumpy look
- Hm something interesting seems to be going on.. can you spot it?


- See it now?
- Yep you got it. Even the single blades of grass can occlude the character's leg...

So it seems that there is some early 1994 magic going on. There's no way the game would store some representation for the position/angle/scale of every blade of grass for every background image. It's most probably some kind of a z-buffer technique where the rendering engine of the game checks what pixel should be drawn first (last) for every frame against a 8bit depth-map that was also rendered for the exact rgb background image. I say for 1994 this is some advanced stuff... even though Z-buffer is known since the end of 70s I only heard about Z-buffer when 3d Studio R4 came along (1995) because one of the IPAS process plugins I used needed it.

So far this is some high-level stuff even for something like a background image. The developer Andrew Spencer probably developed a proprietary instancing process maybe for his own raytracer.. this is a lot of tough work.



But even considering the usage of instancing - the number of blocks visible on this just one picture seems to be staggering for some of the background images. I mean it's not hard to come up with some compression algorithm that compresses just the XYZ pos and the type of block info. The hard thing is designing this whole game world.

What kind of a 3d program was used to stack these blocks together? A lot of the blocks have patterns that are not perfectly XYZ symmetrical so for them to fit together nicely so one maybe had to see what he actually stacks and where.


- The patterns on the wall are wild!

!! But look in the corners of the stone walls (and wherever stoneblocks geometry meets). Notice how progressively dark it gets in the very corners??? This is my friends some sort of AMBIENT OCCLUSION. Yes Ambient occlusion in 1994. I kinda get that this sort of effect might come free with certain raytracing approaches but I am still curious what exactly is this - some wacky area shadow type effect? It goes in every direction so who knows. I guess I really need to look into who Andrew Spencer is as this might be a first non-academic and public demonstration of something being close to what Ambient Occlusion is. (The first ever Ambient occlusion like effect I saw was with 3dsmax plugin Dirty Reyes from Reyes Infografica - long after Ecstatica).

Now let's move to the character's as they are a wonder on their own. It took me a while to figure out a possibilities on how it could be done but in the end it wasn't so hard as the game itself gives you clues if you look hard enough (I have not used any internet resources - all are my own speculations). Basically you look for flaws and from there you build your theory on what it is or isn't.

So most of the game descriptions say that this game uses a proprietary and revolutionary elipsoid technology.


If we look closely at the character we can see that he's created exclusively out of what seems to be spheres and sort of streched spheres (elipsoids!). But are these objects truly 3d?

1. Does it "shade"?

By shading I understand the process during which computer computes the amount of light reflecting off of surface of a 3d object. If we look closely at the elipsoids we can definitely see a highlight a terminator line and basically the whole gradient of colors that seems natural for the shading of a sphere.

When the elipsoids are stretched the highlight also stretches and so every other aspect of the gradient so everything seems to be natural.

So does the shading react to the light direction? Are there any lights in the game?

Well seems not... in every scene, at every position the shading of the spheres stays the same. Which points either to :

1. Every scene (background) has the same direct light (or an point light in an infinite distance) in the same spot (behind camera, in the top left corner of the sky)
2. There are no lights
3. There is absolutely no shading going on and there is no real 3d elipsoid geometry

2. Does it intersect with other with other "geometry" naturally?

Apparenly seems so. Look at the butt of the protagonist.


The two halves of his behind seem to nicely intersect and occlude as if they were real spheres. But there are a bit of jaggies going on (edge noise) between the halves. Maybe a precision problem? Let's explore this phenomenon even further by trying to get the protagonist into the position where this "precision" effect is exaggerated.



Ouch that looks really worrying! That orange thing in the top right is the protagonists face which is made out of several spheres (picture shows left side of his face).

Look at how the "shading" totally fell apart into broad bands of color. And look at the strange edge artifacts - as if the edge pixels got bigger for some reason. Would a shading engine do this? A shading engine should not be spooked like this by having geometry this close to the camera.


Another example of an object close to the camera (the grey thing in forefront - it's a shield of an invisible fighter who chases me) . The shading and the edges fall apart. Everything seems to be "scaled".

3. Any other strange things?

In some cases which is hard to show with a static picture the elipsoids get thinned out a bit too much as if they were losing their volume.

4. If it's not 3d it's...?

Yes, it's a 2d effect. A 2d effect not in a sense of a 2d gradient being created but just an image of a shaded sphere with z-buffer image info too (for object to object occlusions) that is accordingly stretched and rotated. Like this easy example in photoshop (I just realized Photoshop can't use Z-buffer information for composition so I can't show the 3d like overlapping effect.. shame on you ADOBE!!)

I do not want to go deep in on the speculation on the 3d representation of the character skeleton according to which the spheres are stretched and rotated. The whole rotating and stretching algorithm works almost flawlessly good - in some of the scenes where the FOV of the camera causes some heavy distortion the elipsoids also follow this distortion really reall well. The collision mechanism also works well and is also probably based on some 3d box walls representations.

Conclusion

This game features some good techniques and insane tricks for its time. Unfortunately the whole 2d image elipsoid idea turned out to be not as practical as shaded and textured polygons (in Alone in the Dark the polygons were not shaded.. they stayed the same constant color) and so were not used outside the Ecstatica series.

PS: if you know any other games which used some fancy stuff like z-buffer in, before or a bit after 94 please let me know in the comments.