Swift Scenekit - Centering Scntext - the Getboundingboxmin:Max Issue

Swift Scenekit - Centering SCNText - the getBoundingBoxMin:Max issue

OK so my final code solution looks like:

func setCounterValue(counterValue:Int) {

var v1 = SCNVector3(x: 0,y: 0,z: 0)
var v2 = SCNVector3(x: 0,y: 0,z: 0)

_textNode.removeFromParentNode()
_counterValue = counterValue

let newText = SCNText(string: String(format: "%08d", counterValue), extrusionDepth:sDepth)
newText.font = UIFont (name: "Arial", size: 3)
newText.firstMaterial!.diffuse.contents = UIColor.whiteColor()
newText.firstMaterial!.specular.contents = UIColor.whiteColor()

_textNode = SCNNode(geometry: newText)
_textNode.getBoundingBoxMin(&v1, max: &v2)

let dx:Float = Float(v1.x - v2.x)/2.0
let dy:Float = Float(v1.y - v2.y)
_textNode.position = SCNVector3Make(dx, dy, Float(sDepth/2))

node.addChildNode(_textNode)

}

I've left in my couple of global variables, but should make sense.

Thanks for the help all.

SceneKit - SCNText centering incorrectly

The difference of SCNText from other geometries is that SCNText origin point positioned at bottom left corner. In other geometries, it is a bottom center.

To fix text position in parent node you can set its pivotPoint.x to half of width:

SCNVector3 min, max;
[textNode getBoundingBoxMin:&min max:&max];
textNode.pivot = SCNMatrix4MakeTranslation((max.x - min.x) / 2, 0, 0);

To fix subnodes position, you should set their position to half of width plus min:

SCNVector3 min, max;
[textNode getBoundingBoxMin:&min max:&max];
subnode.position = SCNVector3Make((max.x - min.x) / 2 + min.x, (max.y - min.y) / 2 + min.y, 0);

SCNText won't show

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...

Rotate SCNText Node around center of itself (Swift - Scenekit)

you can set the node's pivot property to half its width and height (see getBoundingBoxMin:max:)

Changing the value of string on SCNText produces no change

Setting the string on your SCNText geometry is sufficient.

textNode.geometry.string = "0.5"

If you see no change, then weakSelfText doesn't point to where you think it does. Make sure you're not dropping a reference somewhere:

NSAssert(weakSelfText == textNode.geometry, @"pointers not the same")

SCNText Alignment not working in iOS

It looks like alignment is working -- try adding a second line to your test string like "TEST\nT" and switching between left and right alignment modes. But SCNText adapts to fit the coordinate system of its node, so alignment alone won't necessarily make the text fit into your scene the way you want it to.

You can tweak how a node's content fits into its parent space using the node's pivot property. Set it to a translation transform (SCNMatrix4MakeTranslation) based on the bounding box of your text and you can change the layout however you like.



Related Topics



Leave a reply



Submit