【问题标题】:Retrieve bounds of an augmented object检索增强对象的边界
【发布时间】:2018-08-09 08:19:16
【问题描述】:

我正在尝试将 ViewRenderable 锚定到 ModelRenderable 以便它出现在 Renderable 的右上角,并且我使用以下 sn-p 来放置它。

    //Sets the button north east of the node that it is attached to
    viewRenderable.localPosition = Vector3(
            parent.right.x,
            parent.up.y,
            parent.right.z
    )

但是,viewRenderable 并没有真正按预期显示。它宁愿显示远离ModelRenderable 右上角的几个浮点数。有没有办法获得ModelRenderable 的实际边界,以便更好地放置它?我想要类似于下图的最终结果。

我现在拥有的是这张图片:

我的代码:

//Creation of ViewRenderable
val shareButton = ViewRenderable.builder()
    .setVerticalAlignment(ViewRenderable.VerticalAlignment.TOP)
    .setHorizontalAlignment(ViewRenderable.HorizontalAlignment.RIGHT)
    .setView(view.getARFragment().context, R.layout.share)
    .build()

//Creation of ModelRenderable
val video = ModelRenderable.builder()
    .setSource(view.getARFragment().context,
        Uri.parse("chroma_key_video.sfb")).build()


//Adding Model to the scene | Extension method called on ParentNode
private fun Node.attachVideoNode(videoRenderable: ModelRenderable?) {
    //Show another anchor object
    val videoNode = Node()
    //Attach the new node with the node that this method was called on
    videoNode.setParent(this@attachVideoNode)
    //Set local position
    videoNode.localPosition = this.right //This works fine. The video is rendered next to another ModelRenderable.

    // Set the scale of the node so that the aspect ratio of the video is correct.
    val videoWidth = mediaPlayer?.videoWidth?.toFloat()
    val videoHeight = mediaPlayer?.videoHeight?.toFloat()
    videoHeight?.let {
        videoWidth?.apply {
            videoNode.localScale = Vector3(
                    (this@apply / it), 1.0f, 1.0f)
        }
    }

    // Start playing the video when the first node is placed.
    mediaPlayer?.apply {
        if (!isPlaying) {
            start()
            // Wait to set the renderable until the first frame of the  video becomes available.
            // This prevents the renderable from briefly appearing as a black quad before the video
            // plays.
            texture?.surfaceTexture
                    ?.setOnFrameAvailableListener { surfaceTexture ->
                        videoNode.renderable = videoRenderable
                        surfaceTexture.setOnFrameAvailableListener(null)
                        //Attach a share button
                        videoNode.attachShareButton()
                        ARApplication.log("The local rotation of this videoRenderable is ${videoNode.localRotation}")
                    }
        } else
            videoNode.renderable = videoRenderable
    }
}

//Attaches a share button to any node that it is called on. | Extension method called on VideoNode
private fun Node.attachShareButton() {
    //Create a new node to display the close button
    var closeButton = Node()
    closeButton.setParent(this)
    ARApplication.log("The close button has been added to the scene at world coordinates: ${closeButton.worldPosition}")

    //Set the close button as a renderable
    closeButton.renderable = view.getViewRenderable()
}

【问题讨论】:

    标签: android arcore sceneform


    【解决方案1】:

    我不确切知道您已经得到了什么,但这是在顶部和右侧的 ViewRenderable 上放置 文本 的解决方案

    x x 垂直

    x ox

    x x x

    V - ViewRenderable O - ModelRenderable

    ViewRenderable.builder()
                    .setView(context, R.layout.VIEW_LAYOUT_XXXXX)
                    .setHorizontalAlignment(ViewRenderable.HorizontalAlignment.RIGHT) //Here is where the magic happens
                    .build()
                    .thenAccept(
                            (renderable) -> {
                                YOUR_NODE_XXXXX.setRenderable(renderable);
                                TextView textView = (TextView) renderable.getView();
                                textView.setText(YOUR_TEXT_XXXXX);
                            })
                    .exceptionally(
                            (throwable) -> {
                                throw new AssertionError(R.string.ERROR_MSG_XXXXX, throwable);
                            });
    

    我让_XXXXX在你应该用你的代码改变的一切。

    结果应该是这样的

    编辑。好的,现在看看你的代码,这就是你应该做的:

    首先你需要初始化你的视图(我认为你的“关闭按钮”应该是你的分享按钮)

    private fun Node.attachShareButton() {
    if(shareButton == null){ //Create a new node to display the share button
        var shareButton = Node()
        shareButton.setParent(this)
        shareButton.renderable = view.getViewRenderable()
        shareButton.setLocalPosition(new Vector3(SIZE_OF_BUTTON, SIZE_OF_BUTTON, SIZE_OF_BUTTON));
    }
    
    if (infoCard == null) {
      infoCard = new Node();
      infoCard.setParent(this);
      infoCard.setEnabled(false);
      infoCard.setLocalPosition(new Vector3(0.0f, SIZE_OF_BUTTON*0.55f, 0.0f));
    
      ViewRenderable.builder()
          .setView(context,  R.layout.share)
          .setHorizontalAlignment(ViewRenderable.HorizontalAlignment.RIGHT)
          .build()
          .thenAccept(
              (renderable) -> {
                infoCard.setRenderable(renderable);
              })
          .exceptionally(
              (throwable) -> {
                throw new AssertionError(R.string.ERROR_MSG_XXXXX, throwable);
              });
    }
    
    shareButton.onTap(HitTestResult hitTestResult, MotionEvent motionEvent){
        if (infoCard == null) {
            return;
        }
    
        infoCard.setEnabled(!infoCard.isEnabled()); //This show your card when the button is click/tap
    }
    }
    

    【讨论】:

    • 我完全按照你的建议做了,结果就像this
    • @Clinkz 我用我的结果图片进行编辑。如果您没有得到相同的结果,您可以从 ViewRenderable、Layout 和 ModelRenderable 中发布您所拥有的图片(不是平局)和代码(如果可能)
    • 您可以查看截图here和代码here。这不是整个代码,只是其中的相关部分。
    • @Clinkz 我将图片和代码添加到主要问题中,请接受。并用新信息编辑我的答案。让我知道是否可行,如果不行,我们应该尝试创建一个新课程。
    • 这绝对是救命稻草,对我有用,虽然我一直在寻找 VerticalAlignment,但你为我设定了正确的方向,它对我有用
    猜你喜欢
    • 2019-04-22
    • 2021-02-09
    • 1970-01-01
    • 2019-09-12
    • 2020-04-03
    • 2020-07-20
    • 2019-10-09
    • 1970-01-01
    • 2014-08-02
    相关资源
    最近更新 更多