I have an SCNText which I display in AR app, But I'd like certain words to be a different colour.
I've tried doing the following: How to change colour of the certain words in label - Swift 3 but NSString and labels are different to SCN (of course), so that did not work.
Below is my current code showing what I am doing:
func addARWords(_ words: String) {
let scene = SCNScene()
var highlightedWords = [String]()
textGeometry = SCNText(string: words, extrusionDepth: 1.0)
textGeometry.font = UIFont(name: "Arial", size: 10)
textGeometry.flatness = 0.2
textNode = SCNNode(geometry: textGeometry)
textNode.position = SCNVector3(myTextPosition.x, myTextPosition.y, myTextPosition.z)
textNode.scale = SCNVector3(myTextScale, myTextScale, myTextScale)
scene.rootNode.addChildNode(textNode)
sceneView.scene = scene
}
Mono-object Text
SCNText()class helps you create a 3D mono-object-text, hence you cannot separately access each character inside it (at the moment API doesn't allow you have access to each character). Instead you might use a texture to paint a whole object:But you'll get a texture's artifacts on side faces of
SCNTextobject.So there are two possible approaches to use different textures on different characters in 3D text:
Use 3D text created in 3D package (such as Maya, 3dsMax, Cinema4D, Houdini, Blender) with hand-made UV-mapped textures.
Or, if you prefer SceneKit, use words, containing separate
SCNTextcharacters, each one with a desired texture. So you have to parent several separateSCNTextelements to one ParentNode to move, rotate and scale it as a merged group.Multi-object Text
In case you wanna use
simple color mapping(not a texture) – use my code:And in case you wanna use Not-UV-mapped texture – add this code: