在Scenekit中加载大尺寸.obj模型。如何在scenekit中自动化模型?

时间:2018-02-26 13:49:11

标签: ios swift scenekit .obj

我在Scenekit中加载了一个.obj模型。当我尝试加载像(水瓶)这样的小型模型时,效果非常好。但是当我试图加载更大的模型(如沙发,桌子)时,它会完美地加载,但是从场景中走出边界。

在Scenekit中是否有任何方法,我们可以在scenekit中加载模型,无论模型有多大。它应该适当地安装在场景中。请查看附加的屏幕截图以获得更好的想法。

如果您能提供一些调整大小的提示,请告诉我。

 let url = "https://s3.ap-south-1.amazonaws.com/p9-platform/product/ccbb5daa-982d-4fa1-b475-971453534686.obj"

 let asset = MDLAsset(url: url as URL)
 let scene = SCNScene(mdlAsset: asset)
 let mesh = asset.object(at:0) as! MDLMesh
 let vertexBuffer = mesh.vertexBuffers[0]
 let descripter = mesh.vertexDescriptor
 let submeshes = mesh.submeshes



 for index in 0...(submeshes?.count)! - 1{
    let submesh0 = submeshes![index] as! MDLSubmesh
    let singleMesh = MDLMesh(vertexBuffer: vertexBuffer, vertexCount: mesh.vertexCount, descriptor: descripter, submeshes:  [submesh0])
    let geometry = SCNGeometry(mdlMesh: singleMesh)
    let Node = SCNNode(geometry: geometry)
    let submeshname = submesh0.material?.name as! String
    let materialname = "_\(submeshname)" as! String
    let nodename = submesh0.name as! String
    let revisedName = nodename.replacingOccurrences(of: materialname, with: "")
    Node.name = revisedName
    scene.rootNode.addChildNode(Node)
 }

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以选中此网格的边界框。然后,您将知道最大点和最小点,从而知道它的大小。

然后,您可以计算一个缩放比例值,以帮助您缩放网格。

if let object = asset.object(at: 0) as? MDLMesh {
                    
    let node = SceneComplex.shared.object
    node.geometry = SCNGeometry(mdlMesh: object)
    
    let (minB, maxB) = node.boundingBox
    let diff = maxB.x - minB.x
    let scale = 10.0 / diff // A good scale value depends on many factors
    
    node.scale = SCNVector3(scale, scale, scale)
}