0

I am loading one .obj model in Scenekit. When I trying to load small model like (Water bottle), it works very well. But when I am trying to load bigger models ( Like Sofa, Table), it loads perfectly, but going out of boundary from scene.

Is there any way in Scenekit, where we can load the model in scenekit regardless, how big the model is. It should be fitted in the Scene properly. Please have a look on attached screenshots for better idea.

Let me know, if you can provide me some hints to resize it.

 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

Arasuvel
  • 2,971
  • 1
  • 25
  • 40

2 Answers2

4

An SCNNode has a scale property. I have encountered this issue before, and basically ti just happens when the model is created in a program such as Maya or Blender. The author has made it big so that they can make it more detailed and intricate.

Anyway, what you can try is this:

Node.scale = SCNVector3(0.001, 0.001, 0.001)

Node should not begin with a capital letter either ^_________^

BlackMirrorz
  • 7,217
  • 2
  • 20
  • 31
  • 2
    “The author has made it big so that they can make it more detailed and intricate.” It doesn’t work like that. :) One unit in Scenekit is 1meter, while the model may have been modeled (or better said exported) in inches or centimeters (maya’s default). – Xartec Feb 26 '18 at 17:47
0

You can check the bounding box of this mesh. Then you will know the max point and min point, thus you know how big it is.

Then, you could calculate a scale value which help you scale the mesh.

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)
}
iaomw
  • 720
  • 6
  • 21