2016-07-28 11 views
5

私は、タップジェスチャーでSceneKitの選択されたノードを強調表示しようとしています。残念ながら、私はそれを達成することができませんでした。私ができる最善のことは、ノードがタップされているときに材料を変更することでした。SceneKitノードの周りに境界を追加する

let material = key.geometry!.firstMaterial! 
material.emission.contents = UIColor.blackColor() 

誰かが、私はちょうど全体の代わりにノードの色を変更するオブジェクトの周囲に境界線またはアウトラインを追加しようとして行くことができる方法を提案することはできますか?

+0

? 'SKNode'ではありません。 –

+0

混乱して申し訳ありませんが、私はシーンキットを使用しています.. scnnode –

答えて

2

SCNNodeは、SCNBoundingVolumeプロトコルに準拠しています。

このプロトコルでは、getBoundingBoxMin:max:メソッドが定義されています。

ノードに接続されているジオメトリの境界ボックスの最小座標と最大座標を取得する場合に使用します。

次に、SceneKitプリミティブタイプSCNGeometryPrimitiveTypeLineを使用して、境界ボックスの線を描画します。 SCNGeometryElementを確認してください。

+1

これを行う方法のサンプルコードを投稿してください。私は同じソリューションを探しています – yaali

0

@Karl Sigiscarの答えとここSOで別の答えに基づいて、私はこの思い付いた:あなたは右の `SKSpriteNode`を意味

func createLineNode(fromPos origin: SCNVector3, toPos destination: SCNVector3, color: UIColor) -> SCNNode { 
    let line = lineFrom(vector: origin, toVector: destination) 
    let lineNode = SCNNode(geometry: line) 
    let planeMaterial = SCNMaterial() 
    planeMaterial.diffuse.contents = color 
    line.materials = [planeMaterial] 

    return lineNode 
} 

func lineFrom(vector vector1: SCNVector3, toVector vector2: SCNVector3) -> SCNGeometry { 
    let indices: [Int32] = [0, 1] 

    let source = SCNGeometrySource(vertices: [vector1, vector2]) 
    let element = SCNGeometryElement(indices: indices, primitiveType: .line) 

    return SCNGeometry(sources: [source], elements: [element]) 
} 


func highlightNode(_ node: SCNNode) { 
    let (min, max) = node.boundingBox 
    let zCoord = node.position.z 
    let topLeft = SCNVector3Make(min.x, max.y, zCoord) 
    let bottomLeft = SCNVector3Make(min.x, min.y, zCoord) 
    let topRight = SCNVector3Make(max.x, max.y, zCoord) 
    let bottomRight = SCNVector3Make(max.x, min.y, zCoord) 


    let bottomSide = createLineNode(fromPos: bottomLeft, toPos: bottomRight, color: .yellow) 
    let leftSide = createLineNode(fromPos: bottomLeft, toPos: topLeft, color: .yellow) 
    let rightSide = createLineNode(fromPos: bottomRight, toPos: topRight, color: .yellow) 
    let topSide = createLineNode(fromPos: topLeft, toPos: topRight, color: .yellow) 

    [bottomSide, leftSide, rightSide, topSide].forEach { 
     $0.name = kHighlightingNode // Whatever name you want so you can unhighlight later if needed 
     node.addChildNode($0) 
    } 
} 

func unhighlightNode(_ node: SCNNode) { 
    let highlightningNodes = node.childNodes { (child, stop) -> Bool in 
     child.name == kHighlightingNode 
    } 
    highlightningNodes.forEach { 
     $0.removeFromParentNode() 
    } 
} 
関連する問題