Program.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using Windows.ApplicationModel.Core;
  6. using Windows.UI.Core;
  7. using Urho;
  8. using Urho.Actions;
  9. using Urho.HoloLens;
  10. using Urho.Shapes;
  11. namespace $safeprojectname$
  12. {
  13. /// <summary>
  14. /// Windows Holographic application using SharpDX.
  15. /// </summary>
  16. internal class Program
  17. {
  18. [MTAThread]
  19. static void Main()
  20. {
  21. var options = new ApplicationOptions("Data");
  22. var appViewSource = new UrhoAppViewSource<HelloWorldApplication>(options);
  23. appViewSource.UrhoAppViewCreated += AppViewCreated;
  24. Urho.Application.UnhandledException += Application_UnhandledException;
  25. CoreApplication.Run(appViewSource);
  26. }
  27. static void AppViewCreated(UrhoAppView view)
  28. {
  29. view.WindowIsSet += View_WindowIsSet;
  30. view.AppStarted += View_AppStarted;
  31. }
  32. static void Application_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  33. {
  34. Debug.WriteLine(e.Exception);
  35. if (Debugger.IsAttached)
  36. Debugger.Break();
  37. // e.Handled = true;
  38. }
  39. static void View_AppStarted(HoloApplication app)
  40. {
  41. }
  42. static void View_WindowIsSet(CoreWindow window)
  43. {
  44. // Subscribe to CoreWindow events, for example Input
  45. // window.KeyDown +=
  46. }
  47. }
  48. public class HelloWorldApplication : HoloApplication
  49. {
  50. Node earthNode;
  51. public HelloWorldApplication(ApplicationOptions opts) : base(opts) { }
  52. protected override async void Start()
  53. {
  54. // base.Start() creates a basic scene
  55. base.Start();
  56. // Create a node for the Earth
  57. earthNode = Scene.CreateChild();
  58. earthNode.Position = new Vector3(0, 0, 1); //one meter away
  59. earthNode.SetScale(0.2f); //20cm
  60. //subscribe to some input events:
  61. EnableGestureManipulation = true;
  62. EnableGestureTapped = true;
  63. // Create a Sphere component which is basically
  64. // a StaticModel with CoreData\Models\Sphere.mdl model and NoTexture material.
  65. var earth = earthNode.CreateComponent<Sphere>();
  66. // Override the default material (material is a set of tecniques, parameters and textures)
  67. Material earthMaterial = ResourceCache.GetMaterial("Materials/Earth.xml");
  68. earth.SetMaterial(earthMaterial);
  69. // Same for the Moon
  70. var moonNode = earthNode.CreateChild();
  71. const float moonRelativeSize = 1738.1f / 3963.2f;
  72. moonNode.SetScale(moonRelativeSize);
  73. moonNode.Position = new Vector3(1.6f, 0, 0);
  74. var moon = moonNode.CreateComponent<Sphere>();
  75. // Material.FromImage is the easiest way to create a material from an image (using Diff.xml technique)
  76. moon.SetMaterial(Material.FromImage("Textures/Moon.jpg"));
  77. // Run a few actions to spin the Earth and the Moon, do not await these calls as they have RepeatForever action
  78. earthNode.RunActions(new RepeatForever(new RotateBy(duration: 1f, deltaAngleX: 0, deltaAngleY: -4, deltaAngleZ: 0)));
  79. moonNode.RunActions(new RepeatForever(new RotateAroundBy(1f, earthNode.WorldPosition, 0, -3, 0)));
  80. // requires Microphone capability enabled
  81. await RegisterCortanaCommands(new Dictionary<string, Action> {
  82. { "bigger", () => earthNode.Scale *= 1.2f },
  83. { "smaller", () => earthNode.Scale *= 0.8f }
  84. });
  85. await TextToSpeech("Hello world from UrhoSharp!");
  86. }
  87. // HoloLens optical stabilization (optional)
  88. public override Vector3 FocusWorldPoint => earthNode.WorldPosition;
  89. protected override void OnUpdate(float timeStep)
  90. {
  91. }
  92. // handle input:
  93. Vector3 earthPostionBeforeManipulations;
  94. public override void OnGestureManipulationStarted()
  95. {
  96. earthPostionBeforeManipulations = earthNode.Position;
  97. }
  98. public override void OnGestureManipulationUpdated(Vector3 relativeHandPosition)
  99. {
  100. earthNode.Position = relativeHandPosition + earthPostionBeforeManipulations;
  101. }
  102. public override void OnGestureTapped()
  103. {
  104. }
  105. }
  106. }