From 6055cf61c39137b57cfd4e15b37800c918c5a854 Mon Sep 17 00:00:00 2001 From: workinghard Date: Sat, 28 Mar 2015 21:38:18 -0700 Subject: [PATCH] Init --- Cube4Fun/GameView.swift | 127 +++++++++++++++++++++++++++--- Cube4Fun/GameViewController.swift | 51 ++++++++++-- Cube4Fun/PrimitivesScene.swift | 85 ++++++++++++++++++++ 3 files changed, 247 insertions(+), 16 deletions(-) create mode 100644 Cube4Fun/PrimitivesScene.swift diff --git a/Cube4Fun/GameView.swift b/Cube4Fun/GameView.swift index ccdbff6..8e2ddf5 100644 --- a/Cube4Fun/GameView.swift +++ b/Cube4Fun/GameView.swift @@ -8,9 +8,15 @@ import SceneKit +var lastMousePos: NSPoint = NSPoint() +var startAngle: CGFloat = CGFloat() + class GameView: SCNView { + //var lastMousePos: NSPoint + override func mouseDown(theEvent: NSEvent) { + lastMousePos = theEvent.locationInWindow /* Called when a mouse click occurs */ // check what nodes are clicked @@ -20,31 +26,134 @@ class GameView: SCNView { if hitResults.count > 0 { // retrieved the first clicked object let result: AnyObject = hitResults[0] + var anim = true // get its material let material = result.node!.geometry!.firstMaterial! - // highlight it - SCNTransaction.begin() - SCNTransaction.setAnimationDuration(0.5) + // get its name + if let node = result.node { + if let geom = node.geometry { + if let name:NSString = geom.name { + let myGeometry:SCNText = self.scene!.rootNode.childNodeWithName("myDescr", recursively: true)?.geometry as SCNText + myGeometry.string = name + } + } + } + + if let boxNode = result.node { + if boxNode.name == "myBox" { + anim = false + } + } + + /* + if let rootNode = self.scene?.rootNode { + if let cubeNode = rootNode.childNodeWithName("cubeNode", recursively: true) { + if let boxNode = cubeNode.childNodeWithName("myBox", recursively: true ) { + //anim = false + } + } + } */ + - // on completion - unhighlight - SCNTransaction.setCompletionBlock() { + //let name:NSString = result.node!.geometry!.name! +/* + let myScene:SCNScene = self.scene! + let myRootNode:SCNNode = myScene.rootNode + let myTextNode:SCNNode = myRootNode.childNodeWithName("myDescr", recursively: false)! + let myGeometry:SCNText = myTextNode.geometry as SCNText +*/ + + + + //let nextNode = self.scene + //let text = self.scene?.rootNode.childNodeWithName("myDescr", recursively: true) + + + //println(name); + + if anim { + + // highlight it + SCNTransaction.begin() + SCNTransaction.setAnimationDuration(0.5) + + // on completion - unhighlight + SCNTransaction.setCompletionBlock() { SCNTransaction.begin() SCNTransaction.setAnimationDuration(0.5) material.emission.contents = NSColor.blackColor() + SCNTransaction.commit() + } + + material.emission.contents = NSColor.whiteColor() + SCNTransaction.commit() } - - material.emission.contents = NSColor.redColor() - - SCNTransaction.commit() } } super.mouseDown(theEvent) } + func rotateCamera(var x: CGFloat, var y: CGFloat) { + // Save the angle for reset + startAngle = startAngle + y + + // rotate + if let node = self.scene!.rootNode.childNodeWithName("cubeNode", recursively: true) { + node.runAction(SCNAction.rotateByX(0, y: y, z: 0, duration: 0.5)) + } + //println(startAngle) + } + + override func mouseUp(theEvent: NSEvent) { + // Reset the last location + lastMousePos.x = 0.0 + lastMousePos.y = 0.0 + + super.mouseUp(theEvent) + } + + override func mouseDragged(theEvent: NSEvent) { + let mousePos = theEvent.locationInWindow + if lastMousePos.x > 0.0 { + let drag = ( (lastMousePos.x - mousePos.x ) / 50 ) * -1 + rotateCamera(0.0, y: drag) + lastMousePos = mousePos + //println(drag) + } + + super.mouseDragged(theEvent) + } + + override func keyDown(theEvent: NSEvent) { + + //let myCamera:SCNNode = self.scene!.rootNode.childNodeWithName("Camera", recursively: true)! + + switch (theEvent.keyCode) { + case 123: + self.rotateCamera(0.0, y: -0.1) + // Left + break; + case 124: + self.rotateCamera(0.0, y: 0.1) + //self.rotateByAngle(-1.0); + //right + break; + case 15: + // Reset the cube + self.rotateCamera(0.0, y: -startAngle) + default: + super.keyDown(theEvent) + } + + println(theEvent.keyCode); + + //super.keyDown(theEvent) + } + } diff --git a/Cube4Fun/GameViewController.swift b/Cube4Fun/GameViewController.swift index ac05118..ff6ac4d 100644 --- a/Cube4Fun/GameViewController.swift +++ b/Cube4Fun/GameViewController.swift @@ -15,21 +15,54 @@ class GameViewController: NSViewController { override func awakeFromNib(){ // create a new scene - let scene = SCNScene(named: "art.scnassets/ship.dae")! + //let scene = SCNScene(named: "art.scnassets/ship.dae")! + let scene = PrimitivesScene(); + // create and add a camera to the scene + let camera = SCNCamera() + camera.usesOrthographicProjection = true + camera.orthographicScale = 11 + camera.zNear = 0 + camera.zFar = 100 let cameraNode = SCNNode() - cameraNode.camera = SCNCamera() + cameraNode.camera = camera + cameraNode.name = "myCamera" scene.rootNode.addChildNode(cameraNode) // place the camera - cameraNode.position = SCNVector3(x: 0, y: 0, z: 15) - + var newAngle = (CGFloat)(20.0)*(CGFloat)(M_PI)/180.0 + //newAngle += currentAngle + +// cameraNode.transform = SCNMatrix4MakeRotation(-0.5, 0, 0, 1) + + cameraNode.transform = SCNMatrix4MakeRotation(newAngle, 0, 1, 0) + cameraNode.transform = SCNMatrix4MakeRotation(-0.4, 1, 0, 0) + cameraNode.position = SCNVector3(x: 0, y: 13, z: 30) + //cameraNode.eulerAngles = SCNVector3(x: 1.0, y: 0.0, z: 0.0) + + + /* + let camera = SCNCamera() + camera.usesOrthographicProjection = true + camera.orthographicScale = 9 + camera.zNear = 0 + camera.zFar = 100 + let cameraNode = SCNNode() + cameraNode.position = SCNVector3(x: 0, y: 0, z: 20) + cameraNode.camera = camera + let cameraOrbit = SCNNode() + cameraOrbit.name = "myCamera" + cameraOrbit.addChildNode(cameraNode) + scene.rootNode.addChildNode(cameraOrbit) +*/ + // create and add a light to the scene let lightNode = SCNNode() lightNode.light = SCNLight() - lightNode.light!.type = SCNLightTypeOmni - lightNode.position = SCNVector3(x: 0, y: 10, z: 10) + lightNode.light!.type = SCNLightTypeDirectional + lightNode.light!.color = NSColor.grayColor() + lightNode.position = SCNVector3(x: 20, y: 40, z: 20) scene.rootNode.addChildNode(lightNode) // create and add an ambient light to the scene @@ -38,7 +71,10 @@ class GameViewController: NSViewController { ambientLightNode.light!.type = SCNLightTypeAmbient ambientLightNode.light!.color = NSColor.darkGrayColor() scene.rootNode.addChildNode(ambientLightNode) + + + /* // retrieve the ship node let ship = scene.rootNode.childNodeWithName("ship", recursively: true)! @@ -48,12 +84,13 @@ class GameViewController: NSViewController { animation.duration = 3 animation.repeatCount = MAXFLOAT //repeat forever ship.addAnimation(animation, forKey: nil) + */ // set the scene to the view self.gameView!.scene = scene // allows the user to manipulate the camera - self.gameView!.allowsCameraControl = true + //self.gameView!.allowsCameraControl = true // show statistics such as fps and timing information self.gameView!.showsStatistics = true diff --git a/Cube4Fun/PrimitivesScene.swift b/Cube4Fun/PrimitivesScene.swift new file mode 100644 index 0000000..f19d4d9 --- /dev/null +++ b/Cube4Fun/PrimitivesScene.swift @@ -0,0 +1,85 @@ +// +// PrimitivesScene.swift +// Cube4Fun +// +// Created by Nik on 27.03.15. +// Copyright (c) 2015 DerNik. All rights reserved. +// + +import Cocoa +import SceneKit +import QuartzCore + + +class PrimitivesScene: SCNScene { + override init() { + super.init() + + var radius:CGFloat = 1.0 + + let yCount = 4 + let zCount = 4 + let xCount = 4 + var myIndex = 0 + + + let text = SCNText(string: "Klick", extrusionDepth: 1) + let textNode = SCNNode(geometry: text) + textNode.name = "myDescr" + textNode.position = SCNVector3(x:-12.0 , y: 6.0, z: 0.0) + textNode.scale = SCNVector3Make(0.1, 0.1, 0.1); + + self.rootNode.addChildNode(textNode) + + + let cubeNode = SCNNode() + cubeNode.name = "cubeNode" + + let box = SCNBox(width: 11.0, height: 1.0, length: 11.0, chamferRadius: 0.1) + box.firstMaterial?.diffuse.contents = NSColor.blueColor() + let boxNode = SCNNode(geometry: box) + boxNode.name = "myBox" + boxNode.position = SCNVector3(x:0.0 , y: -6.0, z: 0.0) + cubeNode.addChildNode(boxNode) + + + var y:CGFloat = -4.5 + for row in 0..