1

I have tried to follow the same simple logic as for cylinder, box … , so just by defining the position for textNode, but it is not working.

func makeText(text3D: String, position: SCNVector3, depthOfText: CGFloat, color: NSColor, transparency: CGFloat) -> SCNNode
{
    let textTodraw = SCNText(string: text3D, extrusionDepth: depthOfText)
    textTodraw.firstMaterial?.transparency = transparency
    textTodraw.firstMaterial?.diffuse.contents = color
    let textNode = SCNNode(geometry: textTodraw)
    textNode.position = position        
    return textNode
}
VYT
  • 1,071
  • 19
  • 35
  • Can you please clarify your question and also add how you calling the function abd adding tue text – Coldsteel48 Dec 09 '16 at 18:55
  • 1
    Could you please elaborate on what the expected and actuals results are? – mnuages Dec 09 '16 at 18:55
  • 1
    Just a guess, since your question is pretty vague about what's actually going wrong... but be aware that the "points" in your font size are the same as the units of your 3D coordinate space. The [default font is 36 pt](https://developer.apple.com/reference/scenekit/scntext/1523273-font), so if the rest of your scene is only, say, 3-4 units wide, there might be giant letters just off-camera. – rickster Dec 09 '16 at 19:16
  • @rickster Thanks, you have the right guess! I have missed the default font value. Problem is solved. – VYT Dec 09 '16 at 20:31
  • Yay lucky guess. :) I elaborated on the above a bit and posted it as an answer. (Please vote/accept if you find it helpful.) – rickster Dec 09 '16 at 21:23
  • I have resolved by setting scale to the text node and decrease the size to 0.5 :-https://gist.github.com/shrawan2015/62d27202394101e4eca4e3d9ba0e67a1 – Shrawan Sep 06 '18 at 11:41
  • Don't change the scale - it's a bad idea for many reasons. Simply set the size you want. Flatness is always about 1/100th the size of the text. – Fattie Nov 09 '22 at 15:47

2 Answers2

8

The default font for SCNText is 36 point Helvetica, and a "point" in font size is the same as a unit of scene space. (Well, of local space for the node containing the SCNText geometry. But unless you've set a scale factor on your node, local space units are the same as scene space units.) That means even a short label can be tens of units tall and hundreds of units wide.

It's typical to build SceneKit scenes with smaller scope — for example, simple test scenes like you might throw together in a Swift playground using the default sizes for SCNBox, SCNSphere, etc might be only 3-4 units wide. (And if you're using SceneKit with ARKit, scene units are meters, so some text in 36 "point" font is the size of a few office blocks downtown.)

Also, the anchor point for a text geometry relative to its containing node is at the lower left corner of the text. Put all this together and it's entirely possible that there are giant letters looming over the rest of your scene, hiding just out of camera view.


Note that if you try to fix this by setting a much smaller font on your SCNText, the text might get jagged and chunky. That's because the flatness property is measured relative to the point size of the text (more precisely, it's measured in a coordinate system where one unit == one point of text size). So if you choose a font size that'd be tiny by screen/print standards, you'll need to scale down the flatness accordingly to still get smooth curves in your letters.

Alternatively, you can leave font sizes and flatness alone — instead, set a scale factor on the node containing the text geometry, or set that node's pivot to a transform matrix that scales down its content. For example, if you set a scale factor of 1/72, one unit of scene space is the same as one "inch" (72 points) of text height — depending on the other sizes in your scene, that might make it a bit easier to think of font sizes the way you do in 2D.

rickster
  • 124,678
  • 26
  • 272
  • 326
0

The fact is you generally just use "small numbers" for font sizes in SceneKit.

In 3D you always use real meters. A humanoid robot must be about "2" units tall, a car is about "3" units long and so on.

Very typical sizes for the font is about "0.1"

Note that the flatness value is SMALL, usually about one hundredth the size of the font. (Which is obvious, it's how long the line segments are.)

Typical:

    t.font = UIFont(name: "Blah", size: 0.10)
    t.flatness = 0.001

Set the flatness to about 1/4 the size of the font (hence, 0.025 in the example) to understand what "flatness" is.

I would never change the scale of the type node, that's a bad idea for many reasons. There's absolutely no reason to do so, and it makes it very difficult to genuinely set the flatness appropriately.

But note ...

That being said, on the different platforms and different versions, SCNText() often does a basically bad job drawing text, and it can go to hell at small numbers. So yeah, you may indeed have to scale in practice, if the text construction is crap at (very) small values :/

Fattie
  • 27,874
  • 70
  • 431
  • 719