首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用全球定位系统在ARCore中渲染3D对象?

使用全球定位系统在ARCore中渲染3D对象?
EN

Stack Overflow用户
提问于 2018-08-17 12:15:54
回答 3查看 7.3K关注 0票数 5

我尝试在android中使用arcore构建增强现实导航。AR导航方向在印度可用吗?我只是顺着这个链接,使用arcore开发了具有核心位置的ar导航。https://github.com/appoly/ARCore-Location/tree/legacy

有没有可能在安卓系统中使用ARCore进行ar导航?任何帮助,非常感谢.

EN

回答 3

Stack Overflow用户

发布于 2018-08-21 20:41:29

我也在做这件事,根据我的说法,这是可能的。由于其他一些工作,我还没有完成它,但我可以告诉你可以遵循的步骤来实现这一点。

最初,您需要了解如何在不点击屏幕的情况下放置对象,因为ARCore示例不允许这样做。我在StackOverflow上问了这个问题,最后得到了一个令人满意的答案,实际上是有效的。How to place a object without tapping on the screen。你可以参考这个来实现这一点。

在此之后,您需要找出需要将ar对象显示为方向的方向。为此,请使用以下公式找出标题

代码语言:javascript
复制
let lat1:Double = latSource/180 * .pi
let lng1:Double = lngSource/180 * .pi
let lat2:Double = latDestination/180 * .pi
let lng2:Double = lngDestination/180 * .pi

let y = sin(lng2 - lng1)*cos(lat2)
let x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(lng2 - lng1)

let tan2 = atan2(y, x)
let degre = tan2 * 180 / .pi
if degre < 0 {  return degre+360  }
else {  return degre  }

您需要传送源和目的地的纬度和经度,然后这将返回与目的地的TrueNorth的角度。这是Swift写的,我希望你能把它转换成你想要的语言。Java或Kotlin

然后,您需要将此角度与您的设备罗盘角度进行匹配,当两者都匹配时,使用不放置对象的方法。我已经分享了这个链接。这将为您提供指向目的地的对象。

之后,制作一个循环,并将对象放置在不同的z轴上。这是我写的代码

代码语言:javascript
复制
@Override
public void onUpdate(FrameTime frameTime) {



        frame = fragment.getArSceneView().getArFrame();

        if (frame != null) {

            for (Object o : frame.getUpdatedTrackables(Plane.class)) {

                plane = (Plane) o;

                if (plane.getTrackingState() == TrackingState.TRACKING) {
                    fragment.getPlaneDiscoveryController().hide();

                    Iterator iterableAnchor = frame.getUpdatedAnchors().iterator();

                    if (!iterableAnchor.hasNext()) {
                        method(plane, frame);
                    }
                }
            }
        }

}

public void makeAr(Plane plane, Frame frame ) {

    for (int k = 0; k <10 ; k ++) {
        if (this.degree >= 160 && this.degree <= 170) {
            Toast.makeText(this, "walk", Toast.LENGTH_SHORT).show();
            List<HitResult> hitTest = frame.hitTest(screenCenter().x, screenCenter().y);

            Iterator hitTestIterator = hitTest.iterator();

            while (hitTestIterator.hasNext()) {
                HitResult hitResult = (HitResult) hitTestIterator.next();

                modelAnchor = null;
                modelAnchor = plane.createAnchor(hitResult.getHitPose());

                AnchorNode anchorNode = new AnchorNode(modelAnchor);
                anchorNode.setParent(fragment.getArSceneView().getScene());

                TransformableNode transformableNode = new TransformableNode(fragment.getTransformationSystem());
                transformableNode.setParent(anchorNode);
                transformableNode.setRenderable(MainActivity.this.andyRenderable);

                float x = modelAnchor.getPose().tx();
                float y = modelAnchor.getPose().compose(Pose.makeTranslation(0f, 0f, 0)).ty();

                transformableNode.setWorldPosition(new Vector3(x, y, -k));

            }
        }
    }
}

private Vector3 screenCenter() {
    View vw = findViewById(android.R.id.content);
    return new Vector3(vw.getWidth() / 2f, vw.getHeight() / 2f, 0f);
}

