Skip to content

Instantly share code, notes, and snippets.

@aclima93
Last active July 10, 2018 17:11
Show Gist options
  • Save aclima93/ea3ede691b59bc524bb2e7a1554b66da to your computer and use it in GitHub Desktop.
Save aclima93/ea3ede691b59bc524bb2e7a1554b66da to your computer and use it in GitHub Desktop.
ARKit & CoreLocation + POI + AR Scene
open class LocationTextAnnotationNode: LocationNode {
// image and text that are displayed by the child nodes
public let image: UIImage
public let text: String
// child nodes
public let imageAnnotationNode: SCNNode
public let textAnnotationNode: SCNNode
public init(location: CLLocation?, image: UIImage, text: String) {
self.text = text
self.image = image
// create the child node that holds the location's marker image
let imagePlane = SCNPlane(width: image.size.width / 100, height: image.size.height / 100)
imagePlane.firstMaterial!.diffuse.contents = image
imagePlane.firstMaterial!.lightingModel = .constant
imageAnnotationNode = SCNNode()
imageAnnotationNode.geometry = imagePlane
// create the child node that holds the location's name
let textShape = SCNText(string: text, extrusionDepth: 1)
textShape.firstMaterial!.diffuse.contents = UIColor.white
textShape.firstMaterial!.specular.contents = UIColor.black
textShape.firstMaterial!.lightingModel = .phong
textShape.isWrapped = true
textShape.alignmentMode = CATextLayerAlignmentMode.center.rawValue
textAnnotationNode = SCNNode()
textAnnotationNode.geometry = textShape
// initializer ARCL's LocationNode
super.init(location: location)
scaleRelativeToDistance = true
// apply a billboard constraint so the parent node, so it always faces the user
let billboardConstraint = SCNBillboardConstraint()
billboardConstraint.freeAxes = SCNBillboardAxis.Y
constraints = [billboardConstraint]
// add the child nodes
addChildNode(imageAnnotationNode)
addChildNode(textAnnotationNode)
// center text correctly around (x) and below (y) origin (SCNText's origins in in the bottom left corner)
let min = textAnnotationNode.boundingBox.min
let max = textAnnotationNode.boundingBox.max
textAnnotationNode.pivot = SCNMatrix4MakeTranslation((max.x - min.x) / 2, (max.y - min.y), 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment