[英]Export SCNScene as *.obj in SceneKit
We are trying create a simple polygon via scenekit based on the vertices in the sortedVerts array using the SCNShape init(path: UIBezierPath?, extrusionDepth: CGFloat)-Constructor.我们正在尝试使用 SCNShape init(path: UIBezierPath?, extensionDepth: CGFloat)-Constructor 基于 sortedVerts 数组中的顶点通过场景工具包创建一个简单的多边形。 The exported.dae file is being constructed just fine, however.usd-files as well as.obj-files return empty.导出的.dae 文件构建得很好,但是.usd-files 和.obj-files 返回空。 Whats the reason for that?这是什么原因? We plan on importing the constructed.usd file back into realitykit following that step.我们计划在该步骤之后将Constructed.usd 文件导入realitykit。
The code:编码:
func drawSimplePolygon(from sortedVerts: [Experience.MesspunktSzene]) -> URL {
let scene = SCNScene()
let path = UIBezierPath()
guard let initialPointX = sortedVerts.first?.position(relativeTo: nil).x else { return URL(string: "")! }
guard let initialPointY = sortedVerts.first?.position(relativeTo: nil).z else { return URL(string: "")! }
path.move(to: CGPoint(x: CGFloat(initialPointX), y: CGFloat(initialPointY)))
for i in 1..<sortedVerts.count{
let x = sortedVerts[i].position(relativeTo: nil).x
let z = sortedVerts[i].position(relativeTo: nil).z
path.addLine(to: CGPoint(x: CGFloat(x), y: CGFloat(z)))
}
path.addLine(to: CGPoint(x: CGFloat(initialPointX), y: CGFloat(initialPointY)))
path.close()
let shape = SCNShape(path: path, extrusionDepth: 0.02)
let polygonNode = SCNNode(geometry: shape)
polygonNode.geometry?.firstMaterial?.diffuse.contents = UIColor.green
polygonNode.geometry?.firstMaterial?.lightingModel = .physicallyBased
scene.rootNode.addChildNode(polygonNode)
let url: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("test.obj")
scene.write(to: url, delegate: nil)
return url
}
Thanks谢谢
Maybe this will help to give you an approach.也许这将有助于为您提供一种方法。 It's for export of SCNNodes as.obj File.用于将 SCNNodes 导出为 .obj 文件。
You will need to import SceneKit.ModelIO
- never tested this on the rootNode.您将需要import SceneKit.ModelIO
- 从未在 rootNode 上测试过。 Good luck祝你好运
// Get the Geometry to export, do this using a Guard, and return if nil
guard let geometry = yourNode?.geometry else { return }
let fixedFilenameOBJ = String("MyGeometryObject.obj")
let fixedFilenameMTL = String("MyGeometryObject.mtl")
let fullPathOBJ = getDocumentsDirectory().appendingPathComponent(fixedFilenameOBJ) // for the OBJ file
let fullPathMTL = getDocumentsDirectory().appendingPathComponent(fixedFilenameMTL) // for the MTL file
let mesh = MDLMesh(scnGeometry: geometry)
let asset = MDLAsset()
asset.add(mesh)
do {
try asset.export(to: fullPathOBJ) // this will create two files: the obj + mtl
} catch {
print("Couldn't create file(s)")
return
}
Use this function in addition:另外使用这个 function:
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return paths.first! // paths[0]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.