在这些特定位置添加一些球体?

时间:2017-02-09 13:23:29

标签: swift scenekit

image

我下面有一个SCNBox,我想在这些位置添加一些球体,我该怎么做?

1 个答案:

答案 0 :(得分:0)

func addShape(pos: SCNVector3, to: SCNNode) {

    let radius = Float(0.05)

    let objectSph = SCNSphere(radius: CGFloat(radius))
    let object = SCNNode(geometry: objectSph)
    let position = SCNVector3Make(pos.x, pos.y, pos.z)

    object.position = position
    object.geometry?.firstMaterial?.diffuse.contents = UIColor.red
    to.addChildNode(object)
}

在操场上:

import SceneKit
import PlaygroundSupport

func addShape(pos: SCNVector3, to: SCNNode) {

    let radius = Float(0.05)

    let objectSph = SCNSphere(radius: CGFloat(radius))
    let object = SCNNode(geometry: objectSph)
    let position = SCNVector3Make(pos.x, pos.y, pos.z)

    object.position = position
    object.geometry?.firstMaterial?.diffuse.contents = UIColor.red
    to.addChildNode(object)
}


let scene = SCNScene()
let view = SCNView(frame: CGRect(x: 0, y: 0, width: 600, height: 600))

view.allowsCameraControl = true
view.autoenablesDefaultLighting = true
view.scene = scene


let box = SCNBox(width: 3.0, height: 3.0, length: 3.0, chamferRadius: 0)
let boxNode = SCNNode(geometry: box)

boxNode.geometry?.firstMaterial?.diffuse.contents = UIColor.white
scene.rootNode.addChildNode(boxNode)
scene.background.contents = UIColor.black

addShape(pos: SCNVector3Make(-1.5, 1.5, 1.5), to: boxNode)
addShape(pos: SCNVector3Make(-0.5, 1.5, 1.5), to: boxNode)
addShape(pos: SCNVector3Make(0.5, 1.5, 1.5), to: boxNode)
addShape(pos: SCNVector3Make(1.5, 1.5, 1.5), to: boxNode)

addShape(pos: SCNVector3Make(-1.5, 0.5, 1.5), to: boxNode)
addShape(pos: SCNVector3Make(-0.5, 0.5, 1.5), to: boxNode)
addShape(pos: SCNVector3Make(0.5, 0.5, 1.5), to: boxNode)
addShape(pos: SCNVector3Make(1.5, 0.5, 1.5), to: boxNode)

// and so on...

PlaygroundPage.current.liveView = view

如果我明白了......

enter image description here