Augmented Reality
Augmented Reality
Augmented Reality
## dart: PermissionGroup.camera
'PERMISSION_CAMERA=1',
## dart: PermissionGroup.photos
'PERMISSION_PHOTOS=1',
## dart: [PermissionGroup.location,
PermissionGroup.locationAlways,
PermissionGroup.locationWhenInUse]
'PERMISSION_LOCATION=1',
## dart: PermissionGroup.sensors
'PERMISSION_SENSORS=1',
## dart: PermissionGroup.bluetooth
'PERMISSION_BLUETOOTH=1',´
using PlatformARView
o ARSessionManager: Manages the ARView’s session
of an ARView
o ARAnchorManager: Manages anchor functionalities
//String localObjectReference;
ARNode? localObjectNode;
//String webObjectReference;
ARNode? webObjectNode;
Next, update the empty Container with ARView widget
as below:
ARView(
onARViewCreated: onARViewCreated,
)
Here, you are using the onARViewCreated method for
the onARViewCreated property of the widget:
void onARViewCreated(
ARSessionManager arSessionManager,
ARObjectManager arObjectManager,
ARAnchorManager arAnchorManager,
ARLocationManager arLocationManager) {
// 1
this.arSessionManager = arSessionManager;
this.arObjectManager = arObjectManager;
// 2
this.arSessionManager.onInitialize(
showFeaturePoints: false,
showPlanes: true,
customPlaneTexturePath: "triangle.png",
showWorldOrigin: true,
handleTaps: false,
);
// 3
this.arObjectManager.onInitialize();
}
In the above code, you are doing the following:
1. Defining
both arSessionManager and arObjectManager variabl
es
2. Using ARSessionManager’s onInitialize method to
set session properties
These settings are used to visualize feature points,
planes, world coordinate systems, and more. Here,
you are using the customPlaneTexturePath to refer to
assets defined in your pubspec
3. Also, using the ARObjectManager’s onInitialize to
set up the manager
Build and run your app. You’ll get to see
the ARView like this:
Video Player
00:00
00:25
Creating and removing a local object
Now, you need to use the “Add / Remove Local Object”
button to create or remove the localObjectNode using
the onLocalObjectButtonPressed callback as below:
Future<void> onLocalObjectButtonPressed() async {
// 1
if (localObjectNode != null) {
arObjectManager.removeNode(localObjectNode!);
localObjectNode = null;
} else {
// 2
var newNode = ARNode(
type: NodeType.localGLTF2,
uri: "assets/Chicken_01/Chicken_01.gltf",
scale: Vector3(0.2, 0.2, 0.2),
position: Vector3(0.0, 0.0, 0.0),
rotation: Vector4(1.0, 0.0, 0.0, 0.0));
// 3
bool? didAddLocalNode = await
arObjectManager.addNode(newNode);
localObjectNode = (didAddLocalNode!) ? newNode :
null;
}
}
Here you have done the following:
1. Checked whether the localObjectNode is null or not,
if not null then remove the local object
2. Created a new ARNode object by providing the local
glTF file path and type along with the coordinate
system containing the position, rotations, and other
transformations of the node
3. Added the newNode to the top level (like Stack) of
the ARView and assigned it to the localObjectNode
NodeType is an enum that is used to set up the type of
nodes the plugin supports
including localGLTF2, webGLB, fileSystemAppFolderG
LB, and fileSystemAppFolderGLTF2.
Build and run your app, then click on the Add / Remove
Local Object button:
Video Player
00:00
00:37
Add a remote object
Next, you need to use the Add / Remove Web
Object button with
the onWebObjectAtButtonPressed callback as below:
Future<void> onWebObjectAtButtonPressed() async {
if (webObjectNode != null) {
arObjectManager.removeNode(webObjectNode!);
webObjectNode = null;
} else {
var newNode = ARNode(
type: NodeType.webGLB,
uri:
"https://github.com/KhronosGroup/glTF-Sample-
Models/raw/master/2.0/Duck/glTF-Binary/Duck.glb",
scale: Vector3(0.2, 0.2, 0.2));
bool? didAddWebNode = await
arObjectManager.addNode(newNode);
webObjectNode = (didAddWebNode!) ? newNode :
null;
}
}
The above method is similar to
the onLocalObjectButtonPressed method with a
difference in the URL. Here, the URL is targeting a GLB
file from the web.
Over 200k developers use LogRocket to create better
digital experiences
Learn more →
Build and run your app, then click on the Add / Remove
Web Object button:
Video Player
00:00
00:30
Working with anchors
If you want to track the position or pose changes of your
3D object, you need to define an anchor for that. An
anchor describes or detects feature points and planes in
the real world and simply lets you place 3D objects in the
world.
N.B., a feature point is a distinctive location in images.
For example, corners, junctions and more.
This ensures that the object stays where it is placed, even
if the environment changes over time and impacts your
app’s user experience.
In the end, dispose of the managers using
the dispose method to let go of the resources.
Conclusion
You can find the final project here.
In this tutorial, you learned about building an augmented
reality app using Flutter. For the next step, you can try
rotating or transforming objects using gestures, or
fetching 3D objects using Google Cloud Anchor API or
an external database.
We hope you enjoyed this tutorial. Feel free to reach out
to us if you have any queries. Thank you!
Get set up with LogRocket's modern error tracking in
minutes:
1. Visit https://logrocket.com/signup/ to get an app ID
2. Install LogRocket via npm or script
tag. LogRocket.init() must be called client-side, not
server-side
o npm
o Script tag
3. $ npm i --save logrocket
4.
5. // Code:
6.
7. import LogRocket from 'logrocket';
8. LogRocket.init('app/id');
9. (Optional) Install plugins for deeper integrations with
your stack:
o Redux middleware
o NgRx middleware
o Vuex plugin