Wes W.
Wes W.

Reputation: 93

SCNText not showing in SceneKit

I'm trying to add a simple text overlay in my 3D scene and nothing is showing up. For reference a SCNBox adds and displays just fine. Help?

SCNNode PlaceText(SCNVector3 pos)
{
    var text = SCNText.Create("hello world", 5);
    text.Font = UIFont.FromName("Avenir Heavy", 50);
    text.ChamferRadius = 0.3f;
    text.Flatness = 0.1f;
    text.FirstMaterial.Diffuse.Contents = UIColor.White;
    text.FirstMaterial.Specular.Contents = UIColor.Blue;

    var offset = new SCNVector3(0.2f, 0.2f, 0.2f);
    offset = SCNVector3.Add(pos, offset);

    var textNode = new SCNNode { Position = offset, Geometry = text };
    scnView.Scene.RootNode.AddChildNode(textNode);

    return textNode;
}

Upvotes: 2

Views: 683

Answers (1)

Andy Jazz
Andy Jazz

Reputation: 58043

You forgot to assign a text's SCNGeometry to node.geometry.

Here's a working example written in Swift (mac version):

import SceneKit
import QuartzCore

class GameViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let scene = SCNScene()
        let scnView = self.view as! SCNView
        scnView.scene = scene
        scnView.allowsCameraControl = true
        scnView.backgroundColor = NSColor.darkGray

        func placeText() {
            let text = SCNText(string: "HELLO WORLD", extrusionDepth: 3.0)
            text.font = NSFont(name: "Avenir", size: 18.0)
            text.chamferRadius = 0.3
            text.flatness = 0.1
            text.firstMaterial?.diffuse.contents = NSColor.white
            text.firstMaterial?.specular.contents = NSColor.blue
            let position = SCNVector3(-80,-10,0)
            let textNode = SCNNode()
            textNode.geometry = text                    // YOU MISSED THAT
            textNode.position = position
            scnView.scene!.rootNode.addChildNode(textNode)
        }
        placeText()
    }
}

enter image description here

Upvotes: 2

Related Questions