我应该使用哪个spritekit或sceneKit节点在Arkit中显示网页?因为我们通常将其他iOS视图嵌入到arplane中作为材料。
答案 0 :(得分:1)
在我尝试将WKWebView
实例直接添加到我的节点后,我得到了包含已加载网站背景颜色的空白页:
node.geometry?.firstMaterial?.diffuse.contents = webView
我在ARKit 3D环境中显示WKWebView
内容的唯一方法是加载webView,等待它完全加载,然后截取已加载页面的屏幕截图并将其添加到我的节点。 / p>
class ViewController: UIViewController, ARSCNViewDelegate, WKNavigationDelegate {
@IBOutlet var sceneView: ARSCNView!
let webView = WKWebView(frame: CGRect(x: 0, y: 0, width: 500, height: 500), configuration: WKWebViewConfiguration())
...
override func viewDidLoad() {
super.viewDidLoad()
...
webView.navigationDelegate = self
let url = URL(string: "https://stackoverflow.com")!
webView.load(URLRequest(url: url))
}
// WKWebView delegate method
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
// Page should be loaded by now. However sometimes it took even more to load, so you can use delay and wait more, then take the screenshot.
let screenshot = webView.screenshot()
let node = SCNNode()
node.geometry = SCNPlane(width: 2, height: 2)
node.geometry?.firstMaterial?.diffuse.contents = screenshot
node.geometry?.firstMaterial?.isDoubleSided = true
node.position = SCNVector3(0.1, 0.2, -2)
self.sceneView.scene.rootNode.addChildNode(node)
}
}
webView的截图方法:
extension WKWebView {
func screenshot() -> UIImage? {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, true, 0);
self.drawHierarchy(in: self.bounds, afterScreenUpdates: true);
let snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return snapshotImage;
}
}
答案 1 :(得分:1)
WKWebView
在ARKit上不能很好地工作。 UIWebView
可以显示ARKit中的内容。
答案 2 :(得分:0)
SpriteKit用于2D视图,SceneKit用于3D视图,因此SpriteKit更合适。虽然您可以在3D中使用2D平面