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

岁酱吖の 提交于 2019-12-11 16:28:45

问题


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.



来源:https://stackoverflow.com/questions/48977282/how-to-center-the-pivot-point-of-a-model-in-scenekit-editor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!