在OS X上使用SceneKit旋转相机并移动鼠标

时间:2015-07-15 04:32:19

标签: macos scenekit

由于标题可能暗示我正在尝试设置我自己的事件处理程序以使用场景工具包旋转我的相机节点。我似乎无法找到任何有关如何将“mouseMoved”事件转换为平滑相机运动的好教程。

我可以从哪里开始这个过程,是否有可以查看的工作代码或有关如何完成此操作的建议?

class gameView: NSView
{
    var sceneView: SCNView!
    var camera: SCNNode!
    var ground: SCNNode!
    var light: SCNNode!
    var button: SCNNode!
    var sphere1: SCNNode!
    var sphere2: SCNNode!


    override func mouseMoved(theEvent: NSEvent)
    {
        var x = self.camera.rotation.x
        var y = self.camera.rotation.y
        var z = self.camera.rotation.z
        var w = self.camera.rotation.w
        //WHAT TO DO
        //something happens to x, y, z and w based on mouse movement to make the camera respond by rotating with the mouse
                self.camera.rotation = SCNVector4(x: x, y: y, z: z, w: w)

    }
    override func drawRect(dirtyRect: NSRect)
    {
        super.drawRect(dirtyRect)
        self.becomeFirstResponder()
        //self.enterFullScreenMode(NSScreen.mainScreen()!, withOptions: nil)
        let trackingArea = NSTrackingArea(rect: self.bounds, options: (NSTrackingAreaOptions.MouseMoved | NSTrackingAreaOptions.MouseEnteredAndExited | NSTrackingAreaOptions.ActiveAlways), owner: self, userInfo: nil)
        self.addTrackingArea(trackingArea)
        var image = NSImage(named: "rockTexture.jpg");
        var imgArray = [image!, image!, image!, image!, image!, image!]
        // Drawing code here.
        sceneView = SCNView(frame: self.frame)
        //sceneView.allowsCameraControl = true
        sceneView.scene = SCNScene()
        self.addSubview(sceneView)
        CGWarpMouseCursorPosition(CGPoint(x: 0, y: 0));
        let groundGeometry = SCNFloor()
        groundGeometry.reflectivity = 0
        let groundMaterial = SCNMaterial()
        groundMaterial.diffuse.contents = image
        groundMaterial.reflective.contents = imgArray
        groundGeometry.materials = [groundMaterial]
        ground = SCNNode(geometry: groundGeometry)

        let camera = SCNCamera()
        camera.zFar = 10000
        self.camera = SCNNode()
        self.camera.camera = camera
        self.camera.position = SCNVector3(x: -20, y: 15, z: 20)
        let constraint = SCNLookAtConstraint(target: ground)
        constraint.gimbalLockEnabled = true
        //self.camera.constraints = [constraint]

        let ambientLight = SCNLight()
        ambientLight.color = NSColor.darkGrayColor()
        ambientLight.type = SCNLightTypeAmbient
        self.camera.light = ambientLight

        let spotLight = SCNLight()
        spotLight.type = SCNLightTypeSpot
        spotLight.castsShadow = true
        spotLight.spotInnerAngle = 70.0
        spotLight.spotOuterAngle = 90.0
        spotLight.zFar = 500
        light = SCNNode()
        light.light = spotLight
        light.position = SCNVector3(x: 0, y: 25, z: 25)
        light.constraints = [constraint]

        let sphereGeometry = SCNSphere(radius: 1.5)
        let sphereMaterial = SCNMaterial()
        sphereMaterial.diffuse.contents = NSColor.greenColor()
        sphereGeometry.materials = [sphereMaterial]
        sphere1 = SCNNode(geometry: sphereGeometry)
        sphere1.position = SCNVector3(x: -15, y: 1.5, z: 0)
        sphere2 = SCNNode(geometry: sphereGeometry)
        sphere2.position = SCNVector3(x: 15, y: 1.5, z: 0)

        let buttonGeometry = SCNBox(width: 4, height: 1, length: 4, chamferRadius: 0)
        let buttonMaterial = SCNMaterial()
        buttonMaterial.diffuse.contents = NSColor.redColor()
        buttonGeometry.materials = [buttonMaterial]
        button = SCNNode(geometry: buttonGeometry)
        button.position = SCNVector3(x: 0, y: 0.5, z: 15)

        sceneView.scene?.rootNode.addChildNode(self.camera)
        sceneView.scene?.rootNode.addChildNode(ground)
        sceneView.scene?.rootNode.addChildNode(light)
        sceneView.scene?.rootNode.addChildNode(button)
        sceneView.scene?.rootNode.addChildNode(sphere1)
        sceneView.scene?.rootNode.addChildNode(sphere2)
    }
    override func mouseDown(theEvent: NSEvent)
    {
        var location = self.convertPoint(theEvent.locationInWindow, fromView: nil)
        let hitResults = sceneView.hitTest(location, options: nil)
        if hitResults?.count > 0 {
            let result = hitResults![0] as! SCNHitTestResult
            let node = result.node
            //node.removeFromParentNode()
        }
    }
}

0 个答案:

没有答案