我正在尝试我的第一个SceneKit应用。我的目标是模拟地球表面的视图,并能够将设备的摄像机指向任何方向,并通过摄像机视图覆盖信息。
首先,我只是想让SceneKit摄影机视图与设备的方向相匹配。为了验证它是否正常工作,我在特定的纬度和经度坐标处添加了一堆球体。
除了一个重要问题外,一切正常。该视图从它应该显示的内容左/右(东/西)镜像。我花了好几个小时尝试不同的调整相机的排列。
以下是我的完整测试应用视图控制器。我无法弄清楚正确的更改组合,以使场景正确呈现。北极的球体是正确的。以我自己目前的经度从北极延伸到赤道的球体线如预期的那样出现在头顶上。这是其他领域的不正确。它们从它们应该是东/西镜像,好像镜子是我自己的经度。
如果要测试此代码,请使用SceneKit创建一个新的Game项目。将模板GameViewController.swift文件替换为下面的模板。您还需要在Info.plist中添加“隐私 - 使用时的位置描述”键。我还建议调整for lon in ...
行,以便数字以您自己的经度开头或结尾。然后,您可以查看是否在显示器的正确部分上绘制了球体。这可能还需要稍微调整UIColor(hue: CGFloat(lon + 104) * 2 / 255.0
颜色。
import UIKit
import QuartzCore
import SceneKit
import CoreLocation
import CoreMotion
let EARTH_RADIUS = 6378137.0
class GameViewController: UIViewController, CLLocationManagerDelegate {
var motionManager: CMMotionManager!
var scnCameraArm: SCNNode!
var scnCamera: SCNNode!
var locationManager: CLLocationManager!
var pitchAdjust = 1.0
var rollAdjust = -1.0
var yawAdjust = 0.0
func radians(_ degrees: Double) -> Double {
return degrees * Double.pi / 180
}
func degrees(_ radians: Double) -> Double {
return radians * 180 / Double.pi
}
func setCameraPosition(lat: Double, lon: Double, alt: Double) {
let yaw = lon
let pitch = lat
scnCameraArm.eulerAngles.y = Float(radians(yaw))
scnCameraArm.eulerAngles.x = Float(radians(pitch))
scnCameraArm.eulerAngles.z = 0
scnCamera.position = SCNVector3(x: 0.0, y: 0.0, z: Float(alt + EARTH_RADIUS))
}
func setCameraPosition(loc: CLLocation) {
setCameraPosition(lat: loc.coordinate.latitude, lon: loc.coordinate.longitude, alt: loc.altitude)
}
// MARK: - UIViewController methods
override func viewDidLoad() {
super.viewDidLoad()
// create a new scene
let scene = SCNScene()
let scnCamera = SCNNode()
let camera = SCNCamera()
camera.zFar = 2.5 * EARTH_RADIUS
scnCamera.camera = camera
scnCamera.position = SCNVector3(x: 0.0, y: 0.0, z: Float(EARTH_RADIUS))
self.scnCamera = scnCamera
let scnCameraArm = SCNNode()
scnCameraArm.position = SCNVector3(x: 0, y: 0, z: 0)
scnCameraArm.addChildNode(scnCamera)
self.scnCameraArm = scnCameraArm
scene.rootNode.addChildNode(scnCameraArm)
// create and add an ambient light to the scene
let ambientLightNode = SCNNode()
ambientLightNode.light = SCNLight()
ambientLightNode.light!.type = .ambient
ambientLightNode.light!.color = UIColor.darkGray
scene.rootNode.addChildNode(ambientLightNode)
// retrieve the SCNView
let scnView = self.view as! SCNView
// set the scene to the view
scnView.scene = scene
//scnView.pointOfView = scnCamera
// Draw spheres over part of the western hemisphere
for lon in stride(from: 0, through: -105, by: -15) {
for lat in stride(from: 0, through: 90, by: 15) {
let mat4 = SCNMaterial()
if lat == 90 {
mat4.diffuse.contents = UIColor.yellow
} else if lat == -90 {
mat4.diffuse.contents = UIColor.orange
} else {
//mat4.diffuse.contents = UIColor(red: CGFloat(lat + 90) / 255.0, green: CGFloat(lon + 104) * 4 / 255.0, blue: 1, alpha: 1)
mat4.diffuse.contents = UIColor(hue: CGFloat(lon + 104) * 2 / 255.0, saturation: 1, brightness: CGFloat(255 - lat * 2) / 255.0, alpha: 1)
}
let ball = SCNSphere(radius: 100000)
ball.firstMaterial = mat4
let ballNode = SCNNode(geometry: ball)
ballNode.position = SCNVector3(x: 0.0, y: 0.0, z: Float(100000 + EARTH_RADIUS))
let ballArm = SCNNode()
ballArm.position = SCNVector3(x: 0, y: 0, z: 0)
ballArm.addChildNode(ballNode)
scene.rootNode.addChildNode(ballArm)
ballArm.eulerAngles.y = Float(radians(Double(lon)))
ballArm.eulerAngles.x = Float(radians(Double(lat)))
}
}
// configure the view
scnView.backgroundColor = UIColor(red: 0, green: 191/255, blue: 255/255, alpha: 1) // sky blue
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
let auth = CLLocationManager.authorizationStatus()
switch auth {
case .authorizedWhenInUse:
locationManager.startUpdatingLocation()
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
default:
break
}
motionManager = CMMotionManager()
motionManager.deviceMotionUpdateInterval = 1 / 30
motionManager.startDeviceMotionUpdates(using: .xTrueNorthZVertical, to: OperationQueue.main) { (motion, error) in
if error == nil {
if let motion = motion {
//print("pitch: \(self.degrees(motion.attitude.roll * self.pitchAdjust)), roll: \(self.degrees(motion.attitude.pitch * self.rollAdjust)), yaw: \(self.degrees(-motion.attitude.yaw))")
self.scnCamera.eulerAngles.z = Float(motion.attitude.yaw + self.yawAdjust)
self.scnCamera.eulerAngles.x = Float(motion.attitude.roll * self.pitchAdjust)
self.scnCamera.eulerAngles.y = Float(motion.attitude.pitch * self.rollAdjust)
}
}
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if UIApplication.shared.statusBarOrientation == .landscapeRight {
pitchAdjust = -1.0
rollAdjust = 1.0
yawAdjust = Double.pi
} else {
pitchAdjust = 1.0
rollAdjust = -1.0
yawAdjust = 0.0
}
}
override var shouldAutorotate: Bool {
return false
}
override var prefersStatusBarHidden: Bool {
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .landscape
}
// MARK: - CLLocationManagerDelegate methods
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
manager.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
for loc in locations {
print(loc)
if loc.horizontalAccuracy > 0 && loc.horizontalAccuracy <= 100 {
setCameraPosition(loc: loc)
}
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
}
}
可能需要在setCameraPosition
末尾的motionManager.startDeviceMotionUpdates
方法和/或viewDidLoad
闭包的某种组合中进行更改。
答案 0 :(得分:1)
我并不完全明白错误,但我相信SceneKit在eulerAngles
中使用音高和偏航的方式与你想象的方式不符。我无法找到指定正向俯仰,滚转和偏航方向的章节/节目。
通过将俯仰/偏航设置更改为
,我能够让它工作(至少我认为我已经完成了你想要做的事情) ballArm.eulerAngles.x = -1 * Float(radians(Double(lat)))
我还添加了一些调试颜色和标签,并将场景存档为.SCN格式以将其加载到Xcode场景编辑器中(我认为Xcode 9将在调试器中为您执行此操作)。在每个节点上添加一个名称可以让您了解编辑器中的内容。修改后的代码:
// Draw spheres over part of the western hemisphere
for lon in stride(from: 0, through: -105, by: -15) {
for lat in stride(from: 0, through: 90, by: 15) {
let mat4 = SCNMaterial()
if lon == 0 {
mat4.diffuse.contents = UIColor.black
} else if lon == -105 {
mat4.diffuse.contents = UIColor.green
} else if lat == 90 {
mat4.diffuse.contents = UIColor.yellow
} else if lat == 0 {
mat4.diffuse.contents = UIColor.orange
} else {
mat4.diffuse.contents = UIColor(red: CGFloat(lat + 90) / 255.0, green: CGFloat(lon + 104) * 4 / 255.0, blue: 1, alpha: 1)
//mat4.diffuse.contents = UIColor(hue: CGFloat(lon + 104) * 2 / 255.0, saturation: 1, brightness: CGFloat(255 - lat * 2) / 255.0, alpha: 1)
//mat4.diffuse.contents = UIColor.green
}
let ball = SCNSphere(radius: 100000)
ball.firstMaterial = mat4
let ballNode = SCNNode(geometry: ball)
ballNode.position = SCNVector3(x: 0.0, y: 0.0, z: Float(100000 + EARTH_RADIUS))
let ballArm = SCNNode()
ballArm.position = SCNVector3(x: 0, y: 0, z: 0)
// debugging label
ballArm.name = "\(lat) \(lon)"
ballArm.addChildNode(ballNode)
scene.rootNode.addChildNode(ballArm)
ballArm.eulerAngles.y = Float(radians(Double(lon)))
ballArm.eulerAngles.x = -1 * Float(radians(Double(lat)))
}
}
// configure the view
scnView.backgroundColor = UIColor.cyan
let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let docsFolderURL = urls[urls.count - 1]
let archiveURL = docsFolderURL.appendingPathComponent("rmaddy.scn")
let archivePath = archiveURL.path
// for copy/paste from Simulator's file system
print (archivePath)
let archiveResult = NSKeyedArchiver.archiveRootObject(scene, toFile: archivePath)
print(archiveResult)
这里是场景编辑器中场景的屏幕截图。请注意在节点列表中选择的lat 45 / lon -90节点的球体周围的微弱橙色圆圈。
答案 1 :(得分:1)
感谢Hal对于从我的场景中创建“scn”文件的非常有用的建议。在场景编辑器中查看该场景后,我意识到所有内容都是左/右反转,因为偏航欧拉角(Y轴)旋转与我的想法相反。因此,当我将节点的eulerAngles.y
设置为所需的经度时,我的旋转方向与我想要的方向相反。但由于相机有相同的错误,我所在位置的球体位于正确的位置,但其他一切都是从左到右反转。
解决方案是否定球体和相机位置的经度值。
scnCameraArm.eulerAngles.y = Float(radians(yaw))
需要:
scnCameraArm.eulerAngles.y = -Float(radians(yaw))
和
ballArm.eulerAngles.y = Float(radians(Double(lon)))
需要:
ballArm.eulerAngles.y = -Float(radians(Double(lon)))