diff --git a/example/lib/ArMeasurementScreen.dart b/example/lib/ArMeasurementScreen.dart index fcc80fc..113266e 100644 --- a/example/lib/ArMeasurementScreen.dart +++ b/example/lib/ArMeasurementScreen.dart @@ -1,171 +1,161 @@ +import 'package:flutter/material.dart'; import 'dart:math' as math; import 'package:arkit_plugin/arkit_plugin.dart'; -import 'package:flutter/material.dart'; import 'package:vector_math/vector_math_64.dart' as vector; class ArMeasurementScreen extends StatefulWidget { + const ArMeasurementScreen({Key? key}) : super(key: key); + @override - _ArMeasurementScreenState createState() => _ArMeasurementScreenState(); + State createState() => _ArMeasurementScreenState(); } -class _ArMeasurementScreenState extends State -{ - ARKitController arkitController; - ARKitPlane plane; - ARKitNode node; - String anchorId; - vector.Vector3 lastPosition; +class _ArMeasurementScreenState extends State { + late ARKitController arKitController; + late ARKitPlane plane; + late ARKitNode node; + vector.Vector3? lastPosition; + late String anchorId; @override Widget build(BuildContext context) => Scaffold( - appBar: AppBar(title: const Text('Distance Tracker App')), - body: Container( - child: ARKitSceneView( - showFeaturePoints: true, - planeDetection: ARPlaneDetection.horizontal, - onARKitViewCreated: onARKitViewCreated, - enableTapRecognizer: true, - ), - ), - ); - - void onARKitViewCreated(ARKitController arkitController) - { - this.arkitController = arkitController; - this.arkitController.onAddNodeForAnchor = _handleAddAnchor; - this.arkitController.onUpdateNodeForAnchor = _handleUpdateAnchor; - this.arkitController.onARTap = (List ar) { - final planeTap = ar.firstWhere( - (tap) => tap.type == ARKitHitTestResultType.existingPlaneUsingExtent, - orElse: () => null, + body: Container( + child: ARKitSceneView( + showFeaturePoints: true, + planeDetection: ARPlaneDetection.horizontal, + onARKitViewCreated: onARViewCreated, + enableTapRecognizer: true, + ), + ), + ); + void onARViewCreated(ARKitController arKitController) { + this.arKitController = arKitController; + this.arKitController.onAddNodeForAnchor = addAnchor; + this.arKitController.onUpdateNodeForAnchor = updateAnchor; + this.arKitController.onARTap = (List ar) { + final planeTap = ar.firstWhere((tap) => tap.type == ARKitHitTestResultType.existingPlaneUsingExtent, + // orElse: () => null, ); if (planeTap != null) { - _onPlaneTapHandler(planeTap.worldTransform); + onPlaneTapHandler(planeTap.worldTransform); } }; } - void _handleAddAnchor(ARKitAnchor anchor) - { - if (!(anchor is ARKitPlaneAnchor)) { + void addAnchor(ARKitAnchor anchor) { + if (anchor is! ARKitPlaneAnchor) { return; } - _addPlane(arkitController, anchor); - } - - void _handleUpdateAnchor(ARKitAnchor anchor) - { - if (anchor.identifier != anchorId) { - return; - } - final ARKitPlaneAnchor planeAnchor = anchor; - node.position = - vector.Vector3(planeAnchor.center.x, 0, planeAnchor.center.z); - plane.width.value = planeAnchor.extent.x; - plane.height.value = planeAnchor.extent.z; + addPlane(arKitController, anchor); } - void _addPlane(ARKitController controller, ARKitPlaneAnchor anchor) - { + void addPlane(ARKitController controller, ARKitPlaneAnchor anchor) { anchorId = anchor.identifier; - plane = ARKitPlane( - width: anchor.extent.x, - height: anchor.extent.z, - materials: [ - ARKitMaterial( - transparency: 0.5, - diffuse: ARKitMaterialProperty(color: Colors.white), - ) - ], - ); + + plane = + ARKitPlane(width: anchor.extent.x, height: anchor.extent.z, materials: [ + ARKitMaterial( + transparency: 0.5, diffuse: ARKitMaterialProperty.color(Colors.white)) + ]); node = ARKitNode( geometry: plane, position: vector.Vector3(anchor.center.x, 0, anchor.center.z), rotation: vector.Vector4(1, 0, 0, -math.pi / 2), ); + controller.add(node, parentNodeName: anchor.nodeName); } - void _onPlaneTapHandler(Matrix4 transform) - { - final position = vector.Vector3( - transform.getColumn(3).x, - transform.getColumn(3).y, - transform.getColumn(3).z, - ); + void updateAnchor(ARKitAnchor anchor) { + if (anchor.identifier != anchorId) { + return; + } + + final ARKitPlaneAnchor planeAnchor = anchor as ARKitPlaneAnchor; + node.position = + vector.Vector3(planeAnchor.center.x, 0, planeAnchor.center.z); + plane.width.value = planeAnchor.extent.x; + plane.height.value = planeAnchor.extent.z; + } + + void onPlaneTapHandler(Matrix4 transform) { + final position = vector.Vector3(transform.getColumn(3).x, + transform.getColumn(3).y, transform.getColumn(3).z); + final material = ARKitMaterial( - lightingModelName: ARKitLightingModel.constant, - diffuse: - ARKitMaterialProperty(color: const Color.fromRGBO(255, 153, 83, 1)), - ); - final sphere = ARKitSphere( - radius: 0.003, - materials: [material], - ); - final node = ARKitNode( - geometry: sphere, - position: position, - ); - arkitController.add(node); + lightingModelName: ARKitLightingModel.constant, + diffuse: + ARKitMaterialProperty.color(const Color.fromRGBO(255, 153, 83, 1))); + + final sphere = ARKitSphere(radius: 0.003, materials: [material]); + + final node = ARKitNode(geometry: sphere, position: position); + + arKitController.add(node); + if (lastPosition != null) { - final line = ARKitLine( - fromVector: lastPosition, - toVector: position, - ); + final line = ARKitLine(fromVector: lastPosition as vector.Vector3, toVector: position); + final lineNode = ARKitNode(geometry: line); - arkitController.add(lineNode); - final distance = _calculateDistanceBetweenPoints(position, lastPosition); - final point = _getMiddleVector(position, lastPosition); - _drawText(distance, point); + arKitController.add(lineNode); + + final distance = calculateDistanceBetweenPoint(position, lastPosition as vector.Vector3); + final point = getMiddleVector(position, lastPosition as vector.Vector3); + drawText(distance, point); } lastPosition = position; } - String _calculateDistanceBetweenPoints(vector.Vector3 A, vector.Vector3 B) - { + String calculateDistanceBetweenPoint(vector.Vector3 A, vector.Vector3 B) { final length = A.distanceTo(B); return '${(length * 100).toStringAsFixed(2)} cm'; } - vector.Vector3 _getMiddleVector(vector.Vector3 A, vector.Vector3 B) - { - return vector.Vector3((A.x + B.x) / 2, (A.y + B.y) / 2, (A.z + B.z) / 2); + vector.Vector3 getMiddleVector(vector.Vector3 A, vector.Vector3 B) { + return vector.Vector3( + (A.x + B.x) / 2, + (A.y + B.y) / 2, + (A.z + B.z) / 2, + ); } - void _drawText(String text, vector.Vector3 point) - { + void drawText(String textDistance, vector.Vector3 point) { final textGeometry = ARKitText( - text: text, + text: textDistance, extrusionDepth: 1, materials: [ ARKitMaterial( - diffuse: ARKitMaterialProperty(color: Colors.red), + diffuse: ARKitMaterialProperty.color(Colors.red), ) - ], - ); - const scale = 0.001; - final vectorScale = vector.Vector3(scale, scale, scale); - final node = ARKitNode( - geometry: textGeometry, - position: point, - scale: vectorScale, - ); - arkitController - .getNodeBoundingBox(node) - .then((List result) { - final minVector = result[0]; - final maxVector = result[1]; - final dx = (maxVector.x - minVector.x) / 2 * scale; - final dy = (maxVector.y - minVector.y) / 2 * scale; - final position = vector.Vector3( - node.position.x - dx, - node.position.y - dy, - node.position.z, + ] ); - node.position = position; - }); - arkitController.add(node); + + const scale = 0.001; + final vectorScale = vector.Vector3(scale, scale, scale); + + final node = ARKitNode( + geometry: textGeometry, + position: point, + scale: vectorScale + ); + + arKitController.getNodeBoundingBox(node) + .then((List result) { + final minVector = result[0]; + final maxVector = result[1]; + + final dx = (maxVector.x - minVector.x) / 2 * scale; + final dy = (maxVector.y - minVector.y) / 2 * scale; + + final position = vector.Vector3( + node.position.x - dx, + node.position.y - dy, + node.position.z + ); + node.position = position; + }); + arKitController.add(node); } }