我有两个并排的ARSCNView会话用于立体视图。我试图在立体视图中创建一个框,然后让它旋转。
一切正常,直到我使用self.parentNode.runAction移动parentNode
移动仅发生在右侧视图(SceneView2)中。左侧视图中没有移动。观点也被抵消了。我需要在左视图和右视图中同步运动。 感谢。
以下是代码:
import UIKit
import ARKit
import SceneKit
import CoreLocation
import GLKit
class ViewController1: UIViewController, ARSCNViewDelegate {
@IBOutlet weak var sceneView: ARSCNView!
@IBOutlet weak var SceneView2: ARSCNView!
@IBOutlet weak var Label: UILabel!
var parentNode: SCNNode!
override func viewDidLoad() {
super.viewDidLoad()
addBox()
// Set the view's delegate
sceneView.delegate = self
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let configuration = ARWorldTrackingConfiguration()
configuration.worldAlignment = .gravityAndHeading
configuration.planeDetection = .horizontal
sceneView.session.run(configuration)
movebox()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
sceneView.session.pause()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
func session(_ session: ARSession, didFailWithError error: Error) {
// Present an error message to the user
}
func sessionWasInterrupted(_ session: ARSession) {
// Inform the user that the session has been interrupted, for example, by presenting an overlay
}
func sessionInterruptionEnded(_ session: ARSession) {
// Reset tracking and/or remove existing anchors if consistent tracking is required
}
// UPDATE EVERY FRAME:
func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
DispatchQueue.main.async {
self.updateFrame()
//self.movebox()
}
}
func updateFrame() {
// Clone pointOfView for Second View
let pointOfView : SCNNode = (sceneView.pointOfView?.clone())!
// Determine Adjusted Position for Right Eye
let orientation : SCNQuaternion = pointOfView.orientation
let orientationQuaternion : GLKQuaternion = GLKQuaternionMake(orientation.x, orientation.y, orientation.z, orientation.w)
let eyePos : GLKVector3 = GLKVector3Make(1.0, 0.0, 0.0)
let rotatedEyePos : GLKVector3 = GLKQuaternionRotateVector3(orientationQuaternion, eyePos)
let rotatedEyePosSCNV : SCNVector3 = SCNVector3Make(rotatedEyePos.x, rotatedEyePos.y, rotatedEyePos.z)
let mag : Float = 0.066 // This is the value for the distance between two pupils (in metres). The Interpupilary Distance (IPD).
pointOfView.position.x += rotatedEyePosSCNV.x * mag
pointOfView.position.y += rotatedEyePosSCNV.y * mag
pointOfView.position.z += rotatedEyePosSCNV.z * mag
// Set PointOfView for SecondView
SceneView2.pointOfView = pointOfView
}
func addBox() {
let sideMaterial = SCNMaterial()
sideMaterial.diffuse.contents = UIColor.orange
sideMaterial.locksAmbientWithDiffuse = true;
let box = SCNBox(width: 0.3, height: 0.6, length: 0.1, chamferRadius: 0.005)
box.materials = [sideMaterial, sideMaterial, sideMaterial, sideMaterial, sideMaterial, sideMaterial]
let boxNode = SCNNode()
boxNode.geometry = box
boxNode.position = SCNVector3(0, 0, -0.2)
let scene = SCNScene()
scene.rootNode.addChildNode(boxNode)
parentNode = scene.rootNode
parentNode.position = SCNVector3(0, 0, -1.0)
sceneView.scene = scene
// Set up SceneView2 (Right Eye)
SceneView2.scene = scene
SceneView2.showsStatistics = sceneView.showsStatistics
SceneView2.isPlaying = true // Turn on isPlaying to ensure this ARSCNView recieves updates.
}
func movebox() {
DispatchQueue.main.async {
let rotate = SCNAction.rotateBy(x: 0, y: 5, z: 0, duration: 20)
let moveSequence = SCNAction.sequence([rotate])
let moveLoop = SCNAction.repeatForever(moveSequence)
self.parentNode.runAction(moveLoop)
}
}
}