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

Encoding obj into Binary Format #7

Closed
kion-dgl opened this issue Jul 11, 2024 · 6 comments · Fixed by #17
Closed

Encoding obj into Binary Format #7

kion-dgl opened this issue Jul 11, 2024 · 6 comments · Fixed by #17
Assignees

Comments

@kion-dgl
Copy link
Owner

kion-dgl commented Jul 11, 2024

Now that we've one a sanity check on decoding and re-encoding information. The next step is to start building back content into the game. This is what I had previously, which is encoding the body and the face images followed by encoding all of the body.

doReplace(
    "Miku/miku_body.png",
    "Miku/miku_face.png",
    // Body Encoding
    "Miku/02_BODY.obj",
    "Miku/03_HIPS.obj",
    "Miku/10_LEG_RIGHT_TOP.obj",
    "Miku/11_LEG_RIGHT_BOTTOM.obj",
    "Miku/13_LEG_LEFT_TOP.obj",
    "Miku/14_LEG_LEFT_BOTTOM.obj",
    // Feet
    "Miku/12_RIGHT_FOOT.obj",
    "Miku/15_LEFT_FOOT.obj",
    // Left Arm
    "Miku/07_LEFT_SHOULDER.obj",
    "Miku/08_LEFT_ARM.obj",
    "Miku/09_LEFT_HAND.obj",
    // Right Arm
    "Miku/04_RIGHT_SHOULDER.obj",
    "Miku/05_RIGHT_ARM.obj",
    "Miku/06_RIGHT_HAND.obj",
    // Head
    "Miku/01_HEAD_HAIR.obj",
    "Miku/01_HEAD_FACE.obj",
    "Miku/01_HEAD_MOUTH.obj"
)

This will effectively encode PL00P010.BIN. Which means that the easiest option would be to use this format and swap out the helmet and shoes as needed.

@kion-dgl
Copy link
Owner Author

kion-dgl commented Jul 13, 2024

For some reason I'm not seeing any change. I got the wrong texture it should be PL00T.BIN, I was encoding PL01T.BIN which is roll.

As for the model, I need to make a few changes.

  1. Make sure I have the correct offsets
  2. Replace all of the model files in the game
  3. When I replace the model file, limit the search to just the intended file segment

Also it looks like I'm targeting the roll model, so i'm going to need to update that too.

@kion-dgl
Copy link
Owner Author

Another change I could make is add a test to search for the subsections of the model from the rom and record the output.

That would build in a version of run the tests to make sure you can run it before attempting to mod a rom.

@kion-dgl
Copy link
Owner Author

A few notes on encoding. We have the body which will always be the same. Then we have the head and shoes which will vary depending on the model type. Then we have left arm, buster, which we can point back to the shoulder, followed by buster and bullet(?) and last we have the right arm.

The left and right arms should effectively have the same vertices, so potentially i could same space by pointing to the same list for each of them.

const LIMBS = [
    // Body
    {
      offset: 0x80,
      names: [
        "00_BODY",
        "01_HIP",
        "02_LEG_RIGHT_TOP",
        "03_LEG_RIGHT_BOTTOM",
        "04_LEG_LEFT_TOP",
        "05_LEG_LEFT_BOTTOM",
      ],
    },
    // Head
    {
      offset: 0xb60,
      names: ["10_HAIR", "11_FACE", "12_MOUTH"],
    },
    // Feet
    {
      offset: 0x1800,
      names: ["20_NORM_RIGHT_FOOT", "21_NORM_LEFT_FOOT"],
    },
    // Left Arm
    {
      offset: 0x1dd0,
      names: ["30_LEFT_SHOULDER", "31_LEFT_ARM", "32_LEFT_HAND"],
    },
    // Buster
    {
      offset: 0x2220,
      names: ["40_LEFT_SHOULDER", "41_BUSTER", "42_BULLET_MAYBE"],
    },
    // Right Arm
    {
      offset: 0x26f0,
      names: ["50_RIGHT_SHOULDER", "51_RIGHT_ARM", "52_RIGHT_HAND"],
    },
    // End Limbs
  ];

@kion-dgl kion-dgl self-assigned this Jul 16, 2024
@kion-dgl
Copy link
Owner Author

Looks like I tried to do too much too soon.

image

When I try to load the game, it pretty much breaks. My best explanation is this is what happens when the game is trying to jump somewhere to try and reference a triangle or something it expects to be there but isn't, so the game uses a index it thinks is available but isn't.

It looks like we're going to need to peal things back and try to sanity check what could be causing the problem, and then narrow down and troubleshoot what's going on. I think the first step to take here is to comment out everything except the first part of the body, and then use a hex editor to see if everything checks out as expected.

@kion-dgl
Copy link
Owner Author

After pulling out the hex editor, it looks like we have some problems with having the wrong counts.

MegaMan Legends 2 Miku body debug

Now, we can at least see the body. But it's not without problems. The model doesn't show up at first, then i can move around a bit. Then the model shows, but the character stops moving. Which means the game is probably silently failing somewhere.

This means there are three aspects to confirm here:

  1. Try a clean version of the model where all of the information is zeroed out except for the body to make sure the happy path works
  2. Pass our exported version back into obj exporter to check for the model sanity.
  3. Pull out the hex editor on our updated version to make sure that everything is where expected.

@kion-dgl
Copy link
Owner Author

It turns out the problem was that i was clearing the entire file, and not just replacing the section for the model. Once I figured that out the model worked. Which is lucky that the game doesn't have any special code for how the offsets are treated.

Which is good news for me as I wrote a function to optimize packing the code by seeing how much space can be put into available space first to pack as much as possible in.

There's another optimization to do in code, which would be to see if similar meshes have the same vertices and then re-encode the faces on the models to use an updated vertex list which shares the same vertices. It might not be much compared to the size of the quads and triangles, but since there's not much space, every byte counts.

Another optimization would be to remove the way the game breaks the models into small headers followed by sections of data. If I could update where the game points to for each mesh, then I could have one continuous area to pack information into, which would avoid the loss of having to leave space on the table if no buffer is able to fit in one of the sections.

image

@kion-dgl kion-dgl linked a pull request Jul 19, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: No status
Development

Successfully merging a pull request may close this issue.

1 participant