コード例 #1
0
        public static SCNVector3?HitTestWithInfiniteHorizontalPlane(this ARSCNView sceneView, CGPoint point, SCNVector3 pointOnPlane)
        {
            //if (sceneView.Session == null || ARGamePlay.CurrentFrame == null)
            //{
            //    return null;
            //}
            //
            //var currentFrame = ARGamePlay.CurrentFrame;

            var ray = sceneView.HitTestRayFromScreenPos(point);

            if (ray == null)
            {
                return(null);
            }
            ;

            // Do not intersect with planes above the camera or if the ray is almost parallel to the plane.
            if (ray.Direction.Y > -0.03f)
            {
                return(null);
            }

            return(ray.IntersectionWithHorizontalPlane(pointOnPlane.Y));
        }
コード例 #2
0
        public void CreateARScene(ARSCNView sceneView)
        {
            var scene = SCNScene.FromFile("art.scnassets/ship");

            sceneView.Scene        = scene;
            sceneView.DebugOptions = ARSCNDebugOptions.ShowWorldOrigin | ARSCNDebugOptions.ShowFeaturePoints;
        }
コード例 #3
0
        public override void ViewWillDisappear(bool animated)
        {
            base.ViewWillDisappear(animated);
            ARSCNView SceneView = (View as ARSCNView);

            SceneView.Session.Pause();
        }
コード例 #4
0
        internal TestRun(ViewControllerSessionInfo sessionInfo, ARSCNView sceneView)
        {
            this.sessionInfo = sessionInfo;
            this.sceneView   = sceneView;

            StartNoDetectionTimer();
        }
コード例 #5
0
ファイル: Utilities.cs プロジェクト: zain-tariq/ios-samples
        public static ARHitTestResult SmartHitTest(this ARSCNView self, CGPoint point)
        {
            // Perform the hit test.
            var results = self.HitTest(point, ARHitTestResultType.ExistingPlaneUsingGeometry);

            // 1. Check for a result on an existing plane using geometry.
            var existingPlaneUsingGeometryResult = results.FirstOrDefault(result => result.Type == ARHitTestResultType.ExistingPlaneUsingGeometry);

            if (existingPlaneUsingGeometryResult != null)
            {
                return(existingPlaneUsingGeometryResult);
            }

            // 2. Check for a result on an existing plane, assuming its dimensions are infinite.
            var infinitePlaneResults = self.HitTest(point, ARHitTestResultType.ExistingPlane);

            var infinitePlaneResult = infinitePlaneResults.FirstOrDefault();

            if (infinitePlaneResult != null)
            {
                return(infinitePlaneResult);
            }

            // 3. As a final fallback, check for a result on estimated planes.
            return(results.FirstOrDefault(result => result.Type == ARHitTestResultType.EstimatedHorizontalPlane));
        }
コード例 #6
0
        public static SCNVector3?HitTestWithInfiniteHorizontalPlane(this ARSCNView self, CGPoint point, SCNVector3 pointOnPlane)
        {
            if (self.Session == null || ViewController.CurrentFrame == null)
            {
                return(null);
            }

            var currentFrame = ViewController.CurrentFrame;

            var ray = self.HitTestRayFromScreenPos(point);

            if (ray == null)
            {
                return(null);
            }
            ;

            // Do not intersect with planes above the camera or if the ray is almost parallel to the plane.
            if (ray.Direction.Y > -0.03f)
            {
                return(null);
            }

            return(Utilities.RayIntersectionWithHorizontalPlane(ray.Origin, ray.Direction, pointOnPlane.Y));
        }
コード例 #7
0
        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);
            //ARWorldTrackingSessionConfiguration config;// = new A
            //ARWorldTrackingSessionConfiguration
            ARWorldTrackingConfiguration config = new ARWorldTrackingConfiguration();

            //ARWorldTrackingSessionConfiguration config = new ARWorldTrackingSessionConfiguration{


            //};
            config.PlaneDetection = ARPlaneDetection.Horizontal;
            arSessionConfig       = config;
            arSessionConfig.LightEstimationEnabled = true;

            arSession = new ARSession();

            arSCNView         = new ARSCNView();
            arSCNView.Frame   = View.Bounds;
            arSCNView.Session = arSession;
            arSCNView.AutomaticallyUpdatesLighting = true;
            View.AddSubview(this.arSCNView);
            arSession.Run(this.arSessionConfig, ARSessionRunOptions.RemoveExistingAnchors);



            var scene = SCNScene.FromFile("Models.scnassets/chair/chair.scn");

            SCNNode node = scene.RootNode.ChildNodes[0];


            node.Position = new SCNVector3(0, -1, -1);

            arSCNView.Scene.RootNode.AddNodes(node);
        }
