Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SCNText - background "speech bubble"

Tags:

arkit

scntext

How can I insert a background (e.g. a "speech bubble" or a rectangle) to a SCNtext? Specifically, if I insert "Hello World" as SCNText (and obviously then as a SCNNode in the scene) then how can I add a background for that text only? Would it be a UIimage which will be inserted as a SCNNode at the same position in the "Hello World? (Keep in mind that there is nothing as background of SCNNode in SceneKit)


1 Answers

Here is a sample code. It will add SCNPlane as background of SCNTextNode:

let minVec = textNode.boundingBox.min
let maxVec = textNode.boundingBox.max
let bound = SCNVector3Make(maxVec.x - minVec.x, 
                           maxVec.y - minVec.y, 
                           maxVec.z - minVec.z);

let plane = SCNPlane(width: CGFloat(bound.x + 1), 
                    height: CGFloat(bound.y + 1))
plane.cornerRadius = 0.2
plane.firstMaterial?.diffuse.contents = UIColor.black.withAlphaComponent(0.9)

let planeNode = SCNNode(geometry: plane)
planeNode.position = SCNVector3(CGFloat( minVec.x) + CGFloat(bound.x) / 2 , 
                                CGFloat( minVec.y) + CGFloat(bound.y) / 2,CGFloat(minVec.z - 0.01))

textNode.addChildNode(planeNode)
planeNode.name = "text"

Hope it is helpful to someone

like image 103
Prashant Tukadiya Avatar answered Oct 31 '25 12:10

Prashant Tukadiya