The MDL0 Thread: Currently Under Construction

Started by BlackJax96, January 12, 2012, 02:55:16 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

January 12, 2012, 02:55:16 AM Last Edit: February 04, 2012, 02:21:06 AM by BlackJax96
The Data Format

I will use spaces so that structures within structures can be defined.
How to read:
Structure Name (Total byte count):
(Offset DataType) Name - Description
  Sub Structure Name (Total byte count):

Values that start with 0x are hex values, while values that lack 0x are regular decimal values.
All offsets have a default base being the start of the structure unless specified otherwise.

int, uint & float are 4 bytes
short & ushort are 2 bytes
sbyte & byte are 1 byte

Unsigned (uint, ushort, byte) values cannot be negative.
Signed values (int, float, short, sbyte) can.

A Vector2 is a group of 2 floats, which is 8 bytes.
A Vector3 is a group of 3 floats, which is 12 bytes.
A Matrix43 is a group of 12 floats, which is 48 bytes.

MDL0 Header:
  BRRES Common Header (0x10):
  (0x0 uint) Tag - 'MDL0'
  (0x4 int) Size
  (0x8 int) Version
  (0xC int) Brres Offset
(0x10 int array) Offsets to data - Offsets vary by version.
Each offset points to a resource group for the specified data type, which then offset to actual data entries.

v08: Definitions, Bones, Vertices, Normals, Colors, UVs, Materials, Shaders, Objects, None, Textures, None, None
v09: Definitions, Bones, Vertices, Normals, Colors, UVs, Materials, Shaders, Objects, Textures, Palettes
v10: Definitions, Bones, Vertices, Normals, Colors, UVs, None, None, Materials, Shaders, Objects, Textures, Palettes, None, None
v11: Definitions, Bones, Vertices, Normals, Colors, UVs, None, None, Materials, Shaders, Objects, Textures, None, None

((0x10 + Offset count * 4) int) String Offset

Resource Group:

  How to generate a resource entry:

MDL0 Properties (0x40):
(0x0 uint) Length - always 0x40
(0x4 int) MDL0 Offset
(0x8 int) Unknown 1 - usually 0 or 2
(0xC int) Unknown 2 - usually 0
(0x10 int) Number of Vertices
(0x14 int) Number of Faces
(0x18 int) Unknown 3 - usually 0
(0x1C int) Node Count - The number of influences this model has
(0x20 short) Unknown 4 - usually 0x0101 or 0x0100
(0x22 short) Unknown 5 - usually 0
(0x24 uint) Data Offset - always 0x40, base is start of properties
(0x28 Vector3) Minimum Extents
(0x34 Vector3) Maximum Extents

Node/Influence Table:
(0x0 int) Entry Count
(0x4 int array) Entries
If the influence has multiple bone weights, the value will be -1.
If the influence is a single bone, the value will be that bone's index in the bone list.

Definitions:
Sorry but you are not allowed to view spoiler contents.


Bones:
Sorry but you are not allowed to view spoiler contents.



Vertex Data:
Sorry but you are not allowed to view spoiler contents.


Normal Data:
Sorry but you are not allowed to view spoiler contents.


UV Data:
Sorry but you are not allowed to view spoiler contents.


Color Data:
Sorry but you are not allowed to view spoiler contents.


Objects:
Sorry but you are not allowed to view spoiler contents.


   How to read:

