如何将AR Flutter Plugin与不同的3D对象一起使用?

问题描述 投票:0回答:1

我正在尝试使用ar_flutter_plugin。在 pub.dev 上的 flutter example 中,他们使用 gltb 文件作为名为 Chicken_01.gltb 的 3D 对象,该文件工作正常。但是,当我从互联网下载后尝试其他 gltb 文件时,它们不起作用,甚至没有其他对象起作用,无论我提供链接还是作为本地对象。有人还在他们的 github 存储库页面上提出了同样的issue。我尝试了很多事情,但我无法理解这是怎么回事。请帮忙

注意:我想为 Android 执行此操作。

class LocalAndWebObjectsView extends StatefulWidget {
  const LocalAndWebObjectsView({Key? key}) : super(key: key);

  @override
  State<LocalAndWebObjectsView> createState() => _LocalAndWebObjectsViewState();
}

class _LocalAndWebObjectsViewState extends State<LocalAndWebObjectsView> {
  late ARSessionManager arSessionManager;
  late ARObjectManager arObjectManager;

//String localObjectReference;
  ARNode? localObjectNode;

//String webObjectReference;
  ARNode? webObjectNode;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Local / Web Objects"),
      ),
      body: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 10),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            SizedBox(
              height: MediaQuery.of(context).size.height * .8,
              child: ClipRRect(
                borderRadius: BorderRadius.circular(22),
                child: Container(
                  color: Colors.black,
                  child: ARView(
                    onARViewCreated: onARViewCreated,
                  ),
                ),
              ),
            ),
            Row(
              children: [
                Expanded(
                  child: ElevatedButton(
                      onPressed: () {
                        onLocalObjectButtonPressed();
                      },
                      child: const Text("Add / Remove Local Object")),
                ),
                const SizedBox(
                  width: 10,
                ),
                Expanded(
                  child: ElevatedButton(
                      onPressed: () {
                        onWebObjectAtButtonPressed();
                      },
                      child: const Text("Add / Remove Web Object")),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  void onARViewCreated(
    ARSessionManager arSessionManager,
    ARObjectManager arObjectManager,
    ARAnchorManager arAnchorManager,
    ARLocationManager arLocationManager,
  ) {
    this.arSessionManager = arSessionManager;
    this.arObjectManager = arObjectManager;

    this.arSessionManager.onInitialize(
          showFeaturePoints: false,
          showPlanes: true,
          customPlaneTexturePath: "triangle.png",
          showWorldOrigin: true,
          handleTaps: false,
        );

    this.arObjectManager.onInitialize();
  }

  Future<void> onLocalObjectButtonPressed() async {

    if (localObjectNode != null) {
      arObjectManager.removeNode(localObjectNode!);
      localObjectNode = null;
    } else {

      var newNode = ARNode(
          type: NodeType.localGLTF2,
          uri: "assets/Chicken_01/Chicken_01.gltf",
          scale: vec.Vector3(0.2, 0.2, 0.2),
          position: vec.Vector3(0.0, 0.0, 0.0),
          rotation: vec.Vector4(1.0, 0.0, 0.0, 0.0));

      bool? didAddLocalNode = await arObjectManager.addNode(newNode);
      localObjectNode = (didAddLocalNode!) ? newNode : null;
    }
  }

  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: vec.Vector3(0.2, 0.2, 0.2));
      bool? didAddWebNode = await arObjectManager.addNode(newNode);
      webObjectNode = (didAddWebNode!) ? newNode : null;
    }
  }
}

我已附加 3D 对象files 以及按下按钮显示该对象时出现的错误屏幕截图。

flutter augmented-reality flutter-plugin
1个回答
0
投票

确保包含特定 .gltf 文件的文件夹也具有相应的 .bin 文件。您不必在“uri”属性中访问它,只需确保它存在即可。因此,“Chicken_01.gltf”起作用的原因很可能是因为文件夹“Chicken_01”也有相应的“Chicken_01.bin”文件。

© www.soinside.com 2019 - 2024. All rights reserved.