コード例 #8
0
        // Once we create scene we need to position our AR model in it:
        public void PositionSceneObject(ARSCNView sceneView)
        {
            // Each session has to be configured.
            //  We will use ARWorldTrackingConfiguration to have full access to device orientation,
            // rear camera, device position and to detect real-world flat surfaces:
            var configuration = new ARWorldTrackingConfiguration
            {
                PlaneDetection         = ARPlaneDetection.Horizontal,
                LightEstimationEnabled = true
            };

            // Once we have our configuration we need to run session with it.
            // ResetTracking will just reset tracking by session to start it again from scratch:
            sceneView.Session.Run(configuration, ARSessionRunOptions.ResetTracking);

            // Next we need to find main "node" in the .dae file. In this case it is called "ship":
            var shipNode = sceneView.Scene.RootNode.FindChildNode("ship", true);

            // Then we have to set position of AR object - below I would like to display it in front of camera:
            shipNode.Position = new SCNVector3(0.0f, 0.0f, -20f);

            // Next we need to add ship object to scene:
            sceneView.Scene.RootNode.AddChildNode(shipNode);

            // At the end I configured simple rotating animation to rotate ship object in front of camera:
            shipNode.RunAction(SCNAction.RepeatActionForever(SCNAction.RotateBy(0f, 4f, 0, 5)));
        }
コード例 #9
0
        public static Ray HitTestRayFromScreenPos(this ARSCNView sceneView, CGPoint point)
        {
            //if (sceneView.Session == null || ViewController.CurrentFrame == null)
            //{
            //    return null;
            //}

            var frame = ARGamePlay.CurrentFrame;

            //var frame = ViewController.CurrentFrame;
            if (frame == null || frame.Camera == null || frame.Camera.Transform == null)
            {
                return(null);
            }

            var cameraPos = SCNVector3Ex.PositionFromTransform(frame.Camera.Transform);

            // Note: z: 1.0 will unproject() the screen position to the far clipping plane.
            var positionVec = new SCNVector3((float)point.X, (float)point.Y, 1.0f);
            var screenPosOnFarClippingPlane = sceneView.UnprojectPoint(positionVec);

            var rayDirection = screenPosOnFarClippingPlane - cameraPos; //screenPosOnFarClippingPlane.Subtract(cameraPos);

            rayDirection.Normalize();

            return(new Ray(cameraPos, rayDirection));
        }
コード例 #10
0
        public static SnapshotAnchor Create(ARSCNView capturingView)
        {
            try
            {
                var frame   = capturingView.Session?.CurrentFrame;
                var camera  = frame?.Camera;
                var capture = frame?.CapturedImage;

                if (frame == null || camera == null || capture == null)
                {
                    return(null);
                }

                var image = CIImage.FromImageBuffer(capture);

                var orientation = OrientationUtility.CreateFromDeviceOrientation(UIDevice.CurrentDevice.Orientation);

                var context = new CIContext(new CIContextOptions {
                    UseSoftwareRenderer = false
                });

                var data = context.GetJpegRepresentation(image.CreateByApplyingOrientation(orientation)
                                                         , CGColorSpace.CreateDeviceRGB()
                                                         , new CIImageRepresentationOptions {
                    LossyCompressionQuality = 0.7f
                });

                return(new SnapshotAnchor(data, camera.Transform));
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failed to create image: " + ex.Message);
                return(null);
            }
        }
