mirror of
https://github.com/sampletext32/ParkanPlayground.git
synced 2026-08-15 02:57:49 +04:00
msh improvements
This commit is contained in:
+120
-38
@@ -5,10 +5,16 @@ namespace ParkanPlayground;
|
||||
|
||||
/// <summary>
|
||||
/// MSH-компонент 0x01: таблица узлов модели.
|
||||
/// Для обычного AniMesh node имеет size 0x26.
|
||||
/// У ландшафта metadata/magic1 может иметь другой смысл, например grid_x_count.
|
||||
/// </summary>
|
||||
/// У ландшафта magic1 - grid_x_count
|
||||
public static class Msh0x01
|
||||
{
|
||||
public const int NormalElementSize = 0x26;
|
||||
public const int LodCount = 3;
|
||||
public const int GroupCount = 5;
|
||||
public const int SlotCount = LodCount * GroupCount;
|
||||
|
||||
public static Msh0x01Component ReadComponent(FileStream mshFs, NResArchive archive)
|
||||
{
|
||||
var entry = archive.Files.FirstOrDefault(x => x.FileType == "01 00 00 00");
|
||||
@@ -28,71 +34,147 @@ public static class Msh0x01
|
||||
throw new Exception("Node table component (0x01) payload size is not divisible by element size");
|
||||
}
|
||||
|
||||
if (entry.ElementSize < 8)
|
||||
{
|
||||
throw new Exception("Node table component (0x01) element size is too small");
|
||||
}
|
||||
|
||||
var elementCount = entry.FileLength / entry.ElementSize;
|
||||
var data = new byte[entry.FileLength];
|
||||
|
||||
mshFs.Seek(entry.OffsetInFile, SeekOrigin.Begin);
|
||||
mshFs.ReadExactly(data, 0, data.Length);
|
||||
|
||||
var dataSpan = data.AsSpan();
|
||||
|
||||
var elements = new List<Node>(elementCount);
|
||||
var nodes = new List<Node>(elementCount);
|
||||
|
||||
for (var i = 0; i < elementCount; i++)
|
||||
{
|
||||
var baseOffset = i * entry.ElementSize;
|
||||
var rawBytes = dataSpan.Slice(baseOffset, entry.ElementSize).ToArray();
|
||||
var slots = new ushort[15];
|
||||
Array.Fill(slots, ushort.MaxValue);
|
||||
var slotWords = Math.Min(slots.Length, Math.Max(0, (entry.ElementSize - 8) / 2));
|
||||
for (var slotIndex = 0; slotIndex < slotWords; slotIndex++)
|
||||
var elementSpan = dataSpan.Slice(baseOffset, entry.ElementSize);
|
||||
|
||||
var rawBytes = elementSpan.ToArray();
|
||||
|
||||
var slotIndices = new ushort[SlotCount];
|
||||
Array.Fill(slotIndices, ushort.MaxValue);
|
||||
|
||||
var slotWordCount = Math.Min(
|
||||
slotIndices.Length,
|
||||
Math.Max(0, (entry.ElementSize - 8) / 2));
|
||||
|
||||
for (var slotIndex = 0; slotIndex < slotWordCount; slotIndex++)
|
||||
{
|
||||
slots[slotIndex] =
|
||||
BinaryPrimitives.ReadUInt16LittleEndian(dataSpan.Slice(baseOffset + 8 + slotIndex * 2));
|
||||
slotIndices[slotIndex] =
|
||||
BinaryPrimitives.ReadUInt16LittleEndian(elementSpan.Slice(0x08 + slotIndex * 2, 2));
|
||||
}
|
||||
|
||||
elements.Add(new Node(
|
||||
rawBytes,
|
||||
(NodeFlags)BinaryPrimitives.ReadUInt16LittleEndian(dataSpan.Slice(baseOffset)),
|
||||
BinaryPrimitives.ReadUInt16LittleEndian(dataSpan.Slice(baseOffset + 2)),
|
||||
BinaryPrimitives.ReadUInt16LittleEndian(dataSpan.Slice(baseOffset + 4)),
|
||||
BinaryPrimitives.ReadUInt16LittleEndian(dataSpan.Slice(baseOffset + 6)),
|
||||
slots));
|
||||
nodes.Add(new Node(
|
||||
RawBytes: rawBytes,
|
||||
Flags: (NodeFlags)BinaryPrimitives.ReadUInt16LittleEndian(elementSpan.Slice(0x00, 2)),
|
||||
ParentIndexOrLink: BinaryPrimitives.ReadUInt16LittleEndian(elementSpan.Slice(0x02, 2)),
|
||||
AnimMapStart0x13: BinaryPrimitives.ReadUInt16LittleEndian(elementSpan.Slice(0x04, 2)),
|
||||
FallbackKey0x08: BinaryPrimitives.ReadUInt16LittleEndian(elementSpan.Slice(0x06, 2)),
|
||||
Msh02SlotIndicesByLodAndGroup: slotIndices));
|
||||
}
|
||||
|
||||
return new Msh0x01Component(elements);
|
||||
return new Msh0x01Component(entry.ElementSize, nodes);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>Результат чтения MSH-компонента 0x01.</summary>
|
||||
/// <param name="Elements">Узлы компонента 0x01.</param>
|
||||
public record Msh0x01Component(List<Node> Elements);
|
||||
/// <param name="ElementSize">Размер node entry из NRes metadata.</param>
|
||||
/// <param name="Nodes">Узлы компонента 0x01.</param>
|
||||
public sealed record Msh0x01Component(
|
||||
int ElementSize,
|
||||
List<Node> Nodes)
|
||||
{
|
||||
public bool IsNormalAniMeshNodeTable => ElementSize == NormalElementSize;
|
||||
}
|
||||
|
||||
/// <summary>Узел 0x01.</summary>
|
||||
/// <param name="RawBytes">Сырые байты узла (length = attr3). Нужны для copy-through редких вариантов, например attr3 = 24.</param>
|
||||
/// <param name="Flags">[0x00..0x02] Флаги узла</param>
|
||||
/// <param name="ParentOrLink_possibly_0x02_index">[0x02..0x04] Индекс родителя или связанного узла</param>
|
||||
/// <param name="AnimMapStart">[0x04..0x06] Начало блока в карте анимации Msh0x13 или 0xFFFF</param>
|
||||
/// <param name="FallbackKey">[0x06..0x08] Индекс fallback-ключа в пуле ключей Msh0x08</param>
|
||||
/// <param name="Msh02SlotIndicesByLodAndGroupSlotIndex">[0x08..0x26] Индексы slot в Msh0x02 по формуле lod * 5 + group</param>
|
||||
public record Node(
|
||||
/// <summary>Узел MSH 0x01.</summary>
|
||||
/// <param name="RawBytes">Сырые байты узла. Нужны для copy-through нестандартных вариантов.</param>
|
||||
/// <param name="Flags">[0x00..0x02] Флаги узла.</param>
|
||||
/// <param name="ParentIndexOrLink">[0x02..0x04] Parent node index. 0xFFFF обычно значит root/no parent.</param>
|
||||
/// <param name="AnimMapStart0x13">[0x04..0x06] Начало блока в MSH 0x13 animation map или 0xFFFF.</param>
|
||||
/// <param name="FallbackKey0x08">[0x06..0x08] Fallback key / index в MSH 0x08.</param>
|
||||
/// <param name="Msh02SlotIndicesByLodAndGroup">
|
||||
/// [0x08..0x26] Индексы geometry slot в MSH 0x02.
|
||||
/// Формула: slot = lod * 5 + group. 0xFFFF значит отсутствует.
|
||||
/// </param>
|
||||
public sealed record Node(
|
||||
byte[] RawBytes,
|
||||
NodeFlags Flags,
|
||||
ushort ParentOrLink_possibly_0x02_index,
|
||||
ushort AnimMapStart,
|
||||
ushort FallbackKey,
|
||||
ushort[] Msh02SlotIndicesByLodAndGroupSlotIndex)
|
||||
ushort ParentIndexOrLink,
|
||||
ushort AnimMapStart0x13,
|
||||
ushort FallbackKey0x08,
|
||||
ushort[] Msh02SlotIndicesByLodAndGroup)
|
||||
{
|
||||
public bool IsRoot => ParentIndexOrLink == ushort.MaxValue;
|
||||
|
||||
public int ParentIndexOrMinusOne =>
|
||||
ParentIndexOrLink == ushort.MaxValue ? -1 : ParentIndexOrLink;
|
||||
|
||||
public ushort ResolveSlotIndex(int lod, int group = 0)
|
||||
{
|
||||
var index = lod * 5 + group;
|
||||
return index >= 0 && index < Msh02SlotIndicesByLodAndGroupSlotIndex.Length ? Msh02SlotIndicesByLodAndGroupSlotIndex[index] : ushort.MaxValue;
|
||||
var index = lod * GroupCount + group;
|
||||
|
||||
return index >= 0 && index < Msh02SlotIndicesByLodAndGroup.Length
|
||||
? Msh02SlotIndicesByLodAndGroup[index]
|
||||
: ushort.MaxValue;
|
||||
}
|
||||
|
||||
public bool HasGeometrySlot(int lod, int group = 0) =>
|
||||
ResolveSlotIndex(lod, group) != ushort.MaxValue;
|
||||
|
||||
public int CountLodsForGroup(int group = 0)
|
||||
{
|
||||
var count = 0;
|
||||
|
||||
for (var lod = 0; lod < LodCount; lod++)
|
||||
{
|
||||
if (!HasGeometrySlot(lod, group))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
count++;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Flags]
|
||||
public enum NodeFlags : ushort
|
||||
{
|
||||
// very uncertain
|
||||
MSH01_NODE_FLAG_UNKNOWN_SKIP_RECURSE_0x04 = 0x4,
|
||||
MSH01_NODE_FLAG_STOP_CHILD_TRAVERSAL = 0x10,
|
||||
MSH01_NODE_FLAG_NO_SHADOW = 0x40
|
||||
None = 0,
|
||||
|
||||
/// <summary>
|
||||
/// Still uncertain. In recursive bounds/intersection paths this can suppress/alter child recursion.
|
||||
/// Seen as child_node.flags & 0x04.
|
||||
/// </summary>
|
||||
UnknownSkipChild0x04 = 0x0004,
|
||||
|
||||
/// <summary>
|
||||
/// Stops recursive traversal into children for bounds/render/intersection helpers.
|
||||
/// </summary>
|
||||
StopChildTraversal = 0x0010,
|
||||
|
||||
/// <summary>
|
||||
/// Special render-group-4 mode bit. In render group 4, selects alternate piece render mode.
|
||||
/// Earlier also seen in special render/clip behavior.
|
||||
/// </summary>
|
||||
Group4AltRenderMode = 0x0020,
|
||||
|
||||
/// <summary>
|
||||
/// Exclude from shadow / no shadow. CAniMesh tracks has_any_shadow_casting_piece when this bit is absent.
|
||||
/// </summary>
|
||||
NoShadow = 0x0040,
|
||||
|
||||
/// <summary>
|
||||
/// Used during attached MSH load: if parent/root description contains "central", piece gets hidden/excluded flag.
|
||||
/// Exact semantic name still provisional.
|
||||
/// </summary>
|
||||
CheckParentDescriptionCentral = 0x0800,
|
||||
}
|
||||
Reference in New Issue
Block a user