arcore_flutter_plugin

ARCore SDK 的 Flutter 插件,适用于 Android 平台,用于构建新的增强现实体验。

用法

我写了 2 篇文章来帮助您设置项目并开始使用 ARCore Flutter 插件。

ARCore Flutter 插件:配置.

ARCore Flutter 插件:在平面上添加对象.

配置您的应用程序

要使用此插件,请将 arcore_flutter_plugin 添加到您的 pubspec.yaml 文件中作为 依赖项

  1. 请遵循 官方指南 来启用 ArCore。

  2. 将 Sceneform 库添加到您应用的 build.gradle 文件中。

android {
    // Sceneform libraries use language constructs from Java 8.
    // Add these compile options if targeting minSdkVersion < 26.
    compileOptions {
        sourceCompatibility 1.8
        targetCompatibility 1.8
    }
}

dependencies {
    …

    // Provides ArFragment, and other UX resources.
    implementation 'com.google.ar.sceneform.ux:sceneform-ux:1.8.0'

    // Alternatively, use ArSceneView without the UX dependency.
    implementation 'com.google.ar.sceneform:core:1.8.0'
}
  1. 将 Sceneform 插件导入您的项目(可选)。

示例

最简单的代码示例

import 'package:arcore_flutter_plugin/arcore_flutter_plugin.dart';
import 'package:flutter/material.dart';
import 'package:vector_math/vector_math_64.dart' as vector;

class HelloWorld extends StatefulWidget {
  @override
  _HelloWorldState createState() => _HelloWorldState();
}

class _HelloWorldState extends State<HelloWorld> {
  ArCoreController arCoreController;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Hello World'),
        ),
        body: ArCoreView(
          onArCoreViewCreated: _onArCoreViewCreated,
        ),
      ),
    );
  }

  void _onArCoreViewCreated(ArCoreController controller) {
    arCoreController = controller;

    _addSphere(arCoreController);
    _addCylindre(arCoreController);
    _addCube(arCoreController);
  }

  void _addSphere(ArCoreController controller) {
    final material = ArCoreMaterial(
        color: Color.fromARGB(120, 66, 134, 244));
    final sphere = ArCoreSphere(
      materials: [material],
      radius: 0.1,
    );
    final node = ArCoreNode(
      shape: sphere,
      position: vector.Vector3(0, 0, -1.5),
    );
    controller.addArCoreNode(node);
  }

  void _addCylindre(ArCoreController controller) {
    final material = ArCoreMaterial(
      color: Colors.red,
      reflectance: 1.0,
    );
    final cylindre = ArCoreCylinder(
      materials: [material],
      radius: 0.5,
      height: 0.3,
    );
    final node = ArCoreNode(
      shape: cylindre,
      position: vector.Vector3(0.0, -0.5, -2.0),
    );
    controller.addArCoreNode(node);
  }

  void _addCube(ArCoreController controller) {
    final material = ArCoreMaterial(
      color: Color.fromARGB(120, 66, 134, 244),
      metallic: 1.0,
    );
    final cube = ArCoreCube(
      materials: [material],
      size: vector.Vector3(0.5, 0.5, 0.5),
    );
    final node = ArCoreNode(
      shape: cube,
      position: vector.Vector3(-0.5, 0.5, -3.5),
    );
    controller.addArCoreNode(node);
  }

  @override
  void dispose() {
    arCoreController.dispose();
    super.dispose();
  }
}

有关完整的示例应用程序,请参阅 example 目录。

3D 对象来源

匿名


文档

插件提供的类

截至 2020 年 5 月,此插件总共提供了 13 个类。

  • ArCoreView
  • ArCoreController
  • ArCoreFaceView
  • ArCoreFaceContrller
  • ArCoreSphere
  • ArCoreCylinder
  • ArCoreCube
  • ArCoreNode
  • ArCoeMaterial
  • ArCoreHitTestResult
  • ArCoreRotatingNode
  • ArCorePlane
  • ArCoreReferenceNode

ArCoreView

此类返回视图类型。其中有两种视图。

AUGMENTEDFACE
STANDARDVIEW

其中有 4 个属性。

  • onArCoreViewCreated
  • enableTapRecoginzer
  • enableUpdateListener
  • 类型

onArCoreViewCreated

此属性接受一个 ArCoreController


enableTapRecoginzer

初始设置为 false。它被用作 MethodChannel 的参数。


enableUpdateListener

初始设置为 false。它被用作 MethodChannel 的参数。


类型

它是一个视图类型,可以是 AUGMENTEDFACESTANDARDVIEW。默认设置为 STANDARDVIEW


ArCoreController

此控制器用于使用 addArCoreNode 函数添加 ArNode,使用 addArCoreNodeWithAncher 函数添加带有锚点的 ArCoreNode,以及使用 removeNode 函数删除节点。


ArCoreFaceView

这是一个有状态的小部件,它返回一个 ArCoreAndroidView。它有两个属性:enableAugmentedFacesonArCoreViewCreated

最初,enableAugmentedFaces 被设置为 false。
onArCoreViewCreated 接受一个带有 ArCoreController 参数的函数。


ArCoreFaceController

它使用 dispose 和 loadMesh 方法来控制 FaceView


ArCoreSphere

它是一个 ArCoreShape,接受一个 radiusArCoreMaterial


ArCoreCylender

它是一个 ArCoreShape,接受一个 radiusheightArCoreMaterial


ArCoreCube

它是一个 ArCoreShape,接受一个大小,即 Vector3ArCoreMaterial


ArCoreNode

此小部件用于提供 positionshapescalerotationname


ArCoreMaterial

它用于描述用户创建的虚拟对象的外观。

它具有 colortextureBytesmetallicroughnessreflection 属性。


ArCoreRotatingNode

这是一个 ArCoreNode,具有一个 degreesPerSecond 属性,该属性是一个 double 值。


ArCorePlane

它接受平面的 xy 坐标、ArCorePoseArCorePlaneType

有三种类型的平面:

  • HORIZONTAL_UPWARD_FACING
  • HORIZONTAL_DOWNWARD_FACING
  • VERTICAL

ArCoreReferenceNode

这是一个 ArCoreNode,它具有 ArCoreNode 的所有属性,还具有 objectUrl 和 object3DFileName。


objectUrl

用于远程渲染的 gltf 对象的 URL。


object3DFileName

assets 文件夹中 sfb 对象的文件名。

GitHub

https://github.com/giandifra/arcore_flutter_plugin