コード例 #11
0
        public void PositionScene(ARSCNView sceneView, string arLaunchType)
        {
            var arConfiguration = new ARWorldTrackingConfiguration
            {
                PlaneDetection         = ARPlaneDetection.Horizontal,
                LightEstimationEnabled = true
            };

            sceneView.Session.Run(arConfiguration, ARSessionRunOptions.ResetTracking);

            var sceneShipNode = sceneView.Scene.RootNode.FindChildNode("ship", true);

            sceneShipNode.Position = new SCNVector3(2f, -2f, -9f);

            var animationCycle  = SCNAction.RepeatActionForever(SCNAction.RotateBy(0f, 6f, 0, 5));
            var animationCrash  = SCNAction.RepeatActionForever(SCNAction.RotateBy(0, (float)Math.PI, (float)Math.PI, (float)1));
            var animationNormal = SCNAction.RepeatActionForever(SCNAction.RotateBy(0, 0, 0, 1));
            var animationRotate = SCNAction.RepeatActionForever(SCNAction.RotateBy(0, 0, 2, 1));


            var scenePivotNode = new SCNNode {
                Position = new SCNVector3(0.0f, 2.0f, 0.0f)
            };

            scenePivotNode.RunAction(SCNAction.RepeatActionForever(SCNAction.RotateBy(0, -2, 0, 10)));

            sceneShipNode.RemoveFromParentNode();
            scenePivotNode.AddChildNode(sceneShipNode);

            sceneView.Scene.RootNode.AddChildNode(scenePivotNode);

            sceneShipNode.Scale    = new SCNVector3(0.1f, 0.1f, 0.1f);
            sceneShipNode.Position = new SCNVector3(2f, -2f, -3f);


            switch (arLaunchType)
            {
            case "Rotate Fly":
                sceneShipNode.RunAction(animationRotate);
                break;

            case "Crash Fly":
                sceneShipNode.RunAction(animationCrash);
                break;

            case "Cycle Fly":
                sceneShipNode.RunAction(animationCycle);
                break;

            case "Normal Fly":
                sceneShipNode.RunAction(animationNormal);
                break;

            default:
                scenePivotNode.RemoveAllActions();
                scenePivotNode.RunAction(SCNAction.Unhide());
                break;
            }
        }
コード例 #12
0
 public ViewController(IntPtr handle) : base(handle)
 {
     sceneView = new ARSCNView
     {
         ShowsStatistics = true
     };
     View.AddSubview(sceneView);
 }
コード例 #13
0
 /// <summary>
 /// サンプルを閉じます
 /// </summary>
 public override void Close()
 {
     _arscnView.Session.Pause();
     _arscnView.Session.Dispose();
     _arscnView.RemoveFromSuperview();
     _arscnView.Dispose();
     _arscnView = null;
 }
コード例 #14
0
 public ViewController(IntPtr handle) : base(handle)
 {
     sceneView = new ARSCNView
     {
         AutoenablesDefaultLighting = true,
         ShowsStatistics            = true
     };
     View.AddSubview(sceneView);
 }
コード例 #15
0
        public ViewController(IntPtr handle) : base(handle)
        {
            this.sceneView = new ARSCNView
            {
                AutoenablesDefaultLighting = true
            };

            this.View.AddSubview(this.sceneView);
        }
コード例 #16
0
        internal ScannedObject(ARSCNView sceneView)
        {
            this.sceneView = sceneView;

            ScanName = $"Scan_{DateTime.Now.ToString("s").Replace('-', '_').Replace(':', '_')}";
            base.Init();

            Console.WriteLine("Creating a new ScannedObject");
            notificationObserverHandle = NSNotificationCenter.DefaultCenter.AddObserver(Scan.ScanningStateChangedNotificationName, ScanningStateChanged);
        }
コード例 #17
0
        public SingleFingerGesture(NSSet touches, ARSCNView scnView, VirtualObject lastUsedObject, VirtualObjectManager vom)
            : base(touches, scnView, lastUsedObject, vom)
        {
            var firstTouch = (UITouch)touches.First();

            initialTouchPosition = firstTouch.LocationInView(scnView);
            latestTouchPosition  = initialTouchPosition;

            firstTouchedObject = this.VirtualObjectAt(initialTouchPosition);
        }
コード例 #18
0
 protected ViewController(IntPtr handle) : base(handle)
 {
     this.sceneView = new ARSCNView
     {
         AutoenablesDefaultLighting = true,
         //DebugOptions = ARSCNDebugOptions.ShowFeaturePoints,
         Delegate = new SceneViewDelegate()
     };
     this.View.AddSubview(this.sceneView);
 }
コード例 #19
0
        public ViewController(IntPtr handle) : base(handle)
        {
            this.sceneView = new ARSCNView
            {
                AutoenablesDefaultLighting = true,
                Delegate = new FaceDetection()
            };

            this.View.AddSubview(this.sceneView);
        }
