{"_id":"5b689070f3c8dc0003c6d1db","project":"5a065a6134873d0010b396ab","version":{"_id":"5b689070f3c8dc0003c6d1df","project":"5a065a6134873d0010b396ab","__v":0,"forked_from":"5b4e90b9d9ea0b00031c6194","createdAt":"2018-04-18T18:19:34.288Z","releaseDate":"2018-04-18T18:19:34.288Z","categories":["5b689070f3c8dc0003c6d1a4","5b689070f3c8dc0003c6d1a5","5b689070f3c8dc0003c6d1a6","5b05923ea5a2f9000357b452","5b05f793c2c86c0003cbe414","5b689070f3c8dc0003c6d1a7","5b689070f3c8dc0003c6d1a8","5b689070f3c8dc0003c6d1a9"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.9.0","version":"1.9.0"},"category":{"_id":"5b689070f3c8dc0003c6d1a7","project":"5a065a6134873d0010b396ab","version":"5b689070f3c8dc0003c6d1df","__v":0,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2018-05-24T14:09:29.251Z","from_sync":false,"order":2,"slug":"develop-ar","title":"Augmented Reality"},"user":"579a69d53de0a217007eda56","githubsync":"","__v":0,"parentDoc":null,"updates":[],"next":{"pages":[],"description":""},"createdAt":"2018-05-24T14:12:20.807Z","link_external":false,"link_url":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":3,"body":"Viro supports numerous mechanisms through which users can interact with both the real world and the virtual UI. These are detailed below.\n[block:api-header]\n{\n  \"title\": \"AR Hit Testing\"\n}\n[/block]\nViroViewARCore has a variety of methods used to \"hit-test\" against the real-world. These hit tests can be used to determine, to the best of ARCore's ability, what real-world features exist at a given point on the 2D screen. Note that since a single 2D point on the view corresponds to a 3D ray in the scene, multiple results may be returned (each at a different depth). The results may be anchors -- like planes -- or they may be feature points that have not yet been fully identified.\n\nThe easiest way to add virtual content to a hit result is to use [ARHitTestResult.createAnchoredNode()][1]. In the example below, we perform a hit test and add the 3D object of a star to the hit location.\n\n[1]:https://developer.viromedia.com/virocore/reference/com/viro/core/ARHitTestResult.html#createAnchoredNode()\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"final ViroViewARCore view = ...; //Assume this has been initialized\\nfinal ARScene scene = ...; // Assume this has been initialized\\n\\nPoint point = new Point((int) (view.getWidth() / 2.0), \\n                        (int) (view.getHeight() / 2.0f));\\n\\nview.performARHitTest(point, new ARHitTestListener() {\\n    :::at:::Override\\n    public void onHitTestFinished(ARHitTestResult[] results) {\\n        for (ARHitTestResult result : results) {\\n            ARNode node = result.createAnchoredNode();\\n          \\n            // ARCore may fail to create an anchor\\n            if (node == null) {\\n                continue;\\n            }\\n\\n            // Add a star model to the Node, so the user sees a star where\\n            // the hit test landed\\n            final Object3D star = loadStarModel();\\n            node.addChildNode(star);\\n        }\\n    }\\n});\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"title\": \"Fixed to World Dragging\"\n}\n[/block]\nNormally when dragging a Node through a DragListener, the Node when dragged stays at a fixed distance from from the user, as though the user is dragging the Node across the inner surface of a sphere. This is called ```FIXED_DISTANCE``` dragging. \n\nViro also supports ```FIXED_TO_WORLD``` dragging in AR, where instead of keeping a dragged Node's distance from the user fixed, the dragged Node's distance is instead determined by its intersection with the nearest real-world object. This is useful for dragging a virtual object across a real-world surface, for example.\n[block:api-header]\n{\n  \"title\": \"Fixed to Plane Dragging\"\n}\n[/block]\nFixed to Plane dragging enables you to specify a plane atop which a Node can be dragged. The Node will not be able to leave this plane. You can also specify a maximum distance away from the camera that the Node can travel, to prevent users from dragging the node out to infinity. To do this, set the Node's drag type to ```FIXED_TO_PLANE```. You define the plane along which the Node can be dragged by specifying any point on the plane, and the normal vector of the plane.\n\nIn the example below we allow a Box to only be dragged about a horizontal plane one meter below us, and we stop it from traveling more than five meters away from us.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"Box box = new Box(.2f, .2f, .2f);\\n\\nNode boxNode = new Node();\\nboxNode.setGeometry(box);\\nboxNode.setPosition(new Vector(0, 0, -1));\\nboxNode.setDragListener(new DragListener() {\\n    @Override\\n    public void onDrag(int source, Node node, Vector worldLocation, Vector localLocation) {\\n        Log.i(TAG, \\\"The box is being dragged!\\\");\\n    }\\n});\\n\\n// Set FIXED_TO_PLANE dragging and define the drag plane\\nboxNode.setDragType(Node.DragType.FIXED_TO_PLANE);\\nboxNode.setDragPlanePoint(new Vector(0, -1, 0));\\nboxNode.setDragPlaneNormal(new Vector(0, 1, 0));\\n\\n// Do not allow the box to travel more than 5 meters away from us\\nboxNode.setDragMaxDistance(5);\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"title\": \"Video and Still Capture\"\n}\n[/block]\nViro supports video and still image capture, to make sharing AR experiences easy. To do this, simply grab the [ViroMediaRecorder](https://developer.viromedia.com/virocore/reference/com/viro/core/ViroMediaRecorder.html) from the ViroViewARCore.","excerpt":"","slug":"ar-interaction","type":"basic","title":"Interaction"}
Viro supports numerous mechanisms through which users can interact with both the real world and the virtual UI. These are detailed below. [block:api-header] { "title": "AR Hit Testing" } [/block] ViroViewARCore has a variety of methods used to "hit-test" against the real-world. These hit tests can be used to determine, to the best of ARCore's ability, what real-world features exist at a given point on the 2D screen. Note that since a single 2D point on the view corresponds to a 3D ray in the scene, multiple results may be returned (each at a different depth). The results may be anchors -- like planes -- or they may be feature points that have not yet been fully identified. The easiest way to add virtual content to a hit result is to use [ARHitTestResult.createAnchoredNode()][1]. In the example below, we perform a hit test and add the 3D object of a star to the hit location. [1]:https://developer.viromedia.com/virocore/reference/com/viro/core/ARHitTestResult.html#createAnchoredNode() [block:code] { "codes": [ { "code": "final ViroViewARCore view = ...; //Assume this has been initialized\nfinal ARScene scene = ...; // Assume this has been initialized\n\nPoint point = new Point((int) (view.getWidth() / 2.0), \n (int) (view.getHeight() / 2.0f));\n\nview.performARHitTest(point, new ARHitTestListener() {\n @Override\n public void onHitTestFinished(ARHitTestResult[] results) {\n for (ARHitTestResult result : results) {\n ARNode node = result.createAnchoredNode();\n \n // ARCore may fail to create an anchor\n if (node == null) {\n continue;\n }\n\n // Add a star model to the Node, so the user sees a star where\n // the hit test landed\n final Object3D star = loadStarModel();\n node.addChildNode(star);\n }\n }\n});", "language": "java" } ] } [/block] [block:api-header] { "title": "Fixed to World Dragging" } [/block] Normally when dragging a Node through a DragListener, the Node when dragged stays at a fixed distance from from the user, as though the user is dragging the Node across the inner surface of a sphere. This is called ```FIXED_DISTANCE``` dragging. Viro also supports ```FIXED_TO_WORLD``` dragging in AR, where instead of keeping a dragged Node's distance from the user fixed, the dragged Node's distance is instead determined by its intersection with the nearest real-world object. This is useful for dragging a virtual object across a real-world surface, for example. [block:api-header] { "title": "Fixed to Plane Dragging" } [/block] Fixed to Plane dragging enables you to specify a plane atop which a Node can be dragged. The Node will not be able to leave this plane. You can also specify a maximum distance away from the camera that the Node can travel, to prevent users from dragging the node out to infinity. To do this, set the Node's drag type to ```FIXED_TO_PLANE```. You define the plane along which the Node can be dragged by specifying any point on the plane, and the normal vector of the plane. In the example below we allow a Box to only be dragged about a horizontal plane one meter below us, and we stop it from traveling more than five meters away from us. [block:code] { "codes": [ { "code": "Box box = new Box(.2f, .2f, .2f);\n\nNode boxNode = new Node();\nboxNode.setGeometry(box);\nboxNode.setPosition(new Vector(0, 0, -1));\nboxNode.setDragListener(new DragListener() {\n @Override\n public void onDrag(int source, Node node, Vector worldLocation, Vector localLocation) {\n Log.i(TAG, \"The box is being dragged!\");\n }\n});\n\n// Set FIXED_TO_PLANE dragging and define the drag plane\nboxNode.setDragType(Node.DragType.FIXED_TO_PLANE);\nboxNode.setDragPlanePoint(new Vector(0, -1, 0));\nboxNode.setDragPlaneNormal(new Vector(0, 1, 0));\n\n// Do not allow the box to travel more than 5 meters away from us\nboxNode.setDragMaxDistance(5);", "language": "java" } ] } [/block] [block:api-header] { "title": "Video and Still Capture" } [/block] Viro supports video and still image capture, to make sharing AR experiences easy. To do this, simply grab the [ViroMediaRecorder](https://developer.viromedia.com/virocore/reference/com/viro/core/ViroMediaRecorder.html) from the ViroViewARCore.