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

时间:2019-02-18 01:12:52

标签: 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场景的对象中镜像传感器的方向。我得到了数据并且有了对象,但是我无法弄清楚如何将数据应用于对象的旋转。

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

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

1 个答案:

答案 0 :(得分:0)

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

很抱歉浪费您的时间。