ARCoreSample.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Android.App;
  6. using Android.Content;
  7. using Android.OS;
  8. using Android.Runtime;
  9. using Android.Views;
  10. using Android.Widget;
  11. using Com.Google.AR.Core;
  12. using Urho;
  13. using Urho.Droid;
  14. using Urho.Shapes;
  15. using static Com.Google.AR.Core.Point;
  16. namespace Playgrounds.Droid
  17. {
  18. public class ARCoreSample : SimpleApplication
  19. {
  20. ARCoreComponent arCore;
  21. MonoDebugHud hud;
  22. public ARCoreSample(ApplicationOptions options) : base(options)
  23. {
  24. }
  25. protected override async void Start()
  26. {
  27. base.Start();
  28. arCore = Scene.CreateComponent<ARCoreComponent>();
  29. var boxNode = Scene.CreateChild();
  30. boxNode.Position = new Vector3(0, 0, 0.5f);
  31. boxNode.CreateComponent<Pyramid>();
  32. boxNode.SetScale(0.1f);
  33. hud = new MonoDebugHud(this);
  34. hud.Show(Color.Green, 40);
  35. Input.TouchEnd += Input_TouchEnd;
  36. await arCore.Run(Camera);
  37. }
  38. float touchX, touchY;
  39. void Input_TouchEnd(TouchEndEventArgs e)
  40. {
  41. touchX = e.X;
  42. touchY = e.Y;
  43. }
  44. void ArCore_ConfigRequested(Config config)
  45. {
  46. config.SetPlaneFindingMode(Config.PlaneFindingMode.Horizontal);
  47. }
  48. void OnARFrameUpdated(Frame frame)
  49. {
  50. if (touchX != 0)
  51. {
  52. var hitResult = frame.HitTest(touchX, touchY);
  53. foreach (var hit in hitResult.Take(1))
  54. {
  55. var track = hit.Trackable;
  56. if ((track is Com.Google.AR.Core.Plane plane && plane.IsPoseInPolygon(hit.HitPose)) ||
  57. (track is Com.Google.AR.Core.Point point && point.GetOrientationMode() == OrientationMode.EstimatedSurfaceNormal))
  58. {
  59. var anchor = hit.CreateAnchor();
  60. var testNode = Scene.CreateChild();
  61. testNode.CreateComponent<Urho.Shapes.Cylinder>().Color = Color.Green;
  62. var pose = hit.HitPose;
  63. testNode.Scale = new Vector3(0.1f, 0.2f, 0.1f);
  64. ARCoreComponent.TransformByPose(testNode, pose);
  65. }
  66. }
  67. touchX = touchY = 0;
  68. }
  69. }
  70. }
  71. }