Group Group Group Group Group Group Group Group Group

Can't use SKScene in a SCNPlane for macOS builds

#1

After going through some of 3D iOS Games by Tutorial, I started working on some UI bits for an SceneKit project. Namely, I wanted to create labels and 2D buttons. I was able to utilize the example used in the Breakout example in 3D iOS Games book, to get a SKLabelNode and SKShapeNode into an SCNScene via a SCNPlane, and it works great on iOS.

But I ran into problems when I run using my Mac target. Either the entire Mac UI freezes (requiring me to power down) or if it does come up, it takes 30 seconds or so for the window to come up. Once up it seems to run at a normal frame rate of 30fps (or 60fps depending on the monitor you run on.)
Is there something different I should do to on the Mac to get text and 2D art into a SCNScene?
I also noticed if you created a shape with SKShapeNode(circleOfRadius:) and set the fill color, it will come out as a square; and if you set the strokeColor you can see the stroke is round, but the fill is still square.

The below code is what I use to get an SKLabelNode to a SCNScene:

//SKScene to hold 2D elements that get put onto a plane, then added to the SCNScene
skScene = SKScene(size:CGSize(width: 500, height: 52  ))
skScene.backgroundColor = SKColor(white:0,alpha:0)

//create red box around the SKScene 
let shape = SKShapeNode(rect: CGRect(x: 0, y: 0, width: skScene.frame.size.width, height: skScene.frame.size.height))
shape.strokeColor = SKColor.red
shape.lineWidth = 1
skScene.addChild(shape)

//the label we can update anytime we want
label = SKLabelNode(fontNamed:"Menlo-Bold")
label.fontSize = 48
label.horizontalAlignmentMode = .left
label.verticalAlignmentMode = .center
label.position = CGPoint(x:0,y:skScene.frame.size.height/2)
label.text = "HELLO WORLD!"
skScene.addChild(label)

//create a plane to put the skScene on
let plane = SCNPlane(width:5,height:0.5)
let material = SCNMaterial()
material.lightingModel = SCNMaterial.LightingModel.constant
material.isDoubleSided = true
material.diffuse.contents = skScene
plane.materials = [material]

//Add plane to a node, and node to the SCNScene
hudNode = SCNNode(geometry: plane)
hudNode.name = "HUD"
hudNode.rotation = SCNVector4(x: 1, y: 0, z: 0, w: 3.14159265)
hudNode.position = SCNVector3(x:0, y: 1.5, z: 1)

scnGameScene.rootNode.addChildNode(hudNode)

(I originally posted this to the 2D Games forum by mistake. Sorry for the cross post)

Thanks,
Lee

closed #2

This topic was automatically closed after 166 days. New replies are no longer allowed.