How to Center the Pivot Point of a Model in SceneK

2019-08-16 06:37发布

问题:

Is there anyway to center the pivot of a model? Currently (by default) it is set to the bottom left. I want to set to the center. How can I do that?

Here is the image:

UPDATE: I added another node and added the chair as a child.

So, now it works better but it does not rotate all the way and it resets the position if I continue to rotate. Here is the code for the panned operation:

  @objc func panned(recognizer :UIPanGestureRecognizer) {

        var newAngleY :Float = 0.0

        if recognizer.state == .changed {

            let sceneView = recognizer.view as! ARSCNView
            let touchPoint = recognizer.location(in: sceneView)
            let translation = recognizer.translation(in: sceneView)

            print(translation.x)

            let scnHitTestResults = self.sceneView.hitTest(touchPoint, options: nil)

            if let hitTestResult = scnHitTestResults.first {

                if let parentNode = hitTestResult.node.parent {

                    newAngleY = (Float)(translation.x)*(Float)(Double.pi)/180
                    newAngleY += currentAngleY
                    parentNode.eulerAngles.y = newAngleY
                }

            }
        }

        else if recognizer.state == .ended {
            currentAngleY = newAngleY
        }

    }

回答1:

You can change a pivot of the Chair using the pivotproperty of the SCNNode (Apple Documentation)

You can change this using an SCNMatrix4MakeTranslation e.g:

nodeToAdd.pivot = SCNMatrix4MakeTranslation(0,0,0)

This may solve the problem:

    //1. Get The Bounding Box Of The Node
    let minimum = float3(nodeToAdd.boundingBox.min)
    let maximum = float3(nodeToAdd.boundingBox.max)

    //2. Set The Translation To Be Half Way Between The Vector
    let translation = (maximum - minimum) * 0.5

    //3. Set The Pivot
    nodeToAdd.pivot = SCNMatrix4MakeTranslation(translation.x, translation.y, translation.z)

You could also change it within within a program like Blender or Maya.

Alternatively, and probably much easier, is to edit the model in the SceneKit Editor itself.

If you cant fix it's pivot, then what you can do is to create an Empty Node, and then add the Chair as a child of this, ensuring that it is centered within that.

This way you should be able to transform it's rotation for example more accurately.