Search code examples
swiftscenekitarkitscnnodescntext

SCNText won't show


Before stating what my problem is I want to clarify that there are multiple answers to my question out there, unfortunately, none of them is solving my problem. Please don't mark this as a duplicate.

Now this is the code I have to create my text node

let textNode: SCNNode = {
            let text = SCNText.init(string: "250.9cm", extrusionDepth: 0.01)
            text.font = UIFont(name: "System", size: 32)
            text.flatness = 0.2
            let node = SCNNode.init(geometry:  text)
            node.pivot = SCNMatrix4Scale(node.transform, 1/72, 1/72, 1/72)
            node.scale = SCNVector3(0.1, 0.1, 0.1)
            node.geometry?.materials.first?.diffuse.contents = UIColor.red
            return node
        }()

This is built up by multiple different solutions that i have found and i have tried it in many different ways. However, none is working.

Here i add 1 view as a childNode and then another 3 childNodes to that child.

self.addChildNode(mainNode)

        topNode.position = SCNVector3(mainNode.getMidX(), mainNode.boundingBox.max.y, mainNode.getMidZ())
        bottomNode.position = SCNVector3(mainNode.getMidX(), mainNode.boundingBox.min.y, mainNode.getMidZ())
        textNode.position = SCNVector3(mainNode.getMidX()+0.005, mainNode.getMidY(), mainNode.getMidZ())

        mainNode.addChildNode(topNode)
        mainNode.addChildNode(bottomNode)
        mainNode.addChildNode(textNode)

As you can see in the picture below, everything is displayed properly and exactly in the location i expect. Except the textNode that is nowhere to be found. (note that it's the black line with hat and shoes that is main, top and bottom node)

enter image description here

According to the other position attributes set, the text should appear perfectly in the middle of the black line vertically and slightly to the right horizontally.

What i am missing is something i can't find in other answers. Please come with suggestions how i should approach this.


Solution

  • Well, most likely your text isn’t showing because of the same reason as those other questions you’re sure aren’t duplicates: it’s way too big.

    But I’m answering instead of voting to close as duplicate, because there’s an extra wrinkle or two worth calling out. Right here:

    node.pivot = SCNMatrix4Scale(node.transform, 1/72, 1/72, 1/72)
    node.scale = SCNVector3(0.1, 0.1, 0.1)
    

    First, setting a scale and setting pivot to a scale transform have the same effect — both scale the node’s content, so you’re effectively applying one scale factor that’s the concatenation of the two transforms. (Also, as of these calls, node.transform is identity, so the first line is equivalent to SCNMatrix4MakeScale with the same factor.)

    Second, as explained in this great answer, the pivot property applies the inverse of the transform you provide to the node’s content. Your transform is a reduction in scale, so the transform applied is the inverse of that: an increase in scale.

    (The documentation for pivot is rather unclear on the inverse transformation part. Maybe if you tell Apple about it they’ll fix that doc.)

    Putting it together: you’re scaling a 32 “point” text node down to 1/10x, then up to 72x. That’s the same as scaling it to 7.2x, or just using a 230 “point” font to start with.

    And as noted elsewhere, “point” sizes for fonts are actually meters in SceneKit+ARKit, so you probably have skyscraper-size letters floating somewhere above and to your right (if they’re not invisible because they’re beyond clip depth). Hope you brought climbing gear...