如何从Node.java文件访问增强图像的getIndex

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

我在我的imgdb文件中添加了一些不同的图像,这些图像被arcore sceneform完美检测到。但是,我想为imgdb文件中的每个图像显示不同的3D对象。我相信这可以使用getIndex函数,但我不知道如何使用它,因为我是一个菜鸟。

在下面的代码中,如何将当前检测到的图像的索引分配给变量“value”?

public class AugmentedImageNode extends AnchorNode {

  private static final String TAG = "AugmentedImageNode";

  // The augmented image represented by this node.
  private AugmentedImage image;

 private static CompletableFuture<ModelRenderable> ulCorner;


   public AugmentedImageNode(Context context) {

          if (value == 0) {
              // Upon construction, start loading the models for the corners of the frame.
              if (ulCorner == null) {
                  ulCorner =
                          ModelRenderable.builder()
                                  .setSource(context, Uri.parse("models/tinker.sfb"))
                                  //.setSource(context, Uri.parse("models/borderfence-small2.sfb"))
                                  .build();
                   }
          }

          if (value == 1) {
              // Upon construction, start loading the models for the corners of the frame.
              if (ulCorner == null) {
                  ulCorner =
                          ModelRenderable.builder()
                                  .setSource(context, Uri.parse("models/borderfence-small.sfb"))
                                  //.setSource(context, Uri.parse("models/borderfence-small2.sfb"))
                                  .build();
              }
          }

      }

  /**
   * Called when the AugmentedImage is detected and should be rendered. A Sceneform node tree is
   * created based on an Anchor created from the image. The corners are then positioned based on the
   * extents of the image. There is no need to worry about world coordinates since everything is
   * relative to the center of the image, which is the parent node of the corners.
   */
  @SuppressWarnings({"AndroidApiChecker", "FutureReturnValueIgnored"})
  public void setImage(AugmentedImage image) {
    this.image = image;

    // If any of the models are not loaded, then recurse when all are loaded.
    if (!ulCorner.isDone())// || !urCorner.isDone() || !llCorner.isDone() || !lrCorner.isDone())
      {
      CompletableFuture.allOf(ulCorner)//, urCorner, llCorner, lrCorner)
          .thenAccept((Void aVoid) -> setImage(image))
          .exceptionally(
              throwable -> {
                Log.e(TAG, "Exception loading", throwable);
                return null;
              });
    }

    // Set the anchor based on the center of the image.
    setAnchor(image.createAnchor(image.getCenterPose()));

    // Make the 4 corner nodes.
    Vector3 localPosition = new Vector3();
    Node cornerNode;

    // Upper left corner.
    //localPosition.set(-0.5f * image.getExtentX(), 0.0f, -0.5f * image.getExtentZ());
    localPosition.set(-0.0f * image.getExtentX(), 0.1f, +0.5f * image.getExtentZ());
    cornerNode = new Node();
    cornerNode.setParent(this);
    cornerNode.setLocalPosition(localPosition);
    cornerNode.setLocalRotation(Quaternion.axisAngle(new Vector3(-1f, 0, 0), 90f));
    cornerNode.setRenderable(ulCorner.getNow(null));

  }

    private void setLocalRotation() {
    }

    public AugmentedImage getImage() {
    return image;
  }
}
java android android-studio arcore sceneform
1个回答
0
投票

您可以使用可用于AugmentedImage的getIndex()函数。

public int getIndex ()

从其原始图像数据库返回此增强图像的从零开始的位置索引。

此索引用作数据库中图像的唯一标识符。

如需进一步阅读,请参阅official documentationSceneform AugmentedImage Sample

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