0

ViewController

import UIKit
import SceneKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let scnView = self.view as SCNView

        scnView.backgroundColor = UIColor.blackColor()

        let sizeScnView = CGSize(width: 100.0, height: 100.0)
        let centerView = CGPoint(x: CGRectGetMidX(self.view.frame) - sizeScnView.width/2, y: CGRectGetMidY(self.view.frame) - sizeScnView.height/2)
        let scene = Tile(frame: CGRect(origin: centerView, size: sizeScnView))
        scnView.scene = scene
    }
}

Tile

import UIKit
import SceneKit

class Tile: SCNScene {

    override init() {
        super.init()

        let ball = SCNSphere(radius: 0.5)
        let ballNode = SCNNode(geometry: ball)
        ball.firstMaterial?.diffuse.contents = UIColor.orangeColor()
        self.rootNode.addChildNode(ballNode)

    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
swl
  • 129
  • 2
  • 12
  • Describe what doesn't work about your current code, and what different result you want. (Is there a formatting issue with some lines that don't look like code, but should?) – RobP Dec 10 '14 at 23:29
  • I receive an error message on the "let scene =" line. Error message: extra argument 'frame' in call – swl Dec 11 '14 at 00:42
  • let scene = Tile(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) // does not work either – swl Dec 11 '14 at 01:10
  • `Tile` is a subclass of `SCNScene`. Scenes do not have a `frame` property and no such initializer. `SCNView` does. – mnuages Dec 11 '14 at 11:43

2 Answers2

1

Just like any other UIView you can set the frame of an SCNView (see View Programming Guide for iOS).

SCNScene instances do not have a 2D size (you could compute the bounding box of their root node but that something different).

mnuages
  • 13,049
  • 2
  • 23
  • 40
1

Just reading this article do not know if the Question is still open I am running this in a small test seems to work fine

class ViewController: UIViewController {
   var sceneView: SCNView!
   var camera: SCNNode!
   var light: SCNNode!
   var sphere1: SCNNode!
   override func viewDidLoad() {
    super.viewDidLoad()
     let sizeScnView = CGSize(width: 420.0, height: 580.0)
     let centerView = CGPoint(x: CGRectGetMidX(self.view.frame) - sizeScnView.width/2, y: CGRectGetMidY(self.view.frame) - sizeScnView.height/2)
    sceneView = SCNView(frame: CGRect(origin: centerView, size: sizeScnView))
    sceneView.scene = SCNScene()
    self.view.addSubview(sceneView)
    sceneView.backgroundColor = UIColor.blackColor()
    let camera = SCNCamera()
    camera.zFar = 10000
    self.camera = SCNNode()
    self.camera.camera = camera
    self.camera.position = SCNVector3(x: -20, y: 55, z: 20)
    let constraint = SCNLookAtConstraint(target: ground)
    constraint.gimbalLockEnabled = true
    self.camera.constraints = [constraint]

    let ambientLight = SCNLight()
    ambientLight.color = UIColor.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 = UIColor.greenColor()
    sphereGeometry.materials = [sphereMaterial]
    sphere1 = SCNNode(geometry: sphereGeometry)
    sphere1.position = SCNVector3(x: -15, y: 1.5, z: 0)

   sceneView.scene?.rootNode.addChildNode(self.camera)
   sceneView.scene?.rootNode.addChildNode(light)
   sceneView.scene?.rootNode.addChildNode(sphere1)


   }
}
Mayank Patel
  • 3,868
  • 10
  • 36
  • 59
  • ground could be added to the root scene. Not required sorry for not removing the lines //////let constraint = SCNLookAtConstraint(target: ground) ////////// constraint.gimbalLockEnabled = true – Helmut Taylor Jan 21 '16 at 17:54