如何使用AR Flutter插件与不同的3D对象?

bnlyeluc  于 2023-11-21  发布在  Flutter
关注(0)|答案(2)|浏览(237)


我试图使用ar_flutter_plugin.在flutter examplepub.dev,他们正在使用一个名为Chicken_01.gltb的gltb文件作为3D对象,这是正确的工作.但当我从互联网下载后尝试其他gltb文件,他们不工作,甚至没有其他对象工作,无论我提供链接或作为一个本地对象。有人也提出了同样的issue在他们的github仓库页面。我已经尝试了很多事情,但我不明白这是怎么回事,请帮帮忙
注意:我想为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和错误截图,这是当按下按钮来显示对象.

brgchamk

brgchamk1#

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

x7yiwoj4

x7yiwoj42#

对于其他有这个问题的人,我通过在URL中添加“raw”而不是“blob”来解决它。

相关问题