How weighting works (sort of an old explanation, I'll rewrite it later):
Each object has a vertex node with vertices. Each vertex has an influence, which is either a "hidden" influence or a "bone" influence.

Each bone and hidden influence has a node id. Bone and hidden influences are added to an array called "NodeCache" in the location specified by their nodeId. The NodeCache's size is specified by the nodeCount before the start of the node table at the beginning of the MDL0 (although sometimes it is incorrect for models that aren't v9, so it's safer to correct its size by the highest node id of one of the bones).

Hidden influences have weights, and each weight references its own bone so that the vertices move how you want them to.
Bone influences literally make the vertex follow it with a weight of 1.0.

All of the influences, hidden and bone, are stored in the NodeMix.

Type 5 nodes are for bone influences.
Type 3 nodes are for hidden influences. This type has entries for each weight.

As you read these, you add the hidden or bone influence to the NodeCache in the location specified by the node id.
Once you finish, the NodeCache should be fully populated with hidden or bone influences. We will use this array later to assign the influences to the vertices.

Now when you read the primitives of each object, 0x20 holds the node id that the facepoints will get the influence from.

A 0x20 Matrix is 5 bytes long and is read like this:
1 byte: 0x20
2 bytes (unsigned short): node id
2 bytes (unsigned short): index multiplied by 12 (0x0C)

0x20 matrices can only go up to 9 in count, and then afterwards follows all the facepoints that use one of the 9 node ids in the list above.
When you read the vertex id from the facepoint, you create a new vertex by getting its value from the vertex node assigned to that object at the location of the index and then adding it to a new list of vertices, and then give it an influence by reading the pos/norm matrix id.

The pos/norm matrix id is always the first byte of a facepoint if the object is weighted, and it is an index in the 0x20 matrices that is multiplied by 3.

So, to match up a vertex to its influence, you divide the facepoint's pos/norm matrix id by 3 and then match it with one of the 0x20 matrices' indexes divided by 12. You get the node id of the matched matrix and then retrieve the influence from the NodeCache from the location of the node id.

Sometimes facepoints are repeated (like in triangles) so you need to check the new list of vertices if the vertex exists already, and then edit the index to match that vertex instead. This doesn't matter if you're writing the model though.
   


Materials:
Sorry but you are not allowed to view spoiler contents.


Shaders:
Sorry but you are not allowed to view spoiler contents.


ColorEnv Bit Shifts:
Sorry but you are not allowed to view spoiler contents.


AlphaEnv Bit Shifts:
Sorry but you are not allowed to view spoiler contents.


TRef Bit Shifts:
Sorry but you are not allowed to view spoiler contents.


KSel Bit Shifts:
Sorry but you are not allowed to view spoiler contents.


CMD Bit Shifts:
Sorry but you are not allowed to view spoiler contents.


Textures:
Sorry but you are not allowed to view spoiler contents.

January 12, 2012, 02:55:24 AM #1 Last Edit: January 12, 2012, 03:52:05 AM by BlackJax96
Materials

Header Values:


Alpha Function/Test:


Blend Mode:


Z Mode / Depth Test:


Color Values:


Texture References

Header Values:


Miscellaneous:


Texture Coordinates:


Texture Matrix:


XF Commands:


January 12, 2012, 02:55:32 AM #2 Last Edit: February 11, 2012, 04:39:18 AM by BlackJax96
Shaders

KSel Swap Table:
There are 4 RGBA swap groups in the table.
AlphaTextureSwap and AlphaRasterSwap in a tev stage will manipulate the input colors from the texture and color node by swapping each value with the value specified in the table for the selected swap.

IREF Indirect Texture References:
There can be up to 4 indirect texture stages. They use the map and coordinates specified here.

Header Values:

Stages:
This specifies how many stages the Wii needs to read.
The maximum amount of stages allowed is 16.
Stages are read in the order they appear, so the order of them does matter.

Res 0 - 2:
I'm not sure what these do, they're always 0...

Texture Reference 0 - 7:
This sets which texture references are allowed to be read by the shader.

Stage Settings:
Each stage has 5 groups of data:
KSel - Controls material color inputs.
TRef - Controls texture and raster color inputs.
ColorEnv -  Controls final color output. Seperate from alpha output.
AlphaEnv - Controls final alpha output. Seperate from color output.
CMD Ind Tex - Controls indirect texture settings. Only up to 4 stages can use this.

KSel:

These selections take a color from the material's Konstant TEV Block at the specified index.
Konstant Color Selection:
Sorry but you are not allowed to view spoiler contents.


Konstant Alpha Selection:
All output RGB values are set to the input value's alpha value.
Sorry but you are not allowed to view spoiler contents.


TRef:
This controls what texture references are affected by the settings in the stage(s).
Up to two textures can be affected in each structure, as specified here. If no texture is referenced, then usually a rasterized stage will be generated with no texture, which is usually used to add overlay affects to textures specified in previous stages (such as a darkening/brightening stage, etc).

This texture is affected by the first stage in the structure.
Texture Map Id:
The index of the texture reference to be used for texture colors.

Texture Coordinate:
The texture coordinate index for the texture reference.

Texture Enabled:
Determines whether a texture should be modified or not.

Color Channel:
ColorChannel0 = 0 - Assigns the raster selection to color node 0.
ColorChannel1 = 1 - Assigns the raster selection to color node 1.
BumpAlpha = 5 - Indirect texture bump alpha
NormalizedBumpAlpha = 6 - Indirect texture bump alpha, normalized 0-255
Zero = 7 - Sets the color value to 0.

ColorEnv:

Selection A:
An RGB value with unsigned 8-bit values. (0 <= a <= 255)
Selection B:
An RGB value with unsigned 8-bit values. (0 <= b <= 255)
Selection C:
An RGB value with unsigned 8-bit values. (0 <= c <= 255)
Selection D:
An RGB value with signed 10-bit values. (-1024 <= d <= 1023)

Color Selection Values:
PreviousColor - Takes the previous outputted register color from the last stage.
PreviousAlpha - Takes the previous outputted register alpha from the last stage.
Color0 - Takes the outputted register color 0 from the last stage.
Alpha0 - Takes the outputted register alpha 0 from the last stage.
Color1 - Takes the outputted register color 1 from the last stage.
Alpha1 - Takes the outputted register alpha 1 from the last stage.
Color2 - Takes the outputted register color 2 from the last stage.
Alpha2 - Takes the outputted register alpha 2 from the last stage.
TextureColor - Takes the color from the assigned texture map.
TextureAlpha - Takes the alpha from the assigned texture map.
RasterColor - Takes the color from the assigned color channel.
RasterAlpha - Takes the alpha from the assigned color channel.
One - 1.0 constant.
Half - 0.5 constant.
KonstantColorSelection - The assigned konstant color selection value.
Zero - 0.0 constant.

Bias:
Adds, 0.5, subtracts 0.5, or adds nothing to the final RGB values.

Subtract:
Determines whether d will be subtracted from or added to.

Clamp:
Clamps the final RGB values from 0.0 to 1.0. (1.0 being the same as 255)

Shift:
Scales the final value.
MultiplyBy1       Multiplies by 1
MultiplyBy2       Multiplies by 2
MultiplyBy4        Multiplies by 4
DivideBy2         Multiplies by 1/2

Destination:
PreviousRegister
Register0
Register1
Register2

For output calculation, all input values are converted to decimal by dividing by 255.
The color value for this stage is calculated like this:
destination register = (d ± ((1 - c) * a + c * b) + bias) * scale

AlphaEnv:

Raster Swap:
Swaps raster color values using the shader's KSel Swap Table.
Texture Swap:
Swaps texture color values using the shader's KSel Swap Table.

Different combinations of these selections seem to do different things.
They specify where to get the RGB alpha from.
Selection A:
An RGB value with unsigned 8-bit values. (0 <= a <= 255)
Selection B:
An RGB value with unsigned 8-bit values. (0 <= b <= 255)
Selection C:
An RGB value with unsigned 8-bit values. (0 <= c <= 255)
Selection D:
An RGB value with signed 10-bit values. (-1024 <= d <= 1023)

Alpha Selection Values:
PreviousAlpha - Alpha from the previous register.
Alpha0 - Alpha from register 0.
Alpha1 - Alpha from register 1.
Alpha2 - Alpha from register 2.
TextureAlpha - Alpha from the texture.
RasterAlpha - Alpha from the color channel.
KonstantAlphaSelection - Alpha from the konstant alpha selection.
Zero - 0 constant.

Bias:
Adds, 0.5, subtracts 0.5, or adds nothing to the final RGB values.

Subtract:
Determines whether d will be subtracted from or added to.

Clamp:
Clamps the final RGB values from 0.0 to 1.0. (1.0 being the same as 255)

Shift:
Scales the final value.
MultiplyBy1       Multiplies by 1
MultiplyBy2       Multiplies by 2
MultiplyBy4        Multiplies by 4
DivideBy2         Multiplies by 1/2

Destination:
PreviousRegister
Register0
Register1
Register2

For output calculation, all input values are converted to decimal by dividing by 255.
The alpha value for this stage is calculated like this:
destination register = (d ± ((1 - c) * a + c * b) + bias) * scale

CMD Indirect Texture:

If the raw value is 0, the stage is a direct texture.

Texture Stage:
Index of the Indirect texture being bound.

Texture Format:
Format of indirect texture offsets.

Bias:
Bias added to the texture offsets.

Alpha:
Selects indirect texture alpha output.

Matrix:
Selects texture offset matrix.

S Wrap:
Wrap value of Direct S coordinate.

T Wrap:
Wrap value of Direct T coordinate.

Use Previous Stage:
Add output from previous stage to texture coords.

Unmodified Level Of Detail:
Use the unmodified texture coordinates for LOD.


Reserved another spot, just in case. You never know :P