- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我添加了一个 UIRotationGestureRecognizer
并想用它来旋转用户选择的节点。
当前,它围绕 z 轴旋转,如下所示:
private var startingRotation: CGFloat = 0
@objc private func handleRotation(_ rotation: UIRotationGestureRecognizer) {
guard let node = sceneView.hitTest(rotation.location(in: sceneView), options: nil).first?.node else {
return
}
if rotation.state == .began {
startingRotation = CGFloat(node.rotation.w)
}
node.rotation = SCNVector4(0, 0, 1, -Float(startingRotation + rotation.rotation))
}
如果自从放置节点后相机没有移动,这将正常工作。
但是,如果用户移动到节点的一侧,它就不再围绕相机所面对的轴旋转。
如何始终围绕相机的轴旋转它?
最佳答案
我想我理解你的问题,但你对 Xartec 回答的评论让我有点困惑,不知道我是否真的理解。
目标是围绕通过从相机原点“直通”对象绘制一条线形成的矢量旋转对象。这是垂直于相机平面的矢量,在本例中为手机屏幕。该矢量是相机的 -Z 轴。
根据我对你的目标的理解,这就是你所需要的
private var startingOrientation = GLKQuaternion.identity
private var rotationAxis = GLKVector3Make(0, 0, 0)
@objc private func handleRotation(_ rotation: UIRotationGestureRecognizer) {
guard let node = sceneView.hitTest(rotation.location(in: sceneView), options: nil).first?.node else {
return
}
if rotation.state == .began {
startingOrientation = GLKQuaternion(boxNode.orientation)
let cameraLookingDirection = sceneView.pointOfView!.parentFront
let cameraLookingDirectionInTargetNodesReference = boxNode.convertVector(cameraLookingDirection,
from: sceneView.pointOfView!.parent!)
rotationAxis = GLKVector3(cameraLookingDirectionInTargetNodesReference)
} else if rotation.state == .ended {
startingOrientation = GLKQuaternionIdentity
rotationAxis = GLKVector3Make(0, 0, 0)
} else if rotation.state == .changed {
// This will be the total rotation to apply to the starting orientation
let quaternion = GLKQuaternion(angle: Float(rotation.rotation), axis: rotationAxis)
// Apply the rotation
node.orientation = SCNQuaternion((startingOrientation * quaternion).normalized())
}
}
真正关键的部分是确定要围绕哪个向量旋转,幸运的是 SceneKit 提供了非常方便的方法。遗憾的是,它们并未提供您需要的所有方法。
首先,您需要表示相机正面的矢量(相机始终朝向其前轴)。 SCNNode.localFront
是-Z轴(0, 0, -1),这只是SceneKit中的一个约定。但是您想要在相机的父坐标系中表示 Z 轴的轴。我发现我经常需要它,所以我创建了一个扩展来从 SCNNode
获取 parentFront
。
现在我们有了相机的前轴
let cameraLookingDirection = sceneView.pointOfView!.parentFront
要将其转换为目标的引用系,我们使用 convertVector(_,from:)
得到一个我们可以应用旋转的向量。当场景首次启动时,此方法的结果将是盒子的 -Z 轴(就像在您的静态代码中一样,但您使用了 Z 轴并取反了角度)。
let cameraLookingDirectionInTargetNodesReference = boxNode.convertVector(cameraLookingDirection, from: sceneView.pointOfView!.parent!)
为了实现附加旋转,这是我不清楚您是否需要的部分,我使用了四元数而不是向量旋转。基本上,我在手势开始时采用方框的方向
,并通过四元数乘法应用旋转。这两行:
let quaternion = GLKQuaternion(angle: Float(rotation.rotation), axis: rotationAxis)
node.orientation = SCNQuaternion((startingOrientation * quaternion).normalized())
这个数学也可以用旋转向量或变换矩阵来完成,但这是我熟悉的方法。
extension SCNNode {
/// The local unit Y axis (0, 1, 0) in parent space.
var parentUp: SCNVector3 {
let transform = self.transform
return SCNVector3(transform.m21, transform.m22, transform.m23)
}
/// The local unit X axis (1, 0, 0) in parent space.
var parentRight: SCNVector3 {
let transform = self.transform
return SCNVector3(transform.m11, transform.m12, transform.m13)
}
/// The local unit -Z axis (0, 0, -1) in parent space.
var parentFront: SCNVector3 {
let transform = self.transform
return SCNVector3(-transform.m31, -transform.m32, -transform.m33)
}
}
extension GLKQuaternion {
init(vector: GLKVector3, scalar: Float) {
let glkVector = GLKVector3Make(vector.x, vector.y, vector.z)
self = GLKQuaternionMakeWithVector3(glkVector, scalar)
}
init(angle: Float, axis: GLKVector3) {
self = GLKQuaternionMakeWithAngleAndAxis(angle, axis.x, axis.y, axis.z)
}
func normalized() -> GLKQuaternion {
return GLKQuaternionNormalize(self)
}
static var identity: GLKQuaternion {
return GLKQuaternionIdentity
}
}
func * (left: GLKQuaternion, right: GLKQuaternion) -> GLKQuaternion {
return GLKQuaternionMultiply(left, right)
}
extension SCNQuaternion {
init(_ quaternion: GLKQuaternion) {
self = SCNVector4(quaternion.x, quaternion.y, quaternion.z, quaternion.w)
}
}
extension GLKQuaternion {
init(_ quaternion: SCNQuaternion) {
self = GLKQuaternionMake(quaternion.x, quaternion.y, quaternion.z, quaternion.w)
}
}
extension GLKVector3 {
init(_ vector: SCNVector3) {
self = SCNVector3ToGLKVector3(vector)
}
}
关于ios - 如何在相机向下看的轴上旋转 SCNNode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48530159/
我的场景中有一系列(平面)节点,我需要它们始终面向相机。 如何调整变换/旋转以使其正常工作? 另外,我在哪里进行计算? 目前我正在尝试在 SCNSceneRendererDelegate render
我正在构建一个应用程序,它使用 Scenekit 根据从数据库返回的信息显示场景。我创建了一个带有类 func 的自定义类,它创建了一个包含所有需要绘制的 SCNNode 并将其返回到我的 SCNVi
我想从动画 SCNNode 获取当前节点位置我知道 presentationNode 并且我尝试从那里提取位置形式,但没有运气 这是我的代码 SCNNode * pelvis = [_unit chi
我正在使用 Swift 和 ARKit。我可以使用 ARSCNFaceGeometry、ARFaceTrackingConfiguration 等成功显示和更新面部网格。头部没有移动(它看起来向用户)
我有一个适用于 iOS 的 sceneKit 应用程序。我有一个连接到 SCNNode 的 SCNPhysicsBody。我将 .physicsBody 设置为 nil,对 SCNNode 进行一些更
我有一个 SCNNode 的子类“ExSCNNode”,用于向 SCNNode 添加更多属性和行为。 class ExSCNNode : SCNNode { ... } 我用 ExSCNNode 构建
我正在尝试将 SCNLight 添加到 SceneKit 中的 SCNSphere (SCNNode)。它的效果令人惊叹,唯一的问题是 SCNLight 所连接的源球体是完全黑色的,因为作为源,它根本
因此,我能够在 anchor 位置放置一个框节点。现在,如何旋转场景中的 SCNNode? 我正在尝试修改节点的变换和 eulerAngles,但它们没有任何效果: func renderer(_ r
有人知道如何将 SCNMaterial 转换为表面而不改变 SCNMaterial 包含图像的方向吗? 我想创建包含图像的 ARKit SCNNode,然后将其转换为表面,因为我想在所选位置的表面区域
我正在研究 ARKit 和图像检测。现在我有一个应用程序可以检测图像并将平面放置在屏幕上检测到的对象所在的位置。 我怎样才能在飞机上添加一个像按钮一样的可点击元素。我想在每个检测到的对象上都有一个点击
我正在通过编写一个游戏来学习 SceneKit,在这个游戏中你飞过小行星场躲避物体。最初,我通过移动/旋转相机来做到这一点,但我意识到在某些时候我会用完坐标空间,最好将所有对象移向相机(并在我完成后处
我正在处理枢轴和位置已更改的子节点。我发现了很多SCNNode转换主题,但似乎没有一个能代表我的情况。 我有六个球:(不能发布超过 2 个链接,图片位于 i.stack.imgur.com/v3Lc4
我正在使用 Apple 的 SceneKit 并拥有自定义 .dae 资源。我已将资源转换为 .scn 文件。我正在从 .scn 文件中按名称获取 SCNNode。将 SCNNode 作为子节点放置在
我有以下情况。一个 SCNView,其中显示了一座桥的 .scn。在屏幕的左侧,您可以看到桥梁的视觉表示。 我想做以下事情。 在“模型浏览器”左侧选择一项 缩放到所选项目并聚焦 在提供的屏幕截图上。你
我在 SwiftUI 框架内的“SCNView”中有一个“SCNCylinder”。我的目标是围绕不同的轴并以不同的角度旋转圆柱体。我有两个(某种意义上的)按钮,它们接受用户的 180° 或 90°
我试图通过将其枢轴点设置为任何角来旋转 block ,但似乎设置枢轴点实际上重新定位了节点,使该点保持在原来的位置...... 例如 SCNBox *box = [SCNBox boxWithWidt
假设我有 2 个 SCNNodes node1 和 node2。 我希望 node1 的位置为 node1.position = SCNVector3(0, 0, 0) 我希望 node2 的位置为
我正在使用下面的代码创建一个盒子放置在我的 AR 场景中: let box = SCNBox(width: side, height: side, length: side, chamferRadiu
问题: 我试图让一个节点看着手机,无论手机的方向如何,节点的顶部始终指向手机的北极。假设节点位于中心,手机从 (0, 0, 10) 开始,并假设节点具有类似于手机的矩形几何形状。我希望该矩形的顶部始终
我有一个 scnnode,我使用 UIPanGestureRecognizer 围绕它的 Y 轴旋转。这很好,但我想对化身应用惯性,以便在用户抬起手指后它会一直旋转(并减速),有点像 ScrollVi
我是一名优秀的程序员,十分优秀!