ARKit SCNText Issue

I have been trying to place some text in ARKit using SCNText and it does not appear to do anything. No text is displayed on the screen.


  1. let text = SCNText(string: item.label, extrusionDepth: 5.0)
  2. text.firstMaterial?.diffuse.contents = UIColor.orange
  3. text.firstMaterial?.specular.contents = UIColor.orange
  4. text.font = UIFont(name: "Optima", size: 100)
  5. text.containerFrame = CGRect(x: 0, y: 0, width: 100, height: 44)
  6. let textNode = SCNNode(geometry: text)
  7. textNode.position = SCNVector3(-0.2 + x, -0.9 + delta, -1)
  8. x += 0.12
  9. scene.rootNode.addChildNode(textNode)

Replies

Why is the font size 100 and the frame height only 44?

It's a bug in your code, not in ARKit/SceneKit: Your text is placed outside the camera view frustum.

Objective-C example below:


SCNNode *myTextNode = [SCNNode node];

SCNText *myText = [SCNText textWithString:myCrdLabels[i] extrusionDepth:0.75];

myText.firstMaterial.shininess = 0.75;

myText.firstMaterial.transparency = 0.5;

[myText setSubdivisionLevel:2];

[myTextNode setPosition:SCNVector3Make(myNormalizedCrdsArray[i][0], myNormalizedCrdsArray[i][1], myNormalizedCrdsArray[i][2])];

[myTextNode setGeometry:myText];


This places the text in the position specified by the coordinates in the array and this works well.

I am putting the textNode at the same position where my cubeNode was placed and the cube displayed correctly.


This is my line for adding the position to the textNode:


textNode.position = SCNVector3(-0.2 + x, -0.9 + delta, -1)

@robz: Whether or not your text will be visible, entirely depends on the values in myNormalizedCrdsArray.

@azamsharp: You didn't specify the value of x or delta. (But your text won't be visible regardless.)


In any case, I recommend using the new SceneKit debugging functionality in XCode 9 (see: https://developer.apple.com/videos/play/wwdc2017/404/) to figure out why certain elements in your scene don't appear as you would expect them to.

@azamsharp: You didn't specify the value of x or delta. (But your text won't be visible regardless.)


What do you mean the text won't be visible regardless?


The values are x = 0.1, y = 0.2, z = -1


This means it will be kind of infront of me.


I placed a SCNBox at the same location and I can see it.

Just debug your SceneKit scene in XCode 9 and you'll figure out why. It's way quicker than writing lengthy posts about it here.

Thanks I will check it out. Although I have an ARKit app so not sure how that particular aspect of debugging is going to work in AR view.

I have filed a Radar on this bug. Thanks!

SCNText font size is expressed in “meters” (SceneKit units). So you’re drawing 100 meter tall text and placing it a fraction of a meter in front of you. It’s hit or miss whether the camera will be pointing at part of the extruded text, and you’ll need to adjust the Z buffer range too (there’s a parameter something like adjustsZRangeAutomatically). Note also that the origin of an SCNText node is at the lower left of the text, not in the center (as is the case for the other canned SCNGeometry subclasses), with Y=0 at the font baseline (so you‘ll see descenders with negative Y).


You can debug this by putting an SCNSphere at the desired text location. Tweak the setup until the sphere is visible, then switch to a text node with size equal to the sphere’s radius.


It’s also helpful, in general, to export your SCNScene and then load it into the Xcode Scene Editor. I haven’t tinkered with the Xcode View Debugger’s SceneKit tools but I expect they’ll be a big help for this sort of problem (“I placed it in the scene but can’t find it”).

Thanks!


I updated the code to this:


/
        let scene = SCNScene()
       
        let textGeometry = SCNText(string: "Hello World", extrusionDepth: 1.0)
        textGeometry.firstMaterial?.diffuse.contents = UIColor.black
       
        let textNode = SCNNode(geometry: textGeometry)
        textNode.position = SCNVector3(0,0.1,-1)
        textNode.scale = SCNVector3(0.5,0.5,0.5)
       
        scene.rootNode.addChildNode(textNode)

Now, I can see the text but first it it too large and also it displays to the right side instead of in the front.