WalkMesh.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. private Cameras Cameras;
  22. public static WalkMesh Load(byte[] idb,Cameras c)
  23. {
  24. if (idb == null || idb.Length == 0) return null;
  25. var r = new WalkMesh
  26. {
  27. Cameras = c
  28. };
  29. r.ReadData(idb);
  30. return r;
  31. }
  32. public List<Access> Accesses { get; set; }
  33. public List<VertexPositionColor> Vertices { get; set; }
  34. public int Count => Vertices?.Count / 3 ?? 0;
  35. private void ReadData(byte[] idb)
  36. {
  37. using (var br = new BinaryReader(new MemoryStream(idb)))
  38. {
  39. var count = checked((int)br.ReadUInt32());
  40. const int sides = 3;
  41. var vs = new List<Vert>(count * sides);
  42. Accesses = new List<Access>(count);
  43. foreach (var i in Enumerable.Range(0, vs.Capacity))
  44. vs.Add(new Vert { x = br.ReadInt16(), y = br.ReadInt16(), z = br.ReadInt16(), res = br.ReadInt16() });
  45. foreach (var i in Enumerable.Range(0, Accesses.Capacity))
  46. Accesses.Add(new Access { br.ReadInt16(), br.ReadInt16(), br.ReadInt16() });
  47. Debug.Assert(br.BaseStream.Position == br.BaseStream.Length);
  48. max = new Vector3(vs.Max(x => x.x), vs.Max(x => x.y), vs.Max(x => x.z));
  49. min = new Vector3(vs.Min(x => x.x), vs.Min(x => x.y), vs.Min(x => x.z));
  50. distance = max - min;
  51. var maxvalue = Math.Max(Math.Max(distance.X, distance.Y), distance.Z);
  52. //Matrix scale = Matrix.CreateTranslation(0, 0, -Module.Cameras[0].Zoom);
  53. //Matrix scale = Matrix.CreateScale(1f / 1f);
  54. var scale = Matrix.CreateScale(Cameras[0].Zoom / 4096f);
  55. var move = Matrix.CreateTranslation(Cameras[0].Position*4096f);
  56. Vertices = vs.Select((x, i) => new VertexPositionColor(Vector3.Transform(Vector3.Transform(Vector3.Transform(new Vector3(x.x, x.y , x.z), Cameras[0].RotationMatrix),move), scale), i % sides == 0 ? Color.Red : i % sides == 1 ? Color.Green : Color.Blue)).ToList();
  57. }
  58. }
  59. public Vector3 max { get; set; }
  60. public Vector3 min { get; set; }
  61. public Vector3 distance { get; set; }
  62. }
  63. }