Sample.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. // Copyright (c) 2015 Xamarin Inc
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. using System;
  24. using System.Globalization;
  25. using AtomicEngine;
  26. namespace FeatureExamples
  27. {
  28. public class Sample : NETScriptObject
  29. {
  30. protected const float PixelSize = 0.01f;
  31. protected const float TouchSensitivity = 2;
  32. protected float Yaw { get; set; }
  33. protected float Pitch { get; set; }
  34. protected bool TouchEnabled { get; set; }
  35. protected Node CameraNode { get; set; }
  36. protected Scene scene;
  37. protected UIView UIView { get; set; }
  38. protected Sample()
  39. {
  40. UIView = SampleSelector.UIView;
  41. }
  42. protected void Exit() { GetSubsystem<Engine>().Exit(); }
  43. static readonly Random random = new Random();
  44. /// Return a random float between 0.0 (inclusive) and 1.0 (exclusive.)
  45. public static float NextRandom() { return (float)random.NextDouble(); }
  46. /// Return a random float between 0.0 and range, inclusive from both ends.
  47. public static float NextRandom(float range) { return (float)random.NextDouble() * range; }
  48. /// Return a random float between min and max, inclusive from both ends.
  49. public static float NextRandom(float min, float max) { return (float)((random.NextDouble() * (max - min)) + min); }
  50. /// Return a random integer between min and max - 1.
  51. public static int NextRandom(int min, int max) { return random.Next(min, max); }
  52. public virtual void Start()
  53. {
  54. SubscribeToEvent<UpdateEvent>((e) => { Update(e.TimeStep); });
  55. SubscribeToEvent<KeyDownEvent>(HandleKeyDown);
  56. }
  57. protected virtual void Stop()
  58. {
  59. }
  60. protected virtual void Update(float timeStep)
  61. {
  62. MoveCameraByTouches(timeStep);
  63. }
  64. /// <summary>
  65. /// Move camera for 2D samples
  66. /// </summary>
  67. protected void SimpleMoveCamera2D(float timeStep)
  68. {
  69. // Movement speed as world units per second
  70. const float moveSpeed = 4.0f;
  71. var input = GetSubsystem<Input>();
  72. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  73. if (input.GetKeyDown(Constants.KEY_W)) CameraNode.Translate(Vector3.UnitY * moveSpeed * timeStep);
  74. if (input.GetKeyDown(Constants.KEY_S)) CameraNode.Translate(-Vector3.UnitY * moveSpeed * timeStep);
  75. if (input.GetKeyDown(Constants.KEY_A)) CameraNode.Translate(-Vector3.UnitX * moveSpeed * timeStep);
  76. if (input.GetKeyDown(Constants.KEY_D)) CameraNode.Translate(Vector3.UnitX * moveSpeed * timeStep);
  77. if (input.GetKeyDown(Constants.KEY_PAGEUP))
  78. {
  79. Camera camera = CameraNode.GetComponent<Camera>();
  80. camera.Zoom = camera.Zoom * 1.01f;
  81. }
  82. if (input.GetKeyDown(Constants.KEY_PAGEDOWN))
  83. {
  84. Camera camera = CameraNode.GetComponent<Camera>();
  85. camera.Zoom = camera.Zoom * 0.99f;
  86. }
  87. }
  88. /// <summary>
  89. /// Move camera for 3D samples
  90. /// </summary>
  91. protected void SimpleMoveCamera3D(float timeStep, float moveSpeed = 10.0f)
  92. {
  93. const float mouseSensitivity = .1f;
  94. var input = GetSubsystem<Input>();
  95. var mouseMove = input.MouseMove;
  96. Yaw += mouseSensitivity * mouseMove.X;
  97. Pitch += mouseSensitivity * mouseMove.Y;
  98. Pitch = MathHelper.Clamp(Pitch, -90, 90);
  99. CameraNode.Rotation = new Quaternion(Pitch, Yaw, 0);
  100. if (input.GetKeyDown(Constants.KEY_W)) CameraNode.Translate(Vector3.UnitZ * moveSpeed * timeStep);
  101. if (input.GetKeyDown(Constants.KEY_S)) CameraNode.Translate(-Vector3.UnitZ * moveSpeed * timeStep);
  102. if (input.GetKeyDown(Constants.KEY_A)) CameraNode.Translate(-Vector3.UnitX * moveSpeed * timeStep);
  103. if (input.GetKeyDown(Constants.KEY_D)) CameraNode.Translate(Vector3.UnitX * moveSpeed * timeStep);
  104. }
  105. protected void MoveCameraByTouches(float timeStep)
  106. {
  107. if (!TouchEnabled || CameraNode == null)
  108. return;
  109. var input = GetSubsystem<Input>();
  110. for (uint i = 0, num = input.NumTouches; i < num; ++i)
  111. {
  112. var delta = input.GetTouchDelta(i);
  113. if (delta.X != 0 || delta.Y != 0)
  114. {
  115. var camera = CameraNode.GetComponent<Camera>();
  116. if (camera == null)
  117. return;
  118. var graphics = GetSubsystem<Graphics>();
  119. Yaw += TouchSensitivity * camera.Fov / graphics.Height * delta.X;
  120. Pitch += TouchSensitivity * camera.Fov / graphics.Height * delta.Y;
  121. CameraNode.Rotation = new Quaternion(Pitch, Yaw, 0);
  122. }
  123. else
  124. {
  125. //var cursor = UI.Cursor;
  126. //if (cursor != null && cursor.Visible)
  127. // cursor.Position = state.Position;
  128. }
  129. }
  130. }
  131. protected void SimpleCreateInstructionsWithWasd(string extra = "")
  132. {
  133. SimpleCreateInstructions("Use WASD keys and mouse/touch to move" + extra);
  134. }
  135. protected void SimpleCreateInstructions(string text = "")
  136. {
  137. var layout = new UILayout();
  138. layout.Rect = UIView.Rect;
  139. layout.LayoutPosition = UI_LAYOUT_POSITION.UI_LAYOUT_POSITION_RIGHT_BOTTOM;
  140. layout.LayoutDistributionPosition = UI_LAYOUT_DISTRIBUTION_POSITION.UI_LAYOUT_DISTRIBUTION_POSITION_RIGHT_BOTTOM;
  141. var fontDesc = new UIFontDescription();
  142. fontDesc.Id = "Vera";
  143. fontDesc.Size = 18;
  144. var label = new UIEditField();
  145. label.FontDescription = fontDesc;
  146. label.ReadOnly = true;
  147. label.Multiline = true;
  148. label.AdaptToContentSize = true;
  149. label.Text = text;
  150. layout.AddChild(label);
  151. UIView.AddChild(layout);
  152. }
  153. void CreateLogo()
  154. {
  155. }
  156. void SetWindowAndTitleIcon()
  157. {
  158. }
  159. void CreateConsoleAndDebugHud()
  160. {
  161. }
  162. protected void BackToSelector()
  163. {
  164. Stop();
  165. GetSubsystem<Input>().SetMouseVisible(true);
  166. UnsubscribeFromAllEvents();
  167. var renderer = GetSubsystem<Renderer>();
  168. for (uint i = 0; i < renderer.NumViewports; i++)
  169. {
  170. renderer.SetViewport(i, null);
  171. }
  172. SampleSelector.sampleRef = null;
  173. SampleSelector.UIView.DeleteAllChildren();
  174. scene?.Dispose();
  175. GetSubsystem<ResourceCache>().ReleaseAllResources();
  176. new SampleSelector();
  177. }
  178. void HandleKeyDown(KeyDownEvent e)
  179. {
  180. var renderer = GetSubsystem<Renderer>();
  181. switch (e.Key)
  182. {
  183. case Constants.KEY_ESCAPE:
  184. BackToSelector();
  185. return;
  186. case Constants.KEY_F1:
  187. GetSubsystem<UI>().ToggleConsole();
  188. return;
  189. case Constants.KEY_F2:
  190. GetSubsystem<UI>().ToggleDebugHud();
  191. return;
  192. }
  193. switch (e.Key)
  194. {
  195. case Constants.KEY_1:
  196. var quality = renderer.TextureQuality;
  197. ++quality;
  198. if (quality > 2)
  199. quality = 0;
  200. renderer.TextureQuality = quality;
  201. break;
  202. case Constants.KEY_2:
  203. var mquality = renderer.MaterialQuality;
  204. ++mquality;
  205. if (mquality > 2)
  206. mquality = 0;
  207. renderer.MaterialQuality = mquality;
  208. break;
  209. case Constants.KEY_3:
  210. renderer.SpecularLighting = !renderer.SpecularLighting;
  211. break;
  212. case Constants.KEY_4:
  213. renderer.DrawShadows = !renderer.DrawShadows;
  214. break;
  215. case Constants.KEY_5:
  216. var shadowMapSize = renderer.ShadowMapSize;
  217. shadowMapSize *= 2;
  218. if (shadowMapSize > 2048)
  219. shadowMapSize = 512;
  220. renderer.ShadowMapSize = shadowMapSize;
  221. break;
  222. // shadow depth and filtering quality
  223. case Constants.KEY_6:
  224. var q = (int)renderer.ShadowQuality++;
  225. if (q > 3)
  226. q = 0;
  227. renderer.ShadowQuality = (ShadowQuality)q;
  228. break;
  229. // occlusion culling
  230. case Constants.KEY_7:
  231. var o = !(renderer.MaxOccluderTriangles > 0);
  232. renderer.MaxOccluderTriangles = o ? 5000 : 0;
  233. break;
  234. // instancing
  235. case Constants.KEY_8:
  236. renderer.DynamicInstancing = !renderer.DynamicInstancing;
  237. break;
  238. case Constants.KEY_9:
  239. Image screenshot = new Image();
  240. GetSubsystem<Graphics>().TakeScreenShot(screenshot);
  241. screenshot.SavePNG(GetSubsystem<FileSystem>().ProgramDir + $"Data/Screenshot_{GetType().Name}_{DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss", CultureInfo.InvariantCulture)}.png");
  242. break;
  243. }
  244. }
  245. void InitTouchInput()
  246. {
  247. TouchEnabled = true;
  248. }
  249. }
  250. }