swift 快速奔跑的兔几 本节的内容是:SceneKit命中检测

前端之家收集整理的这篇文章主要介绍了swift 快速奔跑的兔几 本节的内容是:SceneKit命中检测前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

命中检测过程是指在视图上去一点,找出3D中的哪个对象位于视图上这个点的下方。实际上就是要回答:我点了谁?

在对一个SceneKit视图进行命中检测时,会得到一个SCNHitTestResult对象的数组,其中描述了找到的对象,以及该对象的相关信息。

下面这段代码可以让你点击的地方短暂的发亮一下:

  1. import UIKit
  2. import SceneKit
  3. import SpriteKit
  4.  
  5. class SceneKitViewController: UIViewController {
  6.  
  7. override func viewDidLoad() {
  8. super.viewDidLoad()
  9. let sceneView = self.view as! SCNView
  10. sceneView.backgroundColor = UIColor.grayColor()
  11. //sceneView.allowsCameraControl = true
  12. // 添加场景
  13. let sceneOne = SCNScene()
  14. sceneView.scene = sceneOne
  15. // 添加照相机 并指明水平和垂直视角都是45
  16. let cameraOne = SCNCamera()
  17. cameraOne.xFov = 45
  18. cameraOne.yFov = 45
  19. // 将照相机附加到节点
  20. let cameraNode = SCNNode()
  21. cameraNode.camera = cameraOne
  22. cameraNode.position = SCNVector3(0,0,20)
  23. sceneOne.rootNode.addChildNode(cameraNode)
  24. // 添加3D对象
  25. let capsuleOne = SCNCapsule(capRadius: 2.5,height: 10)
  26. let capsuleNodeOne = SCNNode(geometry: capsuleOne)
  27. capsuleNodeOne.position = SCNVector3(0,0)
  28. sceneOne.rootNode.addChildNode(capsuleNodeOne)
  29. // 添加环境光源
  30. let ambientLight = SCNLight()
  31. ambientLight.type = SCNLightTypeAmbient
  32. ambientLight.color = UIColor(white: 0.25,alpha: 1.0)
  33. let ambientNodeOne = SCNNode()
  34. ambientNodeOne.light = ambientLight
  35. sceneOne.rootNode.addChildNode(ambientNodeOne)
  36. // 添加泛光源
  37. let omniLight = SCNLight()
  38. omniLight.type = SCNLightTypeOmni
  39. omniLight.color = UIColor(white:1.0,alpha: 1.0)
  40. let omniNodeOne = SCNNode()
  41. omniNodeOne.light = omniLight
  42. omniNodeOne.position = SCNVector3(-5,8,5)
  43. sceneOne.rootNode.addChildNode(omniNodeOne)
  44. // 为场景中的内容实现动画
  45. let moveUpAndDownAnimation = CABasicAnimation(keyPath: "position")
  46. moveUpAndDownAnimation.byValue = NSValue(SCNVector3:SCNVector3(0,5,0))
  47. // 在最后加速和减速,而不是机械弹跳
  48. moveUpAndDownAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaSEOut)
  49. // 在末端自动返回
  50. moveUpAndDownAnimation.autoreverses = true
  51. // 动画无限次循环
  52. moveUpAndDownAnimation.repeatCount = Float.infinity
  53. moveUpAndDownAnimation.duration = 2
  54. // 添加到一个节点
  55. capsuleNodeOne.addAnimation(moveUpAndDownAnimation,forKey: "upAndDown")
  56. // 创建文本几何体
  57. let text3D = SCNText(string: "BlaBlaBla",extrusionDepth: 0.3)
  58. text3D.font = UIFont.systemFontOfSize(2)
  59. let textNode = SCNNode(geometry: text3D)
  60. textNode.position = SCNVector3(-5,6,0)
  61. capsuleNodeOne.addChildNode(textNode)
  62. // 合并动画
  63. let rotateAnimation = CABasicAnimation(keyPath: "eulerAngles")
  64. rotateAnimation.byValue = NSValue(SCNVector3: SCNVector3(x: Float(0),y: Float(M_PI*2),z: Float(0)))
  65. rotateAnimation.repeatCount = Float.infinity
  66. rotateAnimation.duration = 4
  67. textNode.addAnimation(rotateAnimation,forKey: "rotation")
  68. // 质感和纹理
  69. let redMetallicMaterial = SCNMaterial()
  70. redMetallicMaterial.diffuse.contents = UIColor.grayColor()
  71. redMetallicMaterial.specular.contents = UIColor.whiteColor()
  72. redMetallicMaterial.shininess = 1
  73. capsuleOne.materials = [redMetallicMaterial]
  74. // 文字纹理
  75. let noiseTexture = SKTexture(noiseWithSmoothness:0.25,size:CGSize(width: 512,height: 512),grayscale:true)
  76. let noiseMaterial = SCNMaterial()
  77. noiseMaterial.diffuse.contents = noiseTexture
  78. text3D.materials = [noiseMaterial]
  79. // 法向贴图
  80. let noiseNormalMapTexture = noiseTexture.textureByGeneratingNormalMapWithSmoothness(0.1,contrast: 1)
  81. redMetallicMaterial.normal.contents = noiseNormalMapTexture
  82. // 命中检测
  83. let tapRecognizerOne = UITapGestureRecognizer(target: self,action: "tapped:")
  84. sceneView.addGestureRecognizer(tapRecognizerOne)
  85. // 启动用户交互
  86. sceneView.userInteractionEnabled = true
  87.  
  88. }
  89.  
  90. func tapped(tapRecognizer:UITapGestureRecognizer){
  91. if tapRecognizer.state == UIGestureRecognizerState.Ended{
  92. let sceneView = self.view as! SCNView
  93. let hits = sceneView.hitTest(tapRecognizer.locationInView(tapRecognizer.view),options: nil) as [SCNHitTestResult]
  94.  
  95. for hit in hits {
  96. if let theMaterial = (hit.node.geometry?.materials[0]) as SCNMaterial? {
  97. let highLightAnimation = CABasicAnimation(keyPath: "contents")
  98. highLightAnimation.fromValue = UIColor.blackColor()
  99. highLightAnimation.toValue = UIColor.yellowColor()
  100. highLightAnimation.autoreverses = true
  101. highLightAnimation.repeatCount = 0
  102. highLightAnimation.duration = 0.3
  103.  
  104. theMaterial.emission.addAnimation(highLightAnimation,forKey: "highLight")
  105. }
  106. }
  107. }
  108. }
  109.  
  110.  
  111. }

猜你在找的Swift相关文章