source: OniSplit/Totoro/BodyDatWriter.cs

Last change on this file was 1114, checked in by iritscen, 5 years ago

Adding OniSplit source code (v0.9.99.0). Many thanks to Neo for all his work over the years.

File size: 3.7 KB
Line 
1using System;
2using System.Collections.Generic;
3
4namespace Oni.Totoro
5{
6 internal static class BodyDatWriter
7 {
8 public static ImporterDescriptor Write(Body body, ImporterFile importer)
9 {
10 var trcm = importer.CreateInstance(TemplateTag.TRCM);
11 var trga = importer.CreateInstance(TemplateTag.TRGA);
12 var trta = importer.CreateInstance(TemplateTag.TRTA);
13 var tria = importer.CreateInstance(TemplateTag.TRIA);
14
15 var nodes = body.Nodes;
16 int nodeCount = nodes.Count;
17
18 var geometryDescriptors = new ImporterDescriptor[nodeCount];
19 var translations = new Vector3[nodeCount];
20 var indices = new NodeIndices[nodeCount];
21
22 foreach (var node in nodes)
23 {
24 int nodeIndex = node.Index;
25
26 geometryDescriptors[nodeIndex] = Motoko.GeometryDatWriter.Write(node.Geometry, importer);
27 translations[nodeIndex] = node.Translation;
28
29 int childCount = node.Nodes.Count;
30
31 if (childCount > 0)
32 {
33 indices[nodeIndex].FirstChildIndex = (byte)node.Nodes[0].Index;
34
35 int lastChildIndex = childCount - 1;
36
37 for (int i = 0; i < childCount; i++)
38 {
39 int childIndex = node.Nodes[i].Index;
40
41 if (i != lastChildIndex)
42 indices[childIndex].SiblingIndex = (byte)node.Nodes[i + 1].Index;
43
44 indices[childIndex].ParentIndex = (byte)nodeIndex;
45 }
46 }
47 }
48
49 WriteTRCM(trcm, trga, trta, tria, nodeCount);
50 WriteTRGA(trga, geometryDescriptors);
51 WriteTRTA(trta, translations);
52 WriteTRIA(tria, indices);
53
54 return trcm;
55 }
56
57 private static void WriteTRCM(ImporterDescriptor trcm, ImporterDescriptor trga, ImporterDescriptor trta, ImporterDescriptor tria, int nodeCount)
58 {
59 using (var writer = trcm.OpenWrite(4))
60 {
61 writer.WriteInt16(nodeCount);
62 writer.Skip(78);
63 writer.Write(trga);
64 writer.Write(trta);
65 writer.Write(tria);
66 }
67 }
68
69 private static void WriteTRGA(ImporterDescriptor trga, ImporterDescriptor[] descriptors)
70 {
71 using (var writer = trga.OpenWrite(22))
72 {
73 writer.WriteInt16(descriptors.Length);
74 writer.Write(descriptors);
75 }
76 }
77
78 private static void WriteTRTA(ImporterDescriptor trta, Vector3[] translations)
79 {
80 using (var writer = trta.OpenWrite(22))
81 {
82 writer.WriteInt16(translations.Length);
83 writer.Write(translations);
84 }
85 }
86
87 private struct NodeIndices
88 {
89 public byte ParentIndex;
90 public byte FirstChildIndex;
91 public byte SiblingIndex;
92 }
93
94 private static void WriteTRIA(ImporterDescriptor tria, NodeIndices[] indices)
95 {
96 using (var writer = tria.OpenWrite(22))
97 {
98 writer.WriteInt16(indices.Length);
99
100 foreach (var node in indices)
101 {
102 writer.WriteByte(node.ParentIndex);
103 writer.WriteByte(node.FirstChildIndex);
104 writer.WriteByte(node.SiblingIndex);
105 writer.WriteByte(0);
106 }
107 }
108 }
109 }
110}
Note: See TracBrowser for help on using the repository browser.