To save your SCNNode containing the terrain geometry, you can do something like this:
// Write Data Function
func writeData() {
let fixedFilename = String("SAVED-OBJECT") // just a reference Name
let fullPath = getDocumentsDirectory().appendingPathComponent(fixedFilename)
print("Full Path for write is: \(fullPath)")
do {
guard let data = try? NSKeyedArchiver.archivedData(withRootObject: yourGeometryNode, requiringSecureCoding: false)
else { fatalError("can't encode data") }
try data.write(to: fullPath)
} catch {
print("Couldn't write file")
}
}
To read the saved SCNNode, do something like this:
// Read Data Function
func readData() {
let fixedFilename = String("SAVED-OBJECT")
let fullPath = getDocumentsDirectory().appendingPathComponent(fixedFilename)
print("Full Path for Read is: \(fullPath)")
guard let data = try? Data(contentsOf: fullPath) else {
print("no data found")
return }
do {
yourGeometryNode = try NSKeyedUnarchiver.unarchivedObject(ofClass: SCNNode.self, from: data) {
print("saved data found, reading-in")
}
} catch {
print("error can't decode the saved data")
}
}
I recommend this useful Helper-Function in addition:
// Helper Function
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return paths[0]
}
Note: Child-Nodes will be saved as well.