source: OniSplit/Motoko/GeometryDatWriter.cs@ 1182

Last change on this file since 1182 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: 2.7 KB
Line 
1namespace Oni.Motoko
2{
3 internal class GeometryDatWriter
4 {
5 private Geometry geometry;
6 private ImporterFile importer;
7
8 public static ImporterDescriptor Write(Geometry geometry, ImporterFile importer)
9 {
10 var writer = new GeometryDatWriter
11 {
12 geometry = geometry,
13 importer = importer
14 };
15
16 return writer.WriteGeometry();
17 }
18
19 private ImporterDescriptor WriteGeometry()
20 {
21 var triangleStrips = Stripify.FromTriangleList(geometry.Triangles);
22 var triangleList = Stripify.ToTriangleList(triangleStrips);
23
24 var faceNormals = new Vector3[triangleList.Length / 3];
25 var faceIndices = new int[faceNormals.Length];
26
27 for (int i = 0; i < triangleList.Length; i += 3)
28 {
29 var p0 = geometry.Points[triangleList[i + 0]];
30 var p1 = geometry.Points[triangleList[i + 1]];
31 var p2 = geometry.Points[triangleList[i + 2]];
32
33 var faceNormal = Vector3.Normalize(Vector3.Cross(p1 - p0, p2 - p0));
34 int faceIndex = i / 3;
35
36 faceNormals[faceIndex] = faceNormal;
37 faceIndices[faceIndex] = faceIndex;
38 }
39
40 var m3gm = importer.CreateInstance(TemplateTag.M3GM, geometry.Name);
41 var pnta = importer.CreateInstance(TemplateTag.PNTA);
42 var vcra1 = importer.CreateInstance(TemplateTag.VCRA);
43 var vcra2 = importer.CreateInstance(TemplateTag.VCRA);
44 var txca = importer.CreateInstance(TemplateTag.TXCA);
45 var idxa1 = importer.CreateInstance(TemplateTag.IDXA);
46 var idxa2 = importer.CreateInstance(TemplateTag.IDXA);
47
48 using (var writer = m3gm.OpenWrite(4))
49 {
50 writer.Write(pnta);
51 writer.Write(vcra1);
52 writer.Write(vcra2);
53 writer.Write(txca);
54 writer.Write(idxa1);
55 writer.Write(idxa2);
56
57 if (geometry.TextureName != null)
58 writer.Write(importer.CreateInstance(TemplateTag.TXMP, geometry.TextureName));
59 else
60 writer.Write(0);
61
62 writer.Skip(4);
63 }
64
65 pnta.WritePoints(geometry.Points);
66 vcra1.WriteVectors(geometry.Normals);
67 vcra2.WriteVectors(faceNormals);
68 txca.WriteTexCoords(geometry.TexCoords);
69 idxa1.WriteIndices(triangleStrips);
70 idxa2.WriteIndices(faceIndices);
71
72 return m3gm;
73 }
74 }
75}
Note: See TracBrowser for help on using the repository browser.