Program.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using Windows.ApplicationModel.Core;
  4. using Urho;
  5. using Urho.Actions;
  6. using Urho.SharpReality;
  7. using Urho.Shapes;
  8. using Urho.Resources;
  9. namespace $safeprojectname$
  10. {
  11. internal class Program
  12. {
  13. [MTAThread]
  14. static void Main()
  15. {
  16. var appViewSource = new UrhoAppViewSource<HelloWorldApplication>(new ApplicationOptions("Data"));
  17. appViewSource.UrhoAppViewCreated += OnViewCreated;
  18. CoreApplication.Run(appViewSource);
  19. }
  20. static void OnViewCreated(UrhoAppView view) { }
  21. }
  22. public class HelloWorldApplication : StereoApplication
  23. {
  24. Node earthNode;
  25. Material earthMaterial;
  26. float cloudsOffset;
  27. public HelloWorldApplication(ApplicationOptions opts) : base(opts) { }
  28. protected override async void Start()
  29. {
  30. // Create a basic scene, see StereoApplication
  31. base.Start();
  32. // Enable input
  33. EnableGestureManipulation = true;
  34. EnableGestureTapped = true;
  35. // Create a node for the Earth
  36. earthNode = Scene.CreateChild();
  37. earthNode.Position = new Vector3(0, 0, 1.5f);
  38. earthNode.SetScale(0.3f);
  39. // Scene has a lot of pre-configured components, such as Cameras (eyes), Lights, etc.
  40. DirectionalLight.Brightness = 1f;
  41. DirectionalLight.Node.SetDirection(new Vector3(-1, 0, 0.5f));
  42. //Sphere is just a StaticModel component with Sphere.mdl as a Model.
  43. var earth = earthNode.CreateComponent<Sphere>();
  44. earthMaterial = ResourceCache.GetMaterial("Materials/Earth.xml");
  45. earth.SetMaterial(earthMaterial);
  46. var moonNode = earthNode.CreateChild();
  47. moonNode.SetScale(0.27f);
  48. moonNode.Position = new Vector3(1.2f, 0, 0);
  49. var moon = moonNode.CreateComponent<Sphere>();
  50. moon.SetMaterial(ResourceCache.GetMaterial("Materials/Moon.xml"));
  51. var marsNode = earthNode.CreateChild();
  52. marsNode.Scale = new Vector3(0.7f, 0.7f, 0.7f);
  53. marsNode.Rotation = new Quaternion(x: 0, y: 0, z: 45);
  54. marsNode.Position = new Vector3(3f, 0.5f, 1);
  55. var mars = marsNode.CreateComponent<StaticModel>();
  56. mars.Model = CoreAssets.Models.Sphere; //Same as Sphere, here is just as an example.
  57. //A simple material created from an image:
  58. mars.SetMaterial(Material.FromImage("Textures/Mars.jpg"));
  59. //requires >=15063 build
  60. //var display = Windows.Graphics.Holographic.HolographicDisplay.GetDefault();
  61. if (true)// display != null && !display.IsOpaque)
  62. {
  63. //HoloLens - do nothing
  64. }
  65. else
  66. {
  67. //Since the display is opaque - we can display a custom skybox
  68. var skyboxNode = Scene.CreateChild();
  69. skyboxNode.SetScale(100);
  70. var skybox = skyboxNode.CreateComponent<Skybox>();
  71. skybox.Model = CoreAssets.Models.Box; //see CoreAssets
  72. //Skybox is usally a set of six textures joined together, see FeatureSamples/Core/23_Water sample
  73. skybox.SetMaterial(Material.SkyboxFromImage("Textures/Space.png"));
  74. }
  75. // Run a few actions to spin the Earth, the Moon and the clouds.
  76. earthNode.RunActions(new RepeatForever(new RotateBy(duration: 1f, deltaAngleX: 0, deltaAngleY: -4, deltaAngleZ: 0)));
  77. await TextToSpeech("Hello world from UrhoSharp!");
  78. }
  79. protected override void OnUpdate(float timeStep)
  80. {
  81. // Move clouds via CloudsOffset (see CustomLitSolid.hlsl)
  82. cloudsOffset += 0.00005f;
  83. earthMaterial.SetShaderParameter("CloudsOffset", new Vector2(cloudsOffset, 0));
  84. //NOTE: this could be done via SetShaderParameterAnimation
  85. }
  86. // For HL optical stabilization (optional)
  87. public override Vector3 FocusWorldPoint => earthNode.WorldPosition;
  88. //Handle input:
  89. Vector3 earthPosBeforeManipulations;
  90. public override void OnGestureManipulationStarted() => earthPosBeforeManipulations = earthNode.Position;
  91. public override void OnGestureManipulationUpdated(Vector3 relativeHandPosition) =>
  92. earthNode.Position = relativeHandPosition + earthPosBeforeManipulations;
  93. public override void OnGestureTapped() {}
  94. public override void OnGestureDoubleTapped() {}
  95. }
  96. }