コード例 #20
0
        //Source:: https://msdn.microsoft.com/en-us/magazine/mt830360.aspx
        // Initialize AR scene view
        public void startAR()
        {
            // Create the scene view for displaying the 3-D scene
            ARSCNView sceneView = new ARSCNView();

            sceneView.Frame = View.Frame;
            View            = sceneView;
            CreateARScene(sceneView);
            PositionScene(sceneView);
        }
コード例 #21
0
        public Gesture(NSSet currentTouches, ARSCNView sceneView, VirtualObject lastUsedObject, VirtualObjectManager manager)
        {
            this.currentTouches = currentTouches;
            this.SceneView      = sceneView;
            this.LastUsedObject = lastUsedObject;
            this.manager        = manager;

            // Refresh the current gesture at 60 Hz - This ensures smooth updates even when no
            // new touch events are incoming (but the camera might have moved).
            this.refreshTimer = new System.Threading.Timer(CheckedUpdateGesture, null, 0, 17);
        }
コード例 #22
0
        public void StartAR()
        {
            var sceneView = new ARSCNView
            {
                Frame = View.Frame
            };

            View = sceneView;
            CreateARScene(sceneView);
            PositionScene(sceneView);
        }
コード例 #23
0
        public void StartAR(string arLaunchType)
        {
            var sceneView = new ARSCNView
            {
                Frame = View.Frame
            };

            View = sceneView;
            CreateARScene(sceneView);
            PositionScene(sceneView, arLaunchType);
        }
コード例 #24
0
        // Configure AR scene with 3-D object
        public void CreateARScene(ARSCNView sceneView)
        {
            // lLoading the 3-D asset from file, will eventually create assets
            var scene = SCNScene.FromFile("art.scnassets/ship");

            // Attaching the 3-D object to the scene
            sceneView.Scene = scene;
            // This is for debugging purposes
            sceneView.DebugOptions = ARSCNDebugOptions.ShowWorldOrigin |
                                     ARSCNDebugOptions.ShowFeaturePoints;
        }
コード例 #25
0
ファイル: ARtest.cs プロジェクト: suetarazi/XamarinAppiOS
        /// <summary>
        /// this creates a blank scene to ensure the camera feature of the mobile device is activated
        /// </summary>
        /// <param name="handle">variable name given to connect with the iOS API</param>
        public ViewController(IntPtr handle) : base(handle)
        {
            this.sceneView = new ARSCNView
            {
                AutoenablesDefaultLighting = true,
                DebugOptions =
                    ARSCNDebugOptions.ShowFeaturePoints | ARSCNDebugOptions.ShowWorldOrigin
            };

            this.View.AddSubview(this.sceneView);
        }
コード例 #26
0
        internal static SCNVector3?UnprojectPointLocal(this ARSCNView self, CGPoint point, NMatrix4 planeTransform)
        {
            var result = self.Unproject(point, planeTransform);
            // Convert the result into the plane's local coordinate system.
            var pt            = new SCNVector4(result.X, result.Y, result.Z, 1);
            var invertedPlane = planeTransform.ToSCNMatrix4();

            invertedPlane.Invert();
            var localResult = invertedPlane.Times(pt);

            return(localResult.Xyz);
        }
コード例 #27
0
        // Static factory method
        public static Gesture StartGestureFromTouches(NSSet currentTouches, ARSCNView sceneView, VirtualObject lastUsedObject, VirtualObjectManager manager)
        {
            switch (currentTouches.Count)
            {
            case 1: return(new SingleFingerGesture(currentTouches, sceneView, lastUsedObject, manager));

            case 2: return(new TwoFingerGesture(currentTouches, sceneView, lastUsedObject, manager));

            default:
                return(null);
            }
        }
