简体   繁体   English

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

[英]How to place a 3D Model in AR at center of screen and move it near or far with respect to camera angle on axis?

Below is the code which I have tried.下面是我尝试过的代码。 It gives me the desired result but it is not optimized like camToPlan or MagicPlan app.它给了我想要的结果,但它没有像 camToPlan 或 MagicPlan 应用程序那样优化。 In CamToPlan app center node moves very eficiently as per camera movement.在 CamToPlan 应用程序中心节点根据相机移动非常有效地移动。 If the camera is tilt the anchornode distance changes.如果相机倾斜,则锚节点距离会发生变化。 How to achive the same in below code?如何在下面的代码中实现相同的目标?

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);

Step1: Create addWaterMark() method in your class步骤 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
    }
}

Step 2: Call the addWaterMark() inside addOnUpdateListener第 2 步:在 addOnUpdateListener 中调用 addWaterMark()

  arSceneView.scene.addOnUpdateListener { addWaterMark() }

Note : I created oldWaterMark object for remove the old watermark注意:我创建了oldWaterMark对象来删除旧水印

If you want to change the position change this line如果你想改变位置改变这一行

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM