SpatialMappingTests.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using Urho;
  4. using Urho.Actions;
  5. using Urho.SharpReality;
  6. namespace Playgrounds.SharpReality
  7. {
  8. public class SpatialMappingTests : StereoApplication
  9. {
  10. bool wireframe;
  11. bool mappingEnded;
  12. Vector3 envPositionBeforeManipulations;
  13. Node environmentNode;
  14. DebugHud debugHud;
  15. Material spatMaterial;
  16. public SpatialMappingTests(ApplicationOptions opts) : base(opts) { }
  17. protected override async void Start()
  18. {
  19. base.Start();
  20. new MonoDebugHud(this){ FpsOnly = true }.Show(Color.Green, 50);
  21. debugHud = Engine.CreateDebugHud();
  22. debugHud.ToggleAll();
  23. environmentNode = Scene.CreateChild();
  24. EnableGestureTapped = true;
  25. spatMaterial = new Material();
  26. spatMaterial.SetTechnique(0, CoreAssets.Techniques.NoTexture, 1, 1);
  27. spatMaterial.SetShaderParameter("MatDiffColor", Color.Cyan);
  28. // make sure 'spatialMapping' capabilaty is enabled in the app manifest.
  29. var cortanaAllowed = await RegisterCortanaCommands(new Dictionary<string, Action> {
  30. { "show results" , ShowResults }
  31. });
  32. var spatialMappingAllowed = await StartSpatialMapping(new Vector3(50, 50, 50), color: Color.Yellow);
  33. }
  34. async void ShowResults()
  35. {
  36. EnableGestureManipulation = true;
  37. mappingEnded = true;
  38. StopSpatialMapping();
  39. var previewPosition = LeftCamera.Node.Position + (LeftCamera.Node.Direction * 0.5f);
  40. environmentNode.Position = previewPosition;
  41. await environmentNode.RunActionsAsync(new EaseOut(new ScaleTo(1f, 0.03f), 1f));
  42. }
  43. public override void OnGestureTapped()
  44. {
  45. if (mappingEnded)
  46. return;
  47. wireframe = !wireframe;
  48. foreach (var node in environmentNode.Children)
  49. {
  50. var material = node.GetComponent<StaticModel>().GetMaterial(0);
  51. material.FillMode = wireframe ? FillMode.Wireframe : FillMode.Solid;
  52. }
  53. }
  54. public override Model GenerateModelFromSpatialSurface(SpatialMeshInfo surface)
  55. {
  56. return CreateModelFromVertexData(surface.VertexData, surface.IndexData);
  57. }
  58. public override void OnSurfaceAddedOrUpdated(SpatialMeshInfo surface, Model generatedModel)
  59. {
  60. if (mappingEnded)
  61. return;
  62. bool isNew = false;
  63. StaticModel staticModel = null;
  64. Node node = environmentNode.GetChild(surface.SurfaceId, false);
  65. if (node != null)
  66. {
  67. isNew = false;
  68. staticModel = node.GetComponent<StaticModel>();
  69. }
  70. else
  71. {
  72. isNew = true;
  73. node = environmentNode.CreateChild(surface.SurfaceId);
  74. staticModel = node.CreateComponent<StaticModel>();
  75. }
  76. node.Position = surface.BoundsCenter;
  77. node.Rotation = surface.BoundsRotation;
  78. staticModel.Model = generatedModel;
  79. if (isNew)
  80. {
  81. staticModel.SetMaterial(spatMaterial);
  82. }
  83. }
  84. public override void OnGestureManipulationStarted()
  85. {
  86. envPositionBeforeManipulations = environmentNode.Position;
  87. }
  88. public override void OnGestureManipulationUpdated(Vector3 relativeHandPosition)
  89. {
  90. environmentNode.Position = relativeHandPosition + envPositionBeforeManipulations;
  91. }
  92. }
  93. }