Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
JsonModelReader.cs
浏览该文件的文档.
1using Engine;
3
4namespace Game {
8 public class JsonModelReader {
9 public static Dictionary<string, List<Vector3>> FacesDic = [];
10 public static Dictionary<string, Vector3> NormalDic = [];
11 public static Dictionary<string, List<int>> FacedirecDic = [];
12 public static Dictionary<float, List<int>> TextureRotate = [];
13
14 static JsonModelReader() {
15 FacesDic.Add("north", [Vector3.UnitX, Vector3.Zero, Vector3.UnitY, new Vector3(1, 1, 0)]);
16 FacesDic.Add("south", [new Vector3(1, 0, 1), Vector3.UnitZ, new Vector3(0, 1, 1), new Vector3(1, 1, 1)]);
17 FacesDic.Add("east", [new Vector3(1, 0, 0), new Vector3(1, 0, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 0)]);
18 FacesDic.Add("west", [Vector3.Zero, Vector3.UnitZ, new Vector3(0, 1, 1), Vector3.UnitY]);
19 FacesDic.Add("up", [Vector3.UnitY, new Vector3(0, 1, 1), Vector3.One, new Vector3(1, 1, 0)]);
20 FacesDic.Add("down", [Vector3.Zero, Vector3.UnitZ, new Vector3(1, 0, 1), new Vector3(1, 0, 0)]);
21 NormalDic.Add("north", new Vector3(0, 0, -1));
22 NormalDic.Add("south", new Vector3(0, 0, 1));
23 NormalDic.Add("east", new Vector3(1, 0, 0));
24 NormalDic.Add("west", new Vector3(-1, 0, 0));
25 NormalDic.Add("up", new Vector3(0, 1, 0));
26 NormalDic.Add("down", new Vector3(0, -1, 0));
27 FacedirecDic.Add("north", [0, 2, 1, 0, 3, 2]); //逆
28 FacedirecDic.Add("west", [0, 2, 1, 0, 3, 2]); //逆
29 FacedirecDic.Add("up", [0, 2, 1, 0, 3, 2]); //逆
30 FacedirecDic.Add("south", [0, 1, 2, 0, 2, 3]); //顺
31 FacedirecDic.Add("east", [0, 1, 2, 0, 2, 3]); //顺
32 FacedirecDic.Add("down", [0, 1, 2, 0, 2, 3]); //顺
33 TextureRotate.Add(
34 0f,
35 [
36 0,
37 3,
38 2,
39 3,
40 2,
41 1,
42 0,
43 1
44 ]
45 );
46 TextureRotate.Add(
47 90f,
48 [
49 0,
50 1,
51 0,
52 3,
53 2,
54 3,
55 2,
56 1
57 ]
58 );
59 TextureRotate.Add(
60 180f,
61 [
62 2,
63 1,
64 0,
65 1,
66 0,
67 3,
68 2,
69 3
70 ]
71 );
72 TextureRotate.Add(
73 270f,
74 [
75 2,
76 3,
77 2,
78 1,
79 0,
80 1,
81 0,
82 3
83 ]
84 );
85 }
86
87 public static float ObjConvertFloat(object obj) {
88 if (obj is double v) {
89 return (float)v;
90 }
91 //else if (obj is int) return (float)(int)obj;
92 if (obj is long v1) {
93 return v1;
94 }
95 throw new Exception($"错误的数据转换,不能将{obj.GetType().Name}转换为float");
96 }
97
98 public static JsonModel Load(Stream stream) => throw new Exception("很抱歉,JsonModel功能暂时下线");
99 /*
100 Dictionary<string, ObjModelReader.ObjMesh> Meshes = [];
101 Vector3 FirstPersonOffset = Vector3.One;
102 Vector3 FirstPersonRotation = Vector3.Zero;
103 Vector3 FirstPersonScale = Vector3.One;
104 Vector3 InHandOffset = Vector3.One;
105 Vector3 InHandRotation = Vector3.Zero;
106 Vector3 InHandScale = Vector3.One;
107 string parent = string.Empty;
108 JsonElement jsonObj = JsonDocument.Parse(new StreamReader(stream).ReadToEnd()).RootElement;
109 Vector2 textureSize = Vector2.Zero;
110 Dictionary<string, string> texturemap = [];
111 if (jsonObj.TryGetProperty("display", out JsonElement obj13) && obj13.ValueKind == JsonValueKind.Object)
112 {
113 if (obj13.TryGetProperty("thirdperson_righthand", out JsonElement jobj1) && jobj1.ValueKind == JsonValueKind.Object)
114 {
115 if (jobj1.TryGetProperty("rotation", out JsonElement jsonArray1) && jsonArray1.ValueKind == JsonValueKind.Array)
116 {
117 InHandRotation = new Vector3(jsonArray1[0].GetSingle(), jsonArray1[1].GetSingle(), jsonArray1[2].GetSingle());
118 }
119 if (jobj1.TryGetProperty("translation", out JsonElement jsonArray2) && jsonArray2.ValueKind == JsonValueKind.Array)
120 {
121 InHandOffset = new Vector3(jsonArray2[0].GetSingle(), jsonArray2[1].GetSingle(), jsonArray2[2].GetSingle());
122 }
123 if (jobj1.TryGetProperty("scale", out JsonElement jsonArray3) && jsonArray3.ValueKind == JsonValueKind.Array)
124 {
125 InHandScale = new Vector3(jsonArray3[0].GetSingle(), jsonArray3[1].GetSingle(), jsonArray3[2].GetSingle());
126 }
127 }
128 if (obj13.TryGetProperty("firstperson_righthand", out JsonElement jobj2) && jobj2.ValueKind == JsonValueKind.Object)
129 {
130 if (jobj2.TryGetProperty("rotation", out JsonElement jsonArray1) && jsonArray1.ValueKind == JsonValueKind.Array)
131 {
132 FirstPersonRotation = new Vector3(jsonArray1[0].GetSingle(), jsonArray1[1].GetSingle(), jsonArray1[2].GetSingle());
133 }
134 if (jobj2.TryGetProperty("translation", out JsonElement jsonArray2) && jsonArray2.ValueKind == JsonValueKind.Array)
135 {
136 FirstPersonOffset = new Vector3(jsonArray2[0].GetSingle(), jsonArray2[1].GetSingle(), jsonArray2[2].GetSingle());
137 }
138 if (jobj2.TryGetProperty("scale", out JsonElement jsonArray3) && jsonArray3.ValueKind == JsonValueKind.Array)
139 {
140 FirstPersonScale = new Vector3(jsonArray3[0].GetSingle(), jsonArray3[1].GetSingle(), jsonArray3[2].GetSingle());
141 }
142 }
143 }
144 if (jsonObj.TryGetProperty("parent", out JsonElement obj12) && obj12.ValueKind == JsonValueKind.String)
145 {
146 parent = obj12.GetString();
147 }
148 if (jsonObj.TryGetProperty("textures", out JsonElement jobj5) && jobj5.ValueKind == JsonValueKind.Object)
149 {
150 foreach (JsonProperty item in jobj5.EnumerateObject())
151 {
152 texturemap.Add(item.Name, item.Value.GetString());
153 }
154 }
155 if (jsonObj.TryGetProperty("texture_size", out JsonElement array1) && array1.ValueKind == JsonValueKind.Array)
156 {
157 textureSize = new Vector2(array1[0].GetSingle(), array1[1].GetSingle());
158 }
159 if (jsonObj.TryGetProperty("elements", out JsonElement array2) && array2.ValueKind == JsonValueKind.Array)
160 {
161 int l = 0;
162 foreach (JsonElement jobj in array2.EnumerateArray())
163 {
164 JsonElement from = jobj.GetProperty("from");
165 JsonElement to = jobj.GetProperty("to");
166 string name = "undefined";
167 if (jobj.TryGetProperty("name", out JsonElement obj8) && obj8.ValueKind == JsonValueKind.String)
168 {
169 name = obj8.GetString();
170 }
171 if (!Meshes.TryGetValue(name, out ObjModelReader.ObjMesh objMesh))
172 {
173 objMesh = new ObjModelReader.ObjMesh(name);
174 objMesh.ElementIndex = l;
175 Meshes.Add(name, objMesh);
176 }
177 //if (jobj.TryGetProperty("rotation", out JsonElement jobj8) && jobj8.ValueKind == JsonValueKind.Object)
178 //{ //处理模型旋转
179 //JsonElement ori = jobj8.GetProperty("origin");
180 //float ang = jobj8.GetProperty("angle").GetSingle();
181 //objMesh.MeshMatrix = Matrix.CreateFromAxisAngle(new Vector3(ObjConvertFloat(ori[0]) / 16f, ObjConvertFloat(ori[1]) / 16f, ObjConvertFloat(ori[2]) / 16f), ang);
182 //}
183 Vector3 start = new(from[0].GetSingle(), from[1].GetSingle(), from[2].GetSingle());
184 Vector3 end = new(to[0].GetSingle(), to[1].GetSingle(), to[2].GetSingle());
185 Matrix transform = Matrix.CreateScale(end.X - start.X, end.Y - start.Y, end.Z - start.Z) * Matrix.CreateTranslation(start.X, start.Y, start.Z) * Matrix.CreateScale(0.0625f);//基础缩放变换
186 if (jobj.TryGetProperty("faces", out JsonElement jsonobj2) && jsonobj2.ValueKind == JsonValueKind.Object)
187 {//每个面,开始生成六个面的顶点数据
188 foreach (JsonProperty jobj2 in jsonobj2.EnumerateObject())
189 {
190 ObjModelReader.ObjMesh childMesh = new(jobj2.Name);
191 List<Vector3> vectors = FacesDic[jobj2.Name];//预取出四个面的点
192 JsonElement jobj3 = jobj2.Value;
193 float rotate = 0f;
194 string facename = jobj2.Name;
195 float[] uvs = new float[4];
196 List<Vector2> TexCoords = [];
197 if (jobj3.TryGetProperty("rotation", out JsonElement obj6) && obj6.ValueKind == JsonValueKind.Number)
198 {//处理uv旋转数据
199 rotate = obj6.GetSingle();
200 }
201 if (jobj3.TryGetProperty("uv", out JsonElement uvarr) && uvarr.ValueKind == JsonValueKind.Array)
202 {//处理uv坐标数据
203 int k = 0;
204 foreach (JsonElement element in uvarr.EnumerateArray())
205 {
206 uvs[k] = element.GetSingle() / 16f;
207 k++;
208 }
209 Vector2 center = (new Vector2(uvs[2] - uvs[0], uvs[3] - uvs[1]) / 2f) + new Vector2(uvs[0], uvs[1]);//中心点
210 TexCoords.Add(new Vector2(uvs[TextureRotate[rotate][0]], uvs[TextureRotate[rotate][1]]));//x1,y2
211 TexCoords.Add(new Vector2(uvs[TextureRotate[rotate][2]], uvs[TextureRotate[rotate][3]]));//x1,y2
212 TexCoords.Add(new Vector2(uvs[TextureRotate[rotate][4]], uvs[TextureRotate[rotate][5]]));//x1,y2
213 TexCoords.Add(new Vector2(uvs[TextureRotate[rotate][6]], uvs[TextureRotate[rotate][7]]));//x1,y2
214 }
215 if (jobj3.TryGetProperty("texture", out JsonElement obj5) && obj5.ValueKind == JsonValueKind.String)
216 {//处理贴图数据
217 if (texturemap.TryGetValue(obj5.GetString().Substring(1), out string path))
218 {
219 childMesh.TexturePath = path;
220 }
221 }
222 ObjModelReader.ObjPosition[] ops = new ObjModelReader.ObjPosition[3];
223 ObjModelReader.ObjTexCood[] ots = new ObjModelReader.ObjTexCood[3];
224 ObjModelReader.ObjNormal[] ons = new ObjModelReader.ObjNormal[3];
225 //生成第一个三角面顶点
226 int c1 = FacedirecDic[facename][0];
227 int c2 = FacedirecDic[facename][1];
228 int c3 = FacedirecDic[facename][2];
229 Vector3 p1 = Vector3.Transform(vectors[c1], transform);
230 Vector3 p2 = Vector3.Transform(vectors[c2], transform);
231 Vector3 p3 = Vector3.Transform(vectors[c3], transform);
232 ops[0] = new ObjModelReader.ObjPosition(p1.X, p1.Y, p1.Z);
233 ops[1] = new ObjModelReader.ObjPosition(p2.X, p2.Y, p2.Z);
234 ops[2] = new ObjModelReader.ObjPosition(p3.X, p3.Y, p3.Z);
235 //生成第一个三角面的纹理坐标
236 Vector2 t1 = TexCoords[c1];
237 Vector2 t2 = TexCoords[c2];
238 Vector2 t3 = TexCoords[c3];
239 ots[0] = new ObjModelReader.ObjTexCood(t1.X, t1.Y);
240 ots[1] = new ObjModelReader.ObjTexCood(t2.X, t2.Y);
241 ots[2] = new ObjModelReader.ObjTexCood(t3.X, t3.Y);
242 //生成第一个三角面的顶点法线
243 //Vector3 normal = NormalDic[facename];
244 int startcount = childMesh.Vertices.Count;
245 childMesh.Indices.Add(startcount++);
246 childMesh.Indices.Add(startcount++);
247 childMesh.Indices.Add(startcount++);
248 childMesh.Vertices.Add(new ObjModelReader.ObjVertex { position = ops[0], objNormal = new ObjModelReader.ObjNormal(0, 0, 0), texCood = ots[0] });
249 childMesh.Vertices.Add(new ObjModelReader.ObjVertex { position = ops[1], objNormal = new ObjModelReader.ObjNormal(0, 0, 0), texCood = ots[1] });
250 childMesh.Vertices.Add(new ObjModelReader.ObjVertex { position = ops[2], objNormal = new ObjModelReader.ObjNormal(0, 0, 0), texCood = ots[2] });
251 //生成第二个三角面
252 c1 = FacedirecDic[facename][3];
253 c2 = FacedirecDic[facename][4];
254 c3 = FacedirecDic[facename][5];
255 p1 = Vector3.Transform(vectors[c1], transform);
256 p2 = Vector3.Transform(vectors[c2], transform);
257 p3 = Vector3.Transform(vectors[c3], transform);
258 ops[0] = new ObjModelReader.ObjPosition(p1.X, p1.Y, p1.Z);
259 ops[1] = new ObjModelReader.ObjPosition(p2.X, p2.Y, p2.Z);
260 ops[2] = new ObjModelReader.ObjPosition(p3.X, p3.Y, p3.Z);
261 //生成第二个三角面的纹理坐标
262 t1 = TexCoords[c1];
263 t2 = TexCoords[c2];
264 t3 = TexCoords[c3];
265 ots[0] = new ObjModelReader.ObjTexCood(t1.X, t1.Y);
266 ots[1] = new ObjModelReader.ObjTexCood(t2.X, t2.Y);
267 ots[2] = new ObjModelReader.ObjTexCood(t3.X, t3.Y);
268 //生成第二个三角面的顶点法线
269 childMesh.Indices.Add(startcount++);
270 childMesh.Indices.Add(startcount++);
271 childMesh.Indices.Add(startcount++);
272 childMesh.Vertices.Add(new ObjModelReader.ObjVertex { position = ops[0], objNormal = new ObjModelReader.ObjNormal(0, 0, 0), texCood = ots[0] });
273 childMesh.Vertices.Add(new ObjModelReader.ObjVertex { position = ops[1], objNormal = new ObjModelReader.ObjNormal(0, 0, 0), texCood = ots[1] });
274 childMesh.Vertices.Add(new ObjModelReader.ObjVertex { position = ops[2], objNormal = new ObjModelReader.ObjNormal(0, 0, 0), texCood = ots[2] });
275 objMesh.ChildMeshes.Add(childMesh);
276 }
277 }
278 }
279 l++;
280 }
281 List<ObjModelReader.ObjMesh> objMeshes = [];
282 foreach (var c in Meshes)
283 {
284 objMeshes.Add(c.Value);
285 }
286 if (jsonObj.TryGetProperty("groups", out JsonElement jsonArray) && jsonArray.ValueKind == JsonValueKind.Array)
287 {//解析groups
288 Dictionary<string, ObjModelReader.ObjMesh> Meshes2 = [];
289 int m = 0;
290 foreach (JsonElement jobj10 in jsonArray.EnumerateArray())
291 {
292 string jobj10name = m.ToString();
293 if (jobj10.TryGetProperty("name", out JsonElement jobj10name_) && jobj10name_.ValueKind == JsonValueKind.String) jobj10name = jobj10name_.GetString();
294 ObjModelReader.ObjMesh mesh = new(jobj10name);
295 if (jobj10.TryGetProperty("oringin", out JsonElement jarr2) && jarr2.ValueKind == JsonValueKind.Array)
296 {
297 Vector3 start = new Vector3(jarr2[0].GetSingle(), jarr2[1].GetSingle(), jarr2[2].GetSingle()) / 16f;
298 mesh.MeshMatrix = Matrix.CreateTranslation(start.X, start.Y, start.Z);
299 }
300 if (jobj10.TryGetProperty("children", out JsonElement jarr3) && jarr3.ValueKind == JsonValueKind.Array)
301 {
302 int i = 0;
303 foreach (JsonElement eleindex in jarr3.EnumerateArray())
304 {
305 mesh.ChildMeshes.Add(objMeshes.Find(xp => xp.ElementIndex == (int)eleindex.GetSingle()));
306 i++;
307 }
308 }
309 Meshes2.Add(jobj10name, mesh);
310 m++;
311 }
312 JsonModel jsonModel = ObjModelReader.ObjMeshesToModel<JsonModel>(Meshes2);
313 //if (string.IsNullOrEmpty(parent) == false) try { jsonModel.ParentModel = ContentManager.Get<JsonModel>(parent); } catch { }
314 jsonModel.InHandScale = InHandScale;
315 jsonModel.InHandOffset = InHandOffset;
316 jsonModel.InHandRotation = InHandRotation;
317 jsonModel.FirstPersonOffset = FirstPersonOffset;
318 jsonModel.FirstPersonScale = FirstPersonScale;
319 jsonModel.FirstPersonRotation = FirstPersonRotation;
320 return jsonModel;
321 }
322
323 JsonModel jsonModel2 = ObjModelReader.ObjMeshesToModel<JsonModel>(Meshes);
324 //if (string.IsNullOrEmpty(parent) == false) try { jsonModel2.ParentModel = ContentManager.Get<JsonModel>(parent); } catch { }
325 jsonModel2.InHandScale = InHandScale;
326 jsonModel2.InHandOffset = InHandOffset;
327 jsonModel2.InHandRotation = InHandRotation;
328 jsonModel2.FirstPersonOffset = FirstPersonOffset;
329 jsonModel2.FirstPersonScale = FirstPersonScale;
330 jsonModel2.FirstPersonRotation = FirstPersonRotation;
331 return jsonModel2;
332 */
333 }
334
345}
Engine.Vector3 Vector3
static Dictionary< string, List< Vector3 > > FacesDic
static Dictionary< float, List< int > > TextureRotate
static Dictionary< string, List< int > > FacedirecDic
static Dictionary< string, Vector3 > NormalDic
static float ObjConvertFloat(object obj)
static JsonModel Load(Stream stream)
static readonly Vector3 One
static readonly Vector3 Zero
static readonly Vector3 UnitX
static readonly Vector3 UnitY
static readonly Vector3 UnitZ