Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Model.cs
Go to the documentation of this file.
1using System;
2using Microsoft.Xna.Framework.Content;
3
5
6public sealed class Model
7{
8 private ModelBone root;
9
11
13
14 private object tag;
15
17
18 public ModelBone Root => root;
19
21
22 public object Tag
23 {
24 get
25 {
26 return tag;
27 }
28 set
29 {
30 tag = value;
31 }
32 }
33
35
36 internal Model()
37 {
38 }
39
40 public void CopyBoneTransformsTo(Matrix[] destinationBoneTransforms)
41 {
42 if (destinationBoneTransforms == null)
43 {
44 throw new ArgumentNullException("destinationBoneTransforms");
45 }
46 if (destinationBoneTransforms.Length < bones.Count)
47 {
48 throw new ArgumentOutOfRangeException("destinationBoneTransforms");
49 }
50 int count = bones.Count;
51 for (int i = 0; i < count; i++)
52 {
53 ref Matrix reference = ref destinationBoneTransforms[i];
54 reference = bones[i].transform;
55 }
56 }
57
58 public void CopyAbsoluteBoneTransformsTo(Matrix[] destinationBoneTransforms)
59 {
60 if (destinationBoneTransforms == null)
61 {
62 throw new ArgumentNullException("destinationBoneTransforms");
63 }
64 if (destinationBoneTransforms.Length < bones.Count)
65 {
66 throw new ArgumentOutOfRangeException("destinationBoneTransforms");
67 }
68 int count = bones.Count;
69 for (int i = 0; i < count; i++)
70 {
71 ModelBone modelBone = bones[i];
72 if (modelBone.Parent == null)
73 {
74 ref Matrix reference = ref destinationBoneTransforms[i];
75 reference = modelBone.transform;
76 }
77 else
78 {
79 int index = modelBone.Parent.Index;
80 ref Matrix reference2 = ref destinationBoneTransforms[i];
81 reference2 = modelBone.transform * destinationBoneTransforms[index];
82 }
83 }
84 }
85
86 public void CopyBoneTransformsFrom(Matrix[] sourceBoneTransforms)
87 {
88 if (sourceBoneTransforms == null)
89 {
90 throw new ArgumentNullException("sourceBoneTransforms");
91 }
92 if (sourceBoneTransforms.Length < bones.Count)
93 {
94 throw new ArgumentOutOfRangeException("sourceBoneTransforms");
95 }
96 int count = bones.Count;
97 for (int i = 0; i < count; i++)
98 {
99 bones[i].transform = sourceBoneTransforms[i];
100 }
101 }
102
103 public void Draw(Matrix world, Matrix view, Matrix projection)
104 {
105 int count = meshes.Count;
106 int count2 = bones.Count;
108 if (array == null || array.Length < count2)
109 {
110 array = (sharedDrawBoneMatrices = new Matrix[count2]);
111 }
113 for (int i = 0; i < count; i++)
114 {
115 ModelMesh modelMesh = meshes[i];
116 int index = modelMesh.ParentBone.Index;
117 int count3 = modelMesh.Effects.Count;
118 for (int j = 0; j < count3; j++)
119 {
120 Effect effect = modelMesh.Effects[j];
121 if (effect == null)
122 {
124 }
125 if (!(effect is IEffectMatrices effectMatrices))
126 {
128 }
129 effectMatrices.World = array[index] * world;
130 effectMatrices.View = view;
131 effectMatrices.Projection = projection;
132 }
133 modelMesh.Draw();
134 }
135 }
136
137 internal static Model Read(ContentReader input)
138 {
139 Model model = new Model();
140 model.ReadBones(input);
141 model.ReadMeshes(input);
142 model.root = model.ReadBoneReference(input);
143 model.Tag = input.ReadObject<object>();
144 return model;
145 }
146
147 private void ReadBones(ContentReader input)
148 {
149 int num = input.ReadInt32();
150 ModelBone[] array = new ModelBone[num];
151 for (int i = 0; i < array.Length; i++)
152 {
153 string name = input.ReadObject<string>();
154 Matrix transform = input.ReadMatrix();
155 array[i] = new ModelBone(name, transform, i);
156 }
158 ModelBone[] array2 = array;
159 foreach (ModelBone modelBone in array2)
160 {
161 ModelBone newParent = ReadBoneReference(input);
162 int num2 = input.ReadInt32();
163 ModelBone[] array3 = new ModelBone[num2];
164 for (int k = 0; k < num2; k++)
165 {
166 array3[k] = ReadBoneReference(input);
167 }
168 modelBone.SetParentAndChildren(newParent, array3);
169 }
170 }
171
172 private ModelBone ReadBoneReference(ContentReader input)
173 {
174 int num = bones.Count + 1;
175 int num2 = 0;
176 num2 = ((num > 255) ? input.ReadInt32() : input.ReadByte());
177 if (num2 != 0)
178 {
179 return bones[num2 - 1];
180 }
181 return null;
182 }
183
184 private void ReadMeshes(ContentReader input)
185 {
186 int num = input.ReadInt32();
187 ModelMesh[] array = new ModelMesh[num];
188 for (int i = 0; i < num; i++)
189 {
190 string name = input.ReadObject<string>();
191 ModelBone parentBone = ReadBoneReference(input);
192 BoundingSphere boundingSphere = default(BoundingSphere);
193 boundingSphere.Center = input.ReadVector3();
194 boundingSphere.Radius = input.ReadSingle();
195 object obj = input.ReadObject<object>();
196 ModelMeshPart[] meshParts = ReadMeshParts(input);
197 array[i] = new ModelMesh(name, parentBone, boundingSphere, meshParts, obj);
198 }
200 }
201
202 private static ModelMeshPart[] ReadMeshParts(ContentReader input)
203 {
204 int num = input.ReadInt32();
205 ModelMeshPart[] meshParts = new ModelMeshPart[num];
206 for (int i = 0; i < num; i++)
207 {
208 int vertexOffset = input.ReadInt32();
209 int numVertices = input.ReadInt32();
210 int startIndex = input.ReadInt32();
211 int primitiveCount = input.ReadInt32();
212 object obj = input.ReadObject<object>();
213 meshParts[i] = new ModelMeshPart(vertexOffset, numVertices, startIndex, primitiveCount, obj);
214 int uniqueCopyOfI = i;
215 input.ReadSharedResource(delegate(VertexBuffer vb)
216 {
217 meshParts[uniqueCopyOfI].vertexBuffer = vb;
218 });
219 input.ReadSharedResource(delegate(IndexBuffer ib)
220 {
221 meshParts[uniqueCopyOfI].indexBuffer = ib;
222 });
223 input.ReadSharedResource(delegate(Effect effect)
224 {
225 meshParts[uniqueCopyOfI].Effect = effect;
226 });
227 }
228 return meshParts;
229 }
230}
void SetParentAndChildren(ModelBone newParent, ModelBone[] newChildren)
Definition ModelBone.cs:42
void ReadBones(ContentReader input)
Definition Model.cs:147
void Draw(Matrix world, Matrix view, Matrix projection)
Definition Model.cs:103
void ReadMeshes(ContentReader input)
Definition Model.cs:184
static Matrix[] sharedDrawBoneMatrices
Definition Model.cs:16
static Model Read(ContentReader input)
Definition Model.cs:137
static ModelMeshPart[] ReadMeshParts(ContentReader input)
Definition Model.cs:202
void CopyAbsoluteBoneTransformsTo(Matrix[] destinationBoneTransforms)
Definition Model.cs:58
void CopyBoneTransformsTo(Matrix[] destinationBoneTransforms)
Definition Model.cs:40
ModelBone ReadBoneReference(ContentReader input)
Definition Model.cs:172
void CopyBoneTransformsFrom(Matrix[] sourceBoneTransforms)
Definition Model.cs:86