简体   繁体   English

如何在SceneKit Editor中居中模型的枢轴点

[英]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 ) 您可以使用SCNNode的ivot属性来更改Chair的pivotApple文档

You can change this using an SCNMatrix4MakeTranslation eg: 您可以使用SCNMatrix4MakeTranslation进行更改,例如:

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 . 您也可以在BlenderMaya之类的程序中更改它。

Alternatively, and probably much easier , is to edit the model in the SceneKit Editor itself. 另外, 可能更容易的是,在SceneKit Editor编辑模型。

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. 如果您无法解决它的关键问题,那么您可以做的是创建一个Empty Node ,然后将Chair作为其子项添加,确保其在其中居中。

This way you should be able to transform it's rotation for example more accurately. 这样,您应该能够更准确地转换其旋转度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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