0

ピボットと位置が変更された子ノードを扱っています。多くの SCNNode 変換トピックを見つけましたが、どれも私の状況を表していないようです。

私は6つのボールを持っています:(2つ以上のリンクを投稿することはできません.画像はi.stack.imgur.com/v3Lc4.pngにあります)

そして、それらの上位 4 つを選択し、ピボットを調整し、位置を調整して (ピボットの移動効果に対抗するため)、回転させます。これは私が使用するコードです:

//core code
    let fourBalls = SCNNode()
    for i in 1...4
    {
        let ball = scene.rootNode.childNode(withName: "b" + String(i), recursively: false)!
        ball.removeFromParentNode()
        fourBalls.addChildNode(ball)
    }
    scene.rootNode.addChildNode(fourBalls)

    //adjust the pivot of the fourBalls node
    fourBalls.pivot = SCNMatrix4MakeTranslation(-1.5, 0.5, -5)
    //fix the position
    fourBalls.position = SCNVector3Make(-1.5, 0.5, -5)

    //rotate
    let action = SCNAction.rotateBy(x: 0, y: 0, z: CGFloat(M_PI_2), duration: 2)
    fourBalls.run(action)

それはうまくいった:

これは意図した結果です

ここで、fourBalls 子ノードを解放して rootNode に戻す必要があります。完了ブロックとして配置したこのコードを使用します。

//core problem
//how to release the node with the transform?
            for node in fourBalls.childNodes
            {   node.transform = node.worldTransform
                node.removeFromParentNode()
                self.scene.rootNode.addChildNode(node)
            }

そして、ここに問題があります。私はそれらを間違ってリリースしました:

写真

私の質問は、正しいピボット、位置、および変換プロパティを使用して、子ノードを rootNode に解放する方法です。

試してみたいあなたのための私の完全な GameViewController.swift は次のとおりです。

import SceneKit

class GameViewController: UIViewController {
let scene = SCNScene()
override func viewDidLoad() {
    super.viewDidLoad()

    let ball1 = SCNSphere(radius: 0.4)
    let ball2 = SCNSphere(radius: 0.4)
    let ball3 = SCNSphere(radius: 0.4)
    let ball4 = SCNSphere(radius: 0.4)
    let ball5 = SCNSphere(radius: 0.4)
    let ball6 = SCNSphere(radius: 0.4)
    ball1.firstMaterial?.diffuse.contents = UIColor.purple()
    ball2.firstMaterial?.diffuse.contents = UIColor.white()
    ball3.firstMaterial?.diffuse.contents = UIColor.cyan()
    ball4.firstMaterial?.diffuse.contents = UIColor.green()
    ball5.firstMaterial?.diffuse.contents = UIColor.black()
    ball6.firstMaterial?.diffuse.contents = UIColor.blue()

    let B1 = SCNNode(geometry: ball1)
    B1.position = SCNVector3(x:-2,y:1,z:-5)
    scene.rootNode.addChildNode(B1)
    B1.name = "b1"
    let B2 = SCNNode(geometry: ball2)
    B2.position = SCNVector3(x:-1,y:1,z:-5)
    scene.rootNode.addChildNode(B2)
    B2.name = "b2"
    let B3 = SCNNode(geometry: ball3)
    B3.position = SCNVector3(x:-2,y:0,z:-5)
    scene.rootNode.addChildNode(B3)
    B3.name = "b3"
    let B4 = SCNNode(geometry: ball4)
    B4.position = SCNVector3(x:-1,y:0,z:-5)
    scene.rootNode.addChildNode(B4)
    B4.name = "b4"
    let B5 = SCNNode(geometry: ball5)
    B5.position = SCNVector3(x:-2,y:-1,z:-5)
    scene.rootNode.addChildNode(B5)
    B5.name = "b5"
    let B6 = SCNNode(geometry: ball6)
    B6.position = SCNVector3(x:-1,y:-1,z:-5)
    scene.rootNode.addChildNode(B6)
    B6.name = "b6"

    let cameraNode = SCNNode()
    cameraNode.camera = SCNCamera()
    cameraNode.position = SCNVector3Make(-1.5,0,2)
    scene.rootNode.addChildNode(cameraNode)

    // create and add an ambient light to the scene
    let ambientLightNode = SCNNode()
    ambientLightNode.light = SCNLight()
    ambientLightNode.light!.type = SCNLightTypeAmbient
    ambientLightNode.light!.color = UIColor.yellow()
    scene.rootNode.addChildNode(ambientLightNode)


    let scnView = self.view as! SCNView
    scnView.scene = scene
    scnView.allowsCameraControl = false
    scnView.backgroundColor = UIColor.orange()

    //core code
    let fourBalls = SCNNode()
    for i in 1...4
    {
        let ball = scene.rootNode.childNode(withName: "b" + String(i), recursively: false)!
        ball.removeFromParentNode()
        fourBalls.addChildNode(ball)
    }
    scene.rootNode.addChildNode(fourBalls)

    //adjust the pivot of the fourBalls node
    fourBalls.pivot = SCNMatrix4MakeTranslation(-1.5, 0.5, -5)
    //fix the position
    fourBalls.position = SCNVector3Make(-1.5, 0.5, -5)

    //rotate
    let action = SCNAction.rotateBy(x: 0, y: 0, z: CGFloat(M_PI_2), duration: 2)
    fourBalls.run(action, completionHandler:

        {
            //core problem
            for node in fourBalls.childNodes
            {
                node.transform = node.worldTransform
                node.removeFromParentNode()
                self.scene.rootNode.addChildNode(node)
            }

    })
}

override func shouldAutorotate() -> Bool {
    return true
}

override func prefersStatusBarHidden() -> Bool {
    return true
}

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    if UIDevice.current().userInterfaceIdiom == .phone {
        return .allButUpsideDown
    } else {
        return .all
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Release any cached data, images, etc that aren't in use.
}
}
4

1 に答える 1