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.
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
Include Pods in Main Target and Not in Watchkit Extension
Swift, Parse.Com: How to Pass Data from Query
How to Monitor More Than 20 Regions
Why am I Getting an Error Regarding Bolts Framework and Facebooksdk When I'm Not Even Using Bolts
Programmatically Set Uipageviewcontroller Transition Style to Scroll
Struggling to Convert Objective C Selector and Target Signature to Swift
How to Get Label Name from Button
Changing Selectedimage on Uitabbaritem in Swift
Uitextfield Keyboard with Only Alphabet, No Numbers, No Caps, No Spacebar
How to Check If Device Orientation Is Landscape Left or Right in Swift
How to Unwrap the Elements of an Array in Swift? (Ie. Array<Int> as Array<Int>)
How to Schedule a Same Local Notification in Swift
How to Use Multiple Protocols in Swift with Same Protocol Variables