Swift - 制作 3D 画板

作者: 诸葛俊伟 | 来源:发表于2016-06-26 08:46 被阅读856次

前言

这是我在网上看到的一个有意思的安卓开发项目 - Ink Space,由 Zach Lieberman 创建。后来也有大神做了 Swift 版本,我在他的基础上做了一些修改,分享给大家。这是 Demo 的 Github 地址。文章也同时更新在我的个人博客,欢迎访问和订阅。:)

iPhone's Motion

我们将利用这些属性来控制相机的 x 和 y 欧拉角。

首先,创建一个 CMMotionManager 的实例,并确认它是可用的,因此我们无法用模拟机来跑我们的代码了。

    let motionManager = CMMotionManager()
        
    guard motionManager.gyroAvailable else
    {
        fatalError("CMMotionManager not available.")
    }

然后实现 motion manager,使用一个简单的 tuple 存储 iPhone 初始的位置值(attitude),然后利用这个初值和目前当下的值计算得到相机的旋转角度。

    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 空间画图

因为我已经知道了相机的角度,就可以很简单的将目标利用 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

同时,创建一个新的 CAShapeLayer 来跟踪手指的轨迹。

    currentDrawingLayer = CAShapeLayer()

    let material = SCNMaterial()

    material.diffuse.contents = currentDrawingLayer
    material.lightingModelName = SCNLightingModelConstant
            
    currentDrawingNode.geometry?.materials = [material]

touchesMoved() 方法里,需要将主视图里的位置转换为几何(geometry)中的位置,因为这个几何是 1 乘 1的(x和y方向上都是-0.5 到 +0.5)。我们需要将它转换成 CAShapeLayer 中的坐标位置(512 * 512)。先把第一个触摸点中的 locationInView() 传到 SceneKit 中的 hitTest(),它会返回一个 SCNHitTestResults 的数组,包含了手指的触摸路径信息,然后我们只要把当前结果中的坐标信息(localCoordinates)转换成 CAShapeLayer 中的坐标,然后就可以画出来了。

    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: 诸葛俊伟
欢迎转载,转载请注明出处。非常欢迎 Swifter 们一起讨论一起学习。


如果您对 MongoDB,或者 Heroku 感兴趣,博主有两篇 MongoDB 从入门到精通的文章也许您会感兴趣。

简书地址 - 基于 MongoDB 的 Web APP(建于 Heroku)-PART 1
简书地址 - 基于 MongoDB 的 Web APP(建于 Heroku)-PART 2

博主一直在学习斯坦福的 iOS 课程- Developing iOS 9 with Swift,感兴趣的同学可以浏览我之前的文章哦。欢迎一起讨论一起学习。:)

相关文章

网友评论

    本文标题:Swift - 制作 3D 画板

    本文链接:https://www.haomeiwen.com/subject/bmbadttx.html