命中检测过程是指在视图上去一点,找出3D中的哪个对象位于视图上这个点的下方。实际上就是要回答:我点了谁?
在对一个SceneKit视图进行命中检测时,会得到一个SCNHitTestResult对象的数组,其中描述了找到的对象,以及该对象的相关信息。
下面这段代码可以让你点击的地方短暂的发亮一下:
import UIKitimport SceneKitimport SpriteKitclass SceneKitVIEwController: UIVIEwController { overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() let sceneVIEw = self.vIEw as! SCNVIEw sceneVIEw.backgroundcolor = UIcolor.graycolor() //sceneVIEw.allowsCameraControl = true // 添加场景 let sceneOne = SCNScene() sceneVIEw.scene = sceneOne // 添加照相机 并指明水平和垂直视角都是45度 let cameraOne = SCNCamera() cameraOne.xFov = 45 cameraOne.yFov = 45 // 将照相机附加到节点 let cameraNode = SCNNode() cameraNode.camera = cameraOne cameraNode.position = SCNVector3(0,0,20) sceneOne.rootNode.addChildNode(cameraNode) // 添加3D对象 let capsuleOne = SCNCapsule(capRadius: 2.5,height: 10) let capsuleNodeOne = SCNNode(geometry: capsuleOne) capsuleNodeOne.position = SCNVector3(0,0) sceneOne.rootNode.addChildNode(capsuleNodeOne) // 添加环境光源 let ambIEntlight = SCNlight() ambIEntlight.type = SCNlightTypeAmbIEnt ambIEntlight.color = UIcolor(white: 0.25,Alpha: 1.0) let ambIEntNodeOne = SCNNode() ambIEntNodeOne.light = ambIEntlight sceneOne.rootNode.addChildNode(ambIEntNodeOne) // 添加泛光源 let omnilight = SCNlight() omnilight.type = SCNlightTypeOmni omnilight.color = UIcolor(white:1.0,Alpha: 1.0) let omniNodeOne = SCNNode() omniNodeOne.light = omnilight omniNodeOne.position = SCNVector3(-5,8,5) sceneOne.rootNode.addChildNode(omniNodeOne) // 为场景中的内容实现动画 let moveUpAndDownAnimation = CABasicAnimation(keyPath: "position") moveUpAndDownAnimation.byValue = NSValue(SCNVector3:SCNVector3(0,5,0)) // 在最后加速和减速,而不是机械d跳 moveUpAndDownAnimation.timingFunction = camediatimingFunction(name: kcamediatimingFunctionEaseInEaSEOut) // 在末端自动返回 moveUpAndDownAnimation.autoreverses = true // 动画无限次循环 moveUpAndDownAnimation.repeatCount = float.infinity moveUpAndDownAnimation.duration = 2 // 添加到一个节点 capsuleNodeOne.addAnimation(moveUpAndDownAnimation,forKey: "upAndDown") // 创建文本几何体 let text3D = SCNText(string: "BlaBlaBla",extrusionDepth: 0.3) text3D.Font = UIFont.systemFontOfSize(2) let textNode = SCNNode(geometry: text3D) textNode.position = SCNVector3(-5,6,0) capsuleNodeOne.addChildNode(textNode) // 合并动画 let rotateAnimation = CABasicAnimation(keyPath: "eulerAngles") rotateAnimation.byValue = NSValue(SCNVector3: SCNVector3(x: float(0),y: float(M_PI*2),z: float(0))) rotateAnimation.repeatCount = float.infinity rotateAnimation.duration = 4 textNode.addAnimation(rotateAnimation,forKey: "rotation") // 质感和纹理 let redMetallicMaterial = SCNMaterial() redMetallicMaterial.diffuse.contents = UIcolor.graycolor() redMetallicMaterial.specular.contents = UIcolor.whitecolor() redMetallicMaterial.shininess = 1 capsuleOne.materials = [redMetallicMaterial] // 文字纹理 let noiseTexture = SKTexture(noiseWithSmoothness:0.25,size:CGSize(wIDth: 512,height: 512),grayscale:true) let noiseMaterial = SCNMaterial() noiseMaterial.diffuse.contents = noiseTexture text3D.materials = [noiseMaterial] // 法向贴图 let noisenormalMapTexture = noiseTexture.textureByGeneratingnormalMapWithSmoothness(0.1,contrast: 1) redMetallicMaterial.normal.contents = noisenormalMapTexture // 命中检测 let tapRecognizerOne = UITapGestureRecognizer(target: self,action: "tapped:") sceneVIEw.addGestureRecognizer(tapRecognizerOne) // 启动用户交互 sceneVIEw.userInteractionEnabled = true } func tapped(tapRecognizer:UITapGestureRecognizer){ if tapRecognizer.state == UIGestureRecognizerState.Ended{ let sceneVIEw = self.vIEw as! SCNVIEw let hits = sceneVIEw.hitTest(tapRecognizer.locationInVIEw(tapRecognizer.vIEw),options: nil) as [SCNHitTestResult] for hit in hits { if let theMaterial = (hit.node.geometry?.materials[0]) as SCNMaterial? { let highlightAnimation = CABasicAnimation(keyPath: "contents") highlightAnimation.fromValue = UIcolor.blackcolor() highlightAnimation.tovalue = UIcolor.yellowcolor() highlightAnimation.autoreverses = true highlightAnimation.repeatCount = 0 highlightAnimation.duration = 0.3 theMaterial.emission.addAnimation(highlightAnimation,forKey: "highlight") } } } }}总结
以上是内存溢出为你收集整理的swift 快速奔跑的兔几 本节的内容是:SceneKit命中检测全部内容,希望文章能够帮你解决swift 快速奔跑的兔几 本节的内容是:SceneKit命中检测所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)