如何使用 ARKit 在 ARSCNView 中正确放置 3D 对象?
How to place 3D object correctly in ARSCNView using ARKit?
我花了很长时间尝试将 3D 对象放置在 SceneView 中的正确位置,对象正确添加到 SceneView 中,但它的位置向右或向左移动,
func addObject(x: Float = 0, y: Float = 0, z: Float = -0.5) {
guard let shipScene = SCNScene(named: "art.scnassets/house.scn"),
let shipNode = shipScene.rootNode.childNode(withName: "house", recursively: false)
else {
return
}
shipNode.scale = SCNVector3(0.01, 0.01, 0.01)
shipNode.position = SCNVector3(x,y,z)
sceneView.scene.rootNode.addChildNode(shipNode)
}
这是我加的代码,这个问题是不是和摄像头位置有关,我也改了,好像不行。
例如,请参考附图。
如何正确放置对象?
根据您的问题,您似乎需要调整模型的旋转,以使其正确定位。
鉴于它是一个 scn file
,您应该能够通过调整模型的 Euler Angles
在 Xcode
内的 SceneKit Editor
中执行此操作。
您可以参考以下内容:
Pitch (the x component) is the rotation about the node’s x-axis.
Yaw (the y component) is the rotation about the node’s y-axis.
Roll (the z component) is the rotation about the node’s z-axis.
这是一个示例,说明了在 SceneKit Editor
:
中更改 X Eular Angle
的效果
如果您想以编程方式执行此操作,您可以通过多种方式执行此操作:
(a) 通过修改模型的等角,例如
shipNode.eulerAngles.x = 90
shipNode.eulerAngles.y = 45
shipNode.eulerAngles.z = 90
(b) 通过使用 SCNVector4
即 :
four-component rotation vector specifying the direction of the
rotation axis in the first three components and the angle of rotation
(in radians) in the fourth.
使用这个的例子如下:
///Rotate The Ship 45 Degrees Around It's Y Axis
shipNode.rotation = SCNVector4Make(0, 1, 0, .pi / 4)
为了让生活更轻松一些,您还可以使用以下扩展,以便您可以以度数编写所需的旋转,然后转换为 SCNVector4 所需的弧度,例如:
shipNode.rotation = SCNVector4(0, 1, 0, Float(Int(45).degreesToRadians)))
extension CGFloat{
/// Converts Degrees To Radians
var degreesToRadians:CGFloat { return CGFloat(self) * .pi/180}
}
希望对您有所帮助...
我花了很长时间尝试将 3D 对象放置在 SceneView 中的正确位置,对象正确添加到 SceneView 中,但它的位置向右或向左移动,
func addObject(x: Float = 0, y: Float = 0, z: Float = -0.5) {
guard let shipScene = SCNScene(named: "art.scnassets/house.scn"),
let shipNode = shipScene.rootNode.childNode(withName: "house", recursively: false)
else {
return
}
shipNode.scale = SCNVector3(0.01, 0.01, 0.01)
shipNode.position = SCNVector3(x,y,z)
sceneView.scene.rootNode.addChildNode(shipNode)
}
这是我加的代码,这个问题是不是和摄像头位置有关,我也改了,好像不行。 例如,请参考附图。
如何正确放置对象?
根据您的问题,您似乎需要调整模型的旋转,以使其正确定位。
鉴于它是一个 scn file
,您应该能够通过调整模型的 Euler Angles
在 Xcode
内的 SceneKit Editor
中执行此操作。
您可以参考以下内容:
Pitch (the x component) is the rotation about the node’s x-axis.
Yaw (the y component) is the rotation about the node’s y-axis.
Roll (the z component) is the rotation about the node’s z-axis.
这是一个示例,说明了在 SceneKit Editor
:
X Eular Angle
的效果
如果您想以编程方式执行此操作,您可以通过多种方式执行此操作:
(a) 通过修改模型的等角,例如
shipNode.eulerAngles.x = 90
shipNode.eulerAngles.y = 45
shipNode.eulerAngles.z = 90
(b) 通过使用 SCNVector4
即 :
four-component rotation vector specifying the direction of the rotation axis in the first three components and the angle of rotation (in radians) in the fourth.
使用这个的例子如下:
///Rotate The Ship 45 Degrees Around It's Y Axis
shipNode.rotation = SCNVector4Make(0, 1, 0, .pi / 4)
为了让生活更轻松一些,您还可以使用以下扩展,以便您可以以度数编写所需的旋转,然后转换为 SCNVector4 所需的弧度,例如:
shipNode.rotation = SCNVector4(0, 1, 0, Float(Int(45).degreesToRadians)))
extension CGFloat{
/// Converts Degrees To Radians
var degreesToRadians:CGFloat { return CGFloat(self) * .pi/180}
}
希望对您有所帮助...