89 Dictionary<string, ObjMesh> Meshes = [];
90 Dictionary<string, string> TexturePaths = [];
91 List<ObjPosition> objPositions = [];
92 List<ObjTexCood> objTexCoods = [];
93 List<ObjNormal> objNormals = [];
95 StreamReader streamReader =
new(stream);
97 string CurrentTkey =
null;
98 while (!streamReader.EndOfStream) {
99 string line = streamReader.ReadLine();
100 string[] spl = line.Split([(
char)0x09, (
char)0x20], StringSplitOptions.None);
108 if (Meshes.TryGetValue(spl[1], out
ObjMesh mesh)) {
113 Meshes.Add(spl[1], objMesh);
118 objPositions.Add(
new ObjPosition(spl[1], spl[2], spl[3]));
122 objTexCoods.Add(
new ObjTexCood(spl[1], spl[2]));
126 objNormals.Add(
new ObjNormal(spl[1], spl[2], spl[3]));
130 if (TexturePaths.TryGetValue(spl[1], out CurrentTkey)) {
136 if (
string.IsNullOrEmpty(CurrentTkey)) {
137 CurrentTkey =
"Textures/NoneTexture";
139 objMesh.TexturePath = CurrentTkey;
140 int SideCount = spl.Length - 1;
141 if (SideCount != 3) {
142 throw new Exception(
"模型必须为三角面");
146 while (++i < spl.Length) {
147 string[] param = spl[i].Split([
'/'], StringSplitOptions.None);
148 if (param.Length != 3) {
149 throw new Exception(
"面参数错误");
151 int pa =
int.Parse(param[0]);
152 int pb =
int.Parse(param[1]);
153 int pc =
int.Parse(param[2]);
156 ObjNormal objNormal = objNormals[pc - 1];
159 objMesh.
Vertices.
Add(
new ObjVertex { position = objPosition, objNormal = objNormal, texCood = texCood });
166 return ObjMeshesToModel<ObjModel>(Meshes);
179 objMesh.Vertices.Count
181 MemoryStream stream1 =
new();
182 MemoryStream stream2 =
new();
183 BinaryWriter binaryWriter1 =
new(stream1);
184 BinaryWriter binaryWriter2 =
new(stream2);
187 binaryWriter1.Write(objVertex.
position.
x);
188 binaryWriter1.Write(objVertex.
position.
y);
189 binaryWriter1.Write(objVertex.
position.
z);
193 binaryWriter1.Write(objVertex.
texCood.
tx);
194 binaryWriter1.Write(objVertex.
texCood.
ty);
197 binaryWriter2.Write(objMesh.
Indices[i]);
199 byte[] vs = stream1.ToArray();
200 byte[] ins = stream2.ToArray();
204 vertexBuffer.Tag = vs;
207 indexBuffer.Tag = ins;
218 Type ctype = typeof(T);
219 if (!ctype.IsSubclassOf(typeof(
Model))) {
220 throw new Exception($
"不能将{ctype.Name}转换为Model类型");
222#pragma warning disable IL2087
223 object iobj = Activator.CreateInstance(ctype);
224#pragma warning restore IL2087
227 foreach (KeyValuePair<string, ObjMesh> c
in Meshes) {