SceneKit材质充满随机图案,为什么?

时间:2015-04-29 12:09:10

标签: ios swift scenekit

我有一个简单的触摸检测方法,应该改变被触摸节点的颜色。

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    super.touchesBegan(touches, withEvent: event)

    let touch = touches.first as! UITouch
    let point = touch.locationInView(view)

    let options: [NSObject : AnyObject] = [
        SCNHitTestFirstFoundOnlyKey: NSNumber(bool: true),
        SCNHitTestSortResultsKey: NSNumber(bool: true)
    ]

    if let results = sceneView.hitTest(point, options: options) as? [SCNHitTestResult] {
        if let result = results.first {
            // Red color material
            let material = SCNMaterial()
            material.diffuse.contents = UIColor.redColor()

            // Assign it to the node
            result.node.geometry?.firstMaterial = material
        }
    }
}

我的节点层次结构包含一个使用自定义SCNGeometry制作的节点和8个具有常规SCNBox几何体的节点。

let boxGeometry = SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0)
let boxNode = SCNNode(geometry: boxGeometry)
boxNode.position = SCNVector3(x: vector.x, y: vector.y, z: vector.z)

这里有一些关于我触摸盒子后颜色如何变化的截图。每次轮换时,模式都会闪烁并发生变化。

enter image description here enter image description here

这种奇怪的颜色模式的原因是什么?我只是想让它保持纯色。

1 个答案:

答案 0 :(得分:1)

在我看来,你们彼此之间有两个节点 - 盒子的数量并没有与你在问题中指定的数字相加。