ios - 如何在SceneKit中简单地旋转SCNNode?

标签 ios swift scenekit

我有来自陀螺仪传感器的实时数据流,我想将其镜像到 SceneKit 中的一个简单对象中。

这是类声明以及我如何读取数据(主要用于上下文):

class GraphicsViewController: UIViewController {

@IBOutlet var newView: SCNView!
var device: MetaWear!
let geometry:SCNGeometry = SCNPyramid(width: 1, height: 1, length: 1)
var graphicsScene: SCNScene!
var cameraNode: SCNNode!
var pyramidNode:SCNNode
(...)

required init?(coder aDecoder: NSCoder) {
    self.pyramidNode = SCNNode(geometry: geometry)
    super.init(coder: aDecoder)
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated);
    pyramidNode = SCNNode(geometry: geometry)
    (...)
}

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.updateLabel("Restoring")
        if let state = DeviceState.loadForDevice(device) {
            // Initialize the device
            device.deserialize(state.serializedState)
            self.updateLabel("Connecting")
            device.connectAndSetup().continueWith { t in
                if let error = t.error {
                    // Sorry we couldn't connect
                    print("ERROR: Could not connect!")
                } else {
                    // The result of a connectAndSetup call is a task which completes upon disconnection.
                    t.result!.continueWith {
                        state.serializedState = self.device.serialize()
                        state.saveToUrl(self.device.uniqueUrl)
                        self.updateLabel($0.error?.localizedDescription ?? "Disconnected")
                    }

                    self.updateLabel("Connected")
                    self.device.flashLED(color: .green, intensity: 1.0, _repeat: 3)
                    self.magnetoTest()
                    print("passed magneto test")
                }
            }
        }
    }

到目前为止一切都很好。 magnitoTest 函数如下所示:

func magnetoTest(){
        mbl_mw_mag_bmm150_set_preset(device.board, MBL_MW_MAG_BMM150_PRESET_LOW_POWER);
        mbl_mw_sensor_fusion_write_config(device!.board)

        let gyroData = mbl_mw_mag_bmm150_get_b_field_data_signal(device.board)

        mbl_mw_datasignal_subscribe(gyroData!, bridge(obj: self)) { (context, data) in
            let dataSignal = data!.pointee.valueAs() as MblMwCartesianFloat
            DispatchQueue.main.async{
                print(dataSignal.z)
                let mySelf = Unmanaged<GraphicsViewController>.fromOpaque(context!).takeUnretainedValue()
                mySelf.pyramidNode.rotation = SCNVector4(x: dataSignal.x, y: dataSignal.y, z: dataSignal.z, w: Float(10.0))
                mySelf.testData = SCNVector3(x: dataSignal.x, y: dataSignal.y, z: dataSignal.z)
            }
        }
        mbl_mw_mag_bmm150_enable_b_field_sampling(device.board);
        mbl_mw_mag_bmm150_start(device.board)
    }

我期望 mySelf.PyramidNode.Rotation 能够工作,但事实并非如此。观察数据的变化也没有任何作用:

var testData:SCNVector3? {
        willSet(newValue) {
            print("About to set new data: \(newValue)")
            pyramidNode.rotation = SCNVector4Make(newValue!.x, newValue!.y, newValue!.z, 0.0)
            //pyramidNode.position = newValue!
            print("new rotation: \(pyramidNode.rotation)")

            let rotationAction =   SCNAction.rotate(by: CGFloat(1.0), around: SCNVector3(3,5,0), duration: 2.0)
            pyramidNode.runAction(rotationAction)
            cameraNode.runAction(rotationAction)

            cameraNode.rotation = SCNVector4Make(newValue!.x, newValue!.y, newValue!.z, 0.0)
            cameraNode.position = newValue!
        }
        didSet(val) {
            print("data was set! \(val)")
        }
    }

这样做的结果是一无所获。没有错误,没有警告,金字塔节点也没有变化。这是顶部的打印输出:

    2019-02-18 01:04:23.446607+0100 StarterProject[4001:2128327] [DYMTLInitPlatform] platform initialization successful 2019-02-18
     01:04:23.726821+0100 StarterProject[4001:2128243] [CoreBluetooth] API MISUSE: <CBCentralManager: 0x283ad59c0> has no restore identifier but the delegate implements the centralManager:willRestoreState: method. Restoring will not be supported 
    MainTableViewCtrl says hello to 0 devices 
    AppDelegate says hello 
    ScanTableViewCtrl says hello 
    MainTableViewCtrl says hello to 1 devices 
    2019-02-18 01:04:29.952980+0100 StarterProject[4001:2128243] Metal GPU Frame Capture Enabled 2019-02-18 01:04:29.956667+0100 StarterProject[4001:2128243] Metal API Validation Enabled 
graphics says hello 
Restoring 
Connecting 
Connected 
passed magneto test
About to set new data: Optional(__C.SCNVector3(x: 25.875, y: 74.4375, z: 5.1875)) 
new rotation: SCNVector4(x: 25.875, y: 74.4375, z: 5.1875, w: 0.0) 
data was set! nil
About to set new data: Optional(__C.SCNVector3(x: 25.5, y: 75.875, z: 5.9375)) 
new rotation: SCNVector4(x: 25.5, y: 75.875, z: 5.9375, w: 0.0) 
data was set! Optional(__C.SCNVector3(x: 25.875, y: 74.4375, z: 5.1875))
About to set new data: Optional(__C.SCNVector3(x: 26.25, y: 77.0, z: 4.8125)) 
new rotation: SCNVector4(x: 26.25, y: 77.0, z: 4.8125, w: 0.0) 
data was set! Optional(__C.SCNVector3(x: 25.5, y: 75.875, z: 5.9375))

所有这些似乎都有效,但它不会影响我的场景中金字塔的变化。正如您所知,我尝试以多种方式移动相机和金字塔(比所示的更多)。

总之,我从传感器(快速)获取实时数据。我想镜像 SceneKit 场景中对象中传感器的方向。我获得了数据并且拥有了对象,但我不知道如何将数据应用到对象的旋转。

我根本不明白为什么我对金字塔(或相机)旋转的更改不会改变应用程序中的任何内容。即使是硬编码的值也无法改变金字塔的 View 。

希望您能提供帮助,非常感谢任何提示和提示!

最佳答案

我是新手,我将几何体添加到场景中,而不是节点本身。

抱歉浪费了您的时间。

关于ios - 如何在SceneKit中简单地旋转SCNNode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54739242/

相关文章:

swift - 如何可视化按钮是否设置为触发到另一个 View Controller 的 segue?

ios - 如何获取控制台日志并在 TextView 中显示 [Swift]

ios - 在 iOS SceneKit 中如何将 .dae 文件加载到 SCNNode 中?

swift - ARkit 中未检测到 SceneKit 碰撞

iphone - 从基于窗口的项目开始构建基于导航的项目

ios - swift iOS 中 Decimal Pad 的浮点值

ios - 无法删除 NavigatorIOS 右键单击​​操作上的 Realm 对象

ios - 如何超越 Swift 4 的 Var 命名限制?

ios - ARKit - 如何让物体跟随用户

ios - insertRowsAtIndexPaths 中的 NSInternalInconsistencyException