コード例 #28
0
        public static FeatureHitTestResult?HitTestFromOrigin(this ARSCNView view, SCNVector3 origin, SCNVector3 direction)
        {
            FeatureHitTestResult?result = null;

            ARPointCloud features = null;

            using (var frame = view.Session.CurrentFrame)
            {
                features = frame?.RawFeaturePoints;
            }

            if (features != null)
            {
                var points = features.Points;

                // Determine the point from the whole point cloud which is closest to the hit test ray.
                var closestFeaturePoint = origin;
                var minDistance         = float.MaxValue; // Float.greatestFiniteMagnitude

                for (nuint i = 0; i < features.Count; i++)
                {
                    var feature = points[i];

                    var featurePosition = new SCNVector3((Vector3)feature);
                    var originVector    = origin - featurePosition;

                    var crossProduct = SCNVector3.Cross(originVector, direction);
                    var featureDistanceFromResult = crossProduct.Length;

                    if (featureDistanceFromResult < minDistance)
                    {
                        closestFeaturePoint = featurePosition;
                        minDistance         = featureDistanceFromResult;
                    }
                }

                // Compute the point along the ray that is closest to the selected feature.
                var originToFeature       = closestFeaturePoint - origin;
                var hitTestResult         = origin + (direction * SCNVector3.Dot(direction, originToFeature));
                var hitTestResultDistance = (hitTestResult - origin).Length;

                result = new FeatureHitTestResult
                {
                    Position                   = hitTestResult,
                    DistanceToRayOrigin        = hitTestResultDistance,
                    FeatureHit                 = closestFeaturePoint,
                    FeatureDistanceToHitResult = minDistance
                };
            }

            return(result);
        }
コード例 #29
0
        public TwoFingerGesture(NSSet touches, ARSCNView view, VirtualObject parentObject, VirtualObjectManager manager) : base(touches, view, parentObject, manager)
        {
            var tArray = touches.ToArray <UITouch>();

            FirstTouch  = tArray[0];
            SecondTouch = tArray[1];

            var firstTouchPoint  = FirstTouch.LocationInView(SceneView);
            var secondTouchPoint = SecondTouch.LocationInView(SceneView);

            InitialMidpoint = firstTouchPoint.Add(secondTouchPoint).Divide(2f);

            // Compute the two other corners of the rectangle defined by the two fingers
            // and compute the points in between.
            var thirdCorner  = new CGPoint(firstTouchPoint.X, secondTouchPoint.Y);
            var fourthCorner = new CGPoint(secondTouchPoint.X, firstTouchPoint.Y);

            //  Compute points in between.
            var midpoints = new[]
            {
                thirdCorner.Add(firstTouchPoint).Divide(2f),
                thirdCorner.Add(secondTouchPoint).Divide(2f),
                fourthCorner.Add(firstTouchPoint).Divide(2f),
                fourthCorner.Add(secondTouchPoint).Divide(2f),
                InitialMidpoint.Add(firstTouchPoint).Divide(2f),
                InitialMidpoint.Add(secondTouchPoint).Divide(2f),
                InitialMidpoint.Add(thirdCorner).Divide(2f),
                InitialMidpoint.Add(fourthCorner).Divide(2f)
            };

            // Check if any of the two fingers or their midpoint is touching the object.
            // Based on that, translation, rotation and scale will be enabled or disabled.
            var allPoints = new List <CGPoint>(new[] { firstTouchPoint, secondTouchPoint, thirdCorner, fourthCorner, InitialMidpoint });

            allPoints.AddRange(midpoints);
            FirstTouchedObject = allPoints.Select(pt => this.VirtualObjectAt(pt)).Where(vo => vo != null).FirstOrDefault();

            if (FirstTouchedObject != null)
            {
                ObjectBaseScale  = FirstTouchedObject.Scale.X;
                AllowTranslation = true;
                AllowRotation    = true;
                InitialDistanceBetweenFingers = (firstTouchPoint.Subtract(secondTouchPoint)).Length();
                InitialFingerAngle            = Math.Atan2(InitialMidpoint.X, InitialMidpoint.Y);
                InitialObjectAngle            = FirstTouchedObject.EulerAngles.Y;
            }
            else
            {
                AllowTranslation = false;
                AllowRotation    = false;
            }
        }
コード例 #30
0
 // Position AR scene
 public void PositionScene(ARSCNView sceneView)
 {
     // ARWorldTrackingConfiguration uses the back-facing camera,
     // tracks a device's orientation and position, and detects
     // real-world surfaces, and known images or objects
     using (var arConfiguration = new ARWorldTrackingConfiguration
     {
         PlaneDetection = ARPlaneDetection.Horizontal,
         LightEstimationEnabled = true
     })
         // Run the AR session
         sceneView.Session.Run(arConfiguration, ARSessionRunOptions.ResetTracking);
 }