Skip to content

Instantly share code, notes, and snippets.

@SunXiaoShan
Created October 14, 2018 02:22
Show Gist options
  • Save SunXiaoShan/b1c26fd324093cb8e618e36f680e1f42 to your computer and use it in GitHub Desktop.
Save SunXiaoShan/b1c26fd324093cb8e618e36f680e1f42 to your computer and use it in GitHub Desktop.
class EmojiNode: SCNNode {
var options: [String]
var index = 0
init(with options: [String], width: CGFloat = 0.06, height: CGFloat = 0.06) {
self.options = options
super.init()
let plane = SCNPlane(width: width, height: height)
plane.firstMaterial?.diffuse.contents = (options.first ?? " ").image()
plane.firstMaterial?.isDoubleSided = true
geometry = plane
}
}
extension String {
func image() -> UIImage? {
let size = CGSize(width: 20, height: 22)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
UIColor.clear.set()
let rect = CGRect(origin: .zero, size: size)
UIRectFill(CGRect(origin: .zero, size: size))
(self as AnyObject).draw(in: rect, withAttributes: [.font: UIFont.systemFont(ofSize: 15)])
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment