Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何将3D模型放置在屏幕中心的AR中,并相对于轴上的相机角度将其移近或移远?_Android_Arcore_Sceneform - Fatal编程技术网

Android 如何将3D模型放置在屏幕中心的AR中,并相对于轴上的相机角度将其移近或移远?

Android 如何将3D模型放置在屏幕中心的AR中,并相对于轴上的相机角度将其移近或移远?,android,arcore,sceneform,Android,Arcore,Sceneform,下面是我尝试过的代码。它给了我想要的结果,但它没有像camToPlan或MagicPlan应用程序那样优化。在CamToPlan应用程序中,中心节点根据摄像机的移动非常有效。如果相机倾斜,则锚定节点距离会改变。如何在下面的代码中实现相同的功能 Camera camera = arSceneView.getScene().getCamera(); Vector3 distance = Vector3.subtract(camera.getWorldPosition(), v

下面是我尝试过的代码。它给了我想要的结果,但它没有像camToPlan或MagicPlan应用程序那样优化。在CamToPlan应用程序中,中心节点根据摄像机的移动非常有效。如果相机倾斜,则锚定节点距离会改变。如何在下面的代码中实现相同的功能

Camera camera = arSceneView.getScene().getCamera();
            Vector3 distance = Vector3.subtract(camera.getWorldPosition(), vector3CirclePosition);
            float abs = Math.abs(distance.y);
            float newAngleInRadian = (float) (Math.toRadians(90f) - (float) camera.getLocalRotation().x);
            float zCoordinate = (float) (abs / Math.cos(newAngleInRadian));
            Log.i("1", "zCoordinate::" + zCoordinate + "::" + abs);
            Vector3 cameraPos = arFragment.getArSceneView().getScene().getCamera().getWorldPosition();
            Vector3 cameraForward = arFragment.getArSceneView().getScene().getCamera().getForward();
            Vector3 position = Vector3.add(cameraPos, cameraForward.scaled(zCoordinate));
            redNodeCenter.setWorldPosition(position);
步骤1:在类中创建addWaterMark()方法

 private var oldWaterMark : Node? = null
 
 private fun addWaterMark() {
    ModelRenderable.builder()

            .setSource(context, R.raw.step1)
            .build()
            .thenAccept {
                addNode(it)
            }
            .exceptionally {
                Toast.makeText(context, "Error", Toast.LENGTH_SHORT).show()
                return@exceptionally null
            }

}

private fun addNode(model: ModelRenderable?) {
    if(oldWaterMark!=null){
        arSceneView().scene.removeChild(oldWaterMark)
    }
    model?.let {
        val node = Node().apply {
            setParent(arSceneView().scene)
            var camera = arSceneView().scene.camera

            var ray = camera.screenPointToRay(200f,500f)

           // var local=arSceneView.getScene().getCamera().localPosition

            localPosition = ray.getPoint(1f)
            localRotation = arSceneView().scene.camera.localRotation
            localScale = Vector3(0.3f, 0.3f, 0.3f)


            renderable = it
        }

        arSceneView().scene.addChild(node)
        oldWaterMark = node
    }
}
步骤2:在addOnUpdateListener中调用addWaterMark()

  arSceneView.scene.addOnUpdateListener { addWaterMark() }
注意:我创建了旧水印对象以删除旧水印

如果要更改位置,请更改此行

  camera.screenPointToRay(200f,500f)//200f -> X position, 500f -> Y position

你找到办法了吗?你有解决这个问题的办法吗?矢量3,这是什么;