2015-10-06 113 views
1

我正在使用SceneKit導入人體的三維圖像模型。當我在圖像中選擇特定的位置點時,我希望應用識別身體部位併爲每個部位執行不同的功能。我如何去實施這個?做這個的最好方式是什麼?如何爲單個圖像提供多個選擇選項?

P.s.當圖像旋轉時,它會顯示不同的視圖。我需要應用程序能夠識別身體的部分,即使它是由用戶旋轉。任何指導如何進行,將不勝感激..

回答

1

這是一個簡單的SceneKit採摘的例子。

場景設置在viewDidLoad中,對於您的用例,我希望場景可以從文件加載(最好用另一種方法完成)。這個文件將希望將不同的組件作爲樹狀分層結構中的獨立組件。這個3D身體模型的作者將有希望地標記這些組件,以便您的代碼可以識別當您選擇左股骨(而不是comp2345)時要做什麼。

對於一個複雜的模型來說,任何xy座標都會有幾個'命中',因爲您將返回所有與該命中射線相交的節點。你不妨只使用第一次擊中。

import UIKit 
import SceneKit 

class ViewController: UIViewController { 

    @IBOutlet var scenekitView: SCNView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let scene = SCNScene() 

     let boxNode = SCNNode(geometry: SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0)) 
     boxNode.name = "box" 
     scene.rootNode.addChildNode(boxNode) 

     let sphereNode = SCNNode(geometry: SCNSphere(radius: 1)) 
     sphereNode.name = "sphere" 
     sphereNode.position = SCNVector3Make(2, 0, 0) 
     boxNode.addChildNode(sphereNode) 

     let torusNode = SCNNode(geometry: SCNTorus(ringRadius: 1, pipeRadius: 0.3)) 
     torusNode.name = "torus" 
     torusNode.position = SCNVector3Make(2, 0, 0) 
     sphereNode.addChildNode(torusNode) 

     scenekitView.scene = scene 
     scenekitView.autoenablesDefaultLighting = true 
     scenekitView.allowsCameraControl = true 
    } 

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 

     //get the first touch location in screen coordinates 
     guard let touch = touches.first else { 
      return 
     } 

     //convert the screen coordinates to view coordinates as the SCNView make not take 
     //up the entire screen. 
     let pt = touch.locationInView(self.scenekitView) 

     //pass a ray from the points 2d coordinates into the scene, returning a list 
     //of objects it hits 
     let hits = self.scenekitView.hitTest(pt, options: nil) 

     for hit in hits { 
      //do something with each hit 
      print("touched ", hit.node.name!) 
     } 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 
} 
+0

感謝您的信息兄弟。我的圖像模型將是一個COLLADA文件。當我輸入它時,它本身就是一個完整的實體。你在答案中使用了不同的節點。我的情況是,我導入的整個圖像將是一個節點。有什麼辦法可以用不同的方式引用單個節點嗎?或者如果我可以將我的圖像節點分成多個可以引用的子節點? – Jobs

+0

是的,我預計它會是一個Collada文件。但是,將它導入SceneKit時,它應包含多個節點。 Collada文件可以表示整個場景(當然,場景可能只包含一個節點)。 – lock

+0

您可以簡單介紹一下我應該如何創建多個節點到示例dae文件中的文檔或材料? – Jobs