Skip to content

Commit

Permalink
Cylindrical vertical limiting
Browse files Browse the repository at this point in the history
  • Loading branch information
scihant committed Oct 13, 2016
1 parent cc9b479 commit cfc7605
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
43 changes: 39 additions & 4 deletions CTPanoramaController/CTPanoramaViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import ImageIO
}

@objc public enum CTPanaromaType: Int {
case Cylindirical
case Cylindrical
case Spherical
}

Expand All @@ -28,7 +28,7 @@ import ImageIO

public var image: UIImage? {
didSet {
geometryNode.geometry?.firstMaterial?.diffuse.contents = image
geometryNode?.geometry?.firstMaterial?.diffuse.contents = image
resetCameraAngles()
}
}
Expand All @@ -48,17 +48,18 @@ import ImageIO

private let cameraNode = SCNNode()
private let sceneView = SCNView()
private var geometryNode: SCNNode!
private var geometryNode: SCNNode?
private var prevLocation = CGPoint.zero
private var motionManger = CMMotionManager()
private var prevBounds = CGRect.zero

private var panoramaTypeForCurrentImage: CTPanaromaType {
if let image = image {
if (image.size.width / image.size.height == 2) {
return .Spherical
}
}
return .Cylindirical
return .Cylindrical
}

// MARK: Class lifecycle methods
Expand Down Expand Up @@ -136,6 +137,7 @@ import ImageIO
panSpeed.y = 0 // Prevent vertical movement in a cylindrical panorama
}

guard let geometryNode = geometryNode else {return}
cameraNode.position = geometryNode.position

let scene = SCNScene()
Expand Down Expand Up @@ -190,6 +192,11 @@ import ImageIO
motionManger.startDeviceMotionUpdates(to: OperationQueue.main, withHandler: {[unowned self] (motionData, error) in
if let motionData = motionData {
self.cameraNode.orientation = motionData.look(at: UIApplication.shared.statusBarOrientation)
/*
if (self.panaromaType == .Cylindrical) {
self.cameraNode.eulerAngles.x = 0
self.cameraNode.eulerAngles.z = 0
}*/
}
else {
print("\(error?.localizedDescription)")
Expand All @@ -204,6 +211,26 @@ import ImageIO
cameraNode.eulerAngles = SCNVector3Make(0, 0, 0)
}

private func updateGeometrySize() {
guard let geometryNode = geometryNode else {return}
guard panaromaType == .Cylindrical else {return}

let hitResult = sceneView.hitTest(CGPoint(x: view.bounds.size.width/2, y: view.bounds.size.height/2), options: nil)
guard hitResult.count > 0 else {return}

let hitCoordsInScreenSpace = sceneView.projectPoint(hitResult[0].worldCoordinates)

let tube = geometryNode.geometry as! SCNTube

let top = SCNVector3Make(0, 0, hitCoordsInScreenSpace.z)
let bottom = SCNVector3Make(0, Float(view.bounds.size.height), hitCoordsInScreenSpace.z)

let unprojectedTop = sceneView.unprojectPoint(top)
let unprojectedBottom = sceneView.unprojectPoint(bottom)

tube.height = CGFloat(unprojectedTop.y - unprojectedBottom.y)
}

// MARK: Event handling methods

@objc private func buttonTapped() {
Expand Down Expand Up @@ -234,6 +261,14 @@ import ImageIO
prevLocation = location
}
}

public override func viewDidLayoutSubviews() {
if (view.bounds.size.width != prevBounds.size.width || view.bounds.size.height != prevBounds.size.height) {
updateGeometrySize()
prevBounds = view.bounds
}

}
}

fileprivate extension CMDeviceMotion {
Expand Down
2 changes: 1 addition & 1 deletion CTPanoramaController/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

let image = UIImage(named: "spherical.png")
let image = UIImage(named: "normal.jpg")

let p = CTPanoramaController(image: image!)

Expand Down

0 comments on commit cfc7605

Please sign in to comment.