
3D漢字を縁取り付きで表示するiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@import SceneKit;
@interface ViewController ()
@property (nonatomic, weak) SCNView *sceneView;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createPlate];
[self createCamera];
}
– (void)setupScene {
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];
sv.backgroundColor = [UIColor greenColor];
sv.scene = [SCNScene scene];
sv.scene.physicsWorld.speed = 10.0;
sv.allowsCameraControl = YES;
[self.view addSubview:sv];
self.sceneView = sv;
}
– (void)createPlate {
SCNBox *plate = [SCNBox boxWithWidth:300 height:10 length:200 chamferRadius:0];
plate.firstMaterial.diffuse.contents = [UIColor brownColor];
SCNNode *plateNode = [SCNNode nodeWithGeometry:plate];
plateNode.position = SCNVector3Make(100, –200, 0);
plateNode.physicsBody = [SCNPhysicsBody staticBody];
[self.sceneView.scene.rootNode addChildNode:plateNode];
}
– (void)createTextWithOutlineWithFont:(UIFont*)font position:(SCNVector3)pt {
SCNText *t = [SCNText textWithString:@”漢字“ extrusionDepth:30];
t.materials = ^{
NSMutableArray *arr = [NSMutableArray array];
for (int i=0; i<4; i++) {
SCNMaterial *m = [SCNMaterial material];
m.diffuse.contents = (i==0) ? [UIColor yellowColor] : [UIColor grayColor];
m.diffuse.mipFilter = SCNFilterModeLinear;
m.doubleSided = YES;
[arr addObject:m];
}
return arr;
}();
t.font = font;
t.chamferRadius = 1.0;
t.chamferProfile = ^{
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(1, 0)];
[path addLineToPoint:CGPointMake(1, 5)];
[path addLineToPoint:CGPointMake(-5, 5)];
[path addLineToPoint:CGPointMake(0, 1)];
return path;
}();
SCNNode *tNode = [SCNNode nodeWithGeometry:t];
tNode.position = pt;
[self.sceneView.scene.rootNode addChildNode:tNode];
tNode.physicsBody = [SCNPhysicsBody dynamicBody];
}
– (void)createCamera {
SCNNode *camera = [SCNNode node];
camera.camera = [SCNCamera camera];
camera.camera.zFar = 1000;
camera.position = SCNVector3Make(100, 0, 600);
[self.sceneView.scene.rootNode addChildNode:camera];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (arc4random_uniform(2) == 0) {
[self createTextWithOutlineWithFont:[UIFont fontWithName:@”STHeitiJ-Medium” size:80] position:SCNVector3Make(-25, 160, 0)];
} else {
[self createTextWithOutlineWithFont:[UIFont fontWithName:@”Apple SD Gothic Neo” size:80] position:SCNVector3Make(25, 160, 0)];
}
}
@end