module_field_object_test.cs 9.0 KB

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