Search code examples
swift33dscalescenekit

Swift 3 - SceneKit Get 3d model scale value


I am trying to get the value of a 3d object loaded intosceneView while it is being scaled using two fingers. I can correctly get the rotation, position and orientation however, the scale stays always on 1.

How can I get this value?

var update = Timer()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        sceneView.scene = SCNScene(named: "PKB2");
        sceneView.debugOptions.insert(SCNDebugOptions.showWireframe)
        sceneView.autoenablesDefaultLighting = true
        sceneView.allowsCameraControl = true

        update = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(self.updateFunc), userInfo: nil, repeats: true)

    } 

func updateFunc() {

        var eulerAngles_x = Double((sceneView.pointOfView?.eulerAngles.x)!) * (180.0 / Double.pi)
        if( eulerAngles_x < 0 ) {eulerAngles_x += 360.0}

        var eulerAngles_y = Double((sceneView.pointOfView?.eulerAngles.y)!) * (180.0 / Double.pi)
        if( eulerAngles_y < 0 ) {eulerAngles_y += 360.0}

        var eulerAngles_z = Double((sceneView.pointOfView?.eulerAngles.z)!) * (180.0 / Double.pi)
        if( eulerAngles_z < 0 ) {eulerAngles_z += 360.0}

        x_lbl.text = String(format:"%.2f", eulerAngles_x)
        y_lbl.text = String(format:"%.2f", eulerAngles_y)
        z_lbl.text = String(format:"%.2f", eulerAngles_z)
        w_lbl.text = String(format:"%.2f", (sceneView.pointOfView?.scale.z)!)

        print(sceneView.pointOfView?.scale) // not changing while scaling the object

    }

Solution

  • When using sceneView.allowsCameraControl = true you are manipulating the camera, not the object. Your 3D object is not moving, the camera is moving around it.

    Therefore it's wrong the check the scale of the camera, as the scale isn't changing, it's just zooming in.

    Try checking sceneView.pointOfView?.camera.xFov and sceneView.pointOfView?.camera.yFov instead.


    Also it's worth noting that the docs for allowsCameraControl state:

    Note that the primary purpose of this property is to aid in debugging your application. You may want to implement you own camera controller suitable for your application.