placeObject method

Future<void> placeObject()

Implementation

Future<void> placeObject() async {
  if (_tapLocked) return;
  _tapLocked = true;

  try {
    if (session == null ||
        objects == null ||
        anchors == null ||
        _localModel == null ||
        _nodes.isNotEmpty) {
      return;
    }

    hintState.value = ARHintState.loading;

    final size = MediaQuery.of(session!.buildContext).size;

    final results = await session!.hitTest(
      size.width / 2,
      size.height / 2,
    );

    if (results.isEmpty) {
      hintState.value = ARHintState.showingHint;
      return;
    }

    final hit = results.first;
    final anchor = ARPlaneAnchor(transformation: hit.worldTransform);

    if (await anchors!.addAnchor(anchor) != true) {
      hintState.value = ARHintState.showingHint;
      return;
    }

    final node = ARNode(
      type: NodeType.webGLB,
      uri: _localModel!.path,
      scale: Vector3.all(2.0),
    );

    final added = await objects!.addNode(node, planeAnchor: anchor);
    if (added == true) {
      _nodes.add(node);
      objects!.setActiveNode(node);
      hintState.value = ARHintState.placed;
    } else {
      hintState.value = ARHintState.showingHint;
    }
  } finally {
    _tapLocked = false;
  }
}