How to display web page in ARKit ios

给你一囗甜甜゛ 提交于 2019-12-24 11:02:55

问题


Which spritekit or sceneKit node should I use to display a web page in Arkit? As we usually embed other iOS views in arplane as materials.


回答1:


I got empty pages containing background color of the loaded site after I tried to directly add my WKWebView instance to my node like this:

node.geometry?.firstMaterial?.diffuse.contents = webView

The only way I could show a WKWebView content in my ARKit 3D environment was to load the webView, wait for it to fully load and then take a screenshot of the loaded page and add it to my node.

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

screenshot method for 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;
    }
}



回答2:


WKWebView didn't work well with ARKit. UIWebView can show the contents in ARKit.




回答3:


SpriteKit is used for 2D views and SceneKit for 3D views, so SpriteKit would be more appropriate. Although you can use 2D planes in 3D



来源:https://stackoverflow.com/questions/49954789/how-to-display-web-page-in-arkit-ios

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!