gpt4 book ai didi

swift - 如何在 ARKit 中使用环境贴图?

转载 作者:搜寻专家 更新时间:2023-10-31 19:32:11 24 4
gpt4 key购买 nike

ARKit 2.0 添加了一个名为 AREnvironmentProbeAnchor 的新类。阅读它的说明,似乎 ARKit 可以自动收集环境纹理(立方体贴图?)。我相信我们现在可以创建一些反射(reflect)真实环境的虚拟对象。

但我仍然不清楚这是如何工作的,尤其是环境纹理是如何生成的。有没有人有简单的示例代码来演示这个很酷的功能?

最佳答案

AREnvironmentProbeAnchor(适用于 iOS 12.0+)是基于图像的光照技术的 anchor 。模型的 PBR 着色器可以反射来自周围环境的光。原理很简单:来自源的 6 个正方形图像进入着色 Material 的 env 反射率 channel 。这六个源(一个 rig)有以下方向:+x/-x, +y/-y+z/-z。下图说明了钻机的 6 个方向:

enter image description here

相邻的 zFar 平面看起来像一个立方体,不是吗?

enter image description here

纹理的补丁将在您的相机扫描表面的特定位置可用。 ARKit 使用先进的机器学习算法来覆盖具有完整 360 度纹理的立方体。

AREnvironmentProbeAnchor 用于将此摄影装置定位在场景中的特定点。您所要做的就是在 AR session 中启用环境纹理贴图生成。有两种选择:

ARWorldTrackingConfiguration.EnvironmentTexturing.manual 

使用 手动 环境纹理,您可以通过创建 AREnvironmentProbeAnchor 对象并将它们添加到 session 中来识别场景中需要光照探针纹理贴图的点。

ARWorldTrackingConfiguration.EnvironmentTexturing.automatic 

借助自动环境纹理,ARKit 会自动创建、定位 AREnvironmentProbeAnchor 对象并将其添加到 session 中。

In both cases, ARKit automatically generates environment textures as the session collects camera imagery. Use a delegate method such as session(_:didUpdate:) to find out when a texture is available, and access it from the anchor's environmentTexture property.

If you display AR content using ARSCNView and the automaticallyUpdatesLighting option, SceneKit automatically retrieves AREnvironmentProbeAnchor texture maps and uses them to light the scene.

ViewController.swift 中的代码可能如下所示:

sceneView.automaticallyUpdatesLighting = true

let torusNode = SCNNode(geometry: SCNTorus(ringRadius: 2, pipeRadius: 1.5))
sceneView.scene.rootNode.addChildNode(torusNode)

let reflectiveMaterial = SCNMaterial()
reflectiveMaterial.lightingModel = .physicallyBased
reflectiveMaterial.metalness.contents = 1.0
reflectiveMaterial.roughness.contents = 0
reflectiveMaterial.diffuse.contents = UIImage(named: "brushedMetal.png")
torusNode.geometry?.firstMaterial = [reflectiveMaterial]

let config = ARWorldTrackingConfiguration()
if #available(iOS 12.0, *) {
config.environmentTexturing = .automatic // magic happens here
}
sceneView.session.run(config)

然后使用一个session(...)实例方法:

func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {

guard let envProbeAnchor = anchors.first as? AREnvironmentProbeAnchor
else { return }

print(envProbeAnchor.environmentTexture)
print(envProbeAnchor.extent)
}

关于swift - 如何在 ARKit 中使用环境贴图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50769722/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com