Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access palette notes #14

Closed
wihrl opened this issue Feb 24, 2023 · 7 comments
Closed

Access palette notes #14

wihrl opened this issue Feb 24, 2023 · 7 comments
Assignees
Labels
feature New feature or request

Comments

@wihrl
Copy link

wihrl commented Feb 24, 2023

Hello, I would like to access palette notes per voxel.
image

I managed to get the notes out of the note chunk with the following code:

        var noteChunk = vox.Chunks.Single(x => x.Type == ChunkType.Note);
        var noteList = new List<string>();

        var i = 4; // skip row count
        while (i < noteChunk.Content.Length)
        {
            var noteLength = BitConverter.ToInt32(noteChunk.Content, i);
            i += 4;

            var note = Encoding.ASCII.GetString(noteChunk.Content[i..(i + noteLength)]).Trim();
            i += noteLength;

            noteList.Add(note);
        }

This creates an array with 32 names that match with what's in the editor.
Unfortunately, I have not been able to match colors to the actual group names. The color indexes don't match up at all, in fact, they aren't even in the same order.

I've noticed the existence of ChunkType.IndexMap, maybe I need to use that somehow?
Also, it would be nice if you could access the color index from the public Voxel struct. As things are right now, I had to clone this repo and modify it to expose it.

@sandrofigo sandrofigo added the feature New feature or request label Feb 24, 2023
@sandrofigo
Copy link
Owner

sandrofigo commented Feb 24, 2023

Thank you for bringing this up. I'm going to have a closer look at the color indices and notes.

The features to add would include:

  • Access the palette notes more easily (and per voxel)
  • Possibility to correctly match group names to colors or vice versa
  • Access the color index of a color applied to a voxel more easily (I'm not sure if this will be directly added to the Voxel struct or somewhere else e.g. the Color struct)

I will add these features in the next release if I have time to work on the project (probably in the next weeks).

@sandrofigo sandrofigo self-assigned this Feb 24, 2023
@sandrofigo
Copy link
Owner

@wihrl In the meantime, if you want to have a look for yourself you can try to map the indices with the IMAP chunk (described at the bottom here https://github.com/ephtracy/voxel-model/blob/master/MagicaVoxel-file-format-vox-extension.txt)

@wihrl
Copy link
Author

wihrl commented Feb 24, 2023

Already found a solution based on this: ephtracy/voxel-model#19 (comment)

        // voxel color indexes do not correspond to the palette indexes => need to remap indexes using the index map chunk
        var mapChunk = voxFile.Chunks.Single(x => x.Type == ChunkType.IndexMap);
        var inverseMap = new byte[256];
        for (var i = 0; i < mapChunk.Content.Length; i++)
            inverseMap[mapChunk.Content[i]] = (byte) i;

...

            var paletteIndex = inverseMap[modelVoxel.ColorIndex];
            var noteIndex = 31 - (paletteIndex / 8);
            var note = notes[noteIndex];

Haven't tested it much yet, but it seems to work.

@sandrofigo
Copy link
Owner

@wihrl Do you have a .vox file I can use to verify the upcoming features?

@wihrl
Copy link
Author

wihrl commented Feb 25, 2023

Not really, but for testing you can just add colors where the R and G values correspond to their X and Y position within the palette.
Checking if the lookup works is trivial then.

@sandrofigo
Copy link
Owner

@wihrl With the new version https://github.com/sandrofigo/VoxReader/releases/tag/v4.0.0 most of the requested features are implemented.

Please let me know if you encounter any problems.

@wihrl
Copy link
Author

wihrl commented Mar 25, 2023

Thanks, everything seems to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants