ios - 如何一次缩放和移动所有节点? ARKit swift

标签 ios swift xcode scenekit arkit

我获得了这些功能,可以在点击屏幕时缩放和移动显示在屏幕上的 3D 对象。问题在于,如果我移动或缩放节点(3D 对象),它只会使节点的一部分变大或变小,我想缩放所有内容。

我知道一种解决方案是将 3D 对象作为一个加入 Blender,但稍后,我想将特定动画添加到特定节点,这就是我不想加入它们的原因。

以下是我用于缩放和移动对象的代码:

@objc func panned(recognizer :UIPanGestureRecognizer)
{
    //This function is for scaling
    if recognizer.state == .changed
    {
        guard let sceneView = recognizer.view as? ARSCNView
    else
    {
        return
    }

    let touch = recognizer.location(in: sceneView)
    let translation = recognizer.translation(in: sceneView)

    let hitTestResults = self.sceneView.hitTest(touch, options: nil)

        if let hitTest = hitTestResults.first
        {
            let planeNode = hitTest.node

            self.newAngleY = Float(translation.x) * (Float) (Double.pi)/180
            self.newAngleY += self.currentAngleY
            planeNode.eulerAngles.y = self.newAngleY
        }
        else if recognizer.state == .ended
        {
            self.currentAngleY = self.newAngleY
        } 
    }

}

还有这个:

@objc func pinched(recognizer :UIPinchGestureRecognizer)
{
    if recognizer.state == .changed
    {
        guard let sceneView = recognizer.view as? ARSCNView
            else
            {
                return
            }
    }

    let touch = recognizer.location(in: sceneView)

    let hitTestResults = self.sceneView.hitTest(touch, options: nil)

    if let hitTest = hitTestResults.first
    {

        let planeNode = hitTest.node

        let pinchScaleX = Float(recognizer.scale) * planeNode.scale.x
        let pinchScaleY = Float(recognizer.scale) * planeNode.scale.y
        let pinchScaleZ = Float(recognizer.scale) * planeNode.scale.z

        planeNode.scale = SCNVector3(pinchScaleX, pinchScaleY, pinchScaleZ)

        recognizer.scale = 1 
    }
}

我不知道这是否有帮助,但这是节点的图像: Image of the nodes

提前致谢!

最佳答案

为了实现您正在寻找的“分组”,您必须创建一个空场景节点,该节点将成为所有其他节点的根。

let myRootNode : SCNNode = SCNNode()
sceneView.scene.rootNode.addChildNode(myRootNode)

接下来,将场景中的所有新节点附加到 myRootNode,这样您的所有模型都以同一个节点为父级。

let newChildNode : SCNNode = SCNNode()
myRootNode.addChildNode(newChildNode)

然后您可以将翻译应用到 myRootNode 以将其与所有子节点一起移动。此外,子节点仍然是单独的模型,可以单独或作为一个组移动(平移/旋转)。

在您的 HitTest 中,您应该缩放和旋转 myRootNode 而不是单个模型。

关于ios - 如何一次缩放和移动所有节点? ARKit swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55289106/

相关文章:

ios - 无法在 Swift 中将类型 'UITableViewCell' 的值转换为 'NSIndexPath'

ios - Swift Playground 错误 : extra argument 'timeout' in call

objective-c - Obj-C,在未设置为 'self' 的结果时返回 '[(super or self) init...]'?

javascript - Cordova iOS Javascript 不工作

ios - 从常量类访问基本 url

iphone - 简单的节拍器iPhone应用程序

objective-c - UIViewController - iPad Pro 3rd 顶部和底部出现神秘空白

iphone - 如何删除以编程方式添加的自定义 View ?

ios - 使用 sharedApplication openURL 方法在 Safari 中使用 POST 方法打开 Url

ios - 在 Info.plist CFBundleSupportedPlatforms 或 Mach-O LC_VERSION_MIN 中找不到 phantomjs 的平台系列