Page 1 of 1

Terrain animations using palette rotation

PostPosted: 04 Jul 2017, 18:18
by harold
This technique is something that has been repeated alluded to, but as far as I know it hadn't been successfully replicated. Perhaps I've missed something though, maybe this isn't new.

Anyway, here's how it can be made to work (naturally I will not guarantee that the original KaM does it exactly this way) and a proof of concept, which I probably won't keep around but it'll be there for now.

It takes 3 parts,
1: the textures taken from text0.dat through text3.dat (not that Graphics Reader export, which for reasons I don't entirely understand is different and doesn't respond well to the palette rotations, this is why my experiments failed in the past - I assumed it would be the same data as is in the textx.dat files).
2: the normal palette not the ones embedded in text0.dat etc. Odd, but that's how it is.
3: the palette rotations. Theoretically the CRNG chunks in the embedded palettes should define them, but I can't make any sense of them. They do not match the actual rotations at all as far as I can tell. Maybe there's some alternative way to interpret them, or maybe the game uses rotations that are stored elsewhere. It may be useful to find out more about that, to increase the community's ability to mod the original game. Then again I believe most have switched to playing Remake instead of the original, or I hope so for their sake :)

Anyway, text0.dat etc are ILBM files, like the separate palettes. Unlike the palettes, it's obfuscates in the same manner as mission files. So first XOR all the bytes with 0xEF then parse it as ILBM file. You don't have to parse it very precisely, most of the information won't be used anyway (such as the palette, DPPS, CRNG's, TINY) and the files only ever have a specific format and image dimension. The BODY is compressed with RLE, wikipedia has the exact decoding algorithm.

The palette rotations are:
224..228, the 5-frame waterfall rotation
237..239, the 3-frame swamp rotation (also affects some lava tiles)
240..247, the 8-frame water rotation
248..250, the 3-frame water-plant rotation
Apply them at the right speed, of course. "Applying them" means (conceptually) changing the palette by rotating these ranges (moving 224 into 225, 225 into 226 .. 228 into 224 to close the cycle), of course if you're working with a pixel shader you can apply the the time-offset there and never actually change the palette texture.

As a fun side fact, there is an 8-colour "gap" from 229 to 236. Those indices do not appear in the tile textures yet, so that range could be used to animate more terrain or to have animations with more frames in fan versions of the game. For example, we could (or I could do it) easily put in two 3-frame rotations that make gold rocks and iron rocks glisten, that could be cool. Though IIRC Remake uses a different system to animate its terrain.

And here's the promised proof of concept: ijzerbaard.github.io/KAMwebgl (showing only water and water plants because editing maps is hard)
You need a fairly recent browser for this as it uses webgl2. I've tested it on the most recent Firefox and Chrome.
E: I've put a gold sparkle in it too, but it's not a good one. I'm not an artist after all. With a more careful design it should be possible to make it look nice.

Re: Terrain animations using palette rotation

PostPosted: 05 Jul 2017, 05:09
by Krom
Well done on analysing this!
KaMWebGL also looks very nice :-)
*if assets were freeware it could make a nice KaM-style game

KaM Remake just swaps whole sprites. (in case with water - it renders a background and swaps checker-board pattern "water" on top). This is less efficient, but GPUs are really good at crunching those, so there's no real drawback.

Re: Terrain animations using palette rotation

PostPosted: 05 Jul 2017, 22:06
by harold
I'm not really planning to go anywhere with it, more of a proof of concept deal - more experimenting.
Assets could be loaded from disk, but they'd have to selected manually, it would be a huge pain.. besides, I can't think of game to make with this anyway.

Re: Terrain animations using palette rotation

PostPosted: 06 Jul 2017, 04:35
by Krom
Nice experiment anyway!