无法在 ARKit 中旋转 SCNNode [英] Unable to rotate an SCNNode in ARKit

查看:35
本文介绍了无法在 ARKit 中旋转 SCNNode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 y 轴上旋转 SCNNode,以便我的节点(箭头)指向正确的方向.我想做一个使用 ARKit 的导航应用;每个箭头都需要指向我收到的路线的下一个位置.路径检索正常.

I'm trying to rotate an SCNNode on the y-axis so my node (an arrow) points in the right direction. I want to make a navigation app that uses ARKit; each arrow needs to point to the next location of the route I have received. The retrieval of the route works properly.

节点被添加到场景的rootNode.在某些时候,我遍历节点数组以将它们定位在场景中并将缩放应用于节点(基于距离),这一切都正确完成.但是,当我应用旋转时,这没有效果.旋转是通过修改节点的旋转属性来完成的:

The nodes are added to the rootNode of the scene. At some point I traverse an array of nodes to position them in the scene and apply scaling to the node (based on the distance), which is all done correctly. However, when I apply rotation, this has no effect. The rotation is done by modifying the rotation property of the node:

directionNode.rotation = SCNVector4(x:0, y:1:, z:0, w:Float(bearing))

这没有达到预期的效果.我也试过用runAction方法,也没有效果:

This does not have the desired effect. I also tried to use the runAction method, also no effect:

directionNode.runAction(SCNAction.rotateBy(x: 0, y: CGFloat(bearing), z: 0, duration: 0))

这是定位和缩放节点的代码.

Here's the code that positions and scales the nodes.

for i in 0...(directionNodes.count - 1) {
    let directionNode = directionNodes[i]
    let translation = MatrixHelper.transformMatrix(for: matrix_identity_float4x4, originLocation: startingLocation, location: directionNode.location)
    let position = SCNVector3.positionFromTransform(translation)
    let distance = directionNode.location.distance(from: startingLocation)
    DispatchQueue.main.async {
        let scale = 100 / Float(distance)
        directionNode.scale = SCNVector3(x: scale, y: scale, z: scale)
        directionNode.anchor = ARAnchor(transform: translation)
        directionNode.position = position
        if (i < (self.directionNodes.count - 1)) {
            // Apply rotation to the arrow
            let successiveStepLocation = self.directionNodes[i + 1].location!
            let bearing = directionNode.location.bearingToLocationRadian(successiveStepLocation)
            // rotate
            directionNode.rotation = SCNVector4(x:0, y:1:, z:0, w:Float(bearing))
        }
    }
}

这一切都包含在一个 SCNTransaction 中.

This is all wrapped in a SCNTransaction.

谁能告诉我为什么节点的旋转不起作用?非常感谢任何帮助.

Can anyone tell me why the rotation of the node is not working? Any help is greatly appreciated.

:我忘了提到我尝试旋转的 3D 箭头对象始终指向同一方向.即使当我四处走动"时,箭头也总是指向(在我的情况下)向左.也许这有助于解决我的问题...

: I forgot to mention that the 3D arrow object that I'm trying to rotate always points in the same direction. Even when I 'walk around' it the arrow always points (in my case) to the left. Maybe this helps solving my issue...

推荐答案

更新:我认为问题出在您使用的 singleSided 着色器中.您会看到模型不可见的一面.只需为您的几何体启用 doubleSided 材质:

Update: I reckon the problem is in singleSided shader you're using. You see the invisible side of your model. Just enable doubleSided material for your geometry:

directionNode.geometry?.firstMaterial?.isDoubleSided = true

您需要使用以弧度表示的值(有效范围是 -6.28+6.28).

You need to use values expressed in Radians (effective range is -6.28 to +6.28).

对象旋转的方向很重要 >> CWCCW.在正旋转角度 +.pi.

It's important in what direction your object is rotated >> CW or CCW. You can't see the single-sided geometry (at a certain angle) at a positive angle of rotation +.pi.

它是这样工作的:

myNode.rotation = SCNVector4(x: 0, y: -1, z: 0, w: (.pi * 3) / 2)

...当您也使用 SCNAction 时(不要忘记分配持续时间):

...and when you're using SCNAction, too (and don't forget to assign a duration):

let action = SCNAction.repeatForever(SCNAction.rotate(by: -.pi, 
                                                  around: SCNVector3(0,1,0), 
                                                duration: 1.75))
myNode.runAction(action)

...当您也使用 SCNTransaction 时:

...and when you're using SCNTransaction, too:

let previousTransform = myNode.transform
let rotate = SCNMatrix4MakeRotation(0, 1, 0, (-Float.pi/2))

SCNTransaction.begin()
SCNTransaction.animationDuration = 3.75
myNode.transform = SCNMatrix4Mult(rotate, previousTransform)
SCNTransaction.commit()

这篇关于无法在 ARKit 中旋转 SCNNode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