Program.cs 3.1 KB

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