mirror of
https://github.com/workinghard/Cube4Fun.git
synced 2025-12-14 04:42:09 +00:00
Init
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// on completion - unhighlight
|
||||
SCNTransaction.setCompletionBlock() {
|
||||
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
|
||||
}
|
||||
}
|
||||
} */
|
||||
|
||||
|
||||
//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)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
@@ -39,6 +72,9 @@ class GameViewController: NSViewController {
|
||||
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
|
||||
|
||||
85
Cube4Fun/PrimitivesScene.swift
Normal file
85
Cube4Fun/PrimitivesScene.swift
Normal file
@@ -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..<yCount {
|
||||
var z:CGFloat = -4.5
|
||||
for depth in 0..<zCount {
|
||||
var x:CGFloat = -4.5
|
||||
for column in 0..<xCount {
|
||||
|
||||
let sphereGeometry = SCNSphere(radius: radius)
|
||||
sphereGeometry.name = myIndex.description
|
||||
|
||||
if (row % 2 == 0) {
|
||||
sphereGeometry.firstMaterial?.diffuse.contents = NSColor.redColor()
|
||||
} else {
|
||||
sphereGeometry.firstMaterial?.diffuse.contents = NSColor.greenColor()
|
||||
}
|
||||
|
||||
let sphereNode = SCNNode(geometry: sphereGeometry)
|
||||
sphereNode.position = SCNVector3(x: x, y: y, z: z)
|
||||
|
||||
|
||||
cubeNode.addChildNode(sphereNode)
|
||||
|
||||
x += 3 * CGFloat(radius)
|
||||
myIndex++
|
||||
|
||||
}
|
||||
z += 3 * CGFloat(radius)
|
||||
}
|
||||
y += 3 * CGFloat(radius)
|
||||
}
|
||||
|
||||
cubeNode.runAction(SCNAction.rotateByX(0, y: -0.3, z: 0, duration: 0.0))
|
||||
self.rootNode.addChildNode(cubeNode)
|
||||
|
||||
}
|
||||
|
||||
required init(coder aDecoder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user