
反対向いてる文字をもどすiPhoneアプリのサンプルコードを描いてみます。
import UIKit
import SceneKit
class ViewController: UIViewController {
weak var sceneView : SCNView?
override func viewDidLoad() {
super.viewDidLoad()
setupScene()
createWords()
createCamera()
}
func setupScene() {
let sv = SCNView(frame: view.bounds)
sv.backgroundColor = UIColor(hue: 0.5, saturation: 0.2, brightness: 1, alpha: 1)
sv.scene = SCNScene()
view.addSubview(sv)
sv.allowsCameraControl = true
sceneView = sv
}
func createWords() {
var words = (65…90).map { String(UnicodeScalar($0)) } // A to Z
let size = 1000
let mytext = {(i : Int) -> SCNText in
let text = SCNText(string: words[i], extrusionDepth: 100)
text.font = UIFont(name: “AvenirNext-Medium”, size: CGFloat(size))
text.alignmentMode = kCAAlignmentCenter
return text
}
let mytext2 = {(i : Int) -> SCNText in
let text = SCNText(string: words[i], extrusionDepth: 100)
text.font = UIFont(name: “AvenirNext-Heavy”, size: CGFloat(size))
text.alignmentMode = kCAAlignmentCenter
return text
}
for (i, c) in enumerate(words) {
let x = (Float(i % 5) – 2.5) * Float(size) * 0.1 + Float(size) * 0.05
let y = -(Float(i / 5) – 2.5) * Float(size) * 0.1
let node = SCNNode()
node.name = “word”
node.transform = SCNMatrix4Translate(node.transform, x, y, 0)
sceneView?.scene?.rootNode.addChildNode(node)
let textNode = SCNNode(geometry: mytext(i))
textNode.geometry?.firstMaterial?.diffuse.contents = UIColor(hue: 0.8, saturation: 0.1, brightness: 1, alpha: 1)
var v1 = SCNVector3(x: 0, y: 0, z: 0)
var v2 = SCNVector3(x: 0, y: 0, z: 0)
var box = textNode.getBoundingBoxMin(&v1, max:&v2)
textNode.pivot = SCNMatrix4Translate(textNode.transform, (v2.x + v1.x) * 0.5, (v2.y + v1.y) * 0.5, 0)
textNode.transform = SCNMatrix4Scale(textNode.transform, 0.09, 0.08, 0.1)
node.addChildNode(textNode)
let outlineNode = SCNNode(geometry: mytext2(i))
box = outlineNode.getBoundingBoxMin(&v1, max:&v2) // reuse
outlineNode.geometry?.firstMaterial?.diffuse.contents = UIColor(hue: 0.8, saturation: 1, brightness: 0.5, alpha: 1)
outlineNode.pivot = SCNMatrix4Translate(outlineNode.transform, (v2.x + v1.x) * 0.5, (v2.y + v1.y) * 0.5, 0)
outlineNode.transform = SCNMatrix4Scale(outlineNode.transform, 0.1, 0.1, 0.05)
node.addChildNode(outlineNode)
if arc4random_uniform(5) == 4 {
for n in node.childNodes {
(n as SCNNode).transform = SCNMatrix4Rotate((n as SCNNode).transform, Float(M_PI), 0, 1, 0)
}
}
}
}
func createCamera() {
let camera = SCNNode()
camera.camera = SCNCamera()
camera.camera?.zFar = 1800
camera.position = SCNVector3(x: 0, y: 0, z: 900)
sceneView?.scene?.rootNode.addChildNode(camera)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
let p = touches.anyObject()!.locationInView(sceneView)
if let hit = sceneView?.hitTest(p, options: [SCNHitTestSortResultsKey:true])?.first as? SCNHitTestResult {
if hit.node.parentNode?.name == “word” {
for n in hit.node.parentNode!.childNodes {
(n as SCNNode).transform = SCNMatrix4Rotate((n as SCNNode).transform, Float(M_PI), 0, 1, 0)
}
}
}
}
}