前言
這是我在網(wǎng)上看到的一個有意思的安卓開發(fā)項目 - Ink Space,由 Zach Lieberman 創(chuàng)建。后來也有大神做了 Swift 版本粘衬,我在他的基礎(chǔ)上做了一些修改,分享給大家。這是 Demo 的 Github 地址拌夏。文章也同時更新在我的個人博客,歡迎訪問和訂閱叫编。:)
創(chuàng)建 Camera
我們需要將 Camera 的焦點和旋轉(zhuǎn)點設(shè)置在中心位置辖佣,為此我們需要注意兩點,第一點是 camera 的 pivot 屬性搓逾,第二點是用 “l(fā)ook at consconstraint”卷谈。首先我們需要創(chuàng)建一個節(jié)點,來代表中心點和相機霞篡。
let centreNode = SCNNode()
centreNode.position = SCNVector3(x: 0, y: 0, z: 0)
scene.rootNode.addChildNode(centreNode)
let camera = SCNCamera()
camera.xFov = 20
camera.yFov = 20
然后就是 SCNLookAtConstraint世蔗,表示不管我們?nèi)绾涡D(zhuǎn)相機,它始終指向在中心點朗兵。
let constraint = SCNLookAtConstraint(target: centreNode)
cameraNode.constraints = [constraint]
最后就是設(shè)置 pivot 屬性:
cameraNode.pivot = SCNMatrix4MakeTranslation(0, 0, -cameraDistance)
處理 iPhone 的旋轉(zhuǎn)傾斜動作
接下來我們需要處理 iPhone 的各種旋轉(zhuǎn)傾斜動作來旋轉(zhuǎn)我們的相機污淋。這里需要注意的是,iPhone 的旋轉(zhuǎn) (roll) 指的是左右搖晃余掖,而 iPhone 的傾斜 (pitch) 動作是指前傾后搖寸爆。如下圖:
我們將利用這些屬性來控制相機的 x 和 y 歐拉角。
首先盐欺,創(chuàng)建一個 CMMotionManager 的實例赁豆,并確認它是可用的,因此我們無法用模擬機來跑我們的代碼了冗美。
let motionManager = CMMotionManager()
guard motionManager.gyroAvailable else
{
fatalError("CMMotionManager not available.")
}
然后實現(xiàn) motion manager魔种,使用一個簡單的 tuple 存儲 iPhone 初始的位置值(attitude),然后利用這個初值和目前當(dāng)下的值計算得到相機的旋轉(zhuǎn)角度粉洼。
let queue = NSOperationQueue.mainQueue
motionManager.deviceMotionUpdateInterval = 1 / 30
motionManager.startDeviceMotionUpdatesToQueue(queue())
{
(deviceMotionData: CMDeviceMotion?, error: NSError?) in
if let deviceMotionData = deviceMotionData
{
if (self.initialAttitude == nil)
{
self.initialAttitude = (deviceMotionData.attitude.roll,
deviceMotionData.attitude.pitch)
}
self.cameraNode.eulerAngles.y = Float(self.initialAttitude!.roll - deviceMotionData.attitude.roll)
self.cameraNode.eulerAngles.x = Float(self.initialAttitude!.pitch - deviceMotionData.attitude.pitch)
}
}
在 3D 空間畫圖
因為我已經(jīng)知道了相機的角度节预,就可以很簡單的將目標(biāo)利用 touchesBegan() 畫出來叶摄,它們共享同樣的 attitude。
currentDrawingNode = SCNNode(geometry: SCNBox(width: 1, height: 1, length: 0, chamferRadius: 0))
currentDrawingNode.eulerAngles.x = self.cameraNode.eulerAngles.x
currentDrawingNode.eulerAngles.y = self.cameraNode.eulerAngles.y
同時安拟,創(chuàng)建一個新的 CAShapeLayer 來跟蹤手指的軌跡蛤吓。
currentDrawingLayer = CAShapeLayer()
let material = SCNMaterial()
material.diffuse.contents = currentDrawingLayer
material.lightingModelName = SCNLightingModelConstant
currentDrawingNode.geometry?.materials = [material]
在 touchesMoved()
方法里,需要將主視圖里的位置轉(zhuǎn)換為幾何(geometry)中的位置去扣,因為這個幾何是 1 乘 1的(x和y方向上都是-0.5 到 +0.5)柱衔。我們需要將它轉(zhuǎn)換成 CAShapeLayer 中的坐標(biāo)位置(512 * 512)。先把第一個觸摸點中的 locationInView()
傳到 SceneKit 中的 hitTest()
愉棱,它會返回一個 SCNHitTestResults 的數(shù)組唆铐,包含了手指的觸摸路徑信息,然后我們只要把當(dāng)前結(jié)果中的坐標(biāo)信息(localCoordinates)轉(zhuǎn)換成 CAShapeLayer 中的坐標(biāo)奔滑,然后就可以畫出來了艾岂。
let locationInView = touches.first?.locationInView(view)
if let hitTestResult:SCNHitTestResult = sceneKitView.hitTest(locationInView!, options: nil).filter( { $0.node== currentDrawingNode }).first,
currentDrawingLayer = currentDrawingLayer
{
let drawPath = UIBezierPath(CGPath: currentDrawingLayer.path!)
let newX = CGFloat((hitTestResult.localCoordinates.x + 0.5) * Float(currentDrawingLayerSize))
let newY = CGFloat((hitTestResult.localCoordinates.y + 0.5) * Float(currentDrawingLayerSize))
drawPath.addLineToPoint(CGPoint(x: newX, y: newY))
currentDrawingLayer.path = drawPath.CGPath
}
by: 諸葛俊偉
歡迎轉(zhuǎn)載,轉(zhuǎn)載請注明出處朋其。非常歡迎 Swifter 們一起討論一起學(xué)習(xí)王浴。
如果您對 MongoDB,或者 Heroku 感興趣梅猿,博主有兩篇 MongoDB 從入門到精通的文章也許您會感興趣氓辣。
簡書地址 - 基于 MongoDB 的 Web APP(建于 Heroku)-PART 1
簡書地址 - 基于 MongoDB 的 Web APP(建于 Heroku)-PART 2
博主一直在學(xué)習(xí)斯坦福的 iOS 課程- Developing iOS 9 with Swift,感興趣的同學(xué)可以瀏覽我之前的文章哦袱蚓。歡迎一起討論一起學(xué)習(xí)钞啸。:)