Load Large 3D Object .Scn File in Arscnview Aspect Fit in to the Screen Arkit Swift iOS

Load large 3d Object .scn file in ARSCNView Aspect Fit in to the screen ARKIT Swift iOS

The way I handled this is making sure the 3D model always has a fixed size.

For example, if the 3D model is a small cup or a large house, I insure it always has a width of 25 cm on the scene's coordinate space (while maintaining the ratios between x y z).

You can calculate the width of the bounding box of the node like this:

let mySCN = SCNScene(named: "art.scnassets/\(self.sceneImagename).scn")!

let minX = mySCN.rootNode.boundingBox.min.x
let maxX = mySCN.rootNode.boundingBox.max.x

// change 0.25 to whatever you need
// this value is in meters
let scaleValue = 0.25 / abs(minX - maxX)

// scale all axes of the node using `scaleValue`
// this maintains ratios and does not stretch the model
mySCN.rootNode.scale = SCNVector3(scaleValue, scaleValue, scaleValue)

self.mySceneView.scene = mySCN

You can also calculate the scale value based on height or depth by using the y or z value of the bounding box.

How to handle 3D object size and its position in ARKit

Each SCNNode has a scale property:

Each component of the scale vector multiplies the corresponding
dimension of the node’s geometry. The default scale is 1.0 in all
three dimensions. For example, applying a scale of (2.0, 0.5, 2.0) to
a node containing a cube geometry reduces its height and increases its
width and depth.

Which can be set as follows:

var scale: SCNVector3 { get set }

If for example your node was called myNode, you could thus use the following to scale it by 1/10 of it's original size:

myNode.scale = SCNVector3(0.1, 0.1, 0.1)

Regarding positioning SCNNodes this can be achieved by setting the position property:

The node’s position locates it within the coordinate system of its
parent, as modified by the node’s pivot property. The default position
is the zero vector, indicating that the node is placed at the origin
of the parent node’s coordinate system.

Sample Image

If therefore, you wanted to add your SCNNode to the center of the worldOrigin, and 1m away from the camera you can use the following:

myNode.position = SCNVector3(0, 0, -1)

Hope it helps...



Related Topics



Leave a reply



Submit