module_field_object_test.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace OpenVIII.Fields
  6. {
  7. /// <summary>
  8. /// this works only as a preview for field models and proof-of-concept
  9. /// </summary>
  10. public class ModuleFieldObjectTest
  11. {
  12. #region Fields
  13. public static AlphaTestEffect Ate;
  14. public static BasicEffect Effect;
  15. //private static float _camDistance = 10.0f;
  16. private const float RenderCamDistance = 1200f;
  17. private static int _animFrame;
  18. private static int _animId;
  19. private static bool _bInitialized;
  20. private static Vector3 _camPosition, _camTarget;
  21. private static FieldCharaOne _charaOne;
  22. private static int _debugModelId;
  23. private static float _degrees;
  24. private static FPS_Camera _fpsCamera;
  25. private static int _lastFieldId = -1;
  26. private static Matrix _projectionMatrix, _viewMatrix, _worldMatrix;
  27. private static double _timer;
  28. #endregion Fields
  29. #region Methods
  30. public static void Draw()
  31. {
  32. Memory.Graphics.GraphicsDevice.RasterizerState = RasterizerState.CullNone;
  33. //Memory.graphics.GraphicsDevice.BlendState = BlendState.NonPremultiplied;
  34. Memory.Graphics.GraphicsDevice.BlendState = BlendState.AlphaBlend;
  35. Memory.Graphics.GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  36. Memory.Graphics.GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp;
  37. Memory.Graphics.GraphicsDevice.Clear(Color.Aqua);
  38. if (!_bInitialized)
  39. return;
  40. uint maxAnim = 0;
  41. uint maxFrame = 0;
  42. Ate.Projection = _projectionMatrix;
  43. Ate.View = _viewMatrix;
  44. Ate.World = _worldMatrix;
  45. Effect.Projection = _projectionMatrix;
  46. Effect.View = _viewMatrix;
  47. Effect.World = _worldMatrix;
  48. if (_charaOne.FieldModels == null)
  49. goto _doNotDraw;
  50. if (_debugModelId >= _charaOne.FieldModels.Length)
  51. _debugModelId = 0;
  52. var whichModel = _debugModelId;
  53. if (_charaOne.FieldModels[whichModel].MCH == null)
  54. goto _doNotDraw;
  55. _charaOne.FieldModels[whichModel].MCH.AssignTextureSizes(
  56. _charaOne.FieldModels[whichModel].Textures,
  57. Enumerable.Range(0, _charaOne.FieldModels[whichModel].Textures.Length).ToArray());
  58. maxAnim = _charaOne.FieldModels[whichModel].MCH.GetAnimationCount();
  59. if (_animId >= maxAnim)
  60. _animId = 0;
  61. maxFrame = _charaOne.FieldModels[whichModel].MCH.GetAnimationFramesCount(_animId);
  62. if (_animFrame >= maxFrame)
  63. _animFrame = 0;
  64. var charaCollection =
  65. _charaOne.FieldModels[whichModel].MCH.GetVertexPositions(Vector3.Zero, Quaternion.Identity, _animId, _animFrame);
  66. var vptCollection = new Dictionary<Texture2D, List<VertexPositionColorTexture>>();
  67. for (var i = 0; i < charaCollection.Item2.Length; i += 3)
  68. {
  69. var charaTexture = _charaOne.FieldModels[whichModel].Textures[charaCollection.Item2[i]];
  70. if (!vptCollection.ContainsKey(charaTexture))
  71. vptCollection.Add(charaTexture, new List<VertexPositionColorTexture>());
  72. vptCollection[charaTexture].AddRange(charaCollection.Item1.Skip(i).Take(3).ToArray());
  73. }
  74. foreach (var kvp in vptCollection)
  75. {
  76. Ate.Texture = kvp.Key;
  77. foreach (var pass in Ate.CurrentTechnique.Passes)
  78. {
  79. pass.Apply();
  80. Memory.Graphics.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, kvp.Value.ToArray(), 0, kvp.Value.Count / 3);
  81. }
  82. }
  83. _doNotDraw:
  84. Memory.SpriteBatchStartAlpha();
  85. if (_charaOne.FieldModels == null)
  86. {
  87. Memory.Font.RenderBasicText(
  88. $"FIELD AT: {Memory.FieldHolder.FieldID} - {Memory.FieldHolder.GetString()}\n" +
  89. $"World Map Camera: ={_camPosition}\n" +
  90. $"FPS camera degrees: ={_degrees}°\n" +
  91. "Current model is: =BROKEN\n" +
  92. $"Animation={_animId + 1} of {maxAnim} -- frame: {_animFrame + 1} of {maxFrame}\n" +
  93. "F1 - re-init (for reparsing and live code debugging)\n" +
  94. "F2 - Next field\n" +
  95. "F3 - Previous field\n" +
  96. "F4 - Next animation\n" +
  97. "LMB - Next NPC model\n" +
  98. "NULL: =0", 30, 20, 1f, 2f, lineSpacing: 5);
  99. }
  100. else
  101. Memory.Font.RenderBasicText(
  102. $"FIELD AT: {Memory.FieldHolder.FieldID} - {Memory.FieldHolder.GetString()}\n" +
  103. $"World Map Camera: ={_camPosition}\n" +
  104. $"FPS camera degrees: ={_degrees}°\n" +
  105. $"Current model is: ={_debugModelId + 1} of {_charaOne.FieldModels.Length} which is {new string(_charaOne.FieldModels[_debugModelId].ModelName, 0, 4)}\n" +
  106. $"Animation={_animId + 1} of {maxAnim} -- frame: {_animFrame + 1} of {maxFrame}\n" +
  107. "F1 - re-init (for reparsing and live code debugging)\n" +
  108. "F2 - Next field\n" +
  109. "F3 - Previous field\n" +
  110. "F4 - Next animation\n" +
  111. "LMB - Next NPC model\n" +
  112. "NULL: =0", 30, 20, 1f, 2f, lineSpacing: 5);
  113. Memory.SpriteBatchEnd();
  114. }
  115. public static void Update()
  116. {
  117. if (!_bInitialized)
  118. {
  119. _fpsCamera = new FPS_Camera();
  120. //init renderer
  121. Effect = new BasicEffect(Memory.Graphics.GraphicsDevice);
  122. Effect.EnableDefaultLighting();
  123. Effect.TextureEnabled = true;
  124. Effect.DirectionalLight0.Enabled = true;
  125. Effect.DirectionalLight1.Enabled = false;
  126. Effect.DirectionalLight2.Enabled = false;
  127. Effect.DirectionalLight0.Direction = new Vector3(
  128. -0.349999f,
  129. 0.499999f,
  130. -0.650000f
  131. );
  132. Effect.DirectionalLight0.SpecularColor = new Vector3(0.8500003f, 0.8500003f, 0.8500003f);
  133. Effect.DirectionalLight0.DiffuseColor = new Vector3(1.54999f, 1.54999f, 1.54999f);
  134. _camTarget = new Vector3(0, 0f, 0f);
  135. _camPosition = new Vector3(0, 0f, 0f);
  136. _projectionMatrix = Matrix.CreatePerspectiveFieldOfView(
  137. MathHelper.ToRadians(60),
  138. Memory.Graphics.GraphicsDevice.Viewport.AspectRatio,
  139. 1f, 10000f);
  140. _viewMatrix = Matrix.CreateLookAt(_camPosition, _camTarget,
  141. new Vector3(0f, 1f, 0f));// Y up
  142. //worldMatrix = Matrix.CreateWorld(camTarget, Vector3.
  143. // Forward, Vector3.Up);
  144. _worldMatrix = Matrix.CreateTranslation(0, 0, 0);
  145. //temporarily disabling this, because I'm getting more and more tired of this music playing over and over when debugging
  146. //Memory.musicIndex = 30;
  147. //AV.Music.Play();
  148. Ate = new AlphaTestEffect(Memory.Graphics.GraphicsDevice)
  149. {
  150. Projection = _projectionMatrix,
  151. View = _viewMatrix,
  152. World = _worldMatrix,
  153. FogEnabled = false,
  154. FogColor = Color.CornflowerBlue.ToVector3(),
  155. FogStart = 9.75f,
  156. FogEnd = RenderCamDistance
  157. };
  158. _bInitialized = true;
  159. }
  160. if (_lastFieldId != Memory.FieldHolder.FieldID)
  161. ReInit();
  162. _viewMatrix = _fpsCamera.Update(ref _camPosition, ref _camTarget, ref _degrees);
  163. if (Input2.Button(MouseButtons.LeftButton, ButtonTrigger.OnRelease))
  164. _debugModelId++;
  165. if (Input2.Button(Microsoft.Xna.Framework.Input.Keys.F1, ButtonTrigger.OnRelease))
  166. ReInit();
  167. if (Input2.Button(Microsoft.Xna.Framework.Input.Keys.F2))
  168. Memory.FieldHolder.FieldID++;
  169. if (Input2.Button(Microsoft.Xna.Framework.Input.Keys.F3))
  170. Memory.FieldHolder.FieldID--;
  171. if (Input2.Button(Microsoft.Xna.Framework.Input.Keys.F4))
  172. {
  173. _animId++;
  174. _animFrame = 0;
  175. }
  176. _timer += Memory.ElapsedGameTime.TotalMilliseconds / 1000.0d;
  177. if (_timer > 0.033d)
  178. {
  179. _animFrame++;
  180. _timer = 0f;
  181. }
  182. }
  183. private static void ReInit()
  184. {
  185. _lastFieldId = Memory.FieldHolder.FieldID;
  186. _charaOne = new FieldCharaOne(Memory.FieldHolder.FieldID);
  187. }
  188. #endregion Methods
  189. }
  190. }