简体   繁体   中英

How to Center the Pivot Point of a Model in SceneKit Editor

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
        }

    }

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

You can change this using an SCNMatrix4MakeTranslation eg:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM