NIF / Havok format
Implemented in CreationEngineFormats/Nif/. There is no official NIF specification; the
reference is niftools’ nif.xml, the
description NifSkope is built from. Layouts below are for version 20.2.0.7, BS version 100
(Skyrim Special Edition).
The parser covers what a map needs from a model: the positions, normals, UVs and triangles of its
BSTriShape blocks placed in the model’s space, the diffuse texture of each shape and how it is
mapped, its alpha test, the vertex colors a shader tints it with, and the Havok collision a
footprint is built from. It reads no materials, particles or animation: a skinned shape is
posed by its bones as the file stores them.
debug:dump:plugin:interior-collision-shapes
measures which collision shapes resolve in practice. Truncated or inconsistent data makes NifReader.Parse, TriShapeReader and
NifSceneReader throw InvalidDataException; the other block readers return false.
Header
NifReader, nif.xml Header and BSStreamHeader.
| Field | Type | Condition |
|---|---|---|
| Header string | ASCII, ends with \n | |
| Version | uint32 | |
| Endian type | uint8 | |
| User version | uint32 | |
| Block count | uint32 | |
| BS version | uint32 | |
| Author | ExportString | |
| Unknown int | uint32 | BS version > 130 |
| Process script | ExportString | BS version < 131 |
| Export script | ExportString | |
| Max filepath | ExportString | BS version >= 103 |
| Block type count, block types | uint16, SizedString[] | |
| Block type index | uint16[block count] | |
| Block size | uint32[block count] | |
| String count, max length, strings | uint32, uint32, SizedString[] | |
| Group count, groups | uint32, uint32[] |
A SizedString is a uint32 length and the characters. An ExportString is a byte length, counting a
null terminator, and the characters. Blocks follow the header back to back, so NifDocument
locates block i from the sizes without parsing the blocks before it.
Blocks
| Block | Fields read | Reader |
|---|---|---|
NiNode and subtypes | Name (string index), extra data refs (skipped), controller, flags (uint32), translation, rotation (row-major 3x3), scale, collision object ref, children refs | NiNodeReader |
bhkCollisionObject | Target (skipped), flags (uint16), body ref | CollisionObjectReader |
bhkRigidBody, bhkRigidBodyT | Shape ref, the first field | CollisionObjectReader |
BSShaderTextureSet | int32 count, SizedString textures; slot 0 is the diffuse map, slot 1 the normal map, slot 2 the glow map of a Glow shader | ShaderTextureSetReader |
BSTriShape | See BSTriShape | TriShapeReader |
BSLightingShaderProperty | Shader type (uint32, before the name), NiObjectNET, two uint32 flags (flag 1 bit 12 Model_Space_Normals, bits 26 and 27 Decal and Dynamic_Decal, flag 2 bit 5 Vertex_Colors), UV offset and scale (2 floats each), texture set ref, emissive color and multiple (4 floats), texture clamp mode (uint32), alpha (float) | ShaderPropertyReader |
NiAlphaProperty | NiObjectNET, flags (uint16), threshold (uint8) | AlphaPropertyReader |
BSEffectShaderProperty | NiObjectNET, two uint32 flags (flag 1 bits 3 to 6 Vertex_Alpha, Greyscale_To_PaletteColor, Greyscale_To_PaletteAlpha, Use_Falloff, bit 30 Soft_Effect; flag 2 bit 5 Vertex_Colors), UV offset and scale, source texture (SizedString), texture clamp mode (uint8), three bytes skipped, falloff start and stop angles and opacities (4 floats), base color (Color4), base color scale (float), soft falloff depth (float), greyscale texture (SizedString) | ShaderPropertyReader |
BSWaterShaderProperty | NiObjectNET, two uint32 flags, UV offset and scale, water shader flags (uint32); only its block type is used: it marks a water surface | NifSceneShape.ShaderType |
NiNodeReader accepts NiNode, BSFadeNode, BSLeafAnimNode, BSMultiBoundNode,
BSOrderedNode and BSTreeNode: they share NiNode’s layout up to the children list.
BSFadeNode is the usual root of static meshes.
The rotation of a NiAVObject is nine floats. The code reads them as a row-major matrix and
applies it to column vectors; nif.xml calls the storage column-major without saying how a point is
multiplied. See Spec deviations.
BSTriShape
TriShapeReader, nif.xml BSTriShape and BSVertexDataSSE. The layout is read for BS version
100 only: Fallout 4 (130) widens the triangle count to uint32.
| Field | Type | Read |
|---|---|---|
NiObjectNET, NiAVObject | As for NiNode | Name, local transform |
| Bounding sphere | Center (Vector3), radius (float) | Skipped |
| Skin | Ref | Yes, see Skinned shapes |
| Shader property | Ref | Yes |
| Alpha property | Ref | Yes |
| Vertex desc | uint64 bitfield | Vertex size, UV, normal, tangent and color offsets, attributes |
| Triangle count | uint16 | Yes |
| Vertex count | uint16 | Yes |
| Data size | uint32 | Checked against the counts |
| Vertex data | Vertex count × vertex size bytes | Positions, UVs, normals, tangents, bitangents, colors |
| Triangles | Triangle count × three uint16 | Yes |
Vertex data and triangles are present only when the data size is above 0. A skinned shape stores
its geometry in its NiSkinPartition instead, see Skinned shapes.
The vertex desc (nif.xml BSVertexDesc) gives the vertex size, the offsets of the UVs, normal,
tangent, colors and skinning data, and the attribute flags; its offsets count in units of 4 bytes,
see assumptions. The reader takes the
position (always full precision in SSE, so Full_Precision is ignored), the HalfTexCoord UVs
(v grows down the texture’s rows), the ByteColor4 vertex colors and the bone weights and indices.
An offset that leaves no room for its field in the vertex throws.
The tangent frame follows each field’s condition in BSVertexDataSSE:
| Field | Where | Read when |
|---|---|---|
| Normal | Three normbytes at the normal offset | Normals |
| Bitangent Y | The normbyte after the normal | Normals |
| Tangent | Three normbytes at the tangent offset | Vertex, Normals and Tangents |
| Bitangent Z | The normbyte after the tangent | Vertex, Normals and Tangents |
| Bitangent X | The float after the position | Vertex, Normals and Tangents |
A normbyte maps 0 to 255 onto -1 to 1, see
assumptions. NifSceneShape.Normals turns
the normals into the model’s space by the shape’s model transform, without its scale, and brings
them back to unit length; Tangents and Bitangents are turned the same way.
A shape takes its vertex colors only when its shader property also sets Vertex_Colors, bit 5 of
its second flags (NifSceneShape.VertexColors): the vertex desc says the colors are stored, the
shader that it multiplies its color by them. Without a shader, or with a truncated one, the shape
is untinted.
The data size must equal vertex size × vertex count + 6 × triangle count, and every triangle
index must be below the vertex count; otherwise the reader throws.
A shape’s diffuse texture comes from its shader property: slot 0 of the BSShaderTextureSet a
BSLightingShaderProperty points at, or the source texture of a BSEffectShaderProperty. Other
shader types (sky, water) have none. A lighting shader also gives its normal map, slot 1
(NormalTexturePath), and whether it sets Model_Space_Normals (ModelSpaceNormals): the map then
holds normals in the model’s space instead of the vertices’ tangent space. It gives what the shape
emits (Emissive), with on a Glow shader its glow map, slot 2 (GlowTexturePath), and whether it is a
decal (Decal). A texture set with fewer slots, an empty slot or a truncated one gives no path. The scene also gives each shape the block type of its shader
property (ShaderType), so a reader can tell an effect from a lit surface.
A lighting or effect shader also gives the shape its TextureMapping: a texture coordinate is
uv × UV Scale + UV Offset, then wrapped or clamped along u (S) and v (T) by the clamp mode
(TexClampMode: 0 clamps both, 1 clamps S and wraps T, 2 wraps S and clamps T, 3 wraps both;
a byte in an effect shader). An effect shader also gives the shape its material
(NifSceneShape.Effect), the fields after its source texture. Any other shader, or a truncated
block, gives the nif.xml defaults: no offset, a scale of 1, wrapped both ways.
The shape’s NiAlphaProperty gives its alpha test (test function and threshold) and its blending
(source and destination AlphaFunction), with nif.xml’s flag layout. A missing or truncated block
gives no alpha property.
A shape also takes its lighting shader’s material opacity, the Alpha float after Texture Clamp Mode (NifSceneShape.MaterialAlpha, 1 for any other shader or a truncated one), and its vertex
alphas when the vertex desc stores colors and its shader sets Vertex_Alpha, bit 3 of its first
flags (NifSceneShape.VertexAlphas), whatever Vertex_Colors says. How a map uses them is in
Blending.
Skinned shapes
SkinReader, nif.xml NiSkinInstance, NiSkinData, NiSkinPartition and SkinPartition. A
BSTriShape whose skin ref points at a NiSkinInstance or a BSDismemberSkinInstance has a data
size of 0 and takes its geometry from the skin:
| Block | Field | Read |
|---|---|---|
NiSkinInstance | Data, skin partition | Refs, checked against their block type |
NiSkinInstance | Skeleton root | Skipped |
NiSkinInstance | Bones | Node refs |
NiSkinData | Skin transform | Skipped |
NiSkinData | Bone list | Each bone’s skin transform; bounding sphere and weights skipped |
NiSkinPartition | Vertex desc, vertex data | As for BSTriShape, bone weights and indices included |
SkinPartition | Triangles copy | Indices into the vertex data; every other field skipped |
A skin data with fewer bones than its instance, a vertex data size that isn’t a whole number of vertices, a bone index past the bones, a triangle past the vertices or a bone that isn’t a node under the root throws.
NifSceneReader poses each vertex: every bone with a weight above 0 moves it by its skin transform,
then by the bone node’s transform in the model’s space, and the results are blended by the weights.
The bone index of a vertex points into the instance’s bones. The pose is the one the file stores
the bone nodes in; the game’s tree animations aren’t played. The shape’s own transform and the
skin data’s overall transform aren’t applied to a weighted vertex. See
Spec deviations. The same bones and
weights turn the vertex normals, tangents and bitangents, by the rotation of each, before they’re
brought back to unit length. Most vanilla trees are skinned, so their crowns keep their normals this way.
Scene graph
NifSceneReader walks the graph depth-first from block 0:
- A node of a type
NiNodeReadersupports composes its transform with its parent’s and visits its children. A null child ref (-1) is skipped; a ref past the block table throws. - A
NiSwitchNodevisits only the child at itsIndex, read after theNiNodeeffects, and none whenIndexis past its children. See Spec deviations. - A
BSTriShapeis read withTriShapeReader. Its model transform is every node’s transform from the root down, then its own. The root’s transform is included. A skinned shape’s positions, normals, tangents and bitangents are posed in the model’s space instead, under an identity model transform. - A node or shape named
EditorMarkeris left out with its subtree: it holds geometry the editor shows and the game doesn’t. See Spec deviations. - A node or shape whose
NiAVObjectflags set bit 0, hidden, is left out with its subtree. In vanilla, those are particle emitter meshes, forge markers and the spare parts of some weapons and tools. See Spec deviations. - Any other child type is skipped and reported once in
SkippedBlockTypes:NiTriShape,NiTriStrips,BSLODTriShape,BSDynamicTriShape, particle systems, and nodes whose children aren’t all drawn as is, such asNiBillboardNode(turned to the camera). - A child ref back to a node on the current path is not followed, so a malformed cycle ends.
A transform is a rotation, a translation and a uniform scale, applied as
rotation × (point × scale) + translation. Two compose into one transform of the same form.
Havok shapes
HavokShapeReader. All Havok lengths are scaled by 69.9915 to game units, see
Spec deviations.
| Shape | Layout read | Footprint |
|---|---|---|
bhkBoxShape | Half extents (Vector3) at offset 16 | 8 corners |
bhkSphereShape | Radius at offset 4 | 8-point circle |
bhkCapsuleShape | At offset 16: point 1, radius 1, point 2, radius 2 | Two circles |
bhkConvexVerticesShape | At offset 32: uint32 count, Vector4 vertices | The vertices |
bhkConvexTransformShape | Shape ref, then a 4x4 matrix at offset 20 | Wrapped shape, transformed |
bhkListShape | uint32 count, shape refs | Union of the resolvable sub-shapes |
bhkMoppBvTreeShape | Shape ref, the first field; the MOPP code is skipped | Wrapped shape |
bhkPackedNiTriStripsShape | Data ref at offset 52 | See hkPackedNiTriStripsData |
hkPackedNiTriStripsData | Triangles (8 bytes each, skipped), uint32 vertex count, compressed flag, Vector3 or half-float vertices | The vertices |
bhkCompressedMeshShape | Data ref at offset 52 | See bhkCompressedMeshShapeData |
bhkCompressedMeshShapeData | See below | Big vertices and chunk vertices |
Shapes reached through bhkMoppBvTreeShape are static room geometry and flagged
IsArchitecture.
bhkCompressedMeshShapeData starts with 54 bytes of bit counts, masks, AABB and type bytes, then
four counted material arrays and a named material count, all skipped. Then:
| Field | Layout |
|---|---|
| Chunk transforms | uint32 count, then per transform: translation (Vector4), rotation (hkQuaternion, x y z w) |
| Big vertices | uint32 count, Vector4 each, full precision |
| Big triangles | uint32 count, 12 bytes each, skipped |
| Chunks | uint32 count, then per chunk: translation (Vector4), material index, reference, transform index (uint16), uint32 vertex component count (3 per vertex), uint16 vertices in Havok units × 1000, then indices, strips and welding info, each a counted uint16 array |
A chunk vertex is rotation(chunkTranslation + vertex / 1000) + transformTranslation. That order
is unverified.
Findings from real data
- Top groups aren’t all flat:
DIALhas nested topic children, so the base object lookup skips nested groups. bhkMoppBvTreeShapewrapsbhkCompressedMeshShapein vanilla interiors, notbhkPackedNiTriStripsShape: most placed interior objects resolve through it.NifSceneReaderreads every NIF of the Anniversary Edition archives plus the Moon and Star and Vominheim mods without an error. A few are Skyrim LE (BS version 82 or 83), whoseNiTriShapegeometry the walk skips.EditorMarkeris always spelled that way.