此OnUpdate方法将Ar绘制到目的地。得到这个之后,您需要使用Google Map Api数据安排所有内容。

根据我的说法,这是使用ARCore在AR中进行导航的最好方法,因为ARCore中有非常多的限制,将来可能会有一些其他的库可以让它变得简单。

我也试过ArCore Location,但这对你没有帮助,因为它放置的是2D图像,而不是3D图像,而且当你在路上行走时,它也不稳定。

祝你好运,希望你能完成这件事。

票数 12
EN

Stack Overflow用户

发布于 2019-08-03 19:22:37

这里有三个函数

一种是在现实世界中使用x,y和z添加节点。

二是计算方位角。更多信息请访问:https://www.sunearthtools.com/tools/distance.php#top

第三是根据给定的位置计算x,y和z。

代码语言:javascript
复制
private fun addPointByXYZ(x: Float, y: Float, z: Float, name: String) {
    ViewRenderable.builder().setView(this, R.layout.sample_layout).build().thenAccept {
        val imageView = it.view.findViewById<ImageView>(R.id.imageViewSample)
        val textView = it.view.findViewById<TextView>(R.id.textViewSample)

        textView.text = name

        val node = AnchorNode()
        node.renderable = it
        scene.addChild(node)
        node.worldPosition = Vector3(x, y, z)

        val cameraPosition = scene.camera.worldPosition
        val direction = Vector3.subtract(cameraPosition, node.worldPosition)
        val lookRotation = Quaternion.lookRotation(direction, Vector3.up())
        node.worldRotation = lookRotation
    }
}

private fun bearing(locA: Location, locB: Location): Double {
    val latA = locA.latitude * PI / 180
    val lonA = locA.longitude * PI / 180
    val latB = locB.latitude * PI / 180
    val lonB = locB.longitude * PI / 180

    val deltaOmega = ln(tan((latB / 2) + (PI / 4)) / tan((latA / 2) + (PI / 4)))
    val deltaLongitude = abs(lonA - lonB)

    return atan2(deltaLongitude, deltaOmega)
}

private fun placeARByLocation(myLocation: Location, targetLocation: LatLng, name: String) {
    val tLocation = Location("")
    tLocation.latitude = targetLocation.latitude
    tLocation.longitude = targetLocation.longitude

    val degree = (360 - (bearing(myLocation, tLocation) * 180 / PI))
    val distant = 3.0

    val y = 0.0
    val x = distant * cos(PI * degree / 180)
    val z = -1 * distant * sin(PI * degree / 180)
    addPointByXYZ(x.toFloat(), y.toFloat(), z.toFloat(), name)

    Log.i("ARCore_MyLat", myLocation.latitude.toString())
    Log.i("ARCore_MyLon", myLocation.longitude.toString())
    Log.i("ARCore_TargetLat", targetLocation.latitude.toString())
    Log.i("ARCore_TargetLon", targetLocation.longitude.toString())
    Log.i("ARCore_COMPASS", azimuth.toString())
    Log.i("ARCore_Degree", degree.toString())
    Log.i("ARCore_X", x.toString())
    Log.i("ARCore_Y", y.toString())
    Log.i("ARCore_Z", z.toString())
    Toast.makeText(this@LatLngActivity, "Compass: $azimuth, Degree: $degree", Toast.LENGTH_LONG).show()
}

你在这里看到的方位是来自传感器的指南针角度。

完整的代码在我的gist中。

https://gist.github.com/SinaMN75/5fed506622054d4247112a22ef72f375

票数 3
EN

Stack Overflow用户

发布于 2018-08-17 17:22:45

的AR导航方向是否在印度可用?

是的,今天唯一明智的地方是中国,正如你所看到的here

是否可以在安卓系统中使用ARCore进行ar导航?

不知道我是否得到了,但是的,你可以使用AR核心来导航你的应用程序,如行走,并在屏幕上显示AR对象或从设备运动中获取姿势/帧信息。你可以把这个锚放在这个世界上,把这个锚和GPS定位连接起来,把这个锚保存在云/数据库中,然后加载到其他手机上。或者直接渲染对象并将其保存在数据库中。当用户走近一个地方时,你可以将它加载到他的手机中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51888308

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档