Расположить узлы SCN по кругу - PullRequest
2 голосов
/ 03 мая 2019

Я создаю несколько узлов автоматически и хочу расположить их вокруг себя, потому что в данный момент я просто увеличиваю на 0,1 текущую X-позицию.

capsuleNode.geometry?.firstMaterial?.diffuse.contents = imageView
capsuleNode.position = SCNVector3(self.counterX, self.counterY, self.counterZ)
capsuleNode.name = topic.name
self.sceneLocationView.scene.rootNode.addChildNode(capsuleNode)
self.counterX += 0.1

Итак, вопрос в том, как я могу разместить их всех вокруг себя, а не в одной строке?

У кого-нибудь из вас была математическая функция для этого? Спасибо!

Ответы [ 2 ]

2 голосов
/ 03 мая 2019

Используйте этот код (версия macOS) для его проверки:

import SceneKit

class GameViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let scene = SCNScene()
        let scnView = self.view as! SCNView
        scnView.scene = scene
        scnView.allowsCameraControl = true
        scnView.backgroundColor = NSColor.black

        for i in 1...12 {  // HERE ARE 12 SPHERES

            let sphereNode = SCNNode(geometry: SCNSphere(radius: 1))
            sphereNode.position = SCNVector3(0, 0, 0)

            // ROTATE ABOUT THIS OFFSET PIVOT POINT
            sphereNode.simdPivot.columns.3.x = 5
            sphereNode.geometry?.firstMaterial?.diffuse.contents = NSColor(calibratedHue: CGFloat(i)/12, 
                                                                              saturation: 1, 
                                                                              brightness: 1,                
                                                                                   alpha: 1)

            // ROTATE ABOUT Y AXIS (STEP is 30 DEGREES EXPRESSED IN RADIANS)
            sphereNode.rotation = SCNVector4(0, 1, 0, (-CGFloat.pi * CGFloat(i))/6)
            scene.rootNode.addChildNode(sphereNode)
        }
    }
}

enter image description here

enter image description here

P.S. Вот код для создания 90 сфер:

for i in 1...90 {

    let sphereNode = SCNNode(geometry: SCNSphere(radius: 0.1))
    sphereNode.position = SCNVector3(0, 0, 0)
    sphereNode.simdPivot.columns.3.x = 5
    sphereNode.geometry?.firstMaterial?.diffuse.contents = NSColor(calibratedHue: CGFloat(i)/90, saturation: 1, brightness: 1, alpha: 1)
    sphereNode.rotation = SCNVector4(0, 1, 0, (-CGFloat.pi * (CGFloat(i))/6)/7.5)
    scene.rootNode.addChildNode(sphereNode)
}

enter image description here

1 голос
/ 03 мая 2019

Тебе нужна математика

Как подготовить узлы круга

    var nodes = [SCNNode]()
    for i in  1...20 {
        let node = createSphereNode(withRadius: 0.05, color: .yellow)
        nodes.append(node)
        node.position = SCNVector3(0,0,-1 * 1 / i)
        scene.rootNode.addChildNode(node)
    }

    DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
        self.arrangeNode(nodes: nodes)
    }


   func createSphereNode(withRadius radius: CGFloat, color: UIColor) -> SCNNode {
        let geometry = SCNSphere(radius: radius)
        geometry.firstMaterial?.diffuse.contents = color
        let sphereNode = SCNNode(geometry: geometry)
        return sphereNode
   }

Математика сзади организует вид в круг

func arrangeNode(nodes:[SCNNode]) {

        let radius:CGFloat = 1;

        let angleStep = 2.0 * CGFloat.pi / CGFloat(nodes.count)

        var count:Int = 0

        for node in nodes {
            let xPos:CGFloat = CGFloat(self.sceneView.pointOfView?.position.x ?? 0) + CGFloat(cosf(Float(angleStep) * Float(count))) * (radius - 0)
            let zPos:CGFloat = CGFloat(self.sceneView.pointOfView?.position.z ?? 0) + CGFloat(sinf(Float(angleStep) * Float(count))) * (radius - 0)

            node.position = SCNVector3(xPos, 0, zPos)

            count = count + 1
        }


    }

Примечание: на третьем изображении я установил.

            let xPos:CGFloat =  -1 + CGFloat(cosf(Float(angleStep) * Float(count))) * (radius - 0)
            let zPos:CGFloat = -1 + CGFloat(sinf(Float(angleStep) * Float(count))) * (radius - 0)

Это означает, что если вам нужен обзор вокруг камеры, то

используйте CGFloat(self.sceneView.pointOfView?.position.x ?? 0) или в произвольном месте, затем укажите значение

выход

enter image description here enter image description here

enter image description here

...