WalkMesh.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Linq;
  8. namespace OpenVIII.Fields
  9. {
  10. /// <summary>
  11. /// WalkMesh
  12. /// </summary>
  13. /// <see cref="http://wiki.ffrtt.ru/index.php?title=FF7/Field/Walkmesh"/>
  14. /// <seealso cref="https://github.com/myst6re/deling/blob/master/WalkmeshGLWidget.cpp"/>
  15. /// <seealso cref="https://github.com/myst6re/deling/blob/master/WalkmeshGLWidget.h"/>
  16. /// <seealso cref="https://github.com/myst6re/deling/blob/master/files/InfFile.cpp"/>
  17. /// <seealso cref="https://github.com/myst6re/deling/blob/master/files/IdFile.h"/>
  18. /// <seealso cref="https://github.com/q-gears/q-gears/blob/master/utilities/ffvii_field_model_exporter/src/MeshFile.cpp"/>
  19. public partial class WalkMesh
  20. {
  21. public static WalkMesh Load(byte[] idb)
  22. {
  23. WalkMesh r = new WalkMesh();
  24. r.ReadData(idb);
  25. return r;
  26. }
  27. public List<Access> Accesses { get; private set; }
  28. public List<VertexPositionColor> Vertices { get; private set; }
  29. public int Count => Vertices?.Count / 3 ?? 0;
  30. private void ReadData(byte[] idb)
  31. {
  32. using (BinaryReader br = new BinaryReader(new MemoryStream(idb)))
  33. {
  34. int count = checked((int)br.ReadUInt32());
  35. const int sides = 3;
  36. List<Vert> vs = new List<Vert>(count * sides);
  37. Accesses = new List<Access>(count);
  38. foreach (int i in Enumerable.Range(0, vs.Capacity))
  39. vs.Add(new Vert { x = br.ReadInt16(), y = br.ReadInt16(), z = br.ReadInt16(), res = br.ReadInt16() });
  40. foreach (int i in Enumerable.Range(0, Accesses.Capacity))
  41. Accesses.Add(new Access { br.ReadInt16(), br.ReadInt16(), br.ReadInt16() });
  42. Debug.Assert(br.BaseStream.Position == br.BaseStream.Length);
  43. max = new Vector3(vs.Max(x => x.x), vs.Max(x => x.y), vs.Max(x => x.z));
  44. min = new Vector3(vs.Min(x => x.x), vs.Min(x => x.y), vs.Min(x => x.z));
  45. distance = max - min;
  46. float maxvalue = Math.Max(Math.Max(distance.X, distance.Y), distance.Z);
  47. //Matrix scale = Matrix.CreateTranslation(0, 0, -Module.Cameras[0].Zoom);
  48. //Matrix scale = Matrix.CreateScale(1f / 1f);
  49. Matrix scale = Matrix.CreateScale(Module.Cameras[0].Zoom / 4096f);
  50. Matrix move = Matrix.CreateTranslation(Module.Cameras[0].Position*4096f);
  51. Vertices = vs.Select((x, i) => new VertexPositionColor(Vector3.Transform(Vector3.Transform(Vector3.Transform(new Vector3(x.x, x.y , x.z), Module.Cameras[0].RotationMatrix),move), scale), i % sides == 0 ? Color.Red : i % sides == 1 ? Color.Green : Color.Blue)).ToList();
  52. }
  53. }
  54. public Vector3 max { get; private set; }
  55. public Vector3 min { get; private set; }
  56. public Vector3 distance { get; private set; }
  57. }
  58. }