Reputation: 11
I am trying to create an AR app that have a number of SCNText
and SCNPlane
.
And I am trying to place each SCNText
inside the SCNPlane
, but as this figure shows, the text becomes always outside the plane.
My Question is how can I place each SCNText
inside the SCNPlane
?
This is my code section:
func displayMark() {
var str = ["Orange ","Apple ","Banana ","limon", "onion"]
var x : Float = 1
var i = 0
while i<5 {
let text = SCNText(string: str[i], extrusionDepth: 0)
let material = SCNMaterial()
material.diffuse.contents = UIColor.black
text.materials = [material]
let node = SCNNode()
node.position = SCNVector3(0, x/15 , 0)
node.scale = SCNVector3(0.0009, 0.0009, 0.0009)
let planeNode2 = SCNNode(geometry: text)
node.addChildNode(planeNode2)
let minVec = node.boundingBox.min
let maxVec = node.boundingBox.max
let bound = SCNVector3Make(maxVec.x - minVec.x,
maxVec.y - minVec.y,
maxVec.z - minVec.z);
let plane = SCNPlane(width: CGFloat(bound.x),
height: CGFloat(bound.y))
plane.cornerRadius = 5
plane.firstMaterial?.diffuse.contents = UIColor.gray.withAlphaComponent(0.8)
let planeNode = SCNNode(geometry: plane)
node.addChildNode(planeNode)
planeNode.name = "text"
i = i + 1
x = 1 + x
self.sceneView.scene.rootNode.addChildNode(node)
self.sceneView.autoenablesDefaultLighting = true
}
}
Upvotes: 1
Views: 1334
Reputation: 58093
The SCNText
's peculiarity is that its pivot point
(a.k.a. origin point
) is located at a bottom of its left corner's container.
Like that:
So you need to apply a regular formula to calculate the center location of a pivot point:
/* ( Max dist - Min dist ) / 2 for X and Y */
Here's how a real code for repositioning pivot along X looks like:
sceneView.debugOptions = .showBoundingBoxes
let boundingBox = (abs(textNode.boundingBox.max.x) -
abs(textNode.boundingBox.min.x)) / 2
textNode.simdPivot.columns.3.x = boundingBox
Also it's a good idea to slightly offset the pivot along +Z direction if there's a deep extrusion.
Upvotes: 2