代码测试一
- 下面我们添加一个动作手势.获取手势的移动距离,把这个移动距离作为节点的移动距离.
- 当然这里面只是为了简单的尝试.
添加属性 : var node : SCNNode?
设置属性 : node = sceneView.scene.rootNode.childNodes.first
extension ViewController {
func addPanGes() {
let panGes = UIPanGestureRecognizer(target: self, action: #selector(ViewController.didPan(pan:)))
sceneView.addGestureRecognizer(panGes)
}
@objc func didPan(pan : UIPanGestureRecognizer) {
switch pan.state {
case .changed:
print(pan.translation(in: sceneView))
let point = pan.translation(in: sceneView)
guard let node = node else {return}
node.position = SCNVector3Make(node.position.x + Float(point.x/10), //因为移动速度太快,所以除以10
node.position.y - Float(point.y/10), //因为屏幕Y轴朝下,所以要用-号
node.position.z ) //这里暂时不考虑z轴
pan.setTranslation(.zero, in: sceneView)
default:
break;
}
}
}
效果图
代码测试二
extension ViewController {
//动作手势
func addPanGes() {
let panGes = UIPanGestureRecognizer(target: self, action: #selector(ViewController.didPan(pan:)))
sceneView.addGestureRecognizer(panGes)
let tapGes = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapPan(tap:)))
sceneView.addGestureRecognizer(tapGes)
}
//点击:放置AR对象位置
@objc func tapPan(tap : UIPanGestureRecognizer) {
let planeHitTestResults = sceneView.hitTest(tap.location(in: sceneView), types: .existingPlane)
guard let result = planeHitTestResults.first else {return}
guard let node = node else {return}
node.position = SCNVector3Make(result.worldTransform.columns.3.x,
result.worldTransform.columns.3.y,
result.worldTransform.columns.3.z)
}
//平移
@objc func didPan(pan : UIPanGestureRecognizer) {
switch pan.state {
case .changed:
guard let node = node else {return}
let planeHitTestResults = sceneView.hitTest(pan.location(in: sceneView), types: .existingPlane)
guard let result = planeHitTestResults.first else {return}
let planeHitTestPosition = float3(x: result.worldTransform.columns.3.x,
y: result.worldTransform.columns.3.y,
z: result.worldTransform.columns.3.z)
guard let cameraTransform = sceneView.session.currentFrame?.camera.transform else {return}
let cameraWorldPosition = float3(x: cameraTransform.columns.3.x,
y: cameraTransform.columns.3.y,
z: cameraTransform.columns.3.z)
let positionOffset = planeHitTestPosition - cameraWorldPosition
node.simdPosition = cameraWorldPosition + positionOffset
default:
break;
}
}
}
效果图
- 这个效果和第一个不一样,这里面有Z轴的变化.有关于相机矩阵的内容. 这里面完全扣的是苹果官方源代码中的矩阵操作,我这里是简化了,矩阵内容无法讲解,研究了好久也脑补不上其中的细节原理.还有怎么才能通俗地去理解,先记住吧.
- 效果图里面有几块蓝色的平面,可以忽略,这个只是为了ARkit -- hitTest(_ point:, types:)做个演示.hitTest类型用的是existingPlane,所以只要检测到有平面,不考虑范围.
关于平面的代码可以参考 :
一个Plane.swift文件
import UIKit
import SceneKit
import ARKit
class Plane: SCNNode {
var anchor: ARPlaneAnchor!
var planeGeometry: SCNPlane!
init(withAnchor anchor: ARPlaneAnchor) {
super.init()
self.anchor = anchor
planeGeometry = SCNPlane(width: CGFloat(anchor.extent.x), height: CGFloat(anchor.extent.z))
// 相比把网格视觉化为灰色平面,我更喜欢用科幻风的颜色来渲染
let material = SCNMaterial()
let img = #imageLiteral(resourceName: "tron_grid")
material.diffuse.contents = img
material.lightingModel = .physicallyBased
planeGeometry.materials = [material]
let planeNode = SCNNode(geometry: planeGeometry)
planeNode.position = SCNVector3Make(anchor.center.x, 0, anchor.center.z)
// SceneKit 里的平面默认是垂直的,所以需要旋转90度来匹配 ARKit 中的平面
planeNode.transform = SCNMatrix4MakeRotation(Float(-.pi / 2.0), 1.0, 0.0, 0.0)
setTextureScale()
addChildNode(planeNode)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func update(anchor: ARPlaneAnchor) {
// 随着用户移动,平面 plane 的 范围 extend 和 位置 location 可能会更新。
// 需要更新 3D 几何体来匹配 plane 的新参数。
planeGeometry.width = CGFloat(anchor.extent.x);
planeGeometry.height = CGFloat(anchor.extent.z);
// plane 刚创建时中心点 center 为 0,0,0,node transform 包含了变换参数。
// plane 更新后变换没变但 center 更新了,所以需要更新 3D 几何体的位置
position = SCNVector3Make(anchor.center.x, 0, anchor.center.z)
setTextureScale()
}
func setTextureScale() {
let width = planeGeometry.width
let height = planeGeometry.height
// 平面的宽度/高度 width/height 更新时,我希望 tron grid material 覆盖整个平面,不断重复纹理。
// 但如果网格小于 1 个单位,我不希望纹理挤在一起,所以这种情况下通过缩放更新纹理坐标并裁剪纹理
let material = planeGeometry.materials.first
material?.diffuse.contentsTransform = SCNMatrix4MakeScale(Float(width), Float(height), 1)
material?.diffuse.wrapS = .repeat
material?.diffuse.wrapT = .repeat
}
}
ViewController.swift文件中
添加属性 : var planes = [UUID:Plane]() // 字典,存储场景中当前渲染的所有平面
代理方法 :
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
guard let anchor = anchor as? ARPlaneAnchor else {
return
}
let plane = Plane(withAnchor: anchor)
planes[anchor.identifier] = plane
node.addChildNode(plane)
}
func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
guard let plane = planes[anchor.identifier] else {
return
}
plane.update(anchor: anchor as! ARPlaneAnchor)
}
func renderer(_ renderer: SCNSceneRenderer, didRemove node: SCNNode, for anchor: ARAnchor) {
planes.removeValue(forKey: anchor.identifier)
}
网友评论