Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 45 additions & 33 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:ar_flutter_plugin_2/managers/ar_anchor_manager.dart';
import 'package:ar_flutter_plugin_2/managers/ar_location_manager.dart';
import 'package:ar_flutter_plugin_2/managers/ar_object_manager.dart';
import 'package:ar_flutter_plugin_2/managers/ar_session_manager.dart';
import 'package:ar_flutter_plugin_2/models/ar_anchor.dart'; // You need to import ar Anchor to be able to create anchors and position objects on them.

void main() {
runApp(const MyApp());
Expand Down Expand Up @@ -37,6 +38,7 @@ class ARScene extends StatefulWidget {
class _ARSceneState extends State<ARScene> {
late ARSessionManager arSessionManager;
late ARObjectManager arObjectManager;
late ARAnchorManager? arAnchorManager; // You need an AnchorManager

@override
void dispose() {
Expand All @@ -52,6 +54,7 @@ class _ARSceneState extends State<ARScene> {
) {
arSessionManager = arSessionMgr;
arObjectManager = arObjectMgr;
arAnchorManager = arAnchorMgr;

arSessionManager.onInitialize(
showFeaturePoints: false,
Expand All @@ -63,43 +66,52 @@ class _ARSceneState extends State<ARScene> {
arObjectManager.onInitialize();
print("AR Object Manager initialized successfully");

// Add a sample 3D model with debug logging
try {
final node = ARNode(
type: NodeType.fileSystemAppFolderGLB,
uri: "assets/source/123.glb",
scale: vm.Vector3(0.5, 0.5, 0.5), // Adjusted to a reasonable scale
position: vm.Vector3(0.0, 0.0, -1.0), // 1 meter in front of camera
rotation: vm.Vector4(0.0, 0.0, 0.0, 1.0), // Identity quaternion as Vector4 [x, y, z, w]
);

arObjectManager.addNode(node).then((_) {
print("Node added successfully: $node");
}).catchError((error) {
print("Error adding node: $error");
});
} catch (e) {
print("Exception while creating node: $e");
}

// Handle tap to place node on detected plane
arSessionManager.onPlaneOrPointTap = (List<ARHitTestResult> hitTestResults) {
if (hitTestResults.isNotEmpty) {
final plane = hitTestResults.first;
print("Tapped on plane at: ${plane.worldTransform.getTranslation()}");
try {
final node = ARNode(
type: NodeType.localGLTF2,
uri: "assets/source/123.glb",
scale: vm.Vector3(10,10,10),
position: plane.worldTransform.getTranslation(), // Extract Vector3 from hit result
rotation: vm.Vector4(0.0, 0.0, 0.0, 1.0), // Identity quaternion as Vector4
);
arObjectManager.addNode(node);
print("Node placed on tap successfully: $node");
} catch (e) {
print("Error placing node on tap: $e");
final singleHitTestResult = hitTestResults.first;
print("Tapped on plane at: ${singleHitTestResult.worldTransform.getTranslation()}");
// You need to create an anchor from the coordinates of the tapped plane, and only then add a node to this anchor. :
var newAnchor =
ARPlaneAnchor(transformation: singleHitTestResult.worldTransform);
this.arAnchorManager!.addAnchor(newAnchor).then((bool? didAddAnchor) {
if (didAddAnchor == true) {
try {
final node = ARNode(
type: NodeType.localGLTF2,
uri: "assets/source/123.glb",
//scale: vm.Vector3(10,10,10),
scale: vm.Vector3(1, 1, 1),
// I think your scale was too big
//position: plane.worldTransform.getTranslation(), // Extract Vector3 from hit result
position: vm.Vector3(0, 0, 0),
// You need to set the coordinates relative to where the user tapped, so 0,0,0 if you want to place the object exactly where the tap occurred.
rotation: vm.Vector4(
// 0.0, 0.0, 0.0, 1.0), // Identity quaternion as Vector4
1.0, 0.0, 0.0, 1.0), // Rotation adjustement
);
//arObjectManager.addNode(node)
arObjectManager.addNode(node, planeAnchor:newAnchor).then((bool? success) {
print("addNode result: $success");
if (success == true) {
print("Node added successfully.");
} else {
print("Failure to add the node.");
}
}).catchError((error) {
print("Error : $error");
});
print("Node placed on tap successfully: $node");
} catch (e) {
print("Error placing node on tap: $e");
}
}
else {
print("Error Adding Anchor failed ");
}
}
);

}
};
}
Expand Down