source: OniSplit/Physics/ObjectDatReader.cs@ 1194

Last change on this file since 1194 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: 4.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using Oni.Akira;
4using Oni.Motoko;
5
6namespace Oni.Physics
7{
8 internal class ObjectDatReader
9 {
10 public static ObjectNode ReadObjectGeometry(InstanceDescriptor ofga)
11 {
12 ObjectGeometry[] geometries = null;
13 ObjectParticle[] particles = new ObjectParticle[0];
14
15 if (ofga.Template.Tag == TemplateTag.OFGA)
16 {
17 using (var reader = ofga.OpenRead(16))
18 {
19 var particlesDescriptor = reader.ReadInstance();
20 geometries = ReadGeometries(reader);
21
22 if (particlesDescriptor != null)
23 particles = ReadParticles(particlesDescriptor);
24 }
25 }
26 else if (ofga.Template.Tag == TemplateTag.M3GM)
27 {
28 geometries = new ObjectGeometry[1];
29
30 geometries[0] = new ObjectGeometry
31 {
32 Flags = GunkFlags.NoOcclusion,
33 Geometry = GeometryDatReader.Read(ofga)
34 };
35 }
36
37 return new ObjectNode(geometries, particles);
38 }
39
40 private static ObjectGeometry[] ReadGeometries(BinaryReader reader)
41 {
42 uint partCount = reader.ReadUInt32();
43
44 var nodes = new ObjectGeometry[partCount];
45
46 for (int i = 0; i < nodes.Length; i++)
47 nodes[i] = ReadGeometry(reader);
48
49 return nodes;
50 }
51
52 private static ObjectGeometry ReadGeometry(BinaryReader reader)
53 {
54 var node = new ObjectGeometry
55 {
56 Flags = (GunkFlags)reader.ReadInt32(),
57 Geometry = GeometryDatReader.Read(reader.ReadInstance())
58 };
59
60 reader.Skip(4);
61
62 return node;
63 }
64
65 private static ObjectParticle[] ReadParticles(InstanceDescriptor particlesDescriptor)
66 {
67 using (var reader = particlesDescriptor.OpenRead(22))
68 {
69 var particles = new ObjectParticle[reader.ReadUInt16()];
70
71 for (int i = 0; i < particles.Length; i++)
72 particles[i] = ReadParticle(reader);
73
74 return particles;
75 }
76 }
77
78 private static ObjectParticle ReadParticle(BinaryReader reader)
79 {
80 var particle = new ObjectParticle
81 {
82 ParticleClass = reader.ReadString(64),
83 Tag = reader.ReadString(48),
84 Matrix = reader.ReadMatrix4x3(),
85 DecalScale = reader.ReadVector2(),
86 Flags = (Objects.ParticleFlags)reader.ReadUInt16()
87 };
88
89 reader.Skip(38);
90
91 return particle;
92 }
93
94 public static ObjectAnimation ReadAnimation(InstanceDescriptor oban)
95 {
96 var animation = new ObjectAnimation
97 {
98 Name = oban.Name
99 };
100
101 using (var reader = oban.OpenRead(12))
102 {
103 animation.Flags = (ObjectAnimationFlags)reader.ReadInt32();
104 reader.Skip(48);
105 var scale = reader.ReadMatrix4x3().Scale;
106 reader.Skip(2);
107 animation.Length = reader.ReadUInt16();
108 animation.Stop = reader.ReadInt16();
109 animation.Keys = new ObjectAnimationKey[reader.ReadUInt16()];
110
111 for (int i = 0; i < animation.Keys.Length; i++)
112 {
113 animation.Keys[i] = new ObjectAnimationKey
114 {
115 Scale = scale,
116 Rotation = reader.ReadQuaternion(),
117 Translation = reader.ReadVector3(),
118 Time = reader.ReadInt32()
119 };
120 }
121 }
122
123 return animation;
124 }
125 }
126}
Note: See TracBrowser for help on using the repository browser.