ARKit: Placing an SCNText at a particular point in front of the camera
The coordinate center for an SCNGeometry
is its center point. But when you are creating a SCNText
the center point is somewhere in the bottom left corner:
You need to center the text first. This can be done by checking the bounding box of the node containing your text and setting a pivot transform to change the texts center to its actual center:
func center(node: SCNNode) {
let (min, max) = node.boundingBox
let dx = min.x + 0.5 * (max.x - min.x)
let dy = min.y + 0.5 * (max.y - min.y)
let dz = min.z + 0.5 * (max.z - min.z)
node.pivot = SCNMatrix4MakeTranslation(dx, dy, dz)
}
Edit:
Also note this answer that explains some additional pitfalls:
A text with 16 pts font size is 16 SceneKit units tall. But in ARKit 1 SceneKit units = 1 meter!
SCNText, containerFrame, wrapping and ARKit
Answer from Apple: my font size was too small. If I use a "normal" font size and containing frame on the SCNText object, and then set a scale on the node that contains it, everything wraps as expected. Something like:
extrudedText.font = UIFont(name: definition.fontname, size: 20)!
extrudedText.containerFrame = CGRect(origin: .zero, size: CGSize(width: 100.0, height: 500.0))
...
scale = SCNVector3Make(0.01, 0.01, 0.01)
Related Topics
Is There a Method to Blur a Background in Swiftui
Arkit - Get Current Position of Arcamera in a Scene
Swift. Could Not Build Objective-C Module 'Alamofire'
Isn't There an How to Pinch to Zoom in an Image in Swiftui
Swift: Testing Against Optional Value in Switch Case
Is There a Difference Between Swift 2.0 Protocol Extensions and Java/C# Abstract Classes
How to Check Is a String or Number
Understanding Swift 2.2 Selector Syntax - #Selector()
Swift Pointer Problems with MACh_Task_Basic_Info
Rounding in Swift with Round()
How to Convert a Uiimage to a Cvpixelbuffer
Safariviewcontroller: How to Grab Oauth Token from Url
How to Require That a Protocol Can Only Be Adopted by a Specific Class
Why Do Enums Have Computed Properties But Not Stored Properties in Swift
How to Call a Function When a Swiftui Picker Selection Changes
Is There a Zip Function to Create Tuples with More Than 2 Elements