olexale / arkit_flutter_plugin

ARKit Flutter Plugin
MIT License
802 stars 227 forks source link

Question: Viewing 3d object model from client-server url #160

Open Amritacharya opened 3 years ago

Amritacharya commented 3 years ago

Hello, I want to keep the 3D models in my own private server and want to view them in ARSceneView. Suppose the model is located at https://example.com/example.obj. How can I view this in my app? Thanks

eduardoGo commented 3 years ago

I'm trying doing it but without sucess... Do you have some update about that?

AKosmachyov commented 2 years ago

You could implement it with Swift. You need to download the model file and then add it to the scene.

import ModelIO
import SceneKit

func download() {
    let modelLocalURL = FileManager.default.temporaryDirectory.appendingPathComponent("model.obj")
    let modelRemoteURL = "..."

    guard let url = URL(string: modelRemoteURL) else { return }
    URLSession.shared.downloadTask(with: url) { (tempFileUrl, response, error) in
        if let tempFileUrl = tempFileUrl {
            do {
                let data = try Data(contentsOf: tempFileUrl)
                try data.write(to: modelLocalURL)
                loadModel(modelLocalURL)
            } catch {
                print("Error")
            }
        }
    }.resume()
}

func loadModel(_ localURL: URL) {
    let asset = MDLAsset(url: localURL)
    asset.loadTextures()
    var object: MDLObject? = asset.object(at: 0)
    guard let mdlObject = object else { return }
    // Wrap the ModelIO object in a SceneKit object
    let node = SCNNode(mdlObject: mdlObject)
    scene.rootNode.addChildNode(node)
}