Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding an SKLabelNode to Scene Kit View

I am building a game using Scene Kit. In order to present the score I wanted to use an SKLabelNode on the screen, however, when I attach it to a SCNNode, it looks very blurry: enter image description here

Here is the code that I have written to do this, please let me know if there is a better way to go about doing this without having the text be so blurry. Thank you so much!

func initHUD() {

    let skScene = SKScene(size: CGSize(width: 100, height: 100))
    skScene.backgroundColor = UIColor(white: 0.0, alpha: 0.0)

    labelNode = SKLabelNode()
    labelNode.fontSize = 20
    labelNode.position.y = 50
    labelNode.position.x = 50

    skScene.addChild(labelNode)

    let plane = SCNPlane(width: 1, height: 1)
    let material = SCNMaterial()
    material.lightingModelName = SCNLightingModelConstant
    material.doubleSided = true
    material.diffuse.contents = skScene
    plane.materials = [material]

    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, z: -5)
}

func updateHUD() {
    labelNode.text = "\(score)"
}
like image 855
DevelUpGames Avatar asked Feb 06 '23 20:02

DevelUpGames


1 Answers

The typical way to do a HUD for a SceneKit scene is to create a SpriteKit SKScene and set it as the overlaySKScene of your SceneKit view. Then it renders at full resolution and always at the same view-relative size and position.

like image 112
rickster Avatar answered Feb 13 '23 06:02

rickster