i'm learning scenekit
these days.but there's some problem.
i create an .scn file and put a sphere at (0,0,0) and i use these codes to put a camera on a node
let frontCamera = SCNCamera()
frontCamera.yFov = 45
frontCamera.xFov = 45
let topNode = SCNNode()
topNode.camera = frontCamera
topNode.position = SCNVector3(4, 0, 0)
topNode.orientation = SCNQuaternion(0, 0, 0, 0)
scene!.rootNode.addChildNode(topNode)
topView.pointOfView = topNode
topView.allowsCameraControl = true
when i run i cannot see anything until I click on my simulator and use this property , allowsCameraControl
I set.
could you tell me where is wrong about my code?Thanks a lot
Creating an SCNQuaternion
of all zeroes doesn't really mean anything. You've specified a rotation of 0, along an axis specified by an all-zero "unit" vector. If you try this modified version of your code, you'll see nothing has really changed after you try to change topNode
's orientation. You're still rotating around an axis that is zero in all 3 components:
let topNode = SCNNode()
topNode.camera = frontCamera
topNode.position = SCNVector3(4, 0, 0)
print(topNode.orientation, topNode.rotation)
-> SCNVector4(x: 0.0, y: 0.0, z: 0.0, w: 1.0) SCNVector4(x: 0.0, y: 0.0, z: 0.0, w: 0.0)
topNode.orientation = SCNQuaternion(0, 0, 0, 0)
print(topNode.orientation, topNode.rotation)
->SCNVector4(x: 0.0, y: 0.0, z: 0.0, w: 1.0) SCNVector4(x: 0.0, y: 0.0, z: 0.0, w: 3.14159)
You've moved out the X axis 4 units to place your camera (topNode.position
). In the usual orientation, that means 4 units to the right, with positive Y running from bottom of your screen to the top, and positive Z running out of the screen to your eye. You want to rotate around the Y axis. The camera's orientation is down its parent node's minus-Z axis. So let's rotate 1/4 way clockwise, and try setting the rotation
instead (easier for me to think about than quaternions):
topNode.rotation = SCNVector4Make(0, 1, 0, Float(M_PI_2))
print(topNode.orientation, topNode.rotation)
-> SCNVector4(x: 0.0, y: 1.0, z: 0.0, w: -4.37114e-08) SCNVector4(x: 0.0, y: 1.0, z: 0.0, w: 3.14159)
You will probably find it helpful to log out, or even display, the camera node's rotation
, orientation
, and eulerAngles
(they all express the same notion, just using different axes) as you manipulate the camera manually.
For completeness, here's the entire viewDidLoad
:
@IBOutlet weak var sceneView: SCNView!
override func viewDidLoad() {
super.viewDidLoad()
sceneView.scene = SCNScene()
let sphere = SCNSphere(radius: 1.0)
let sphereNode = SCNNode(geometry: sphere)
sceneView.scene?.rootNode.addChildNode(sphereNode)
let frontCamera = SCNCamera()
frontCamera.yFov = 45
frontCamera.xFov = 45
let topNode = SCNNode()
topNode.camera = frontCamera
topNode.position = SCNVector3(4, 0, 0)
sceneView.scene?.rootNode.addChildNode(topNode)
print(topNode.orientation, topNode.rotation)
topNode.orientation = SCNQuaternion(0, 0, 0, 0)
print(topNode.orientation, topNode.rotation)
topNode.rotation = SCNVector4Make(0, 1, 0, Float(M_PI_2))
print(topNode.orientation, topNode.rotation)
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments