raysan5 / raylib

A simple and easy-to-use library to enjoy videogames programming
http://www.raylib.com
zlib License
21.84k stars 2.21k forks source link

[models] Latest MagicaVoxel .vox models not loading #3324

Closed vinnyhorgan closed 1 year ago

vinnyhorgan commented 1 year ago

I downloaded magicavoxel's latest version from their website and tried to load the example .vox files it comes with. But they fail and it loads the default cube. I tried downloading the .vox models from the raylib examples resources folder and it worked correctly. I assume the file format might have slightly changed.

raysan5 commented 1 year ago

@vinnyhorgan Current vox_loader.h was a user contribution, it probably needs some review...

bztsrc commented 1 year ago

If you attach (or link) a few example .vox files here that ain't working then I can take a look!

I can try to fix vox_loader.h, or if that's not possible, then I might be able to add a new, raylib compatible importer to rmodels.c (I've implemented several vox loaders before, here's one and another).

But for that, I would need an example vox file to reproduce the issue first. I'd like to see if it's complying with the spec (so a bug in vox_loader.h) or if it's really in a new format.

On a different note, it would be interesting to see if it's possible to convert that vox into M3D with m3dconv and if raylib can load that converted model correctly. (Regardless I'd like to fix vox import, this is just for a temporary workaround in the meantime.)

Cheers, bzt

vinnyhorgan commented 1 year ago

Hi! Thanks for your interest, actually any vox model exported with the latest version of magicavoxel doesn't seem to load... I see they have added animation support in the last version. Maybe it's that

bztsrc commented 1 year ago

Okay, but I'd need some failing model files to test with (I don't have MagicaVoxel installed, and I never will as it's not Open Source). Could you please attach some model files?

BTW, I've checked. The chr_knight.vox in raylib examples (31031 bytes) is definitely not the same as the MagicaVoxel example (2688 bytes).

However I was able to load both with raylib without any issues. chr_knight2

I've also converted both to M3D (both resulted in a file of 634 bytes), and raylib loaded both converted models without a prob.

Cheers, bzt

vinnyhorgan commented 1 year ago

Try this:

chr_knight.zip

bztsrc commented 1 year ago

Ok, I've checked. This is definitely a 3rd variant (24289 bytes).

But I have no issues loading it with raylib. (I'm using the model example code, just replaced the filename, nothing else changed). chr_knight3_vox

I've also tried to convert this new vox model into M3D (no surprise, again 634 bytes), and that worked like a charm too: chr_knight_m3d Here's the file: chr_knight.zip

So unfortunately it works for me. Do you have another vox file perhaps? I have to reproduce your issue and see the relevant error message in order to help.

Cheers, bzt

bztsrc commented 1 year ago

I might have found something. Are you using the latest raylib? A few months ago there was a little change in vox_loader.h.

I've dumped the vox models, here's the header from the raylib example:

00000000  56 4f 58 20 96 00 00 00  4d 41 49 4e 00 00 00 00  |VOX ....MAIN....|

And the one from MagicaVoxel repository:

00000000  56 4f 58 20 96 00 00 00  4d 41 49 4e 00 00 00 00  |VOX ....MAIN....|

As you can see, both has 0x96 at offset 4 (which translates to version 150).

Now the model file you've attached on the other hand looks like this:

00000000  56 4f 58 20 c8 00 00 00  4d 41 49 4e 00 00 00 00  |VOX ....MAIN....|

It has 0xC8 at offset 4 (which translates to version 200). This means in order to load this vox model, you'll need a not-older-than-3-months raylib version, one that already includes the commit I've linked.

Please upgrade your raylib and violá, that will solve your issue!

Cheers, bzt

vinnyhorgan commented 1 year ago

I see! I always use the raylib release so I'm still stuck at 4.5.0! When I'm home I'll give it a try. While we're at it, is there any way of supporting the new vox animations you can export from magicavoxel?

bztsrc commented 1 year ago

You're welcome!

I always use the raylib release so I'm still stuck at 4.5.0!

In that case I recommend to open the vox file with a hex editor, and simply change the byte at offset 4 from 0xC8 to 0x96. That way the model will work with the latest release too. (It's not that this version does anything, because new versions might introduce new chunks, but they never change already existing chunks in retrospect. At least, not happened so far and I assume MagicaVoxel wants to keep backward compatibility, so it probably never will happen.)

is there any way of supporting the new vox animations

Well, they're not exactly new. Animation support is 7 years old or something like that. As for the vox_loader.h, you should ask Johann Nadalutti (the original author) if he has any plans. As for M3D, animated voxels are supported in raylib, but unfortunately not in the m3dconv's vox importer. It is on the roadmap though (for quite some time actually, I might just bump this on my TODO list if using M3D works for you).

Cheers, bzt

raysan5 commented 1 year ago

@bztsrc thank you very much for taking care of this issue!

As it seems the issue has been addressed, I'm closing it.